| ... | ... | @@ -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,34 @@ 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 buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void { |
| 11 | 39 | if (!build_options.have_llvm) { |
| 12 | 40 | return error.ZigCompilerNotBuiltWithLLVMExtensions; |
| 13 | 41 | } |
| ... | ... | @@ -17,176 +45,190 @@ pub fn buildWasiLibcSysroot(comp: *Compilation) !void { |
| 17 | 45 | defer arena_allocator.deinit(); |
| 18 | 46 | const arena = &arena_allocator.allocator; |
| 19 | 47 | |
| 20 | | { |
| 21 | | // Compile crt sources. |
| 22 | | var args = std.ArrayList([]const u8).init(arena); |
| 23 | | try addCCArgs(comp, arena, &args, false); |
| 24 | | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 25 | | |
| 26 | | var crt_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 27 | | for (crt_src_files) |file_path| { |
| 28 | | try crt_sources.append(.{ |
| 29 | | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 30 | | "libc", try sanitize(arena, file_path), |
| 31 | | }), |
| 32 | | .extra_flags = args.items, |
| 33 | | }); |
| 34 | | } |
| 35 | | try comp.build_crt_file("crt", .Obj, crt_sources.items); |
| 36 | | } |
| 37 | | |
| 38 | | { |
| 39 | | // Compile WASI libc (sysroot). |
| 40 | | var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 41 | | |
| 42 | | { |
| 43 | | // Compile dlmalloc. |
| 48 | switch (crt_file) { |
| 49 | .crt1_o => { |
| 44 | 50 | var args = std.ArrayList([]const u8).init(arena); |
| 45 | | try addCCArgs(comp, arena, &args, true); |
| 46 | | try args.appendSlice(&[_][]const u8{ |
| 47 | | "-I", |
| 48 | | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 49 | | "libc", |
| 50 | | "wasi", |
| 51 | | "dlmalloc", |
| 52 | | "include", |
| 53 | | }), |
| 51 | try addCCArgs(comp, arena, &args, false); |
| 52 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 53 | return comp.build_crt_file("crt1", .Obj, &[1]Compilation.CSourceFile{ |
| 54 | .{ |
| 55 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 56 | "libc", try sanitize(arena, crt1_src_file), |
| 57 | }), |
| 58 | .extra_flags = args.items, |
| 59 | }, |
| 54 | 60 | }); |
| 55 | | |
| 56 | | for (dlmalloc_src_files) |file_path| { |
| 57 | | try libc_sources.append(.{ |
| 61 | }, |
| 62 | .crt1_reactor_o => { |
| 63 | var args = std.ArrayList([]const u8).init(arena); |
| 64 | try addCCArgs(comp, arena, &args, false); |
| 65 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 66 | return comp.build_crt_file("crt1-reactor", .Obj, &[1]Compilation.CSourceFile{ |
| 67 | .{ |
| 58 | 68 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 59 | | "libc", try sanitize(arena, file_path), |
| 69 | "libc", try sanitize(arena, crt1_reactor_src_file), |
| 60 | 70 | }), |
| 61 | 71 | .extra_flags = args.items, |
| 62 | | }); |
| 63 | | } |
| 64 | | } |
| 65 | | |
| 66 | | { |
| 67 | | // Compile libc-bottom-half. |
| 72 | }, |
| 73 | }); |
| 74 | }, |
| 75 | .crt1_command_o => { |
| 68 | 76 | var args = std.ArrayList([]const u8).init(arena); |
| 69 | | try addCCArgs(comp, arena, &args, true); |
| 77 | try addCCArgs(comp, arena, &args, false); |
| 70 | 78 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 71 | | |
| 72 | | for (libc_bottom_half_src_files) |file_path| { |
| 73 | | try libc_sources.append(.{ |
| 79 | return comp.build_crt_file("crt1-command", .Obj, &[1]Compilation.CSourceFile{ |
| 80 | .{ |
| 74 | 81 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 75 | | "libc", try sanitize(arena, file_path), |
| 82 | "libc", try sanitize(arena, crt1_command_src_file), |
| 76 | 83 | }), |
| 77 | 84 | .extra_flags = args.items, |
| 85 | }, |
| 86 | }); |
| 87 | }, |
| 88 | .libc_a => { |
| 89 | var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 90 | |
| 91 | { |
| 92 | // Compile dlmalloc. |
| 93 | var args = std.ArrayList([]const u8).init(arena); |
| 94 | try addCCArgs(comp, arena, &args, true); |
| 95 | try args.appendSlice(&[_][]const u8{ |
| 96 | "-I", |
| 97 | try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 98 | "libc", |
| 99 | "wasi", |
| 100 | "dlmalloc", |
| 101 | "include", |
| 102 | }), |
| 78 | 103 | }); |
| 104 | |
| 105 | for (dlmalloc_src_files) |file_path| { |
| 106 | try libc_sources.append(.{ |
| 107 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 108 | "libc", try sanitize(arena, file_path), |
| 109 | }), |
| 110 | .extra_flags = args.items, |
| 111 | }); |
| 112 | } |
| 79 | 113 | } |
| 80 | | } |
| 81 | 114 | |
| 82 | | { |
| 83 | | // Compile libc-top-half. |
| 115 | { |
| 116 | // Compile libc-bottom-half. |
| 117 | var args = std.ArrayList([]const u8).init(arena); |
| 118 | try addCCArgs(comp, arena, &args, true); |
| 119 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 120 | |
| 121 | for (libc_bottom_half_src_files) |file_path| { |
| 122 | try libc_sources.append(.{ |
| 123 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 124 | "libc", try sanitize(arena, file_path), |
| 125 | }), |
| 126 | .extra_flags = args.items, |
| 127 | }); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | { |
| 132 | // Compile libc-top-half. |
| 133 | var args = std.ArrayList([]const u8).init(arena); |
| 134 | try addCCArgs(comp, arena, &args, true); |
| 135 | try addLibcTopHalfIncludes(comp, arena, &args); |
| 136 | |
| 137 | for (libc_top_half_src_files) |file_path| { |
| 138 | try libc_sources.append(.{ |
| 139 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 140 | "libc", try sanitize(arena, file_path), |
| 141 | }), |
| 142 | .extra_flags = args.items, |
| 143 | }); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | try comp.build_crt_file("c", .Lib, libc_sources.items); |
| 148 | }, |
| 149 | .libwasi_emulated_process_clocks_a => { |
| 84 | 150 | var args = std.ArrayList([]const u8).init(arena); |
| 85 | 151 | try addCCArgs(comp, arena, &args, true); |
| 86 | | try addLibcTopHalfIncludes(comp, arena, &args); |
| 152 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 87 | 153 | |
| 88 | | for (libc_top_half_src_files) |file_path| { |
| 89 | | try libc_sources.append(.{ |
| 154 | var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 155 | for (emulated_process_clocks_src_files) |file_path| { |
| 156 | try emu_clocks_sources.append(.{ |
| 90 | 157 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 91 | 158 | "libc", try sanitize(arena, file_path), |
| 92 | 159 | }), |
| 93 | 160 | .extra_flags = args.items, |
| 94 | 161 | }); |
| 95 | 162 | } |
| 96 | | } |
| 97 | | |
| 98 | | try comp.build_crt_file("c", .Lib, libc_sources.items); |
| 99 | | } |
| 100 | | |
| 101 | | { |
| 102 | | // Compile emulated process clocks. |
| 103 | | var args = std.ArrayList([]const u8).init(arena); |
| 104 | | try addCCArgs(comp, arena, &args, true); |
| 105 | | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 106 | | |
| 107 | | var emu_clocks_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 108 | | for (emulated_process_clocks_src_files) |file_path| { |
| 109 | | try emu_clocks_sources.append(.{ |
| 110 | | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 111 | | "libc", try sanitize(arena, file_path), |
| 112 | | }), |
| 113 | | .extra_flags = args.items, |
| 114 | | }); |
| 115 | | } |
| 116 | | try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items); |
| 117 | | } |
| 118 | | |
| 119 | | { |
| 120 | | // Compile emulated getpid. |
| 121 | | var args = std.ArrayList([]const u8).init(arena); |
| 122 | | try addCCArgs(comp, arena, &args, true); |
| 123 | | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 124 | | |
| 125 | | var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 126 | | for (emulated_getpid_src_files) |file_path| { |
| 127 | | try emu_getpid_sources.append(.{ |
| 128 | | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 129 | | "libc", try sanitize(arena, file_path), |
| 130 | | }), |
| 131 | | .extra_flags = args.items, |
| 132 | | }); |
| 133 | | } |
| 134 | | try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items); |
| 135 | | } |
| 136 | | |
| 137 | | { |
| 138 | | // Compile emulated mman. |
| 139 | | var args = std.ArrayList([]const u8).init(arena); |
| 140 | | try addCCArgs(comp, arena, &args, true); |
| 141 | | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 142 | | |
| 143 | | var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 144 | | for (emulated_mman_src_files) |file_path| { |
| 145 | | try emu_mman_sources.append(.{ |
| 146 | | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 147 | | "libc", try sanitize(arena, file_path), |
| 148 | | }), |
| 149 | | .extra_flags = args.items, |
| 150 | | }); |
| 151 | | } |
| 152 | | try comp.build_crt_file("wasi-emulated-mman", .Lib, emu_mman_sources.items); |
| 153 | | } |
| 154 | | |
| 155 | | { |
| 156 | | // Compile emulated signals. |
| 157 | | var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 158 | | |
| 159 | | { |
| 163 | try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, emu_clocks_sources.items); |
| 164 | }, |
| 165 | .libwasi_emulated_getpid_a => { |
| 160 | 166 | var args = std.ArrayList([]const u8).init(arena); |
| 161 | 167 | try addCCArgs(comp, arena, &args, true); |
| 168 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 162 | 169 | |
| 163 | | for (emulated_signal_bottom_half_src_files) |file_path| { |
| 164 | | try emu_signal_sources.append(.{ |
| 170 | var emu_getpid_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 171 | for (emulated_getpid_src_files) |file_path| { |
| 172 | try emu_getpid_sources.append(.{ |
| 165 | 173 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 166 | 174 | "libc", try sanitize(arena, file_path), |
| 167 | 175 | }), |
| 168 | 176 | .extra_flags = args.items, |
| 169 | 177 | }); |
| 170 | 178 | } |
| 171 | | } |
| 172 | | |
| 173 | | { |
| 179 | try comp.build_crt_file("wasi-emulated-getpid", .Lib, emu_getpid_sources.items); |
| 180 | }, |
| 181 | .libwasi_emulated_mman_a => { |
| 174 | 182 | var args = std.ArrayList([]const u8).init(arena); |
| 175 | 183 | try addCCArgs(comp, arena, &args, true); |
| 176 | | try addLibcTopHalfIncludes(comp, arena, &args); |
| 177 | | try args.append("-D_WASI_EMULATED_SIGNAL"); |
| 184 | try addLibcBottomHalfIncludes(comp, arena, &args); |
| 178 | 185 | |
| 179 | | for (emulated_signal_top_half_src_files) |file_path| { |
| 180 | | try emu_signal_sources.append(.{ |
| 186 | var emu_mman_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 187 | for (emulated_mman_src_files) |file_path| { |
| 188 | try emu_mman_sources.append(.{ |
| 181 | 189 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 182 | 190 | "libc", try sanitize(arena, file_path), |
| 183 | 191 | }), |
| 184 | 192 | .extra_flags = args.items, |
| 185 | 193 | }); |
| 186 | 194 | } |
| 187 | | } |
| 195 | try comp.build_crt_file("wasi-emulated-mman", .Lib, emu_mman_sources.items); |
| 196 | }, |
| 197 | .libwasi_emulated_signal_a => { |
| 198 | var emu_signal_sources = std.ArrayList(Compilation.CSourceFile).init(arena); |
| 199 | |
| 200 | { |
| 201 | var args = std.ArrayList([]const u8).init(arena); |
| 202 | try addCCArgs(comp, arena, &args, true); |
| 203 | |
| 204 | for (emulated_signal_bottom_half_src_files) |file_path| { |
| 205 | try emu_signal_sources.append(.{ |
| 206 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 207 | "libc", try sanitize(arena, file_path), |
| 208 | }), |
| 209 | .extra_flags = args.items, |
| 210 | }); |
| 211 | } |
| 212 | } |
| 188 | 213 | |
| 189 | | try comp.build_crt_file("wasi-emulated-signal", .Lib, emu_signal_sources.items); |
| 214 | { |
| 215 | var args = std.ArrayList([]const u8).init(arena); |
| 216 | try addCCArgs(comp, arena, &args, true); |
| 217 | try addLibcTopHalfIncludes(comp, arena, &args); |
| 218 | try args.append("-D_WASI_EMULATED_SIGNAL"); |
| 219 | |
| 220 | for (emulated_signal_top_half_src_files) |file_path| { |
| 221 | try emu_signal_sources.append(.{ |
| 222 | .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ |
| 223 | "libc", try sanitize(arena, file_path), |
| 224 | }), |
| 225 | .extra_flags = args.items, |
| 226 | }); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | try comp.build_crt_file("wasi-emulated-signal", .Lib, emu_signal_sources.items); |
| 231 | }, |
| 190 | 232 | } |
| 191 | 233 | } |
| 192 | 234 | |
| ... | ... | @@ -1084,11 +1126,9 @@ const libc_top_half_src_files = [_][]const u8{ |
| 1084 | 1126 | "wasi/libc-top-half/sources/arc4random.c", |
| 1085 | 1127 | }; |
| 1086 | 1128 | |
| 1087 | | const crt_src_files = &[_][]const u8{ |
| 1088 | | "wasi/libc-bottom-half/crt/crt1.c", |
| 1089 | | "wasi/libc-bottom-half/crt/crt1-command.c", |
| 1090 | | "wasi/libc-bottom-half/crt/crt1-reactor.c", |
| 1091 | | }; |
| 1129 | const crt1_src_file = "wasi/libc-bottom-half/crt/crt1.c"; |
| 1130 | const crt1_command_src_file = "wasi/libc-bottom-half/crt/crt1-command.c"; |
| 1131 | const crt1_reactor_src_file = "wasi/libc-bottom-half/crt/crt1-reactor.c"; |
| 1092 | 1132 | |
| 1093 | 1133 | const emulated_process_clocks_src_files = &[_][]const u8{ |
| 1094 | 1134 | "wasi/libc-bottom-half/clocks/clock.c", |