| ... | ... | @@ -32,7 +32,6 @@ const Module = @import("Module.zig"); |
| 32 | 32 | const InternPool = @import("InternPool.zig"); |
| 33 | 33 | const BuildId = std.Build.CompileStep.BuildId; |
| 34 | 34 | const Cache = std.Build.Cache; |
| 35 | | const translate_c = @import("translate_c.zig"); |
| 36 | 35 | const c_codegen = @import("codegen/c.zig"); |
| 37 | 36 | const libtsan = @import("libtsan.zig"); |
| 38 | 37 | const Zir = @import("Zir.zig"); |
| ... | ... | @@ -88,7 +87,7 @@ failed_win32_resources: if (build_options.only_core_functionality) void else std |
| 88 | 87 | misc_failures: std.AutoArrayHashMapUnmanaged(MiscTask, MiscError) = .{}, |
| 89 | 88 | |
| 90 | 89 | keep_source_files_loaded: bool, |
| 91 | | use_clang: bool, |
| 90 | c_frontend: CFrontend, |
| 92 | 91 | sanitize_c: bool, |
| 93 | 92 | /// When this is `true` it means invoking clang as a sub-process is expected to inherit |
| 94 | 93 | /// stdin, stdout, stderr, and if it returns non success, to forward the exit code. |
| ... | ... | @@ -515,6 +514,8 @@ pub const cache_helpers = struct { |
| 515 | 514 | } |
| 516 | 515 | }; |
| 517 | 516 | |
| 517 | pub const CFrontend = enum { clang, aro }; |
| 518 | |
| 518 | 519 | pub const ClangPreprocessorMode = enum { |
| 519 | 520 | no, |
| 520 | 521 | /// This means we are doing `zig cc -E -o <path>`. |
| ... | ... | @@ -1046,14 +1047,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1046 | 1047 | break :pic explicit; |
| 1047 | 1048 | } else pie or must_pic; |
| 1048 | 1049 | |
| 1049 | | // Make a decision on whether to use Clang for translate-c and compiling C files. |
| 1050 | | const use_clang = if (options.use_clang) |explicit| explicit else blk: { |
| 1051 | | if (build_options.have_llvm) { |
| 1052 | | // Can't use it if we don't have it! |
| 1053 | | break :blk false; |
| 1054 | | } |
| 1055 | | // It's not planned to do our own translate-c or C compilation. |
| 1056 | | break :blk true; |
| 1050 | // Make a decision on whether to use Clang or Aro for translate-c and compiling C files. |
| 1051 | const c_frontend: CFrontend = blk: { |
| 1052 | if (!build_options.have_llvm) break :blk .aro; |
| 1053 | if (options.use_clang) |explicit| if (explicit) break :blk .clang; |
| 1054 | break :blk .clang; |
| 1057 | 1055 | }; |
| 1058 | 1056 | |
| 1059 | 1057 | const is_safe_mode = switch (options.optimize_mode) { |
| ... | ... | @@ -1677,7 +1675,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1677 | 1675 | .astgen_work_queue = std.fifo.LinearFifo(*Module.File, .Dynamic).init(gpa), |
| 1678 | 1676 | .embed_file_work_queue = std.fifo.LinearFifo(*Module.EmbedFile, .Dynamic).init(gpa), |
| 1679 | 1677 | .keep_source_files_loaded = options.keep_source_files_loaded, |
| 1680 | | .use_clang = use_clang, |
| 1678 | .c_frontend = c_frontend, |
| 1681 | 1679 | .clang_argv = options.clang_argv, |
| 1682 | 1680 | .c_source_files = options.c_source_files, |
| 1683 | 1681 | .rc_source_files = options.rc_source_files, |
| ... | ... | @@ -3918,9 +3916,7 @@ pub const CImportResult = struct { |
| 3918 | 3916 | /// This API is currently coupled pretty tightly to stage1's needs; it will need to be reworked |
| 3919 | 3917 | /// a bit when we want to start using it from self-hosted. |
| 3920 | 3918 | pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult { |
| 3921 | | if (!build_options.have_llvm) |
| 3922 | | return error.ZigCompilerNotBuiltWithLLVMExtensions; |
| 3923 | | |
| 3919 | if (build_options.only_c) unreachable; // @cImport is not needed for bootstrapping |
| 3924 | 3920 | const tracy_trace = trace(@src()); |
| 3925 | 3921 | defer tracy_trace.end(); |
| 3926 | 3922 | |
| ... | ... | @@ -3967,7 +3963,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult { |
| 3967 | 3963 | var argv = std.ArrayList([]const u8).init(comp.gpa); |
| 3968 | 3964 | defer argv.deinit(); |
| 3969 | 3965 | |
| 3970 | | try argv.append(""); // argv[0] is program name, actual args start at [1] |
| 3966 | try argv.append(@tagName(comp.c_frontend)); // argv[0] is program name, actual args start at [1] |
| 3971 | 3967 | try comp.addTranslateCCArgs(arena, &argv, .c, out_dep_path); |
| 3972 | 3968 | |
| 3973 | 3969 | try argv.append(out_h_path); |
| ... | ... | @@ -3975,31 +3971,43 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult { |
| 3975 | 3971 | if (comp.verbose_cc) { |
| 3976 | 3972 | dump_argv(argv.items); |
| 3977 | 3973 | } |
| 3974 | var tree = switch (comp.c_frontend) { |
| 3975 | .aro => tree: { |
| 3976 | if (builtin.zig_backend == .stage2_c) @panic("the CBE cannot compile Aro yet!"); |
| 3977 | const translate_c = @import("aro_translate_c.zig"); |
| 3978 | _ = translate_c; |
| 3979 | break :tree undefined; |
| 3980 | }, |
| 3981 | .clang => tree: { |
| 3982 | if (!build_options.have_llvm) unreachable; |
| 3983 | const translate_c = @import("translate_c.zig"); |
| 3984 | |
| 3985 | // Convert to null terminated args. |
| 3986 | const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, argv.items.len + 1); |
| 3987 | new_argv_with_sentinel[argv.items.len] = null; |
| 3988 | const new_argv = new_argv_with_sentinel[0..argv.items.len :null]; |
| 3989 | for (argv.items, 0..) |arg, i| { |
| 3990 | new_argv[i] = try arena.dupeZ(u8, arg); |
| 3991 | } |
| 3978 | 3992 | |
| 3979 | | // Convert to null terminated args. |
| 3980 | | const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, argv.items.len + 1); |
| 3981 | | new_argv_with_sentinel[argv.items.len] = null; |
| 3982 | | const new_argv = new_argv_with_sentinel[0..argv.items.len :null]; |
| 3983 | | for (argv.items, 0..) |arg, i| { |
| 3984 | | new_argv[i] = try arena.dupeZ(u8, arg); |
| 3985 | | } |
| 3986 | | |
| 3987 | | const c_headers_dir_path_z = try comp.zig_lib_directory.joinZ(arena, &[_][]const u8{"include"}); |
| 3988 | | var errors = std.zig.ErrorBundle.empty; |
| 3989 | | errdefer errors.deinit(comp.gpa); |
| 3990 | | var tree = translate_c.translate( |
| 3991 | | comp.gpa, |
| 3992 | | new_argv.ptr, |
| 3993 | | new_argv.ptr + new_argv.len, |
| 3994 | | &errors, |
| 3995 | | c_headers_dir_path_z, |
| 3996 | | ) catch |err| switch (err) { |
| 3997 | | error.OutOfMemory => return error.OutOfMemory, |
| 3998 | | error.SemanticAnalyzeFail => { |
| 3999 | | return CImportResult{ |
| 4000 | | .out_zig_path = "", |
| 4001 | | .cache_hit = actual_hit, |
| 4002 | | .errors = errors, |
| 3993 | const c_headers_dir_path_z = try comp.zig_lib_directory.joinZ(arena, &[_][]const u8{"include"}); |
| 3994 | var errors = std.zig.ErrorBundle.empty; |
| 3995 | errdefer errors.deinit(comp.gpa); |
| 3996 | break :tree translate_c.translate( |
| 3997 | comp.gpa, |
| 3998 | new_argv.ptr, |
| 3999 | new_argv.ptr + new_argv.len, |
| 4000 | &errors, |
| 4001 | c_headers_dir_path_z, |
| 4002 | ) catch |err| switch (err) { |
| 4003 | error.OutOfMemory => return error.OutOfMemory, |
| 4004 | error.SemanticAnalyzeFail => { |
| 4005 | return CImportResult{ |
| 4006 | .out_zig_path = "", |
| 4007 | .cache_hit = actual_hit, |
| 4008 | .errors = errors, |
| 4009 | }; |
| 4010 | }, |
| 4003 | 4011 | }; |
| 4004 | 4012 | }, |
| 4005 | 4013 | }; |
| ... | ... | @@ -4249,6 +4257,9 @@ fn reportRetryableEmbedFileError( |
| 4249 | 4257 | } |
| 4250 | 4258 | |
| 4251 | 4259 | fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.Progress.Node) !void { |
| 4260 | if (comp.c_frontend == .aro) { |
| 4261 | return comp.failCObj(c_object, "aro does not support compiling C objects yet", .{}); |
| 4262 | } |
| 4252 | 4263 | if (!build_options.have_llvm) { |
| 4253 | 4264 | return comp.failCObj(c_object, "clang not available: compiler built without LLVM extensions", .{}); |
| 4254 | 4265 | } |