From 88d2a0f470ebe6b24719e8c30fb64a1c5f9e0223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 9 Feb 2022 16:19:20 +0200 Subject: [PATCH] compile cmph from source --- build.zig | 77 +++++++++++++++++++++++++++++----- include/deps/cmph/config.h | 86 ++++++++++++++++++++++++++++++++++++++ src/main.zig | 85 +++++++++++++++++-------------------- 3 files changed, 190 insertions(+), 58 deletions(-) create mode 100644 include/deps/cmph/config.h diff --git a/build.zig b/build.zig index 4232e78..3f88864 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,7 @@ -const Builder = @import("std").build.Builder; +const std = @import("std"); +const zbs = std.build; -pub fn build(b: *Builder) void { +pub fn build(b: *zbs.Builder) void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. Other options @@ -11,24 +12,78 @@ pub fn build(b: *Builder) void { // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const mode = b.standardReleaseOptions(); + const cmph = b.addStaticLibrary("cmph", null); + cmph.setTarget(target); + cmph.setBuildMode(mode); + cmph.linkLibC(); + cmph.force_pic = true; + cmph.addCSourceFiles(&.{ + "deps/cmph/src/bdz.c", + //"deps/cmph/src/bdz_gen_lookup_table.c", + "deps/cmph/src/bdz_ph.c", + //"deps/cmph/src/bm_numbers.c", + "deps/cmph/src/bmz8.c", + "deps/cmph/src/bmz.c", + "deps/cmph/src/brz.c", + "deps/cmph/src/buffer_entry.c", + //"deps/cmph/src/buffer_manage.c", + "deps/cmph/src/buffer_manager.c", + "deps/cmph/src/chd.c", + "deps/cmph/src/chd_ph.c", + "deps/cmph/src/chm.c", + //"deps/cmph/src/cmph_benchmark.c", + "deps/cmph/src/cmph.c", + "deps/cmph/src/cmph_structs.c", + "deps/cmph/src/compressed_rank.c", + "deps/cmph/src/compressed_seq.c", + //"deps/cmph/src/djb2_hash.c", + "deps/cmph/src/fch_buckets.c", + "deps/cmph/src/fch.c", + //"deps/cmph/src/fnv_hash.c", + "deps/cmph/src/graph.c", + "deps/cmph/src/hash.c", + //"deps/cmph/src/hashtree.c" + "deps/cmph/src/jenkins_hash.c", + "deps/cmph/src/linear_string_map.c", + //"deps/cmph/src/main.c", + "deps/cmph/src/miller_rabin.c", + //"deps/cmph/src/sdbm_hash.c", + "deps/cmph/src/select.c", + "deps/cmph/src/vqueue.c", + //"deps/cmph/src/vstack.c", + //"deps/cmph/src/wingetopt.c", + }, &.{ + "-W", + "-Wno-unused-function", + "-fomit-frame-pointer", + //"-DDEBUG", + }); + cmph.addIncludeDir("deps/cmph/src"); + cmph.addIncludeDir("include/deps/cmph"); + const exe = b.addExecutable("init-exe", "src/main.zig"); exe.setTarget(target); exe.setBuildMode(mode); - exe.linkLibC(); - exe.linkSystemLibrary("cmph"); + addCmphDeps(exe, cmph); exe.install(); { const turbonss_test = b.addTest("src/main.zig"); - turbonss_test.linkLibC(); - turbonss_test.linkSystemLibrary("cmph"); + addCmphDeps(turbonss_test, cmph); const test_step = b.step("test", "Run the tests"); test_step.dependOn(&turbonss_test.step); } - const run_cmd = exe.run(); - run_cmd.step.dependOn(b.getInstallStep()); - - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + { + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + } +} + +fn addCmphDeps(exe: *zbs.LibExeObjStep, cmph: *zbs.LibExeObjStep) void { + exe.linkLibC(); + exe.linkLibrary(cmph); + exe.addIncludeDir("deps/cmph/src"); } diff --git a/include/deps/cmph/config.h b/include/deps/cmph/config.h new file mode 100644 index 0000000..77afecc --- /dev/null +++ b/include/deps/cmph/config.h @@ -0,0 +1,86 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_GETOPT_H 1 + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_HOPSCOTCH_MAP_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the `check' library (-lcheck). */ +/* #undef HAVE_LIBCHECK */ + +/* Define to 1 if you have the header file. */ +#define HAVE_MATH_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define if g++ supports C++0x features. */ +/* #undef HAVE_STDCXX_0X */ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "cmph" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "cmph" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "cmph 2.0.2" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "cmph" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "2.0.2" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#define VERSION "2.0.2" + +/* Number of bits in a file offset, on hosts where this is settable. */ +/* #undef _FILE_OFFSET_BITS */ + +/* Define to make fseeko etc. visible, on some hosts. */ +/* #undef _LARGEFILE_SOURCE */ + +/* Define for large files, on AIX-style hosts. */ +/* #undef _LARGE_FILES */ diff --git a/src/main.zig b/src/main.zig index 7d9dbac..c0a6bf3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,35 +1,15 @@ const std = @import("std"); -const ArrayList = std.ArrayList; const c = @cImport({ @cDefine("DEBUG", "1"); @cInclude("stdio.h"); + @cInclude("string.h"); @cInclude("cmph.h"); }); const tempMph = "temp.mph"; -pub fn main() !void { - - // var hash: ?*c.cmph_t = c.cmph_new(config); - // c.cmph_config_destroy(config); - // _ = c.cmph_dump(hash, mphf_fd); - // c.cmph_destroy(hash); - // _ = c.fclose(mphf_fd); - // mphf_fd = c.fopen("temp.mph", "r"); - // hash = c.cmph_load(mphf_fd); - // while (i < nkeys) { - // var key: [*c]const u8 = vector[i]; - // var id: c_uint = c.cmph_search(hash, key, @bitCast(c.cmph_uint32, @truncate(c_uint, 10))); - // try stdout.print("key:{s} -- hash:{u}\n", key, id); - // i +%= 1; - // } - // c.cmph_destroy(hash); - // c.cmph_io_vector_adapter_destroy(source); - // _ = c.fclose(mphf_fd); - // return 0; - //} -} +pub fn main() !void {} test "simple cmph usage" { var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); @@ -38,28 +18,26 @@ test "simple cmph usage" { const stderr = std.io.getStdErr().writer(); _ = stdout; - var vector = ArrayList([*:0]const u8).init(arena); - try vector.append("aaaaaaaaaa"); - try vector.append("bbbbbbbbbb"); - try vector.append("cccccccccc"); - try vector.append("dddddddddd"); - try vector.append("eeeeeeeeee"); - try vector.append("ffffffffff"); - try vector.append("gggggggggg"); - try vector.append("hhhhhhhhhh"); - try vector.append("iiiiiiiiii"); - try vector.append("jjjjjjjjjj"); + var vector = std.ArrayList([*:0]const u8).init(arena); + try vector.appendSlice(&.{ + "aaaaaaaaaa", + "bbbbbbbbbb", + "cccccccccc", + "dddddddddd", + "eeeeeeeeee", + "ffffffffff", + "gggggggggg", + "hhhhhhhhhh", + "iiiiiiiiii", + "jjjjjjjjjj", + }); + var vector2 = try vector.clone(); + var nkeys = @truncate(c_uint, vector.items.len); - var mphf_fh = try std.fs.cwd().createFile(tempMph, .{}); - defer { - std.fs.cwd().deleteFile(tempMph) catch |err| { - stderr.print("error removing file: {s}\n", .{@errorName(err)}) catch {}; - }; - } - var mphf_fd = c.fdopen(mphf_fh.handle, "w"); - var vector_len = @truncate(c_uint, vector.items.len); + var mphf_fd = c.fopen(tempMph, "w"); var cvector = @ptrCast([*c][*c]u8, vector.toOwnedSlice().ptr); - var source = c.cmph_io_vector_adapter(cvector, vector_len); + var source = c.cmph_io_vector_adapter(cvector, nkeys); + defer c.cmph_io_vector_adapter_destroy(source); var config: *c.cmph_config_t = c.cmph_config_new(source) orelse return error.OutOfMemory; c.cmph_config_set_algo(config, c.CMPH_BRZ); c.cmph_config_set_mphf_fd(config, mphf_fd); @@ -67,11 +45,10 @@ test "simple cmph usage" { c.cmph_config_destroy(config); _ = c.cmph_dump(hash, mphf_fd); c.cmph_destroy(hash); - mphf_fh.close(); + try closeCFile(mphf_fd); - mphf_fh = try std.fs.cwd().openFile(tempMph, .{ .mode = .read_only }); - mphf_fd = c.fdopen(mphf_fh.handle, "r"); - defer mphf_fh.close(); + mphf_fd = c.fopen(tempMph, "r"); + defer _ = c.fclose(mphf_fd); var got_hash: ?*c.cmph_t = c.cmph_load(mphf_fd); if (got_hash) |real_hash| { @@ -79,6 +56,20 @@ test "simple cmph usage" { } else { try stderr.print("failed to unwrap hash\n", .{}); } + defer c.cmph_destroy(hash); + + try stderr.print("\n", .{}); + for (vector2.items) |key| { + var id = c.cmph_search(hash, key, @truncate(c_uint, c.strlen(key))); + try stderr.print("key: {s}, id: {d}\n", .{ key, id }); + } +} + +fn closeCFile(f: *c.FILE) !void { + var close_code = c.fclose(f); + if (close_code != 0) { + return std.os.unexpectedErrno(std.os.errno(close_code)); + } } const expectEqual = std.testing.expectEqual; @@ -87,7 +78,7 @@ test "char**" { var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); const arena = arena_instance.allocator(); - var arrl = ArrayList([*:0]const u8).init(arena); + var arrl = std.ArrayList([*:0]const u8).init(arena); try arrl.append("foo"); try arrl.append("bar");