authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-04 11:57:28-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-04 11:57:28-04:00
logd73808f3ff7d36b32d53cc38cf9a5b29b1ea57d4
tree37127821805144c8c7e949c4fcbd3b827eca50bf
parente89c42655cf9851cdf02065bc75cda0e27884966
signaturelock-open Commit is signed but in an unrecognized format.

remove `zig BUILD_INFO` hack

Rather than stuffing configuration information into the Zig binary, the build script reads it from config.h. This solves a problem for package maintainers and improves the use case of deterministic builds. closes #3758

2 files changed, 77 insertions(+), 47 deletions(-)

build.zig+77-35
......@@ -34,22 +34,7 @@ pub fn build(b: *Builder) !void {
3434
3535 const test_step = b.step("test", "Run all the tests");
3636
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);
5338 ctx.llvm = try findLLVM(b, ctx.llvm_config_exe);
5439
5540 var test_stage2 = b.addTest("src-self-hosted/test.zig");
......@@ -262,25 +247,6 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep {
262247 return result;
263248}
264249
265fn 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
284250fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
285251 exe.addIncludeDir("src");
286252 exe.addIncludeDir(ctx.cmake_binary_dir);
......@@ -376,3 +342,79 @@ const Context = struct {
376342 dia_guids_lib: []const u8,
377343 llvm: LibraryDep,
378344};
345
346fn 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}
src/main.cpp-12
......@@ -260,18 +260,6 @@ static int main0(int argc, char **argv) {
260260 char *arg0 = argv[0];
261261 Error err;
262262
263 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
264 printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
265 ZIG_CMAKE_BINARY_DIR,
266 ZIG_CXX_COMPILER,
267 ZIG_LLVM_CONFIG_EXE,
268 ZIG_LLD_INCLUDE_PATH,
269 ZIG_LLD_LIBRARIES,
270 ZIG_CLANG_LIBRARIES,
271 ZIG_DIA_GUIDS_LIB);
272 return 0;
273 }
274
275263 if (argc >= 2 && (strcmp(argv[1], "clang") == 0 ||
276264 strcmp(argv[1], "-cc1") == 0 || strcmp(argv[1], "-cc1as") == 0))
277265 {