| ... | @@ -53,7 +53,8 @@ pub fn build(b: *Builder) !void { | ... | @@ -53,7 +53,8 @@ pub fn build(b: *Builder) !void { |
| 53 | const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false; | 53 | const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false; |
| 54 | | 54 | |
| 55 | const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false; | 55 | const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false; |
| 56 | const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse false; | 56 | const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false; |
| | 57 | const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse is_stage1; |
| 57 | const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); | 58 | const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); |
| 58 | | 59 | |
| 59 | b.installDirectory(InstallDirectoryOptions{ | 60 | b.installDirectory(InstallDirectoryOptions{ |
| ... | @@ -76,6 +77,8 @@ pub fn build(b: *Builder) !void { | ... | @@ -76,6 +77,8 @@ pub fn build(b: *Builder) !void { |
| 76 | const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source"); | 77 | const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source"); |
| 77 | const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm; | 78 | const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm; |
| 78 | | 79 | |
| | 80 | const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig"; |
| | 81 | |
| 79 | var exe = b.addExecutable("zig", "src/main.zig"); | 82 | var exe = b.addExecutable("zig", "src/main.zig"); |
| 80 | exe.install(); | 83 | exe.install(); |
| 81 | exe.setBuildMode(mode); | 84 | exe.setBuildMode(mode); |
| ... | @@ -86,15 +89,51 @@ pub fn build(b: *Builder) !void { | ... | @@ -86,15 +89,51 @@ pub fn build(b: *Builder) !void { |
| 86 | exe.addBuildOption(bool, "skip_non_native", skip_non_native); | 89 | exe.addBuildOption(bool, "skip_non_native", skip_non_native); |
| 87 | exe.addBuildOption(bool, "have_llvm", enable_llvm); | 90 | exe.addBuildOption(bool, "have_llvm", enable_llvm); |
| 88 | if (enable_llvm) { | 91 | if (enable_llvm) { |
| 89 | const config_h_text = if (config_h_path_option) |config_h_path| | 92 | if (is_stage1) { |
| 90 | try std.fs.cwd().readFileAlloc(b.allocator, toNativePathSep(b, config_h_path), max_config_h_bytes) | 93 | exe.addIncludeDir("src"); |
| 91 | else | 94 | exe.addIncludeDir("deps/SoftFloat-3e/source/include"); |
| 92 | try findAndReadConfigH(b); | 95 | |
| | 96 | const softfloat = b.addStaticLibrary("softfloat", null); |
| | 97 | softfloat.setBuildMode(.ReleaseFast); |
| | 98 | softfloat.setTarget(target); |
| | 99 | softfloat.addIncludeDir("deps/SoftFloat-3e-prebuilt"); |
| | 100 | softfloat.addIncludeDir("deps/SoftFloat-3e/source/8086"); |
| | 101 | softfloat.addIncludeDir("deps/SoftFloat-3e/source/include"); |
| | 102 | softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" }); |
| | 103 | exe.linkLibrary(softfloat); |
| | 104 | |
| | 105 | const exe_cflags = &[_][]const u8{ |
| | 106 | "-std=c++14", |
| | 107 | "-D__STDC_CONSTANT_MACROS", |
| | 108 | "-D__STDC_FORMAT_MACROS", |
| | 109 | "-D__STDC_LIMIT_MACROS", |
| | 110 | "-D_GNU_SOURCE", |
| | 111 | "-fvisibility-inlines-hidden", |
| | 112 | "-fno-exceptions", |
| | 113 | "-fno-rtti", |
| | 114 | "-Werror=type-limits", |
| | 115 | "-Wno-missing-braces", |
| | 116 | "-Wno-comment", |
| | 117 | }; |
| | 118 | exe.addCSourceFiles(&stage1_sources, exe_cflags); |
| | 119 | exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); |
| | 120 | exe.addCSourceFiles(&zig_cpp_sources, exe_cflags); |
| | 121 | } |
| 93 | | 122 | |
| 94 | var ctx = parseConfigH(b, config_h_text); | 123 | for (clang_libs) |lib_name| { |
| 95 | ctx.llvm = try findLLVM(b, ctx.llvm_config_exe); | 124 | exe.linkSystemLibrary(lib_name); |
| | 125 | } |
| | 126 | |
| | 127 | for (lld_libs) |lib_name| { |
| | 128 | exe.linkSystemLibrary(lib_name); |
| | 129 | } |
| 96 | | 130 | |
| 97 | try configureStage2(b, exe, ctx, tracy != null); | 131 | for (llvm_libs) |lib_name| { |
| | 132 | exe.linkSystemLibrary(lib_name); |
| | 133 | } |
| | 134 | |
| | 135 | // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. |
| | 136 | exe.linkSystemLibrary("c++"); |
| 98 | } | 137 | } |
| 99 | if (link_libc) { | 138 | if (link_libc) { |
| 100 | exe.linkLibC(); | 139 | exe.linkLibC(); |
| ... | @@ -141,7 +180,10 @@ pub fn build(b: *Builder) !void { | ... | @@ -141,7 +180,10 @@ pub fn build(b: *Builder) !void { |
| 141 | break :v b.fmt("{}+{}{}", .{ version_string, git_sha_trimmed, dirty_suffix }); | 180 | break :v b.fmt("{}+{}{}", .{ version_string, git_sha_trimmed, dirty_suffix }); |
| 142 | } | 181 | } |
| 143 | }; | 182 | }; |
| 144 | exe.addBuildOption([]const u8, "version", version); | 183 | exe.addBuildOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version)); |
| | 184 | |
| | 185 | const semver = try std.SemanticVersion.parse(version); |
| | 186 | exe.addBuildOption(std.SemanticVersion, "semver", semver); |
| 145 | | 187 | |
| 146 | exe.addBuildOption([]const []const u8, "log_scopes", log_scopes); | 188 | exe.addBuildOption([]const []const u8, "log_scopes", log_scopes); |
| 147 | exe.addBuildOption([]const []const u8, "zir_dumps", zir_dumps); | 189 | exe.addBuildOption([]const []const u8, "zir_dumps", zir_dumps); |
| ... | @@ -226,327 +268,359 @@ pub fn build(b: *Builder) !void { | ... | @@ -226,327 +268,359 @@ pub fn build(b: *Builder) !void { |
| 226 | test_step.dependOn(docs_step); | 268 | test_step.dependOn(docs_step); |
| 227 | } | 269 | } |
| 228 | | 270 | |
| 229 | fn dependOnLib(b: *Builder, lib_exe_obj: anytype, dep: LibraryDep) void { | 271 | const softfloat_sources = [_][]const u8{ |
| 230 | for (dep.libdirs.items) |lib_dir| { | 272 | "deps/SoftFloat-3e/source/8086/f128M_isSignalingNaN.c", |
| 231 | lib_exe_obj.addLibPath(lib_dir); | 273 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF128M.c", |
| 232 | } | 274 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF16UI.c", |
| 233 | const lib_dir = fs.path.join( | 275 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF32UI.c", |
| 234 | b.allocator, | 276 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF64UI.c", |
| 235 | &[_][]const u8{ dep.prefix, "lib" }, | 277 | "deps/SoftFloat-3e/source/8086/s_f128MToCommonNaN.c", |
| 236 | ) catch unreachable; | 278 | "deps/SoftFloat-3e/source/8086/s_f16UIToCommonNaN.c", |
| 237 | for (dep.system_libs.items) |lib| { | 279 | "deps/SoftFloat-3e/source/8086/s_f32UIToCommonNaN.c", |
| 238 | const static_bare_name = if (mem.eql(u8, lib, "curses")) | 280 | "deps/SoftFloat-3e/source/8086/s_f64UIToCommonNaN.c", |
| 239 | @as([]const u8, "libncurses.a") | 281 | "deps/SoftFloat-3e/source/8086/s_propagateNaNF128M.c", |
| 240 | else | 282 | "deps/SoftFloat-3e/source/8086/s_propagateNaNF16UI.c", |
| 241 | b.fmt("lib{}.a", .{lib}); | 283 | "deps/SoftFloat-3e/source/8086/softfloat_raiseFlags.c", |
| 242 | const static_lib_name = fs.path.join( | 284 | "deps/SoftFloat-3e/source/f128M_add.c", |
| 243 | b.allocator, | 285 | "deps/SoftFloat-3e/source/f128M_div.c", |
| 244 | &[_][]const u8{ lib_dir, static_bare_name }, | 286 | "deps/SoftFloat-3e/source/f128M_eq.c", |
| 245 | ) catch unreachable; | 287 | "deps/SoftFloat-3e/source/f128M_eq_signaling.c", |
| 246 | const have_static = fileExists(static_lib_name) catch unreachable; | 288 | "deps/SoftFloat-3e/source/f128M_le.c", |
| 247 | if (have_static) { | 289 | "deps/SoftFloat-3e/source/f128M_le_quiet.c", |
| 248 | lib_exe_obj.addObjectFile(static_lib_name); | 290 | "deps/SoftFloat-3e/source/f128M_lt.c", |
| 249 | } else { | 291 | "deps/SoftFloat-3e/source/f128M_lt_quiet.c", |
| 250 | lib_exe_obj.linkSystemLibrary(lib); | 292 | "deps/SoftFloat-3e/source/f128M_mul.c", |
| 251 | } | 293 | "deps/SoftFloat-3e/source/f128M_mulAdd.c", |
| 252 | } | 294 | "deps/SoftFloat-3e/source/f128M_rem.c", |
| 253 | for (dep.libs.items) |lib| { | 295 | "deps/SoftFloat-3e/source/f128M_roundToInt.c", |
| 254 | lib_exe_obj.addObjectFile(lib); | 296 | "deps/SoftFloat-3e/source/f128M_sqrt.c", |
| 255 | } | 297 | "deps/SoftFloat-3e/source/f128M_sub.c", |
| 256 | for (dep.includes.items) |include_path| { | 298 | "deps/SoftFloat-3e/source/f128M_to_f16.c", |
| 257 | lib_exe_obj.addIncludeDir(include_path); | 299 | "deps/SoftFloat-3e/source/f128M_to_f32.c", |
| 258 | } | 300 | "deps/SoftFloat-3e/source/f128M_to_f64.c", |
| 259 | } | 301 | "deps/SoftFloat-3e/source/f128M_to_i32.c", |
| 260 | | 302 | "deps/SoftFloat-3e/source/f128M_to_i32_r_minMag.c", |
| 261 | fn fileExists(filename: []const u8) !bool { | 303 | "deps/SoftFloat-3e/source/f128M_to_i64.c", |
| 262 | fs.cwd().access(filename, .{}) catch |err| switch (err) { | 304 | "deps/SoftFloat-3e/source/f128M_to_i64_r_minMag.c", |
| 263 | error.FileNotFound => return false, | 305 | "deps/SoftFloat-3e/source/f128M_to_ui32.c", |
| 264 | else => return err, | 306 | "deps/SoftFloat-3e/source/f128M_to_ui32_r_minMag.c", |
| 265 | }; | 307 | "deps/SoftFloat-3e/source/f128M_to_ui64.c", |
| 266 | return true; | 308 | "deps/SoftFloat-3e/source/f128M_to_ui64_r_minMag.c", |
| 267 | } | 309 | "deps/SoftFloat-3e/source/f16_add.c", |
| 268 | | 310 | "deps/SoftFloat-3e/source/f16_div.c", |
| 269 | fn addCppLib(b: *Builder, lib_exe_obj: anytype, cmake_binary_dir: []const u8, lib_name: []const u8) void { | 311 | "deps/SoftFloat-3e/source/f16_eq.c", |
| 270 | lib_exe_obj.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{ | 312 | "deps/SoftFloat-3e/source/f16_isSignalingNaN.c", |
| 271 | cmake_binary_dir, | 313 | "deps/SoftFloat-3e/source/f16_lt.c", |
| 272 | "zigcpp", | 314 | "deps/SoftFloat-3e/source/f16_mul.c", |
| 273 | b.fmt("{}{}{}", .{ lib_exe_obj.target.libPrefix(), lib_name, lib_exe_obj.target.staticLibSuffix() }), | 315 | "deps/SoftFloat-3e/source/f16_mulAdd.c", |
| 274 | }) catch unreachable); | 316 | "deps/SoftFloat-3e/source/f16_rem.c", |
| 275 | } | 317 | "deps/SoftFloat-3e/source/f16_roundToInt.c", |
| 276 | | 318 | "deps/SoftFloat-3e/source/f16_sqrt.c", |
| 277 | const LibraryDep = struct { | 319 | "deps/SoftFloat-3e/source/f16_sub.c", |
| 278 | prefix: []const u8, | 320 | "deps/SoftFloat-3e/source/f16_to_f128M.c", |
| 279 | libdirs: ArrayList([]const u8), | 321 | "deps/SoftFloat-3e/source/f16_to_f64.c", |
| 280 | libs: ArrayList([]const u8), | 322 | "deps/SoftFloat-3e/source/f32_to_f128M.c", |
| 281 | system_libs: ArrayList([]const u8), | 323 | "deps/SoftFloat-3e/source/f64_to_f128M.c", |
| 282 | includes: ArrayList([]const u8), | 324 | "deps/SoftFloat-3e/source/f64_to_f16.c", |
| | 325 | "deps/SoftFloat-3e/source/i32_to_f128M.c", |
| | 326 | "deps/SoftFloat-3e/source/s_add256M.c", |
| | 327 | "deps/SoftFloat-3e/source/s_addCarryM.c", |
| | 328 | "deps/SoftFloat-3e/source/s_addComplCarryM.c", |
| | 329 | "deps/SoftFloat-3e/source/s_addF128M.c", |
| | 330 | "deps/SoftFloat-3e/source/s_addM.c", |
| | 331 | "deps/SoftFloat-3e/source/s_addMagsF16.c", |
| | 332 | "deps/SoftFloat-3e/source/s_addMagsF32.c", |
| | 333 | "deps/SoftFloat-3e/source/s_addMagsF64.c", |
| | 334 | "deps/SoftFloat-3e/source/s_approxRecip32_1.c", |
| | 335 | "deps/SoftFloat-3e/source/s_approxRecipSqrt32_1.c", |
| | 336 | "deps/SoftFloat-3e/source/s_approxRecipSqrt_1Ks.c", |
| | 337 | "deps/SoftFloat-3e/source/s_approxRecip_1Ks.c", |
| | 338 | "deps/SoftFloat-3e/source/s_compare128M.c", |
| | 339 | "deps/SoftFloat-3e/source/s_compare96M.c", |
| | 340 | "deps/SoftFloat-3e/source/s_countLeadingZeros16.c", |
| | 341 | "deps/SoftFloat-3e/source/s_countLeadingZeros32.c", |
| | 342 | "deps/SoftFloat-3e/source/s_countLeadingZeros64.c", |
| | 343 | "deps/SoftFloat-3e/source/s_countLeadingZeros8.c", |
| | 344 | "deps/SoftFloat-3e/source/s_eq128.c", |
| | 345 | "deps/SoftFloat-3e/source/s_invalidF128M.c", |
| | 346 | "deps/SoftFloat-3e/source/s_isNaNF128M.c", |
| | 347 | "deps/SoftFloat-3e/source/s_le128.c", |
| | 348 | "deps/SoftFloat-3e/source/s_lt128.c", |
| | 349 | "deps/SoftFloat-3e/source/s_mul128MTo256M.c", |
| | 350 | "deps/SoftFloat-3e/source/s_mul64To128M.c", |
| | 351 | "deps/SoftFloat-3e/source/s_mulAddF128M.c", |
| | 352 | "deps/SoftFloat-3e/source/s_mulAddF16.c", |
| | 353 | "deps/SoftFloat-3e/source/s_mulAddF32.c", |
| | 354 | "deps/SoftFloat-3e/source/s_mulAddF64.c", |
| | 355 | "deps/SoftFloat-3e/source/s_negXM.c", |
| | 356 | "deps/SoftFloat-3e/source/s_normRoundPackMToF128M.c", |
| | 357 | "deps/SoftFloat-3e/source/s_normRoundPackToF16.c", |
| | 358 | "deps/SoftFloat-3e/source/s_normRoundPackToF32.c", |
| | 359 | "deps/SoftFloat-3e/source/s_normRoundPackToF64.c", |
| | 360 | "deps/SoftFloat-3e/source/s_normSubnormalF128SigM.c", |
| | 361 | "deps/SoftFloat-3e/source/s_normSubnormalF16Sig.c", |
| | 362 | "deps/SoftFloat-3e/source/s_normSubnormalF32Sig.c", |
| | 363 | "deps/SoftFloat-3e/source/s_normSubnormalF64Sig.c", |
| | 364 | "deps/SoftFloat-3e/source/s_remStepMBy32.c", |
| | 365 | "deps/SoftFloat-3e/source/s_roundMToI64.c", |
| | 366 | "deps/SoftFloat-3e/source/s_roundMToUI64.c", |
| | 367 | "deps/SoftFloat-3e/source/s_roundPackMToF128M.c", |
| | 368 | "deps/SoftFloat-3e/source/s_roundPackToF16.c", |
| | 369 | "deps/SoftFloat-3e/source/s_roundPackToF32.c", |
| | 370 | "deps/SoftFloat-3e/source/s_roundPackToF64.c", |
| | 371 | "deps/SoftFloat-3e/source/s_roundToI32.c", |
| | 372 | "deps/SoftFloat-3e/source/s_roundToI64.c", |
| | 373 | "deps/SoftFloat-3e/source/s_roundToUI32.c", |
| | 374 | "deps/SoftFloat-3e/source/s_roundToUI64.c", |
| | 375 | "deps/SoftFloat-3e/source/s_shiftLeftM.c", |
| | 376 | "deps/SoftFloat-3e/source/s_shiftNormSigF128M.c", |
| | 377 | "deps/SoftFloat-3e/source/s_shiftRightJam256M.c", |
| | 378 | "deps/SoftFloat-3e/source/s_shiftRightJam32.c", |
| | 379 | "deps/SoftFloat-3e/source/s_shiftRightJam64.c", |
| | 380 | "deps/SoftFloat-3e/source/s_shiftRightJamM.c", |
| | 381 | "deps/SoftFloat-3e/source/s_shiftRightM.c", |
| | 382 | "deps/SoftFloat-3e/source/s_shortShiftLeft64To96M.c", |
| | 383 | "deps/SoftFloat-3e/source/s_shortShiftLeftM.c", |
| | 384 | "deps/SoftFloat-3e/source/s_shortShiftRightExtendM.c", |
| | 385 | "deps/SoftFloat-3e/source/s_shortShiftRightJam64.c", |
| | 386 | "deps/SoftFloat-3e/source/s_shortShiftRightJamM.c", |
| | 387 | "deps/SoftFloat-3e/source/s_shortShiftRightM.c", |
| | 388 | "deps/SoftFloat-3e/source/s_sub1XM.c", |
| | 389 | "deps/SoftFloat-3e/source/s_sub256M.c", |
| | 390 | "deps/SoftFloat-3e/source/s_subM.c", |
| | 391 | "deps/SoftFloat-3e/source/s_subMagsF16.c", |
| | 392 | "deps/SoftFloat-3e/source/s_subMagsF32.c", |
| | 393 | "deps/SoftFloat-3e/source/s_subMagsF64.c", |
| | 394 | "deps/SoftFloat-3e/source/s_tryPropagateNaNF128M.c", |
| | 395 | "deps/SoftFloat-3e/source/softfloat_state.c", |
| | 396 | "deps/SoftFloat-3e/source/ui32_to_f128M.c", |
| | 397 | "deps/SoftFloat-3e/source/ui64_to_f128M.c", |
| 283 | }; | 398 | }; |
| 284 | | 399 | |
| 285 | fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { | 400 | const stage1_sources = [_][]const u8{ |
| 286 | const shared_mode = try b.exec(&[_][]const u8{ llvm_config_exe, "--shared-mode" }); | 401 | "src/stage1/analyze.cpp", |
| 287 | const is_static = mem.startsWith(u8, shared_mode, "static"); | 402 | "src/stage1/ast_render.cpp", |
| 288 | const libs_output = if (is_static) | 403 | "src/stage1/bigfloat.cpp", |
| 289 | try b.exec(&[_][]const u8{ | 404 | "src/stage1/bigint.cpp", |
| 290 | llvm_config_exe, | 405 | "src/stage1/buffer.cpp", |
| 291 | "--libfiles", | 406 | "src/stage1/codegen.cpp", |
| 292 | "--system-libs", | 407 | "src/stage1/dump_analysis.cpp", |
| 293 | }) | 408 | "src/stage1/errmsg.cpp", |
| 294 | else | 409 | "src/stage1/error.cpp", |
| 295 | try b.exec(&[_][]const u8{ | 410 | "src/stage1/heap.cpp", |
| 296 | llvm_config_exe, | 411 | "src/stage1/ir.cpp", |
| 297 | "--libs", | 412 | "src/stage1/ir_print.cpp", |
| 298 | }); | 413 | "src/stage1/mem.cpp", |
| 299 | const includes_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--includedir" }); | 414 | "src/stage1/os.cpp", |
| 300 | const libdir_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--libdir" }); | 415 | "src/stage1/parser.cpp", |
| 301 | const prefix_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--prefix" }); | 416 | "src/stage1/range_set.cpp", |
| 302 | | 417 | "src/stage1/stage1.cpp", |
| 303 | var result = LibraryDep{ | 418 | "src/stage1/target.cpp", |
| 304 | .prefix = mem.tokenize(prefix_output, " \r\n").next().?, | 419 | "src/stage1/tokenizer.cpp", |
| 305 | .libs = ArrayList([]const u8).init(b.allocator), | 420 | "src/stage1/util.cpp", |
| 306 | .system_libs = ArrayList([]const u8).init(b.allocator), | 421 | "src/stage1/softfloat_ext.cpp", |
| 307 | .includes = ArrayList([]const u8).init(b.allocator), | 422 | }; |
| 308 | .libdirs = ArrayList([]const u8).init(b.allocator), | 423 | const optimized_c_sources = [_][]const u8{ |
| 309 | }; | 424 | "src/stage1/parse_f128.c", |
| 310 | { | 425 | }; |
| 311 | var it = mem.tokenize(libs_output, " \r\n"); | 426 | const zig_cpp_sources = [_][]const u8{ |
| 312 | while (it.next()) |lib_arg| { | 427 | // These are planned to stay even when we are self-hosted. |
| 313 | if (mem.startsWith(u8, lib_arg, "-l")) { | 428 | "src/zig_llvm.cpp", |
| 314 | try result.system_libs.append(lib_arg[2..]); | 429 | "src/zig_clang.cpp", |
| 315 | } else { | 430 | "src/zig_clang_driver.cpp", |
| 316 | if (fs.path.isAbsolute(lib_arg)) { | 431 | "src/zig_clang_cc1_main.cpp", |
| 317 | try result.libs.append(lib_arg); | 432 | "src/zig_clang_cc1as_main.cpp", |
| 318 | } else { | 433 | // https://github.com/ziglang/zig/issues/6363 |
| 319 | var lib_arg_copy = lib_arg; | 434 | "src/windows_sdk.cpp", |
| 320 | if (mem.endsWith(u8, lib_arg, ".lib")) { | | |
| 321 | lib_arg_copy = lib_arg[0 .. lib_arg.len - 4]; | | |
| 322 | } | | |
| 323 | try result.system_libs.append(lib_arg_copy); | | |
| 324 | } | | |
| 325 | } | | |
| 326 | } | | |
| 327 | } | | |
| 328 | { | | |
| 329 | var it = mem.tokenize(includes_output, " \r\n"); | | |
| 330 | while (it.next()) |include_arg| { | | |
| 331 | if (mem.startsWith(u8, include_arg, "-I")) { | | |
| 332 | try result.includes.append(include_arg[2..]); | | |
| 333 | } else { | | |
| 334 | try result.includes.append(include_arg); | | |
| 335 | } | | |
| 336 | } | | |
| 337 | } | | |
| 338 | { | | |
| 339 | var it = mem.tokenize(libdir_output, " \r\n"); | | |
| 340 | while (it.next()) |libdir| { | | |
| 341 | if (mem.startsWith(u8, libdir, "-L")) { | | |
| 342 | try result.libdirs.append(libdir[2..]); | | |
| 343 | } else { | | |
| 344 | try result.libdirs.append(libdir); | | |
| 345 | } | | |
| 346 | } | | |
| 347 | } | | |
| 348 | return result; | | |
| 349 | } | | |
| 350 | | | |
| 351 | fn configureStage2(b: *Builder, exe: anytype, ctx: Context, need_cpp_includes: bool) !void { | | |
| 352 | exe.addIncludeDir("src"); | | |
| 353 | exe.addIncludeDir(ctx.cmake_binary_dir); | | |
| 354 | addCppLib(b, exe, ctx.cmake_binary_dir, "zigcpp"); | | |
| 355 | assert(ctx.lld_include_dir.len != 0); | | |
| 356 | exe.addIncludeDir(ctx.lld_include_dir); | | |
| 357 | { | | |
| 358 | var it = mem.tokenize(ctx.lld_libraries, ";"); | | |
| 359 | while (it.next()) |lib| { | | |
| 360 | exe.addObjectFile(lib); | | |
| 361 | } | | |
| 362 | } | | |
| 363 | { | | |
| 364 | var it = mem.tokenize(ctx.clang_libraries, ";"); | | |
| 365 | while (it.next()) |lib| { | | |
| 366 | exe.addObjectFile(lib); | | |
| 367 | } | | |
| 368 | } | | |
| 369 | dependOnLib(b, exe, ctx.llvm); | | |
| 370 | | | |
| 371 | // Boy, it sure would be nice to simply linkSystemLibrary("c++") and rely on zig's | | |
| 372 | // ability to provide libc++ right? Well thanks to C++ not having a stable ABI this | | |
| 373 | // will cause linker errors. It would work in the situation when `zig cc` is used to | | |
| 374 | // build LLVM, Clang, and LLD, however when depending on them as system libraries, system | | |
| 375 | // libc++ must be used. | | |
| 376 | const cross_compile = false; // TODO | | |
| 377 | if (cross_compile) { | | |
| 378 | // In this case we assume that zig cc was used to build the LLVM, Clang, LLD dependencies. | | |
| 379 | exe.linkSystemLibrary("c++"); | | |
| 380 | } else { | | |
| 381 | if (exe.target.getOsTag() == .linux) { | | |
| 382 | // First we try to static link against gcc libstdc++. If that doesn't work, | | |
| 383 | // we fall back to -lc++ and cross our fingers. | | |
| 384 | addCxxKnownPath(b, ctx, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) { | | |
| 385 | error.RequiredLibraryNotFound => { | | |
| 386 | exe.linkSystemLibrary("c++"); | | |
| 387 | }, | | |
| 388 | else => |e| return e, | | |
| 389 | }; | | |
| 390 | | | |
| 391 | exe.linkSystemLibrary("pthread"); | | |
| 392 | } else if (exe.target.isFreeBSD()) { | | |
| 393 | try addCxxKnownPath(b, ctx, exe, "libc++.a", null, need_cpp_includes); | | |
| 394 | exe.linkSystemLibrary("pthread"); | | |
| 395 | } else if (exe.target.isDarwin()) { | | |
| 396 | if (addCxxKnownPath(b, ctx, exe, "libgcc_eh.a", "", need_cpp_includes)) { | | |
| 397 | // Compiler is GCC. | | |
| 398 | try addCxxKnownPath(b, ctx, exe, "libstdc++.a", null, need_cpp_includes); | | |
| 399 | exe.linkSystemLibrary("pthread"); | | |
| 400 | // TODO LLD cannot perform this link. | | |
| 401 | // Set ZIG_SYSTEM_LINKER_HACK env var to use system linker ld instead. | | |
| 402 | // See https://github.com/ziglang/zig/issues/1535 | | |
| 403 | } else |err| switch (err) { | | |
| 404 | error.RequiredLibraryNotFound => { | | |
| 405 | // System compiler, not gcc. | | |
| 406 | exe.linkSystemLibrary("c++"); | | |
| 407 | }, | | |
| 408 | else => |e| return e, | | |
| 409 | } | | |
| 410 | } | | |
| 411 | | | |
| 412 | if (ctx.dia_guids_lib.len != 0) { | | |
| 413 | exe.addObjectFile(ctx.dia_guids_lib); | | |
| 414 | } | | |
| 415 | } | | |
| 416 | } | | |
| 417 | | | |
| 418 | fn addCxxKnownPath( | | |
| 419 | b: *Builder, | | |
| 420 | ctx: Context, | | |
| 421 | exe: anytype, | | |
| 422 | objname: []const u8, | | |
| 423 | errtxt: ?[]const u8, | | |
| 424 | need_cpp_includes: bool, | | |
| 425 | ) !void { | | |
| 426 | const path_padded = try b.exec(&[_][]const u8{ | | |
| 427 | ctx.cxx_compiler, | | |
| 428 | b.fmt("-print-file-name={}", .{objname}), | | |
| 429 | }); | | |
| 430 | const path_unpadded = mem.tokenize(path_padded, "\r\n").next().?; | | |
| 431 | if (mem.eql(u8, path_unpadded, objname)) { | | |
| 432 | if (errtxt) |msg| { | | |
| 433 | warn("{}", .{msg}); | | |
| 434 | } else { | | |
| 435 | warn("Unable to determine path to {}\n", .{objname}); | | |
| 436 | } | | |
| 437 | return error.RequiredLibraryNotFound; | | |
| 438 | } | | |
| 439 | exe.addObjectFile(path_unpadded); | | |
| 440 | | | |
| 441 | // TODO a way to integrate with system c++ include files here | | |
| 442 | // cc -E -Wp,-v -xc++ /dev/null | | |
| 443 | if (need_cpp_includes) { | | |
| 444 | // I used these temporarily for testing something but we obviously need a | | |
| 445 | // more general purpose solution here. | | |
| 446 | //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0"); | | |
| 447 | //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-unknown-linux-gnu"); | | |
| 448 | //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward"); | | |
| 449 | } | | |
| 450 | } | | |
| 451 | | | |
| 452 | const Context = struct { | | |
| 453 | cmake_binary_dir: []const u8, | | |
| 454 | cxx_compiler: []const u8, | | |
| 455 | llvm_config_exe: []const u8, | | |
| 456 | lld_include_dir: []const u8, | | |
| 457 | lld_libraries: []const u8, | | |
| 458 | clang_libraries: []const u8, | | |
| 459 | dia_guids_lib: []const u8, | | |
| 460 | llvm: LibraryDep, | | |
| 461 | }; | 435 | }; |
| 462 | | 436 | |
| 463 | const max_config_h_bytes = 1 * 1024 * 1024; | 437 | const clang_libs = [_][]const u8{ |
| 464 | | 438 | "clangFrontendTool", |
| 465 | fn findAndReadConfigH(b: *Builder) ![]const u8 { | 439 | "clangCodeGen", |
| 466 | var check_dir = fs.path.dirname(b.zig_exe).?; | 440 | "clangFrontend", |
| 467 | while (true) { | 441 | "clangDriver", |
| 468 | var dir = try fs.cwd().openDir(check_dir, .{}); | 442 | "clangSerialization", |
| 469 | defer dir.close(); | 443 | "clangSema", |
| 470 | | 444 | "clangStaticAnalyzerFrontend", |
| 471 | const config_h_text = dir.readFileAlloc(b.allocator, "config.h", max_config_h_bytes) catch |err| switch (err) { | 445 | "clangStaticAnalyzerCheckers", |
| 472 | error.FileNotFound => { | 446 | "clangStaticAnalyzerCore", |
| 473 | const new_check_dir = fs.path.dirname(check_dir); | 447 | "clangAnalysis", |
| 474 | if (new_check_dir == null or mem.eql(u8, new_check_dir.?, check_dir)) { | 448 | "clangASTMatchers", |
| 475 | std.debug.warn("Unable to find config.h file relative to Zig executable.\n", .{}); | 449 | "clangAST", |
| 476 | std.debug.warn("`zig build` must be run using a Zig executable within the source tree.\n", .{}); | 450 | "clangParse", |
| 477 | std.process.exit(1); | 451 | "clangSema", |
| 478 | } | 452 | "clangBasic", |
| 479 | check_dir = new_check_dir.?; | 453 | "clangEdit", |
| 480 | continue; | 454 | "clangLex", |
| 481 | }, | 455 | "clangARCMigrate", |
| 482 | else => |e| return e, | 456 | "clangRewriteFrontend", |
| 483 | }; | 457 | "clangRewrite", |
| 484 | return config_h_text; | 458 | "clangCrossTU", |
| 485 | } else unreachable; // TODO should not need `else unreachable`. | 459 | "clangIndex", |
| 486 | } | 460 | "clangToolingCore", |
| 487 | | 461 | }; |
| 488 | fn parseConfigH(b: *Builder, config_h_text: []const u8) Context { | 462 | const lld_libs = [_][]const u8{ |
| 489 | var ctx: Context = .{ | 463 | "lldDriver", |
| 490 | .cmake_binary_dir = undefined, | 464 | "lldMinGW", |
| 491 | .cxx_compiler = undefined, | 465 | "lldELF", |
| 492 | .llvm_config_exe = undefined, | 466 | "lldCOFF", |
| 493 | .lld_include_dir = undefined, | 467 | "lldMachO", |
| 494 | .lld_libraries = undefined, | 468 | "lldWasm", |
| 495 | .clang_libraries = undefined, | 469 | "lldReaderWriter", |
| 496 | .dia_guids_lib = undefined, | 470 | "lldCore", |
| 497 | .llvm = undefined, | 471 | "lldYAML", |
| 498 | }; | 472 | "lldCommon", |
| 499 | | 473 | }; |
| 500 | const mappings = [_]struct { prefix: []const u8, field: []const u8 }{ | 474 | // This list can be re-generated with `llvm-config --libfiles` and then |
| 501 | .{ | 475 | // reformatting using your favorite text editor. Note we do not execute |
| 502 | .prefix = "#define ZIG_CMAKE_BINARY_DIR ", | 476 | // `llvm-config` here because we are cross compiling. |
| 503 | .field = "cmake_binary_dir", | 477 | const llvm_libs = [_][]const u8{ |
| 504 | }, | 478 | "LLVMXRay", |
| 505 | .{ | 479 | "LLVMWindowsManifest", |
| 506 | .prefix = "#define ZIG_CXX_COMPILER ", | 480 | "LLVMSymbolize", |
| 507 | .field = "cxx_compiler", | 481 | "LLVMDebugInfoPDB", |
| 508 | }, | 482 | "LLVMOrcJIT", |
| 509 | .{ | 483 | "LLVMOrcError", |
| 510 | .prefix = "#define ZIG_LLD_INCLUDE_PATH ", | 484 | "LLVMJITLink", |
| 511 | .field = "lld_include_dir", | 485 | "LLVMObjectYAML", |
| 512 | }, | 486 | "LLVMMCA", |
| 513 | .{ | 487 | "LLVMLTO", |
| 514 | .prefix = "#define ZIG_LLD_LIBRARIES ", | 488 | "LLVMPasses", |
| 515 | .field = "lld_libraries", | 489 | "LLVMCoroutines", |
| 516 | }, | 490 | "LLVMObjCARCOpts", |
| 517 | .{ | 491 | "LLVMExtensions", |
| 518 | .prefix = "#define ZIG_CLANG_LIBRARIES ", | 492 | "LLVMLineEditor", |
| 519 | .field = "clang_libraries", | 493 | "LLVMLibDriver", |
| 520 | }, | 494 | "LLVMInterpreter", |
| 521 | .{ | 495 | "LLVMFuzzMutate", |
| 522 | .prefix = "#define ZIG_LLVM_CONFIG_EXE ", | 496 | "LLVMMCJIT", |
| 523 | .field = "llvm_config_exe", | 497 | "LLVMExecutionEngine", |
| 524 | }, | 498 | "LLVMRuntimeDyld", |
| 525 | .{ | 499 | "LLVMDWARFLinker", |
| 526 | .prefix = "#define ZIG_DIA_GUIDS_LIB ", | 500 | "LLVMDlltoolDriver", |
| 527 | .field = "dia_guids_lib", | 501 | "LLVMOption", |
| 528 | }, | 502 | "LLVMDebugInfoGSYM", |
| 529 | }; | 503 | "LLVMCoverage", |
| 530 | | 504 | "LLVMXCoreDisassembler", |
| 531 | var lines_it = mem.tokenize(config_h_text, "\r\n"); | 505 | "LLVMXCoreCodeGen", |
| 532 | while (lines_it.next()) |line| { | 506 | "LLVMXCoreDesc", |
| 533 | inline for (mappings) |mapping| { | 507 | "LLVMXCoreInfo", |
| 534 | if (mem.startsWith(u8, line, mapping.prefix)) { | 508 | "LLVMX86Disassembler", |
| 535 | var it = mem.split(line, "\""); | 509 | "LLVMX86CodeGen", |
| 536 | _ = it.next().?; // skip the stuff before the quote | 510 | "LLVMX86AsmParser", |
| 537 | const quoted = it.next().?; // the stuff inside the quote | 511 | "LLVMX86Desc", |
| 538 | @field(ctx, mapping.field) = toNativePathSep(b, quoted); | 512 | "LLVMX86Info", |
| 539 | } | 513 | "LLVMWebAssemblyDisassembler", |
| 540 | } | 514 | "LLVMWebAssemblyCodeGen", |
| 541 | } | 515 | "LLVMWebAssemblyDesc", |
| 542 | return ctx; | 516 | "LLVMWebAssemblyAsmParser", |
| 543 | } | 517 | "LLVMWebAssemblyInfo", |
| 544 | | 518 | "LLVMSystemZDisassembler", |
| 545 | fn toNativePathSep(b: *Builder, s: []const u8) []u8 { | 519 | "LLVMSystemZCodeGen", |
| 546 | const duplicated = mem.dupe(b.allocator, u8, s) catch unreachable; | 520 | "LLVMSystemZAsmParser", |
| 547 | for (duplicated) |*byte| switch (byte.*) { | 521 | "LLVMSystemZDesc", |
| 548 | '/' => byte.* = fs.path.sep, | 522 | "LLVMSystemZInfo", |
| 549 | else => {}, | 523 | "LLVMSparcDisassembler", |
| 550 | }; | 524 | "LLVMSparcCodeGen", |
| 551 | return duplicated; | 525 | "LLVMSparcAsmParser", |
| 552 | } | 526 | "LLVMSparcDesc", |
| | 527 | "LLVMSparcInfo", |
| | 528 | "LLVMRISCVDisassembler", |
| | 529 | "LLVMRISCVCodeGen", |
| | 530 | "LLVMRISCVAsmParser", |
| | 531 | "LLVMRISCVDesc", |
| | 532 | "LLVMRISCVUtils", |
| | 533 | "LLVMRISCVInfo", |
| | 534 | "LLVMPowerPCDisassembler", |
| | 535 | "LLVMPowerPCCodeGen", |
| | 536 | "LLVMPowerPCAsmParser", |
| | 537 | "LLVMPowerPCDesc", |
| | 538 | "LLVMPowerPCInfo", |
| | 539 | "LLVMNVPTXCodeGen", |
| | 540 | "LLVMNVPTXDesc", |
| | 541 | "LLVMNVPTXInfo", |
| | 542 | "LLVMMSP430Disassembler", |
| | 543 | "LLVMMSP430CodeGen", |
| | 544 | "LLVMMSP430AsmParser", |
| | 545 | "LLVMMSP430Desc", |
| | 546 | "LLVMMSP430Info", |
| | 547 | "LLVMMipsDisassembler", |
| | 548 | "LLVMMipsCodeGen", |
| | 549 | "LLVMMipsAsmParser", |
| | 550 | "LLVMMipsDesc", |
| | 551 | "LLVMMipsInfo", |
| | 552 | "LLVMLanaiDisassembler", |
| | 553 | "LLVMLanaiCodeGen", |
| | 554 | "LLVMLanaiAsmParser", |
| | 555 | "LLVMLanaiDesc", |
| | 556 | "LLVMLanaiInfo", |
| | 557 | "LLVMHexagonDisassembler", |
| | 558 | "LLVMHexagonCodeGen", |
| | 559 | "LLVMHexagonAsmParser", |
| | 560 | "LLVMHexagonDesc", |
| | 561 | "LLVMHexagonInfo", |
| | 562 | "LLVMBPFDisassembler", |
| | 563 | "LLVMBPFCodeGen", |
| | 564 | "LLVMBPFAsmParser", |
| | 565 | "LLVMBPFDesc", |
| | 566 | "LLVMBPFInfo", |
| | 567 | "LLVMAVRDisassembler", |
| | 568 | "LLVMAVRCodeGen", |
| | 569 | "LLVMAVRAsmParser", |
| | 570 | "LLVMAVRDesc", |
| | 571 | "LLVMAVRInfo", |
| | 572 | "LLVMARMDisassembler", |
| | 573 | "LLVMARMCodeGen", |
| | 574 | "LLVMARMAsmParser", |
| | 575 | "LLVMARMDesc", |
| | 576 | "LLVMARMUtils", |
| | 577 | "LLVMARMInfo", |
| | 578 | "LLVMAMDGPUDisassembler", |
| | 579 | "LLVMAMDGPUCodeGen", |
| | 580 | "LLVMMIRParser", |
| | 581 | "LLVMipo", |
| | 582 | "LLVMInstrumentation", |
| | 583 | "LLVMVectorize", |
| | 584 | "LLVMLinker", |
| | 585 | "LLVMIRReader", |
| | 586 | "LLVMAsmParser", |
| | 587 | "LLVMFrontendOpenMP", |
| | 588 | "LLVMAMDGPUAsmParser", |
| | 589 | "LLVMAMDGPUDesc", |
| | 590 | "LLVMAMDGPUUtils", |
| | 591 | "LLVMAMDGPUInfo", |
| | 592 | "LLVMAArch64Disassembler", |
| | 593 | "LLVMMCDisassembler", |
| | 594 | "LLVMAArch64CodeGen", |
| | 595 | "LLVMCFGuard", |
| | 596 | "LLVMGlobalISel", |
| | 597 | "LLVMSelectionDAG", |
| | 598 | "LLVMAsmPrinter", |
| | 599 | "LLVMDebugInfoDWARF", |
| | 600 | "LLVMCodeGen", |
| | 601 | "LLVMTarget", |
| | 602 | "LLVMScalarOpts", |
| | 603 | "LLVMInstCombine", |
| | 604 | "LLVMAggressiveInstCombine", |
| | 605 | "LLVMTransformUtils", |
| | 606 | "LLVMBitWriter", |
| | 607 | "LLVMAnalysis", |
| | 608 | "LLVMProfileData", |
| | 609 | "LLVMObject", |
| | 610 | "LLVMTextAPI", |
| | 611 | "LLVMBitReader", |
| | 612 | "LLVMCore", |
| | 613 | "LLVMRemarks", |
| | 614 | "LLVMBitstreamReader", |
| | 615 | "LLVMAArch64AsmParser", |
| | 616 | "LLVMMCParser", |
| | 617 | "LLVMAArch64Desc", |
| | 618 | "LLVMMC", |
| | 619 | "LLVMDebugInfoCodeView", |
| | 620 | "LLVMDebugInfoMSF", |
| | 621 | "LLVMBinaryFormat", |
| | 622 | "LLVMAArch64Utils", |
| | 623 | "LLVMAArch64Info", |
| | 624 | "LLVMSupport", |
| | 625 | "LLVMDemangle", |
| | 626 | }; |