wttr/build.zig

161 lines
4.9 KiB
Zig

const std = @import("std");
const GitVersion = @import("build/GitVersion.zig");
const Coverage = @import("build/Coverage.zig");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const httpz = b.dependency("httpz", .{
.target = target,
.optimize = optimize,
});
const zeit = b.dependency("zeit", .{
.target = target,
.optimize = optimize,
});
const openflights = b.dependency("openflights", .{});
const maxminddb_upstream = b.dependency("maxminddb", .{});
// Build sunriset as a static library
const sunriset = b.addLibrary(.{
.name = "sunriset",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
sunriset.addIncludePath(b.path("libs/sunriset"));
sunriset.addCSourceFiles(.{
.root = b.path("libs/sunriset"),
.files = &.{
"sunriset.c",
},
.flags = &.{ "-D_DEFAULT_SOURCE", "-DSUNRISET_NO_MAIN" },
});
sunriset.linkLibC();
sunriset.linkSystemLibrary("m");
// Build phoon as a static library
const phoon = b.addLibrary(.{
.name = "phoon",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
phoon.addIncludePath(b.path("libs/phoon_14Aug2014"));
phoon.addCSourceFiles(.{
.root = b.path("libs/phoon_14Aug2014"),
.files = &.{
"astro.c",
"date_parse.c",
},
.flags = &.{ "-std=c99", "-D_DEFAULT_SOURCE" },
});
phoon.linkLibC();
phoon.linkSystemLibrary("m");
// Build libmaxminddb as a static library
const maxminddb = b.addLibrary(.{
.name = "maxminddb",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
// Generate maxminddb_config.h
const maxminddb_config = b.addConfigHeader(.{
.style = .blank,
.include_path = "maxminddb_config.h",
}, .{
.PACKAGE_VERSION = "1.11.0",
.MMDB_UINT128_USING_MODE = 1,
});
maxminddb.addConfigHeader(maxminddb_config);
maxminddb.addIncludePath(maxminddb_upstream.path("include"));
maxminddb.addIncludePath(maxminddb_upstream.path("src"));
maxminddb.addCSourceFiles(.{
.root = maxminddb_upstream.path(""),
.files = &.{
"src/data-pool.c",
"src/maxminddb.c",
},
.flags = &.{},
});
maxminddb.installHeadersDirectory(maxminddb_upstream.path("include"), "", .{});
const version = GitVersion.getVersion(b, .{});
const download_geoip = b.option(bool, "download-geoip", "Download GeoIP database for tests") orelse false;
const build_options = b.addOptions();
build_options.addOption([]const u8, "version", version);
build_options.addOption(bool, "download_geoip", download_geoip);
const root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
root_module.addImport("httpz", httpz.module("httpz"));
root_module.addImport("zeit", zeit.module("zeit"));
root_module.addAnonymousImport("airports.dat", .{
.root_source_file = openflights.path("data/airports.dat"),
});
root_module.addOptions("build_options", build_options);
root_module.addIncludePath(maxminddb_upstream.path("include"));
root_module.addIncludePath(b.path("libs/phoon_14Aug2014"));
root_module.addIncludePath(b.path("libs/sunriset"));
root_module.addConfigHeader(maxminddb_config);
const libs = &[_]*std.Build.Step.Compile{
maxminddb,
phoon,
sunriset,
};
const exe = b.addExecutable(.{
.name = "wttr",
.root_module = root_module,
});
configureCompilationUnit(exe, libs);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const tests = b.addTest(.{ .root_module = root_module });
configureCompilationUnit(tests, libs);
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_tests.step);
// Coverage step
const cov_step = Coverage.addCoverageStep(b, root_module, exe.name);
configureCompilationUnit(cov_step.test_exe, libs);
}
fn configureCompilationUnit(compile: *std.Build.Step.Compile, libs: []const *std.Build.Step.Compile) void {
for (libs) |lib| compile.linkLibrary(lib);
compile.linkLibC();
}