| ... | ... | @@ -1,121 +1,152 @@ |
| 1 | | fn make(step: *Step, options: Step.MakeOptions) !void { |
| 2 | | const prog_node = options.progress_node; |
| 3 | | const b = step.owner; |
| 4 | | const translate_c: *TranslateC = @fieldParentPtr("step", step); |
| 5 | | const arena = b.graph.arena; |
| 6 | | |
| 7 | | var argv_list = std.array_list.Managed([]const u8).init(b.allocator); |
| 8 | | try argv_list.append(b.graph.zig_exe); |
| 9 | | try argv_list.append("translate-c"); |
| 10 | | if (translate_c.link_libc) { |
| 11 | | try argv_list.append("-lc"); |
| 12 | | } |
| 1 | const TranslateC = @This(); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Io = std.Io; |
| 5 | const Configuration = std.Build.Configuration; |
| 6 | const allocPrint = std.fmt.allocPrint; |
| 7 | const assert = std.debug.assert; |
| 8 | |
| 9 | const Step = @import("../Step.zig"); |
| 10 | const Maker = @import("../../Maker.zig"); |
| 11 | const PkgConfig = @import("../PkgConfig.zig"); |
| 13 | 12 | |
| 14 | | try argv_list.append("--cache-dir"); |
| 15 | | try argv_list.append(b.cache_root.path orelse "."); |
| 13 | pub fn make( |
| 14 | translate_c: *TranslateC, |
| 15 | step_index: Configuration.Step.Index, |
| 16 | maker: *Maker, |
| 17 | progress_node: std.Progress.Node, |
| 18 | ) Step.ExtendedMakeError!void { |
| 19 | _ = translate_c; |
| 20 | const graph = maker.graph; |
| 21 | const arena = graph.arena; // TODO don't leak into the process arena |
| 22 | const step = maker.stepByIndex(step_index); |
| 23 | const conf = &maker.scanned_config.configuration; |
| 24 | const conf_step = step_index.ptr(conf); |
| 25 | const conf_tc = conf_step.extended.get(conf.extra).translate_c; |
| 26 | const cache_root = graph.local_cache_root; |
| 16 | 27 | |
| 17 | | try argv_list.append("--global-cache-dir"); |
| 18 | | try argv_list.append(b.graph.global_cache_root.path orelse "."); |
| 28 | var argv: std.ArrayList([]const u8) = .empty; |
| 19 | 29 | |
| 20 | | if (!translate_c.target.query.isNative()) { |
| 21 | | try argv_list.append("-target"); |
| 22 | | try argv_list.append(try translate_c.target.query.zigTriple(b.allocator)); |
| 30 | try argv.ensureUnusedCapacity(arena, 10); |
| 31 | argv.appendAssumeCapacity(graph.zig_exe); |
| 32 | argv.appendAssumeCapacity("translate-c"); |
| 33 | if (conf_tc.flags.link_libc) { |
| 34 | argv.appendAssumeCapacity("-lc"); |
| 23 | 35 | } |
| 24 | 36 | |
| 25 | | switch (translate_c.optimize) { |
| 26 | | .Debug => {}, // Skip since it's the default. |
| 27 | | else => try argv_list.append(b.fmt("-O{s}", .{@tagName(translate_c.optimize)})), |
| 37 | argv.appendAssumeCapacity("--cache-dir"); |
| 38 | argv.appendAssumeCapacity(cache_root.path orelse "."); |
| 39 | |
| 40 | argv.appendAssumeCapacity("--global-cache-dir"); |
| 41 | argv.appendAssumeCapacity(graph.global_cache_root.path orelse "."); |
| 42 | |
| 43 | if (conf_tc.target.get(conf).?.query.unwrap()) |compact_query| { |
| 44 | const query = compact_query.get(conf).unwrap(conf); |
| 45 | argv.appendAssumeCapacity("-target"); |
| 46 | argv.appendAssumeCapacity(try query.zigTriple(arena)); |
| 28 | 47 | } |
| 29 | 48 | |
| 30 | | for (translate_c.include_dirs.items) |include_dir| { |
| 31 | | try include_dir.appendZigProcessFlags(b, &argv_list, step); |
| 49 | switch (conf_tc.flags.optimize) { |
| 50 | .debug, .default => {}, // Skip since it's the default. |
| 51 | else => argv.appendAssumeCapacity(try allocPrint(arena, "-O{t}", .{conf_tc.flags.optimize})), |
| 32 | 52 | } |
| 33 | 53 | |
| 34 | | for (translate_c.c_macros.items) |c_macro| { |
| 35 | | try argv_list.append("-D"); |
| 36 | | try argv_list.append(c_macro); |
| 54 | try argv.ensureUnusedCapacity(arena, conf_tc.include_dirs.len * 2); |
| 55 | for (0..conf_tc.include_dirs.len) |i| |
| 56 | try Step.Compile.appendIncludeDirFlags(conf_tc.include_dirs.get(conf.extra, i), &argv, step_index, maker); |
| 57 | |
| 58 | for (conf_tc.c_macros.slice) |c_macro| { |
| 59 | (try argv.addManyAsArray(arena, 2)).* = .{ "-D", c_macro.slice(conf) }; |
| 37 | 60 | } |
| 38 | 61 | |
| 39 | 62 | var prev_search_strategy: std.Build.Module.SystemLib.SearchStrategy = .paths_first; |
| 40 | 63 | var prev_preferred_link_mode: std.builtin.LinkMode = .dynamic; |
| 64 | var seen_system_libs: std.AutoArrayHashMapUnmanaged(Configuration.String, []const []const u8) = .empty; |
| 41 | 65 | |
| 42 | | for (translate_c.system_libs.items) |*system_lib| { |
| 43 | | var seen_system_libs: std.StringHashMapUnmanaged([]const []const u8) = .empty; |
| 66 | for (conf_tc.system_libs.slice) |system_lib_index| { |
| 67 | const system_lib = system_lib_index.get(conf); |
| 68 | const system_lib_name = system_lib.name.slice(conf); |
| 44 | 69 | const system_lib_gop = try seen_system_libs.getOrPut(arena, system_lib.name); |
| 45 | 70 | if (system_lib_gop.found_existing) { |
| 46 | | try argv_list.appendSlice(system_lib_gop.value_ptr.*); |
| 71 | try argv.appendSlice(arena, system_lib_gop.value_ptr.*); |
| 47 | 72 | continue; |
| 48 | 73 | } else { |
| 49 | 74 | system_lib_gop.value_ptr.* = &.{}; |
| 50 | 75 | } |
| 51 | 76 | |
| 52 | | if (system_lib.search_strategy != prev_search_strategy or |
| 53 | | system_lib.preferred_link_mode != prev_preferred_link_mode) |
| 77 | if ((system_lib.flags.search_strategy != prev_search_strategy or |
| 78 | system_lib.flags.preferred_link_mode != prev_preferred_link_mode)) |
| 54 | 79 | { |
| 55 | | switch (system_lib.search_strategy) { |
| 56 | | .no_fallback => switch (system_lib.preferred_link_mode) { |
| 57 | | .dynamic => try argv_list.append("-search_dylibs_only"), |
| 58 | | .static => try argv_list.append("-search_static_only"), |
| 80 | try argv.ensureUnusedCapacity(arena, 1); |
| 81 | switch (system_lib.flags.search_strategy) { |
| 82 | .no_fallback => switch (system_lib.flags.preferred_link_mode) { |
| 83 | .dynamic => argv.appendAssumeCapacity("-search_dylibs_only"), |
| 84 | .static => argv.appendAssumeCapacity("-search_static_only"), |
| 59 | 85 | }, |
| 60 | | .paths_first => switch (system_lib.preferred_link_mode) { |
| 61 | | .dynamic => try argv_list.append("-search_paths_first"), |
| 62 | | .static => try argv_list.append("-search_paths_first_static"), |
| 86 | .paths_first => switch (system_lib.flags.preferred_link_mode) { |
| 87 | .dynamic => argv.appendAssumeCapacity("-search_paths_first"), |
| 88 | .static => argv.appendAssumeCapacity("-search_paths_first_static"), |
| 63 | 89 | }, |
| 64 | | .mode_first => switch (system_lib.preferred_link_mode) { |
| 65 | | .dynamic => try argv_list.append("-search_dylibs_first"), |
| 66 | | .static => try argv_list.append("-search_static_first"), |
| 90 | .mode_first => switch (system_lib.flags.preferred_link_mode) { |
| 91 | .dynamic => argv.appendAssumeCapacity("-search_dylibs_first"), |
| 92 | .static => argv.appendAssumeCapacity("-search_static_first"), |
| 67 | 93 | }, |
| 68 | 94 | } |
| 69 | | prev_search_strategy = system_lib.search_strategy; |
| 70 | | prev_preferred_link_mode = system_lib.preferred_link_mode; |
| 95 | prev_search_strategy = system_lib.flags.search_strategy; |
| 96 | prev_preferred_link_mode = system_lib.flags.preferred_link_mode; |
| 71 | 97 | } |
| 72 | 98 | |
| 73 | 99 | const prefix: []const u8 = prefix: { |
| 74 | | if (system_lib.needed) break :prefix "-needed-l"; |
| 75 | | if (system_lib.weak) break :prefix "-weak-l"; |
| 100 | if (system_lib.flags.needed) break :prefix "-needed-l"; |
| 101 | if (system_lib.flags.weak) break :prefix "-weak-l"; |
| 76 | 102 | break :prefix "-l"; |
| 77 | 103 | }; |
| 78 | | switch (system_lib.use_pkg_config) { |
| 79 | | .no => try argv_list.append(b.fmt("{s}{s}", .{ prefix, system_lib.name })), |
| 80 | | .yes, .force => { |
| 81 | | if (Step.Compile.runPkgConfig(&translate_c.step, system_lib.name)) |result| { |
| 82 | | try argv_list.appendSlice(result.cflags); |
| 83 | | try argv_list.appendSlice(result.libs); |
| 104 | l: { |
| 105 | pc: { |
| 106 | const force = switch (system_lib.flags.use_pkg_config) { |
| 107 | .no => break :pc, |
| 108 | .yes => false, |
| 109 | .force => true, |
| 110 | }; |
| 111 | |
| 112 | const pkg_conf_node = progress_node.start("pkg-config", 0); |
| 113 | defer pkg_conf_node.end(); |
| 114 | |
| 115 | if (PkgConfig.run(maker, step, pkg_conf_node, system_lib_name, force)) |result| { |
| 116 | try argv.appendSlice(arena, result.cflags); |
| 117 | try argv.appendSlice(arena, result.libs); |
| 84 | 118 | try seen_system_libs.put(arena, system_lib.name, result.cflags); |
| 119 | break :l; |
| 85 | 120 | } else |err| switch (err) { |
| 86 | | error.PkgConfigInvalidOutput, |
| 87 | | error.PkgConfigCrashed, |
| 88 | | error.PkgConfigFailed, |
| 89 | | error.PkgConfigNotInstalled, |
| 121 | error.PkgConfigUnavailable, |
| 90 | 122 | error.PackageNotFound, |
| 91 | | => switch (system_lib.use_pkg_config) { |
| 92 | | .yes => { |
| 93 | | // pkg-config failed, so fall back to linking the library |
| 94 | | // by name directly. |
| 95 | | try argv_list.append(b.fmt("{s}{s}", .{ |
| 96 | | prefix, |
| 97 | | system_lib.name, |
| 98 | | })); |
| 99 | | }, |
| 100 | | .force => { |
| 101 | | std.debug.panic("pkg-config failed for library {s}", .{system_lib.name}); |
| 102 | | }, |
| 103 | | .no => unreachable, |
| 123 | => { |
| 124 | // pkg-config failed, so fall back to linking the library by name directly. |
| 125 | assert(!force); |
| 126 | break :pc; |
| 104 | 127 | }, |
| 105 | | |
| 106 | 128 | else => |e| return e, |
| 107 | 129 | } |
| 108 | | }, |
| 130 | } |
| 131 | try argv.append(arena, try allocPrint(arena, "{s}{s}", .{ |
| 132 | prefix, system_lib_name, |
| 133 | })); |
| 109 | 134 | } |
| 110 | 135 | } |
| 111 | 136 | |
| 112 | | const c_source_path = translate_c.source.getPath2(b, step); |
| 113 | | try argv_list.append(c_source_path); |
| 137 | try argv.ensureUnusedCapacity(arena, 2); |
| 138 | |
| 139 | const c_source_path = try maker.resolveLazyPathIndexAbs(arena, conf_tc.src_path, step_index); |
| 140 | argv.appendAssumeCapacity(c_source_path); |
| 141 | |
| 142 | argv.appendAssumeCapacity("--listen=-"); |
| 143 | const output_dir_path = (Step.evalZigProcess(step_index, maker, argv.items, progress_node, false) catch |err| switch (err) { |
| 144 | error.NeedCompileErrorCheck => unreachable, |
| 145 | else => |e| return e, |
| 146 | }).?; |
| 114 | 147 | |
| 115 | | try argv_list.append("--listen=-"); |
| 116 | | const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false, options.web_server, options.gpa); |
| 148 | const stem = Io.Dir.path.stem(Io.Dir.path.basename(c_source_path)); |
| 149 | const out_basename = try allocPrint(arena, "{s}.zig", .{stem}); |
| 117 | 150 | |
| 118 | | const basename = std.fs.path.stem(std.fs.path.basename(c_source_path)); |
| 119 | | translate_c.out_basename = b.fmt("{s}.zig", .{basename}); |
| 120 | | translate_c.output_file.path = output_dir.?.joinString(b.allocator, translate_c.out_basename) catch @panic("OOM"); |
| 151 | maker.generatedPath(conf_tc.output_file).* = try output_dir_path.join(arena, out_basename); |
| 121 | 152 | } |