authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-01 15:41:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-02 20:43:01-07:00
logb3596d72b07880a49228a5756d509ad51100a53a
tree6799a453d3fa6e01e59c8f9b01ba08a62786f3da
parent8f867eaf84a2c270a2d1eb7208730519036eb364

CLI: add --host-target, --host-cpu, and --host-dynamic-linker

These are advanced options that make it possible to simultaneously target the "host" computer, while overriding assumptions about the host computer such as what CPU features are available, or what OS version range to target. This is useful only for the case of integrating with system package manager builds which want to pretend the host is also the target.

1 files changed, 21 insertions(+), 1 deletions(-)

src/main.zig+21-1
......@@ -969,6 +969,9 @@ fn buildOutputType(
969969 .libc_paths_file = try EnvVar.ZIG_LIBC.get(arena),
970970 .link_objects = .{},
971971 .native_system_include_paths = &.{},
972 .host_triple = null,
973 .host_cpu = null,
974 .host_dynamic_linker = null,
972975 };
973976
974977 // before arg parsing, check for the NO_COLOR environment variable
......@@ -1262,6 +1265,12 @@ fn buildOutputType(
12621265 mod_opts.optimize_mode = parseOptimizeMode(arg["-O".len..]);
12631266 } else if (mem.eql(u8, arg, "--dynamic-linker")) {
12641267 create_module.dynamic_linker = args_iter.nextOrFatal();
1268 } else if (mem.eql(u8, arg, "--host-target")) {
1269 create_module.host_triple = args_iter.nextOrFatal();
1270 } else if (mem.eql(u8, arg, "--host-cpu")) {
1271 create_module.host_cpu = args_iter.nextOrFatal();
1272 } else if (mem.eql(u8, arg, "--host-dynamic-linker")) {
1273 create_module.host_dynamic_linker = args_iter.nextOrFatal();
12651274 } else if (mem.eql(u8, arg, "--sysroot")) {
12661275 const next_arg = args_iter.nextOrFatal();
12671276 create_module.sysroot = next_arg;
......@@ -3455,6 +3464,9 @@ const CreateModule = struct {
34553464 each_lib_rpath: ?bool,
34563465 libc_paths_file: ?[]const u8,
34573466 link_objects: std.ArrayListUnmanaged(Compilation.LinkObject),
3467 host_triple: ?[]const u8,
3468 host_cpu: ?[]const u8,
3469 host_dynamic_linker: ?[]const u8,
34583470};
34593471
34603472fn createModule(
......@@ -3539,7 +3551,15 @@ fn createModule(
35393551 }
35403552
35413553 const target_query = parseTargetQueryOrReportFatalError(arena, target_parse_options);
3542 const target = resolveTargetQueryOrFatal(target_query);
3554 const adjusted_target_query = a: {
3555 if (!target_query.isNative()) break :a target_query;
3556 if (create_module.host_triple) |triple| target_parse_options.arch_os_abi = triple;
3557 if (create_module.host_cpu) |cpu| target_parse_options.cpu_features = cpu;
3558 if (create_module.host_dynamic_linker) |dl| target_parse_options.dynamic_linker = dl;
3559 break :a parseTargetQueryOrReportFatalError(arena, target_parse_options);
3560 };
3561
3562 const target = resolveTargetQueryOrFatal(adjusted_target_query);
35433563 break :t .{
35443564 .result = target,
35453565 .is_native_os = target_query.isNativeOs(),