| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const mem = std.mem; |
| 2 | 3 | const path = std.fs.path; |
| 3 | 4 | |
| 4 | 5 | const Allocator = std.mem.Allocator; |
| ... | ... | @@ -7,7 +8,44 @@ const build_options = @import("build_options"); |
| 7 | 8 | const target_util = @import("target.zig"); |
| 8 | 9 | const musl = @import("musl.zig"); |
| 9 | 10 | |
| 10 | | pub fn buildWasiLibcSysroot(comp: *Compilation) !void { |
| 11 | pub const CRTFile = enum { |
| 12 | crt1_o, |
| 13 | crt1_reactor_o, |
| 14 | crt1_command_o, |
| 15 | libc_a, |
| 16 | libwasi_emulated_process_clocks_a, |
| 17 | libwasi_emulated_getpid_a, |
| 18 | libwasi_emulated_mman_a, |
| 19 | libwasi_emulated_signal_a, |
| 20 | }; |
| 21 | |
| 22 | pub fn getEmulatedLibCRTFile(lib_name: []const u8) ?CRTFile { |
| 23 | if (mem.eql(u8, lib_name, "wasi-emulated-process-clocks")) { |
| 24 | return .libwasi_emulated_process_clocks_a; |
| 25 | } |
| 26 | if (mem.eql(u8, lib_name, "wasi-emulated-getpid")) { |
| 27 | return .libwasi_emulated_getpid_a; |
| 28 | } |
| 29 | if (mem.eql(u8, lib_name, "wasi-emulated-mman")) { |
| 30 | return .libwasi_emulated_mman_a; |
| 31 | } |
| 32 | if (mem.eql(u8, lib_name, "wasi-emulated-signal")) { |
| 33 | return .libwasi_emulated_signal_a; |
| 34 | } |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | pub fn emulatedLibCRFileLibName(crt_file: CRTFile) []const u8 { |
| 39 | return switch (crt_file) { |
| 40 | .libwasi_emulated_process_clocks_a => "libwasi-emulated-process-clocks.a", |
| 41 | .libwasi_emulated_getpid_a => "libwasi-emulated-getpid.a", |
| 42 | .libwasi_emulated_mman_a => "libwasi-emulated-mman.a", |
| 43 | .libwasi_emulated_signal_a => "libwasi-emulated-signal.a", |
| 44 | else => unreachable, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void { |
| 11 | 49 | if (!build_options.have_llvm) { |
| 12 | 50 | return error.ZigCompilerNotBuiltWithLLVMExtensions; |
| 13 | 51 | } |
| ... | ... | @@ -17,190 +55,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void { |
| 17 | 55 | defer arena_allocator.deinit(); |
| 18 | 56 | const arena = &arena_allocator.allocator; |
| 19 | 57 | |
| 20 | | { |
| 21 | | // Compile crt sources. |
| 22 | | var args = std.ArrayList([]const u8).init(arena); |
| 23 | | try addCCArgs(comp, arena, &args, false); |
| 24 | | try args.appendSlice(&[_][]const u8{ |
| 25 | | "-I", |
| 26 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 27 | | "libc", |
| 28 | | "wasi", |
| 29 | | "libc-bottom-half", |
| 30 | | "headers", |
| 31 | | "private", |
| 32 | | }), |
| 58 | switch (crt_file) { |
| 59 | .crt1_o => { |
| 60 | var args = std.ArrayList([]const u8).init(arena); |
| 61 | try addCCArgs(comp, arena, &args, false); |
| 62 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 63 | return comp.build_crt_file("crt1", .Obj, &[1]Compilation.CSourceFile{ |
| 64 | .{ |
| 65 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 66 | "libc", try sanitize(arena, crt1_src_file), |
| 67 | }), |
| 68 | .extra_flags = args.items, |
| 69 | }, |
| 70 | }); |
| 71 | }, |
| 72 | .crt1_reactor_o => { |
| 73 | var args = std.ArrayList([]const u8).init(arena); |
| 74 | try addCCArgs(comp, arena, &args, false); |
| 75 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 76 | return comp.build_crt_file("crt1-reactor", .Obj, &[1]Compilation.CSourceFile{ |
| 77 | .{ |
| 78 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 79 | "libc", try sanitize(arena, crt1_reactor_src_file), |
| 80 | }), |
| 81 | .extra_flags = args.items, |
| 82 | }, |
| 83 | }); |
| 84 | }, |
| 85 | .crt1_command_o => { |
| 86 | var args = std.ArrayList([]const u8).init(arena); |
| 87 | try addCCArgs(comp, arena, &args, false); |
| 88 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 89 | return comp.build_crt_file("crt1-command", .Obj, &[1]Compilation.CSourceFile{ |
| 90 | .{ |
| 91 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 92 | "libc", try sanitize(arena, crt1_command_src_file), |
| 93 | }), |
| 94 | .extra_flags = args.items, |
| 95 | }, |
| 96 | }); |
| 97 | }, |
| 98 | .libc_a => { |
| 99 | var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 100 | |
| 101 | { |
| 102 | // Compile dlmalloc. |
| 103 | var args = std.ArrayList([]const u8).init(arena); |
| 104 | try addCCArgs(comp, arena, &args, true); |
| 105 | try args.appendSlice(&[_][]const u8{ |
| 106 | "-I", |
| 107 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 108 | "libc", |
| 109 | "wasi", |
| 110 | "dlmalloc", |
| 111 | "include", |
| 112 | }), |
| 113 | }); |
| 33 | 114 | |
| 34 | | "-I", |
| 35 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 36 | | "libc", |
| 37 | | "wasi", |
| 38 | | "libc-bottom-half", |
| 39 | | "cloudlibc", |
| 40 | | "src", |
| 41 | | "include", |
| 42 | | }), |
| 115 | for (dlmalloc_src_files) |file_path| { |
| 116 | try libc_sources.append(.{ |
| 117 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 118 | "libc", try sanitize(arena, file_path), |
| 119 | }), |
| 120 | .extra_flags = args.items, |
| 121 | }); |
| 122 | } |
| 123 | } |
| 43 | 124 | |
| 44 | | "-I", |
| 45 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 46 | | "libc", |
| 47 | | "wasi", |
| 48 | | "libc-bottom-half", |
| 49 | | "cloudlibc", |
| 50 | | "src", |
| 51 | | }), |
| 52 | | }); |
| 125 | { |
| 126 | // Compile libc-bottom-half. |
| 127 | var args = std.ArrayList([]const u8).init(arena); |
| 128 | try addCCArgs(comp, arena, &args, true); |
| 129 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 53 | 130 | |
| 54 | | var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 55 | | for (crt_src_files) |file_path| { |
| 56 | | try comp_sources.append(.{ |
| 57 | | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 58 | | "libc", try sanitize(arena, file_path), |
| 59 | | }), |
| 60 | | .extra_flags = args.items, |
| 61 | | }); |
| 62 | | } |
| 63 | | try comp.build_crt_file("crt", .Obj, comp_sources.items); |
| 64 | | } |
| 131 | for (libc_bottom_half_src_files) |file_path| { |
| 132 | try libc_sources.append(.{ |
| 133 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 134 | "libc", try sanitize(arena, file_path), |
| 135 | }), |
| 136 | .extra_flags = args.items, |
| 137 | }); |
| 138 | } |
| 139 | } |
| 65 | 140 | |
| 66 | | { |
| 67 | | // Compile WASI libc (sysroot). |
| 68 | | var comp_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 141 | { |
| 142 | // Compile libc-top-half. |
| 143 | var args = std.ArrayList([]const u8).init(arena); |
| 144 | try addCCArgs(comp, arena, &args, true); |
| 145 | try addLibcTopHalfIncludes(comp, arena, &args); |
| 146 | |
| 147 | for (libc_top_half_src_files) |file_path| { |
| 148 | try libc_sources.append(.{ |
| 149 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 150 | "libc", try sanitize(arena, file_path), |
| 151 | }), |
| 152 | .extra_flags = args.items, |
| 153 | }); |
| 154 | } |
| 155 | } |
| 69 | 156 | |
| 70 | | { |
| 71 | | // Compile dlmalloc. |
| 157 | try comp.build_crt_file("c", .Lib, libc_sources.items); |
| 158 | }, |
| 159 | .libwasi_emulated_process_clocks_a => { |
| 72 | 160 | var args = std.ArrayList([]const u8).init(arena); |
| 73 | 161 | try addCCArgs(comp, arena, &args, true); |
| 74 | | try args.appendSlice(&[_][]const u8{ |
| 75 | | "-I", |
| 76 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 77 | | "libc", |
| 78 | | "wasi", |
| 79 | | "dlmalloc", |
| 80 | | "include", |
| 81 | | }), |
| 82 | | }); |
| 162 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 83 | 163 | |
| 84 | | for (dlmalloc_src_files) |file_path| { |
| 85 | | try comp_sources.append(.{ |
| 164 | var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 165 | for (emulated_process_clocks_src_files) |file_path| { |
| 166 | try emu_clocks_sources.append(.{ |
| 86 | 167 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 87 | 168 | "libc", try sanitize(arena, file_path), |
| 88 | 169 | }), |
| 89 | 170 | .extra_flags = args.items, |
| 90 | 171 | }); |
| 91 | 172 | } |
| 92 | | } |
| 93 | | |
| 94 | | { |
| 95 | | // Compile libc-bottom-half. |
| 173 | try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items); |
| 174 | }, |
| 175 | .libwasi_emulated_getpid_a => { |
| 96 | 176 | var args = std.ArrayList([]const u8).init(arena); |
| 97 | 177 | try addCCArgs(comp, arena, &args, true); |
| 98 | | try args.appendSlice(&[_][]const u8{ |
| 99 | | "-I", |
| 100 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 101 | | "libc", |
| 102 | | "wasi", |
| 103 | | "libc-bottom-half", |
| 104 | | "headers", |
| 105 | | "private", |
| 106 | | }), |
| 107 | | |
| 108 | | "-I", |
| 109 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 110 | | "libc", |
| 111 | | "wasi", |
| 112 | | "libc-bottom-half", |
| 113 | | "cloudlibc", |
| 114 | | "src", |
| 115 | | }), |
| 116 | | |
| 117 | | "-I", |
| 118 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 119 | | "libc", |
| 120 | | "wasi", |
| 121 | | "libc-bottom-half", |
| 122 | | "cloudlibc", |
| 123 | | "src", |
| 124 | | "include", |
| 125 | | }), |
| 126 | | }); |
| 178 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 127 | 179 | |
| 128 | | for (libc_bottom_half_src_files) |file_path| { |
| 129 | | try comp_sources.append(.{ |
| 180 | var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 181 | for (emulated_getpid_src_files) |file_path| { |
| 182 | try emu_getpid_sources.append(.{ |
| 130 | 183 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 131 | 184 | "libc", try sanitize(arena, file_path), |
| 132 | 185 | }), |
| 133 | 186 | .extra_flags = args.items, |
| 134 | 187 | }); |
| 135 | 188 | } |
| 136 | | } |
| 137 | | |
| 138 | | { |
| 139 | | // Compile libc-top-half. |
| 189 | try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items); |
| 190 | }, |
| 191 | .libwasi_emulated_mman_a => { |
| 140 | 192 | var args = std.ArrayList([]const u8).init(arena); |
| 141 | 193 | try addCCArgs(comp, arena, &args, true); |
| 142 | | try args.appendSlice(&[_][]const u8{ |
| 143 | | "-I", |
| 144 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 145 | | "libc", |
| 146 | | "wasi", |
| 147 | | "libc-top-half", |
| 148 | | "musl", |
| 149 | | "src", |
| 150 | | "include", |
| 151 | | }), |
| 152 | | |
| 153 | | "-I", |
| 154 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 155 | | "libc", |
| 156 | | "wasi", |
| 157 | | "libc-top-half", |
| 158 | | "musl", |
| 159 | | "src", |
| 160 | | "internal", |
| 161 | | }), |
| 162 | | |
| 163 | | "-I", |
| 164 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 165 | | "libc", |
| 166 | | "wasi", |
| 167 | | "libc-top-half", |
| 168 | | "musl", |
| 169 | | "arch", |
| 170 | | "wasm32", |
| 171 | | }), |
| 194 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 172 | 195 | |
| 173 | | "-I", |
| 174 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 175 | | "libc", |
| 176 | | "wasi", |
| 177 | | "libc-top-half", |
| 178 | | "musl", |
| 179 | | "arch", |
| 180 | | "generic", |
| 181 | | }), |
| 182 | | |
| 183 | | "-I", |
| 184 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 185 | | "libc", |
| 186 | | "wasi", |
| 187 | | "libc-top-half", |
| 188 | | "headers", |
| 189 | | "private", |
| 190 | | }), |
| 191 | | }); |
| 192 | | |
| 193 | | for (libc_top_half_src_files) |file_path| { |
| 194 | | try comp_sources.append(.{ |
| 196 | var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 197 | for (emulated_mman_src_files) |file_path| { |
| 198 | try emu_mman_sources.append(.{ |
| 195 | 199 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 196 | 200 | "libc", try sanitize(arena, file_path), |
| 197 | 201 | }), |
| 198 | 202 | .extra_flags = args.items, |
| 199 | 203 | }); |
| 200 | 204 | } |
| 201 | | } |
| 205 | try comp.build_crt_file("wasi-emulated-mman", .Lib, emu_mman_sources.items); |
| 206 | }, |
| 207 | .libwasi_emulated_signal_a => { |
| 208 | var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 209 | |
| 210 | { |
| 211 | var args = std.ArrayList([]const u8).init(arena); |
| 212 | try addCCArgs(comp, arena, &args, true); |
| 202 | 213 | |
| 203 | | try comp.build_crt_file("c", .Lib, comp_sources.items); |
| 214 | for (emulated_signal_bottom_half_src_files) |file_path| { |
| 215 | try emu_signal_sources.append(.{ |
| 216 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 217 | "libc", try sanitize(arena, file_path), |
| 218 | }), |
| 219 | .extra_flags = args.items, |
| 220 | }); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | { |
| 225 | var args = std.ArrayList([]const u8).init(arena); |
| 226 | try addCCArgs(comp, arena, &args, true); |
| 227 | try addLibcTopHalfIncludes(comp, arena, &args); |
| 228 | try args.append("-D_WASI_EMULATED_SIGNAL"); |
| 229 | |
| 230 | for (emulated_signal_top_half_src_files) |file_path| { |
| 231 | try emu_signal_sources.append(.{ |
| 232 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 233 | "libc", try sanitize(arena, file_path), |
| 234 | }), |
| 235 | .extra_flags = args.items, |
| 236 | }); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | try comp.build_crt_file("wasi-emulated-signal", .Lib, emu_signal_sources.items); |
| 241 | }, |
| 204 | 242 | } |
| 205 | 243 | } |
| 206 | 244 | |
| ... | ... | @@ -251,6 +289,99 @@ fn addCCArgs( |
| 251 | 289 | }); |
| 252 | 290 | } |
| 253 | 291 | |
| 292 | fn addLibcBottomHalfIncludes( |
| 293 | comp: *Compilation, |
| 294 | arena: *Allocator, |
| 295 | args: *std.ArrayList([]const u8), |
| 296 | ) error{OutOfMemory}!void { |
| 297 | try args.appendSlice(&[_][]const u8{ |
| 298 | "-I", |
| 299 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 300 | "libc", |
| 301 | "wasi", |
| 302 | "libc-bottom-half", |
| 303 | "headers", |
| 304 | "private", |
| 305 | }), |
| 306 | |
| 307 | "-I", |
| 308 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 309 | "libc", |
| 310 | "wasi", |
| 311 | "libc-bottom-half", |
| 312 | "cloudlibc", |
| 313 | "src", |
| 314 | "include", |
| 315 | }), |
| 316 | |
| 317 | "-I", |
| 318 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 319 | "libc", |
| 320 | "wasi", |
| 321 | "libc-bottom-half", |
| 322 | "cloudlibc", |
| 323 | "src", |
| 324 | }), |
| 325 | }); |
| 326 | } |
| 327 | |
| 328 | fn addLibcTopHalfIncludes( |
| 329 | comp: *Compilation, |
| 330 | arena: *Allocator, |
| 331 | args: *std.ArrayList([]const u8), |
| 332 | ) error{OutOfMemory}!void { |
| 333 | try args.appendSlice(&[_][]const u8{ |
| 334 | "-I", |
| 335 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 336 | "libc", |
| 337 | "wasi", |
| 338 | "libc-top-half", |
| 339 | "musl", |
| 340 | "src", |
| 341 | "include", |
| 342 | }), |
| 343 | |
| 344 | "-I", |
| 345 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 346 | "libc", |
| 347 | "wasi", |
| 348 | "libc-top-half", |
| 349 | "musl", |
| 350 | "src", |
| 351 | "internal", |
| 352 | }), |
| 353 | |
| 354 | "-I", |
| 355 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 356 | "libc", |
| 357 | "wasi", |
| 358 | "libc-top-half", |
| 359 | "musl", |
| 360 | "arch", |
| 361 | "wasm32", |
| 362 | }), |
| 363 | |
| 364 | "-I", |
| 365 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 366 | "libc", |
| 367 | "wasi", |
| 368 | "libc-top-half", |
| 369 | "musl", |
| 370 | "arch", |
| 371 | "generic", |
| 372 | }), |
| 373 | |
| 374 | "-I", |
| 375 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 376 | "libc", |
| 377 | "wasi", |
| 378 | "libc-top-half", |
| 379 | "headers", |
| 380 | "private", |
| 381 | }), |
| 382 | }); |
| 383 | } |
| 384 | |
| 254 | 385 | const dlmalloc_src_files = [_][]const u8{ |
| 255 | 386 | "wasi/dlmalloc/src/dlmalloc.c", |
| 256 | 387 | }; |
| ... | ... | @@ -1005,11 +1136,9 @@ const libc_top_half_src_files = [_][]const u8{ |
| 1005 | 1136 | "wasi/libc-top-half/sources/arc4random.c", |
| 1006 | 1137 | }; |
| 1007 | 1138 | |
| 1008 | | const crt_src_files = &[_][]const u8{ |
| 1009 | | "wasi/libc-bottom-half/crt/crt1.c", |
| 1010 | | "wasi/libc-bottom-half/crt/crt1-command.c", |
| 1011 | | "wasi/libc-bottom-half/crt/crt1-reactor.c", |
| 1012 | | }; |
| 1139 | const crt1_src_file = "wasi/libc-bottom-half/crt/crt1.c"; |
| 1140 | const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c"; |
| 1141 | const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c"; |
| 1013 | 1142 | |
| 1014 | 1143 | const emulated_process_clocks_src_files = &[_][]const u8{ |
| 1015 | 1144 | "wasi/libc-bottom-half/clocks/clock.c", |
| ... | ... | @@ -1025,6 +1154,11 @@ const emulated_mman_src_files = &[_][]const u8{ |
| 1025 | 1154 | "wasi/libc-bottom-half/mman/mman.c", |
| 1026 | 1155 | }; |
| 1027 | 1156 | |
| 1028 | | const emulated_signal_src_files = &[_][]const u8{ |
| 1157 | const emulated_signal_bottom_half_src_files = &[_][]const u8{ |
| 1029 | 1158 | "wasi/libc-bottom-half/signal/signal.c", |
| 1030 | 1159 | }; |
| 1160 | |
| 1161 | const emulated_signal_top_half_src_files = &[_][]const u8{ |
| 1162 | "wasi/libc-top-half/musl/src/signal/psignal.c", |
| 1163 | "wasi/libc-top-half/musl/src/string/strsignal.c", |
| 1164 | }; |