|
| 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(.{ |
| 49 | + .name = name, |
| 50 | + .target = target, |
| 51 | + .optimize = optimize, |
| 52 | + }) else b.addSharedLibrary(.{ |
| 53 | + .name = name, |
| 54 | + .target = target, |
| 55 | + .optimize = optimize, |
| 56 | + }); |
| 57 | + |
| 58 | + webui.addCSourceFile(.{ |
| 59 | + .file = .{ |
| 60 | + .path = "src/webui.c", |
| 61 | + }, |
| 62 | + .flags = if (enable_tls) |
| 63 | + &[_][]const u8{ |
| 64 | + "-DNO_SSL", |
| 65 | + "-DWEBUI_TLS", |
| 66 | + "-DNO_SSL_DL", |
| 67 | + "-DOPENSSL_API_1_1", |
| 68 | + } |
| 69 | + else |
| 70 | + &[_][]const u8{ |
| 71 | + "-DNO_SSL", |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + webui.linkLibC(); |
| 76 | + |
| 77 | + webui.addIncludePath(.{ |
| 78 | + .path = "include", |
| 79 | + }); |
| 80 | + |
| 81 | + return webui; |
| 82 | +} |
| 83 | + |
| 84 | +fn build_civetweb(b: *Build, optimize: OptimizeMode, target: CrossTarget, is_static: bool, enable_tls: bool) *Compile { |
| 85 | + const name = "civetweb"; |
| 86 | + const civetweb = if (is_static) b.addStaticLibrary(.{ |
| 87 | + .name = name, |
| 88 | + .target = target, |
| 89 | + .optimize = optimize, |
| 90 | + }) else b.addSharedLibrary(.{ |
| 91 | + .name = name, |
| 92 | + .target = target, |
| 93 | + .optimize = optimize, |
| 94 | + }); |
| 95 | + |
| 96 | + civetweb.addIncludePath(.{ |
| 97 | + .path = "include", |
| 98 | + }); |
| 99 | + |
| 100 | + const cflags = if (target.os_tag == .windows and !enable_tls) &[_][]const u8{ |
| 101 | + "-DNO_SSL", |
| 102 | + "-DNDEBUG", |
| 103 | + "-DNO_CACHING", |
| 104 | + "-DNO_CGI", |
| 105 | + "-DUSE_WEBSOCKET", |
| 106 | + "-DMUST_IMPLEMENT_CLOCK_GETTIME", |
| 107 | + } else if (target.os_tag == .windows and enable_tls) &[_][]const u8{ |
| 108 | + "-DNDEBUG", |
| 109 | + "-DNO_CACHING", |
| 110 | + "-DNO_CGI", |
| 111 | + "-DUSE_WEBSOCKET", |
| 112 | + "-DWEBUI_TLS", |
| 113 | + "-DNO_SSL_DL", |
| 114 | + "-DOPENSSL_API_1_1", |
| 115 | + "-DMUST_IMPLEMENT_CLOCK_GETTIME", |
| 116 | + } else if (target.os_tag != .windows and enable_tls) |
| 117 | + &[_][]const u8{ |
| 118 | + "-DNDEBUG", |
| 119 | + "-DNO_CACHING", |
| 120 | + "-DNO_CGI", |
| 121 | + "-DUSE_WEBSOCKET", |
| 122 | + "-DWEBUI_TLS", |
| 123 | + "-DNO_SSL_DL", |
| 124 | + "-DOPENSSL_API_1_1", |
| 125 | + } |
| 126 | + else |
| 127 | + &[_][]const u8{ |
| 128 | + "-DNO_SSL", |
| 129 | + "-DNDEBUG", |
| 130 | + "-DNO_CACHING", |
| 131 | + "-DNO_CGI", |
| 132 | + "-DUSE_WEBSOCKET", |
| 133 | + }; |
| 134 | + |
| 135 | + civetweb.addCSourceFile(.{ |
| 136 | + .file = .{ |
| 137 | + .path = "src/civetweb/civetweb.c", |
| 138 | + }, |
| 139 | + .flags = cflags, |
| 140 | + }); |
| 141 | + |
| 142 | + civetweb.linkLibC(); |
| 143 | + |
| 144 | + if (target.os_tag == .windows) { |
| 145 | + civetweb.linkSystemLibrary("ws2_32"); |
| 146 | + if (enable_tls) { |
| 147 | + civetweb.linkSystemLibrary("bcrypt"); |
| 148 | + } |
| 149 | + } |
| 150 | + if (enable_tls) { |
| 151 | + civetweb.linkSystemLibrary("ssl"); |
| 152 | + civetweb.linkSystemLibrary("crypto"); |
| 153 | + } |
| 154 | + |
| 155 | + return civetweb; |
| 156 | +} |
0 commit comments