|
| 1 | +const std = @import("std"); |
| 2 | +const builtin = @import("builtin"); |
| 3 | + |
| 4 | +const Build = std.Build; |
| 5 | +const OptimizeMode = std.builtin.OptimizeMode; |
| 6 | +const CrossTarget = std.zig.CrossTarget; |
| 7 | +const Compile = Build.Step.Compile; |
| 8 | +const Module = Build.Module; |
| 9 | + |
| 10 | +const min_zig_string = "0.11.0"; |
| 11 | + |
| 12 | +const default_isStatic = true; |
| 13 | +const default_enableTLS = false; |
| 14 | + |
| 15 | +const current_zig = builtin.zig_version; |
| 16 | + |
| 17 | +comptime { |
| 18 | + const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable; |
| 19 | + if (current_zig.order(min_zig) == .lt) { |
| 20 | + @compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig })); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +pub fn build(b: *std.Build) void { |
| 25 | + const isStatic = b.option(bool, "is_static", "whether lib is static") orelse default_isStatic; |
| 26 | + const enableTLS = b.option(bool, "enable_tls", "whether lib enable tls") orelse default_enableTLS; |
| 27 | + const target = b.standardTargetOptions(.{}); |
| 28 | + const optimize = b.standardOptimizeOption(.{}); |
| 29 | + |
| 30 | + if (enableTLS) { |
| 31 | + std.log.info("enable TLS support", .{}); |
| 32 | + if (!target.isNative()) { |
| 33 | + std.log.info("when enable tls, not support cross compile", .{}); |
| 34 | + std.os.exit(1); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + const webui = build_webui(b, optimize, target, isStatic, enableTLS); |
| 39 | + webui.linkLibrary(build_civetweb(b, optimize, target, isStatic, enableTLS)); |
| 40 | + |
| 41 | + webui.installHeader("include/webui.h", "webui.h"); |
| 42 | + |
| 43 | + b.installArtifact(webui); |
| 44 | +} |
| 45 | + |
| 46 | +fn build_webui(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static: bool, enable_tls: bool) *Compile { |
| 47 | + const name = "webui"; |
| 48 | + const webui = if (is_static) b.addStaticLibrary(.{ .name = name, .target = target, .optimize = optimize }) else b.addSharedLibrary(.{ .name = name, .target = target, .optimize = optimize }); |
| 49 | + |
| 50 | + webui.addCSourceFile(.{ |
| 51 | + .file = .{ .path = "src/webui.c" }, |
| 52 | + .flags = if (enable_tls) |
| 53 | + &[_][]const u8{ "-DNO_SSL", "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1" } |
| 54 | + else |
| 55 | + &[_][]const u8{"-DNO_SSL"}, |
| 56 | + }); |
| 57 | + |
| 58 | + webui.linkLibC(); |
| 59 | + |
| 60 | + webui.addIncludePath(.{ .path = "include" }); |
| 61 | + |
| 62 | + return webui; |
| 63 | +} |
| 64 | + |
| 65 | +fn build_civetweb(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static: bool, enable_tls: bool) *Compile { |
| 66 | + const name = "civetweb"; |
| 67 | + const civetweb = if (is_static) b.addStaticLibrary(.{ .name = name, .target = target, .optimize = optimize }) else b.addSharedLibrary(.{ .name = name, .target = target, .optimize = optimize }); |
| 68 | + |
| 69 | + civetweb.addIncludePath(.{ .path = "include" }); |
| 70 | + |
| 71 | + const extra_flags = if (target.os_tag == .windows) "-DMUST_IMPLEMENT_CLOCK_GETTIME" else ""; |
| 72 | + |
| 73 | + const cflags = if (enable_tls) [_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-DWEBUI_TLS", "-DNO_SSL_DL", "-DOPENSSL_API_1_1", extra_flags } else [_][]const u8{ "-DNDEBUG", "-DNO_CACHING", "-DNO_CGI", "-DUSE_WEBSOCKET", "-DNO_SSL", extra_flags, "", "" }; |
| 74 | + |
| 75 | + civetweb.addCSourceFile(.{ |
| 76 | + .file = .{ .path = "src/civetweb/civetweb.c" }, |
| 77 | + .flags = &cflags, |
| 78 | + }); |
| 79 | + |
| 80 | + civetweb.linkLibC(); |
| 81 | + |
| 82 | + if (target.os_tag == .windows) { |
| 83 | + civetweb.linkSystemLibrary("ws2_32"); |
| 84 | + if (enable_tls) { |
| 85 | + civetweb.linkSystemLibrary("bcrypt"); |
| 86 | + } |
| 87 | + } |
| 88 | + if (enable_tls) { |
| 89 | + civetweb.linkSystemLibrary("ssl"); |
| 90 | + civetweb.linkSystemLibrary("crypto"); |
| 91 | + } |
| 92 | + |
| 93 | + return civetweb; |
| 94 | +} |
0 commit comments