1
Fork 0

compile cmph from source

This commit is contained in:
Motiejus Jakštys 2022-02-09 16:19:20 +02:00 committed by Motiejus Jakštys
parent 3f4711d518
commit 88d2a0f470
3 changed files with 190 additions and 58 deletions

View File

@ -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 // Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which // 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 // 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. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions(); 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"); const exe = b.addExecutable("init-exe", "src/main.zig");
exe.setTarget(target); exe.setTarget(target);
exe.setBuildMode(mode); exe.setBuildMode(mode);
exe.linkLibC(); addCmphDeps(exe, cmph);
exe.linkSystemLibrary("cmph");
exe.install(); exe.install();
{ {
const turbonss_test = b.addTest("src/main.zig"); const turbonss_test = b.addTest("src/main.zig");
turbonss_test.linkLibC(); addCmphDeps(turbonss_test, cmph);
turbonss_test.linkSystemLibrary("cmph");
const test_step = b.step("test", "Run the tests"); const test_step = b.step("test", "Run the tests");
test_step.dependOn(&turbonss_test.step); test_step.dependOn(&turbonss_test.step);
} }
{
const run_cmd = exe.run(); const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); 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");
} }

View File

@ -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 <dlfcn.h> header file. */
#define HAVE_DLFCN_H 1
/* Define to 1 if you have the <getopt.h> header file. */
#define HAVE_GETOPT_H 1
/* Define to 1 if you have the <hopscotch_map.h> header file. */
/* #undef HAVE_HOPSCOTCH_MAP_H */
/* Define to 1 if you have the <inttypes.h> 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 <math.h> header file. */
#define HAVE_MATH_H 1
/* Define to 1 if you have the <memory.h> 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 <stdint.h> header file. */
#define HAVE_STDINT_H 1
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
/* Define to 1 if you have the <strings.h> header file. */
#define HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
/* Define to 1 if you have the <unistd.h> 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 */

View File

@ -1,35 +1,15 @@
const std = @import("std"); const std = @import("std");
const ArrayList = std.ArrayList;
const c = @cImport({ const c = @cImport({
@cDefine("DEBUG", "1"); @cDefine("DEBUG", "1");
@cInclude("stdio.h"); @cInclude("stdio.h");
@cInclude("string.h");
@cInclude("cmph.h"); @cInclude("cmph.h");
}); });
const tempMph = "temp.mph"; const tempMph = "temp.mph";
pub fn main() !void { 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;
//}
}
test "simple cmph usage" { test "simple cmph usage" {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); 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(); const stderr = std.io.getStdErr().writer();
_ = stdout; _ = stdout;
var vector = ArrayList([*:0]const u8).init(arena); var vector = std.ArrayList([*:0]const u8).init(arena);
try vector.append("aaaaaaaaaa"); try vector.appendSlice(&.{
try vector.append("bbbbbbbbbb"); "aaaaaaaaaa",
try vector.append("cccccccccc"); "bbbbbbbbbb",
try vector.append("dddddddddd"); "cccccccccc",
try vector.append("eeeeeeeeee"); "dddddddddd",
try vector.append("ffffffffff"); "eeeeeeeeee",
try vector.append("gggggggggg"); "ffffffffff",
try vector.append("hhhhhhhhhh"); "gggggggggg",
try vector.append("iiiiiiiiii"); "hhhhhhhhhh",
try vector.append("jjjjjjjjjj"); "iiiiiiiiii",
"jjjjjjjjjj",
});
var vector2 = try vector.clone();
var nkeys = @truncate(c_uint, vector.items.len);
var mphf_fh = try std.fs.cwd().createFile(tempMph, .{}); var mphf_fd = c.fopen(tempMph, "w");
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 cvector = @ptrCast([*c][*c]u8, vector.toOwnedSlice().ptr); 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; 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_algo(config, c.CMPH_BRZ);
c.cmph_config_set_mphf_fd(config, mphf_fd); c.cmph_config_set_mphf_fd(config, mphf_fd);
@ -67,11 +45,10 @@ test "simple cmph usage" {
c.cmph_config_destroy(config); c.cmph_config_destroy(config);
_ = c.cmph_dump(hash, mphf_fd); _ = c.cmph_dump(hash, mphf_fd);
c.cmph_destroy(hash); 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.fopen(tempMph, "r");
mphf_fd = c.fdopen(mphf_fh.handle, "r"); defer _ = c.fclose(mphf_fd);
defer mphf_fh.close();
var got_hash: ?*c.cmph_t = c.cmph_load(mphf_fd); var got_hash: ?*c.cmph_t = c.cmph_load(mphf_fd);
if (got_hash) |real_hash| { if (got_hash) |real_hash| {
@ -79,6 +56,20 @@ test "simple cmph usage" {
} else { } else {
try stderr.print("failed to unwrap hash\n", .{}); 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; const expectEqual = std.testing.expectEqual;
@ -87,7 +78,7 @@ test "char**" {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator); var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const arena = arena_instance.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("foo");
try arrl.append("bar"); try arrl.append("bar");