| ... | ... | @@ -53,7 +53,8 @@ pub fn build(b: *Builder) !void { |
| 53 | 53 | const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false; |
| 54 | 54 | |
| 55 | 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 | 58 | const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); |
| 58 | 59 | |
| 59 | 60 | b.installDirectory(InstallDirectoryOptions{ |
| ... | ... | @@ -76,6 +77,8 @@ pub fn build(b: *Builder) !void { |
| 76 | 77 | const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source"); |
| 77 | 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 | 82 | var exe = b.addExecutable("zig", "src/main.zig"); |
| 80 | 83 | exe.install(); |
| 81 | 84 | exe.setBuildMode(mode); |
| ... | ... | @@ -86,15 +89,51 @@ pub fn build(b: *Builder) !void { |
| 86 | 89 | exe.addBuildOption(bool, "skip_non_native", skip_non_native); |
| 87 | 90 | exe.addBuildOption(bool, "have_llvm", enable_llvm); |
| 88 | 91 | if (enable_llvm) { |
| 89 | | const config_h_text = if (config_h_path_option) |config_h_path| |
| 90 | | try std.fs.cwd().readFileAlloc(b.allocator, toNativePathSep(b, config_h_path), max_config_h_bytes) |
| 91 | | else |
| 92 | | try findAndReadConfigH(b); |
| 92 | if (is_stage1) { |
| 93 | exe.addIncludeDir("src"); |
| 94 | exe.addIncludeDir("deps/SoftFloat-3e/source/include"); |
| 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); |
| 95 | | ctx.llvm = try findLLVM(b, ctx.llvm_config_exe); |
| 123 | for (clang_libs) |lib_name| { |
| 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 | 138 | if (link_libc) { |
| 100 | 139 | exe.linkLibC(); |
| ... | ... | @@ -141,7 +180,10 @@ pub fn build(b: *Builder) !void { |
| 141 | 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 | 188 | exe.addBuildOption([]const []const u8, "log_scopes", log_scopes); |
| 147 | 189 | exe.addBuildOption([]const []const u8, "zir_dumps", zir_dumps); |
| ... | ... | @@ -226,327 +268,359 @@ pub fn build(b: *Builder) !void { |
| 226 | 268 | test_step.dependOn(docs_step); |
| 227 | 269 | } |
| 228 | 270 | |
| 229 | | fn dependOnLib(b: *Builder, lib_exe_obj: anytype, dep: LibraryDep) void { |
| 230 | | for (dep.libdirs.items) |lib_dir| { |
| 231 | | lib_exe_obj.addLibPath(lib_dir); |
| 232 | | } |
| 233 | | const lib_dir = fs.path.join( |
| 234 | | b.allocator, |
| 235 | | &[_][]const u8{ dep.prefix, "lib" }, |
| 236 | | ) catch unreachable; |
| 237 | | for (dep.system_libs.items) |lib| { |
| 238 | | const static_bare_name = if (mem.eql(u8, lib, "curses")) |
| 239 | | @as([]const u8, "libncurses.a") |
| 240 | | else |
| 241 | | b.fmt("lib{}.a", .{lib}); |
| 242 | | const static_lib_name = fs.path.join( |
| 243 | | b.allocator, |
| 244 | | &[_][]const u8{ lib_dir, static_bare_name }, |
| 245 | | ) catch unreachable; |
| 246 | | const have_static = fileExists(static_lib_name) catch unreachable; |
| 247 | | if (have_static) { |
| 248 | | lib_exe_obj.addObjectFile(static_lib_name); |
| 249 | | } else { |
| 250 | | lib_exe_obj.linkSystemLibrary(lib); |
| 251 | | } |
| 252 | | } |
| 253 | | for (dep.libs.items) |lib| { |
| 254 | | lib_exe_obj.addObjectFile(lib); |
| 255 | | } |
| 256 | | for (dep.includes.items) |include_path| { |
| 257 | | lib_exe_obj.addIncludeDir(include_path); |
| 258 | | } |
| 259 | | } |
| 260 | | |
| 261 | | fn fileExists(filename: []const u8) !bool { |
| 262 | | fs.cwd().access(filename, .{}) catch |err| switch (err) { |
| 263 | | error.FileNotFound => return false, |
| 264 | | else => return err, |
| 265 | | }; |
| 266 | | return true; |
| 267 | | } |
| 268 | | |
| 269 | | fn addCppLib(b: *Builder, lib_exe_obj: anytype, cmake_binary_dir: []const u8, lib_name: []const u8) void { |
| 270 | | lib_exe_obj.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{ |
| 271 | | cmake_binary_dir, |
| 272 | | "zigcpp", |
| 273 | | b.fmt("{}{}{}", .{ lib_exe_obj.target.libPrefix(), lib_name, lib_exe_obj.target.staticLibSuffix() }), |
| 274 | | }) catch unreachable); |
| 275 | | } |
| 276 | | |
| 277 | | const LibraryDep = struct { |
| 278 | | prefix: []const u8, |
| 279 | | libdirs: ArrayList([]const u8), |
| 280 | | libs: ArrayList([]const u8), |
| 281 | | system_libs: ArrayList([]const u8), |
| 282 | | includes: ArrayList([]const u8), |
| 271 | const softfloat_sources = [_][]const u8{ |
| 272 | "deps/SoftFloat-3e/source/8086/f128M_isSignalingNaN.c", |
| 273 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF128M.c", |
| 274 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF16UI.c", |
| 275 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF32UI.c", |
| 276 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF64UI.c", |
| 277 | "deps/SoftFloat-3e/source/8086/s_f128MToCommonNaN.c", |
| 278 | "deps/SoftFloat-3e/source/8086/s_f16UIToCommonNaN.c", |
| 279 | "deps/SoftFloat-3e/source/8086/s_f32UIToCommonNaN.c", |
| 280 | "deps/SoftFloat-3e/source/8086/s_f64UIToCommonNaN.c", |
| 281 | "deps/SoftFloat-3e/source/8086/s_propagateNaNF128M.c", |
| 282 | "deps/SoftFloat-3e/source/8086/s_propagateNaNF16UI.c", |
| 283 | "deps/SoftFloat-3e/source/8086/softfloat_raiseFlags.c", |
| 284 | "deps/SoftFloat-3e/source/f128M_add.c", |
| 285 | "deps/SoftFloat-3e/source/f128M_div.c", |
| 286 | "deps/SoftFloat-3e/source/f128M_eq.c", |
| 287 | "deps/SoftFloat-3e/source/f128M_eq_signaling.c", |
| 288 | "deps/SoftFloat-3e/source/f128M_le.c", |
| 289 | "deps/SoftFloat-3e/source/f128M_le_quiet.c", |
| 290 | "deps/SoftFloat-3e/source/f128M_lt.c", |
| 291 | "deps/SoftFloat-3e/source/f128M_lt_quiet.c", |
| 292 | "deps/SoftFloat-3e/source/f128M_mul.c", |
| 293 | "deps/SoftFloat-3e/source/f128M_mulAdd.c", |
| 294 | "deps/SoftFloat-3e/source/f128M_rem.c", |
| 295 | "deps/SoftFloat-3e/source/f128M_roundToInt.c", |
| 296 | "deps/SoftFloat-3e/source/f128M_sqrt.c", |
| 297 | "deps/SoftFloat-3e/source/f128M_sub.c", |
| 298 | "deps/SoftFloat-3e/source/f128M_to_f16.c", |
| 299 | "deps/SoftFloat-3e/source/f128M_to_f32.c", |
| 300 | "deps/SoftFloat-3e/source/f128M_to_f64.c", |
| 301 | "deps/SoftFloat-3e/source/f128M_to_i32.c", |
| 302 | "deps/SoftFloat-3e/source/f128M_to_i32_r_minMag.c", |
| 303 | "deps/SoftFloat-3e/source/f128M_to_i64.c", |
| 304 | "deps/SoftFloat-3e/source/f128M_to_i64_r_minMag.c", |
| 305 | "deps/SoftFloat-3e/source/f128M_to_ui32.c", |
| 306 | "deps/SoftFloat-3e/source/f128M_to_ui32_r_minMag.c", |
| 307 | "deps/SoftFloat-3e/source/f128M_to_ui64.c", |
| 308 | "deps/SoftFloat-3e/source/f128M_to_ui64_r_minMag.c", |
| 309 | "deps/SoftFloat-3e/source/f16_add.c", |
| 310 | "deps/SoftFloat-3e/source/f16_div.c", |
| 311 | "deps/SoftFloat-3e/source/f16_eq.c", |
| 312 | "deps/SoftFloat-3e/source/f16_isSignalingNaN.c", |
| 313 | "deps/SoftFloat-3e/source/f16_lt.c", |
| 314 | "deps/SoftFloat-3e/source/f16_mul.c", |
| 315 | "deps/SoftFloat-3e/source/f16_mulAdd.c", |
| 316 | "deps/SoftFloat-3e/source/f16_rem.c", |
| 317 | "deps/SoftFloat-3e/source/f16_roundToInt.c", |
| 318 | "deps/SoftFloat-3e/source/f16_sqrt.c", |
| 319 | "deps/SoftFloat-3e/source/f16_sub.c", |
| 320 | "deps/SoftFloat-3e/source/f16_to_f128M.c", |
| 321 | "deps/SoftFloat-3e/source/f16_to_f64.c", |
| 322 | "deps/SoftFloat-3e/source/f32_to_f128M.c", |
| 323 | "deps/SoftFloat-3e/source/f64_to_f128M.c", |
| 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 { |
| 286 | | const shared_mode = try b.exec(&[_][]const u8{ llvm_config_exe, "--shared-mode" }); |
| 287 | | const is_static = mem.startsWith(u8, shared_mode, "static"); |
| 288 | | const libs_output = if (is_static) |
| 289 | | try b.exec(&[_][]const u8{ |
| 290 | | llvm_config_exe, |
| 291 | | "--libfiles", |
| 292 | | "--system-libs", |
| 293 | | }) |
| 294 | | else |
| 295 | | try b.exec(&[_][]const u8{ |
| 296 | | llvm_config_exe, |
| 297 | | "--libs", |
| 298 | | }); |
| 299 | | const includes_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--includedir" }); |
| 300 | | const libdir_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--libdir" }); |
| 301 | | const prefix_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--prefix" }); |
| 302 | | |
| 303 | | var result = LibraryDep{ |
| 304 | | .prefix = mem.tokenize(prefix_output, " \r\n").next().?, |
| 305 | | .libs = ArrayList([]const u8).init(b.allocator), |
| 306 | | .system_libs = ArrayList([]const u8).init(b.allocator), |
| 307 | | .includes = ArrayList([]const u8).init(b.allocator), |
| 308 | | .libdirs = ArrayList([]const u8).init(b.allocator), |
| 309 | | }; |
| 310 | | { |
| 311 | | var it = mem.tokenize(libs_output, " \r\n"); |
| 312 | | while (it.next()) |lib_arg| { |
| 313 | | if (mem.startsWith(u8, lib_arg, "-l")) { |
| 314 | | try result.system_libs.append(lib_arg[2..]); |
| 315 | | } else { |
| 316 | | if (fs.path.isAbsolute(lib_arg)) { |
| 317 | | try result.libs.append(lib_arg); |
| 318 | | } else { |
| 319 | | var lib_arg_copy = lib_arg; |
| 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, |
| 400 | const stage1_sources = [_][]const u8{ |
| 401 | "src/stage1/analyze.cpp", |
| 402 | "src/stage1/ast_render.cpp", |
| 403 | "src/stage1/bigfloat.cpp", |
| 404 | "src/stage1/bigint.cpp", |
| 405 | "src/stage1/buffer.cpp", |
| 406 | "src/stage1/codegen.cpp", |
| 407 | "src/stage1/dump_analysis.cpp", |
| 408 | "src/stage1/errmsg.cpp", |
| 409 | "src/stage1/error.cpp", |
| 410 | "src/stage1/heap.cpp", |
| 411 | "src/stage1/ir.cpp", |
| 412 | "src/stage1/ir_print.cpp", |
| 413 | "src/stage1/mem.cpp", |
| 414 | "src/stage1/os.cpp", |
| 415 | "src/stage1/parser.cpp", |
| 416 | "src/stage1/range_set.cpp", |
| 417 | "src/stage1/stage1.cpp", |
| 418 | "src/stage1/target.cpp", |
| 419 | "src/stage1/tokenizer.cpp", |
| 420 | "src/stage1/util.cpp", |
| 421 | "src/stage1/softfloat_ext.cpp", |
| 422 | }; |
| 423 | const optimized_c_sources = [_][]const u8{ |
| 424 | "src/stage1/parse_f128.c", |
| 425 | }; |
| 426 | const zig_cpp_sources = [_][]const u8{ |
| 427 | // These are planned to stay even when we are self-hosted. |
| 428 | "src/zig_llvm.cpp", |
| 429 | "src/zig_clang.cpp", |
| 430 | "src/zig_clang_driver.cpp", |
| 431 | "src/zig_clang_cc1_main.cpp", |
| 432 | "src/zig_clang_cc1as_main.cpp", |
| 433 | // https://github.com/ziglang/zig/issues/6363 |
| 434 | "src/windows_sdk.cpp", |
| 461 | 435 | }; |
| 462 | 436 | |
| 463 | | const max_config_h_bytes = 1 * 1024 * 1024; |
| 464 | | |
| 465 | | fn findAndReadConfigH(b: *Builder) ![]const u8 { |
| 466 | | var check_dir = fs.path.dirname(b.zig_exe).?; |
| 467 | | while (true) { |
| 468 | | var dir = try fs.cwd().openDir(check_dir, .{}); |
| 469 | | defer dir.close(); |
| 470 | | |
| 471 | | const config_h_text = dir.readFileAlloc(b.allocator, "config.h", max_config_h_bytes) catch |err| switch (err) { |
| 472 | | error.FileNotFound => { |
| 473 | | const new_check_dir = fs.path.dirname(check_dir); |
| 474 | | if (new_check_dir == null or mem.eql(u8, new_check_dir.?, check_dir)) { |
| 475 | | std.debug.warn("Unable to find config.h file relative to Zig executable.\n", .{}); |
| 476 | | std.debug.warn("`zig build` must be run using a Zig executable within the source tree.\n", .{}); |
| 477 | | std.process.exit(1); |
| 478 | | } |
| 479 | | check_dir = new_check_dir.?; |
| 480 | | continue; |
| 481 | | }, |
| 482 | | else => |e| return e, |
| 483 | | }; |
| 484 | | return config_h_text; |
| 485 | | } else unreachable; // TODO should not need `else unreachable`. |
| 486 | | } |
| 487 | | |
| 488 | | fn parseConfigH(b: *Builder, config_h_text: []const u8) Context { |
| 489 | | var ctx: Context = .{ |
| 490 | | .cmake_binary_dir = undefined, |
| 491 | | .cxx_compiler = undefined, |
| 492 | | .llvm_config_exe = undefined, |
| 493 | | .lld_include_dir = undefined, |
| 494 | | .lld_libraries = undefined, |
| 495 | | .clang_libraries = undefined, |
| 496 | | .dia_guids_lib = undefined, |
| 497 | | .llvm = undefined, |
| 498 | | }; |
| 499 | | |
| 500 | | const mappings = [_]struct { prefix: []const u8, field: []const u8 }{ |
| 501 | | .{ |
| 502 | | .prefix = "#define ZIG_CMAKE_BINARY_DIR ", |
| 503 | | .field = "cmake_binary_dir", |
| 504 | | }, |
| 505 | | .{ |
| 506 | | .prefix = "#define ZIG_CXX_COMPILER ", |
| 507 | | .field = "cxx_compiler", |
| 508 | | }, |
| 509 | | .{ |
| 510 | | .prefix = "#define ZIG_LLD_INCLUDE_PATH ", |
| 511 | | .field = "lld_include_dir", |
| 512 | | }, |
| 513 | | .{ |
| 514 | | .prefix = "#define ZIG_LLD_LIBRARIES ", |
| 515 | | .field = "lld_libraries", |
| 516 | | }, |
| 517 | | .{ |
| 518 | | .prefix = "#define ZIG_CLANG_LIBRARIES ", |
| 519 | | .field = "clang_libraries", |
| 520 | | }, |
| 521 | | .{ |
| 522 | | .prefix = "#define ZIG_LLVM_CONFIG_EXE ", |
| 523 | | .field = "llvm_config_exe", |
| 524 | | }, |
| 525 | | .{ |
| 526 | | .prefix = "#define ZIG_DIA_GUIDS_LIB ", |
| 527 | | .field = "dia_guids_lib", |
| 528 | | }, |
| 529 | | }; |
| 530 | | |
| 531 | | var lines_it = mem.tokenize(config_h_text, "\r\n"); |
| 532 | | while (lines_it.next()) |line| { |
| 533 | | inline for (mappings) |mapping| { |
| 534 | | if (mem.startsWith(u8, line, mapping.prefix)) { |
| 535 | | var it = mem.split(line, "\""); |
| 536 | | _ = it.next().?; // skip the stuff before the quote |
| 537 | | const quoted = it.next().?; // the stuff inside the quote |
| 538 | | @field(ctx, mapping.field) = toNativePathSep(b, quoted); |
| 539 | | } |
| 540 | | } |
| 541 | | } |
| 542 | | return ctx; |
| 543 | | } |
| 544 | | |
| 545 | | fn toNativePathSep(b: *Builder, s: []const u8) []u8 { |
| 546 | | const duplicated = mem.dupe(b.allocator, u8, s) catch unreachable; |
| 547 | | for (duplicated) |*byte| switch (byte.*) { |
| 548 | | '/' => byte.* = fs.path.sep, |
| 549 | | else => {}, |
| 550 | | }; |
| 551 | | return duplicated; |
| 552 | | } |
| 437 | const clang_libs = [_][]const u8{ |
| 438 | "clangFrontendTool", |
| 439 | "clangCodeGen", |
| 440 | "clangFrontend", |
| 441 | "clangDriver", |
| 442 | "clangSerialization", |
| 443 | "clangSema", |
| 444 | "clangStaticAnalyzerFrontend", |
| 445 | "clangStaticAnalyzerCheckers", |
| 446 | "clangStaticAnalyzerCore", |
| 447 | "clangAnalysis", |
| 448 | "clangASTMatchers", |
| 449 | "clangAST", |
| 450 | "clangParse", |
| 451 | "clangSema", |
| 452 | "clangBasic", |
| 453 | "clangEdit", |
| 454 | "clangLex", |
| 455 | "clangARCMigrate", |
| 456 | "clangRewriteFrontend", |
| 457 | "clangRewrite", |
| 458 | "clangCrossTU", |
| 459 | "clangIndex", |
| 460 | "clangToolingCore", |
| 461 | }; |
| 462 | const lld_libs = [_][]const u8{ |
| 463 | "lldDriver", |
| 464 | "lldMinGW", |
| 465 | "lldELF", |
| 466 | "lldCOFF", |
| 467 | "lldMachO", |
| 468 | "lldWasm", |
| 469 | "lldReaderWriter", |
| 470 | "lldCore", |
| 471 | "lldYAML", |
| 472 | "lldCommon", |
| 473 | }; |
| 474 | // This list can be re-generated with `llvm-config --libfiles` and then |
| 475 | // reformatting using your favorite text editor. Note we do not execute |
| 476 | // `llvm-config` here because we are cross compiling. |
| 477 | const llvm_libs = [_][]const u8{ |
| 478 | "LLVMXRay", |
| 479 | "LLVMWindowsManifest", |
| 480 | "LLVMSymbolize", |
| 481 | "LLVMDebugInfoPDB", |
| 482 | "LLVMOrcJIT", |
| 483 | "LLVMOrcError", |
| 484 | "LLVMJITLink", |
| 485 | "LLVMObjectYAML", |
| 486 | "LLVMMCA", |
| 487 | "LLVMLTO", |
| 488 | "LLVMPasses", |
| 489 | "LLVMCoroutines", |
| 490 | "LLVMObjCARCOpts", |
| 491 | "LLVMExtensions", |
| 492 | "LLVMLineEditor", |
| 493 | "LLVMLibDriver", |
| 494 | "LLVMInterpreter", |
| 495 | "LLVMFuzzMutate", |
| 496 | "LLVMMCJIT", |
| 497 | "LLVMExecutionEngine", |
| 498 | "LLVMRuntimeDyld", |
| 499 | "LLVMDWARFLinker", |
| 500 | "LLVMDlltoolDriver", |
| 501 | "LLVMOption", |
| 502 | "LLVMDebugInfoGSYM", |
| 503 | "LLVMCoverage", |
| 504 | "LLVMXCoreDisassembler", |
| 505 | "LLVMXCoreCodeGen", |
| 506 | "LLVMXCoreDesc", |
| 507 | "LLVMXCoreInfo", |
| 508 | "LLVMX86Disassembler", |
| 509 | "LLVMX86CodeGen", |
| 510 | "LLVMX86AsmParser", |
| 511 | "LLVMX86Desc", |
| 512 | "LLVMX86Info", |
| 513 | "LLVMWebAssemblyDisassembler", |
| 514 | "LLVMWebAssemblyCodeGen", |
| 515 | "LLVMWebAssemblyDesc", |
| 516 | "LLVMWebAssemblyAsmParser", |
| 517 | "LLVMWebAssemblyInfo", |
| 518 | "LLVMSystemZDisassembler", |
| 519 | "LLVMSystemZCodeGen", |
| 520 | "LLVMSystemZAsmParser", |
| 521 | "LLVMSystemZDesc", |
| 522 | "LLVMSystemZInfo", |
| 523 | "LLVMSparcDisassembler", |
| 524 | "LLVMSparcCodeGen", |
| 525 | "LLVMSparcAsmParser", |
| 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 | }; |