|
1 | 1 | const std = @import("std"); |
2 | 2 |
|
| 3 | +const sources_crypto = [_][]const u8{ |
| 4 | + "aes.c", |
| 5 | + "aesce.c", |
| 6 | + "aesni.c", |
| 7 | + "aria.c", |
| 8 | + "asn1parse.c", |
| 9 | + "asn1write.c", |
| 10 | + "base64.c", |
| 11 | + "bignum.c", |
| 12 | + "bignum_core.c", |
| 13 | + "bignum_mod.c", |
| 14 | + "bignum_mod_raw.c", |
| 15 | + "block_cipher.c", |
| 16 | + "camellia.c", |
| 17 | + "ccm.c", |
| 18 | + "chacha20.c", |
| 19 | + "chachapoly.c", |
| 20 | + "cipher.c", |
| 21 | + "cipher_wrap.c", |
| 22 | + "cmac.c", |
| 23 | + "constant_time.c", |
| 24 | + "ctr_drbg.c", |
| 25 | + "des.c", |
| 26 | + "dhm.c", |
| 27 | + "ecdh.c", |
| 28 | + "ecdsa.c", |
| 29 | + "ecjpake.c", |
| 30 | + "ecp.c", |
| 31 | + "ecp_curves.c", |
| 32 | + "ecp_curves_new.c", |
| 33 | + "entropy.c", |
| 34 | + "entropy_poll.c", |
| 35 | + "error.c", |
| 36 | + "gcm.c", |
| 37 | + "hkdf.c", |
| 38 | + "hmac_drbg.c", |
| 39 | + "lmots.c", |
| 40 | + "lms.c", |
| 41 | + "md.c", |
| 42 | + "md5.c", |
| 43 | + "memory_buffer_alloc.c", |
| 44 | + "nist_kw.c", |
| 45 | + "oid.c", |
| 46 | + "padlock.c", |
| 47 | + "pem.c", |
| 48 | + "pk.c", |
| 49 | + "pk_ecc.c", |
| 50 | + "pk_wrap.c", |
| 51 | + "pkcs12.c", |
| 52 | + "pkcs5.c", |
| 53 | + "pkparse.c", |
| 54 | + "pkwrite.c", |
| 55 | + "platform.c", |
| 56 | + "platform_util.c", |
| 57 | + "poly1305.c", |
| 58 | + "psa_crypto.c", |
| 59 | + "psa_crypto_aead.c", |
| 60 | + "psa_crypto_cipher.c", |
| 61 | + "psa_crypto_client.c", |
| 62 | + "psa_crypto_driver_wrappers_no_static.c", |
| 63 | + "psa_crypto_ecp.c", |
| 64 | + "psa_crypto_ffdh.c", |
| 65 | + "psa_crypto_hash.c", |
| 66 | + "psa_crypto_mac.c", |
| 67 | + "psa_crypto_pake.c", |
| 68 | + "psa_crypto_rsa.c", |
| 69 | + "psa_crypto_se.c", |
| 70 | + "psa_crypto_slot_management.c", |
| 71 | + "psa_crypto_storage.c", |
| 72 | + "psa_its_file.c", |
| 73 | + "psa_util.c", |
| 74 | + "ripemd160.c", |
| 75 | + "rsa.c", |
| 76 | + "rsa_alt_helpers.c", |
| 77 | + "sha1.c", |
| 78 | + "sha256.c", |
| 79 | + "sha3.c", |
| 80 | + "sha512.c", |
| 81 | + "threading.c", |
| 82 | + "timing.c", |
| 83 | + "version.c", |
| 84 | + "version_features.c", |
| 85 | +}; |
| 86 | + |
| 87 | +const sources_x509 = [_][]const u8{ |
| 88 | + "pkcs7.c", |
| 89 | + "x509.c", |
| 90 | + "x509_create.c", |
| 91 | + "x509_crl.c", |
| 92 | + "x509_crt.c", |
| 93 | + "x509_csr.c", |
| 94 | + "x509write.c", |
| 95 | + "x509write_crt.c", |
| 96 | + "x509write_csr.c", |
| 97 | +}; |
| 98 | + |
| 99 | +const sources_tls = [_][]const u8{ |
| 100 | + "debug.c", |
| 101 | + "mps_reader.c", |
| 102 | + "mps_trace.c", |
| 103 | + "net_sockets.c", |
| 104 | + "ssl_cache.c", |
| 105 | + "ssl_ciphersuites.c", |
| 106 | + "ssl_client.c", |
| 107 | + "ssl_cookie.c", |
| 108 | + "ssl_debug_helpers_generated.c", |
| 109 | + "ssl_msg.c", |
| 110 | + "ssl_ticket.c", |
| 111 | + "ssl_tls.c", |
| 112 | + "ssl_tls12_client.c", |
| 113 | + "ssl_tls12_server.c", |
| 114 | + "ssl_tls13_client.c", |
| 115 | + "ssl_tls13_generic.c", |
| 116 | + "ssl_tls13_keys.c", |
| 117 | + "ssl_tls13_server.c", |
| 118 | +}; |
| 119 | + |
3 | 120 | pub fn build(b: *std.Build) void { |
4 | 121 | const target = b.standardTargetOptions(.{}); |
5 | 122 | const optimize = b.standardOptimizeOption(.{}); |
6 | 123 |
|
7 | 124 | const upstream = b.dependency("mbedtls", .{}); |
8 | 125 |
|
9 | | - const mod = b.createModule(.{ |
10 | | - .link_libc = true, |
11 | | - .target = target, |
12 | | - .optimize = optimize, |
| 126 | + const lib = b.addLibrary(.{ |
| 127 | + .name = "mbedtls", |
| 128 | + .linkage = .static, |
| 129 | + .root_module = b.createModule(.{ |
| 130 | + .link_libc = true, |
| 131 | + .target = target, |
| 132 | + .optimize = optimize, |
| 133 | + }), |
13 | 134 | }); |
| 135 | + lib.addIncludePath(upstream.path("include/")); |
| 136 | + lib.installHeadersDirectory(upstream.path("include/psa"), "psa", .{}); |
| 137 | + lib.installHeadersDirectory(upstream.path("include/mbedtls"), "mbedtls", .{}); |
| 138 | + |
| 139 | + // The original mbedtls build builds three separate libraries that you can link separately, |
| 140 | + // but for the sake of simplicity we're making one. |
| 141 | + lib.addCSourceFiles(.{ .root = upstream.path("library"), .files = &sources_crypto }); |
| 142 | + lib.addCSourceFiles(.{ .root = upstream.path("library"), .files = &sources_x509 }); |
| 143 | + lib.addCSourceFiles(.{ .root = upstream.path("library"), .files = &sources_tls }); |
| 144 | + |
| 145 | + switch (target.result.os.tag) { |
| 146 | + .windows => { |
| 147 | + lib.root_module.linkSystemLibrary("bcrypt", .{}); |
| 148 | + }, |
| 149 | + .freebsd => { |
| 150 | + // mbedtls contains a bug: it explicitly defines _POSIX_C_SOURCE |
| 151 | + // despite using the explicit_bzero function, which is only defined as a GNU extension. |
| 152 | + // On Linux, _GNU_SOURCE explicitly makes it available, while on FreeBSD |
| 153 | + // this results in an `implicit-function-declaration` warning, which zig always converts to an error. |
| 154 | + // We explicitly expose all BSD* functions to make this available. |
| 155 | + lib.root_module.addCMacro("__BSD_VISIBLE", "1"); |
| 156 | + }, |
| 157 | + else => {} |
| 158 | + } |
14 | 159 |
|
15 | | - _ = mod; // stub |
16 | | - _ = upstream; // stub |
| 160 | + b.installArtifact(lib); |
17 | 161 |
|
18 | 162 | // Smoke unit test |
19 | 163 | const test_mod = b.addModule("test", .{ |
20 | 164 | .root_source_file = b.path("tests.zig"), |
21 | 165 | .target = target, |
22 | 166 | .optimize = optimize, |
23 | 167 | }); |
24 | | - // TODO: mod.linkLibrary(lib); |
| 168 | + test_mod.linkLibrary(lib); |
25 | 169 |
|
26 | 170 | const run_mod_tests = b.addRunArtifact(b.addTest(.{ .root_module = test_mod })); |
27 | 171 |
|
|
0 commit comments