| ... | ... | @@ -34,22 +34,7 @@ pub fn build(b: *Builder) !void { |
| 34 | 34 | |
| 35 | 35 | const test_step = b.step("test", "Run all the tests"); |
| 36 | 36 | |
| 37 | | // find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library |
| 38 | | const build_info = try b.exec(&[_][]const u8{ |
| 39 | | b.zig_exe, |
| 40 | | "BUILD_INFO", |
| 41 | | }); |
| 42 | | var index: usize = 0; |
| 43 | | var ctx = Context{ |
| 44 | | .cmake_binary_dir = nextValue(&index, build_info), |
| 45 | | .cxx_compiler = nextValue(&index, build_info), |
| 46 | | .llvm_config_exe = nextValue(&index, build_info), |
| 47 | | .lld_include_dir = nextValue(&index, build_info), |
| 48 | | .lld_libraries = nextValue(&index, build_info), |
| 49 | | .clang_libraries = nextValue(&index, build_info), |
| 50 | | .dia_guids_lib = nextValue(&index, build_info), |
| 51 | | .llvm = undefined, |
| 52 | | }; |
| 37 | var ctx = try findAndParseConfigH(b); |
| 53 | 38 | ctx.llvm = try findLLVM(b, ctx.llvm_config_exe); |
| 54 | 39 | |
| 55 | 40 | var test_stage2 = b.addTest("src-self-hosted/test.zig"); |
| ... | ... | @@ -262,25 +247,6 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { |
| 262 | 247 | return result; |
| 263 | 248 | } |
| 264 | 249 | |
| 265 | | fn nextValue(index: *usize, build_info: []const u8) []const u8 { |
| 266 | | const start = index.*; |
| 267 | | while (true) : (index.* += 1) { |
| 268 | | switch (build_info[index.*]) { |
| 269 | | '\n' => { |
| 270 | | const result = build_info[start..index.*]; |
| 271 | | index.* += 1; |
| 272 | | return result; |
| 273 | | }, |
| 274 | | '\r' => { |
| 275 | | const result = build_info[start..index.*]; |
| 276 | | index.* += 2; |
| 277 | | return result; |
| 278 | | }, |
| 279 | | else => continue, |
| 280 | | } |
| 281 | | } |
| 282 | | } |
| 283 | | |
| 284 | 250 | fn configureStage2(b: *Builder, exe: var, ctx: Context) !void { |
| 285 | 251 | exe.addIncludeDir("src"); |
| 286 | 252 | exe.addIncludeDir(ctx.cmake_binary_dir); |
| ... | ... | @@ -376,3 +342,79 @@ const Context = struct { |
| 376 | 342 | dia_guids_lib: []const u8, |
| 377 | 343 | llvm: LibraryDep, |
| 378 | 344 | }; |
| 345 | |
| 346 | fn findAndParseConfigH(b: *Builder) !Context { |
| 347 | var check_dir = fs.path.dirname(b.zig_exe).?; |
| 348 | const config_h_text = while (true) { |
| 349 | var dir = try fs.cwd().openDir(check_dir, .{}); |
| 350 | defer dir.close(); |
| 351 | |
| 352 | const max_bytes = 1 * 1024 * 1024; |
| 353 | const config_h_text = dir.readFileAlloc(b.allocator, "config.h", max_bytes) catch |err| switch (err) { |
| 354 | error.FileNotFound => { |
| 355 | check_dir = fs.path.dirname(check_dir) orelse { |
| 356 | std.debug.warn("Unable to find config.h file relative to Zig executable.\n", .{}); |
| 357 | std.debug.warn("`zig build` must be run using a Zig executable within the source tree.\n", .{}); |
| 358 | std.process.exit(1); |
| 359 | }; |
| 360 | continue; |
| 361 | }, |
| 362 | else => |e| return e, |
| 363 | }; |
| 364 | break config_h_text; |
| 365 | } else unreachable; // TODO should not need `else unreachable`. |
| 366 | |
| 367 | var ctx: Context = .{ |
| 368 | .cmake_binary_dir = undefined, |
| 369 | .cxx_compiler = undefined, |
| 370 | .llvm_config_exe = undefined, |
| 371 | .lld_include_dir = undefined, |
| 372 | .lld_libraries = undefined, |
| 373 | .clang_libraries = undefined, |
| 374 | .dia_guids_lib = undefined, |
| 375 | .llvm = undefined, |
| 376 | }; |
| 377 | |
| 378 | const mappings = [_]struct { prefix: []const u8, field: []const u8 }{ |
| 379 | .{ |
| 380 | .prefix = "#define ZIG_CMAKE_BINARY_DIR ", |
| 381 | .field = "cmake_binary_dir", |
| 382 | }, |
| 383 | .{ |
| 384 | .prefix = "#define ZIG_CXX_COMPILER ", |
| 385 | .field = "cxx_compiler", |
| 386 | }, |
| 387 | .{ |
| 388 | .prefix = "#define ZIG_LLD_INCLUDE_PATH ", |
| 389 | .field = "lld_include_dir", |
| 390 | }, |
| 391 | .{ |
| 392 | .prefix = "#define ZIG_LLD_LIBRARIES ", |
| 393 | .field = "lld_libraries", |
| 394 | }, |
| 395 | .{ |
| 396 | .prefix = "#define ZIG_CLANG_LIBRARIES ", |
| 397 | .field = "clang_libraries", |
| 398 | }, |
| 399 | .{ |
| 400 | .prefix = "#define ZIG_LLVM_CONFIG_EXE ", |
| 401 | .field = "llvm_config_exe", |
| 402 | }, |
| 403 | .{ |
| 404 | .prefix = "#define ZIG_DIA_GUIDS_LIB ", |
| 405 | .field = "dia_guids_lib", |
| 406 | }, |
| 407 | }; |
| 408 | |
| 409 | var lines_it = mem.tokenize(config_h_text, "\r\n"); |
| 410 | while (lines_it.next()) |line| { |
| 411 | inline for (mappings) |mapping| { |
| 412 | if (mem.startsWith(u8, line, mapping.prefix)) { |
| 413 | var it = mem.separate(line, "\""); |
| 414 | _ = it.next().?; // skip the stuff before the quote |
| 415 | @field(ctx, mapping.field) = it.next().?; // the stuff inside the quote |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | return ctx; |
| 420 | } |