| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | const Allocator = std.mem.Allocator; |
| 4 | const cli = @import("cli.zig"); |
| 5 | const Dependencies = @import("compile.zig").Dependencies; |
| 6 | const aro = @import("aro"); |
| 7 | |
| 8 | const PreprocessError = error{ ArgError, GeneratedSourceError, PreprocessError, FileTooBig, OutOfMemory, WriteFailed }; |
| 9 | |
| 10 | pub fn preprocess( |
| 11 | comp: *aro.Compilation, |
| 12 | writer: *std.Io.Writer, |
| 13 | /// Expects argv[0] to be the command name |
| 14 | argv: []const []const u8, |
| 15 | maybe_dependencies: ?*Dependencies, |
| 16 | ) PreprocessError!void { |
| 17 | var driver: aro.Driver = .{ .comp = comp, .diagnostics = comp.diagnostics, .aro_name = "arocc" }; |
| 18 | defer driver.deinit(); |
| 19 | |
| 20 | var macro_buf: std.ArrayList(u8) = .empty; |
| 21 | defer macro_buf.deinit(comp.gpa); |
| 22 | |
| 23 | var discard_buffer: [64]u8 = undefined; |
| 24 | var discarding: std.Io.Writer.Discarding = .init(&discard_buffer); |
| 25 | _ = driver.parseArgs(&discarding.writer, &macro_buf, argv) catch |err| switch (err) { |
| 26 | error.FatalError => return error.ArgError, |
| 27 | error.OutOfMemory => |e| return e, |
| 28 | error.WriteFailed => unreachable, |
| 29 | }; |
| 30 | try comp.initSearchPath(driver.includes.items, false); |
| 31 | |
| 32 | if (hasAnyErrors(comp)) return error.ArgError; |
| 33 | |
| 34 | // .include_system_defines gives us things like _WIN32 |
| 35 | const builtin_macros = comp.generateBuiltinMacros(.include_system_defines) catch |err| switch (err) { |
| 36 | error.FatalError => return error.GeneratedSourceError, |
| 37 | else => |e| return e, |
| 38 | }; |
| 39 | const user_macros = comp.addSourceFromBuffer("<command line>", macro_buf.items) catch |err| switch (err) { |
| 40 | error.FatalError => return error.GeneratedSourceError, |
| 41 | else => |e| return e, |
| 42 | }; |
| 43 | const source = driver.inputs.items[0]; |
| 44 | |
| 45 | if (hasAnyErrors(comp)) return error.GeneratedSourceError; |
| 46 | |
| 47 | comp.generated_buf.items.len = 0; |
| 48 | var pp = aro.Preprocessor.init(comp, .{ .base_file = source.id }) catch |err| switch (err) { |
| 49 | error.FatalError => return error.GeneratedSourceError, |
| 50 | error.OutOfMemory => |e| return e, |
| 51 | }; |
| 52 | defer pp.deinit(); |
| 53 | |
| 54 | if (comp.langopts.ms_extensions) { |
| 55 | comp.ms_cwd_source_id = source.id; |
| 56 | } |
| 57 | |
| 58 | pp.preserve_whitespace = true; |
| 59 | pp.linemarkers = .line_directives; |
| 60 | |
| 61 | pp.preprocessSources(.{ .main = source, .builtin = builtin_macros, .command_line = user_macros }) catch |err| switch (err) { |
| 62 | error.FatalError => return error.PreprocessError, |
| 63 | else => |e| return e, |
| 64 | }; |
| 65 | |
| 66 | if (hasAnyErrors(comp)) return error.PreprocessError; |
| 67 | |
| 68 | try pp.prettyPrintTokens(writer, .result_only); |
| 69 | |
| 70 | if (maybe_dependencies) |dependencies| { |
| 71 | for (comp.sources.values()) |comp_source| { |
| 72 | if (comp_source.id == builtin_macros.id or comp_source.id == user_macros.id) continue; |
| 73 | if (comp_source.id.index == .unused or comp_source.id.index == .generated) continue; |
| 74 | const duped_path = try dependencies.allocator.dupe(u8, comp_source.path); |
| 75 | errdefer dependencies.allocator.free(duped_path); |
| 76 | try dependencies.list.append(dependencies.allocator, duped_path); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn hasAnyErrors(comp: *aro.Compilation) bool { |
| 82 | return comp.diagnostics.errors != 0; |
| 83 | } |
| 84 | |
| 85 | /// `arena` is used for temporary -D argument strings. |
| 86 | /// The arena should be kept alive at least as long as `argv`. |
| 87 | pub fn appendAroArgs(arena: Allocator, argv: *std.ArrayList([]const u8), options: cli.Options, system_include_paths: []const []const u8, include_env_value: ?[]const u8) !void { |
| 88 | try argv.appendSlice(arena, &.{ |
| 89 | "-E", |
| 90 | "--comments", |
| 91 | "-fuse-line-directives", |
| 92 | "-fgnuc-version=4.2.1", |
| 93 | "--target=x86_64-windows-msvc", |
| 94 | "--emulate=msvc", |
| 95 | "-nostdinc", |
| 96 | "-DRC_INVOKED", |
| 97 | "-D_WIN32", // undocumented, but defined by default |
| 98 | }); |
| 99 | for (options.extra_include_paths.items) |extra_include_path| { |
| 100 | try argv.append(arena, "-I"); |
| 101 | try argv.append(arena, extra_include_path); |
| 102 | } |
| 103 | |
| 104 | for (system_include_paths) |include_path| { |
| 105 | try argv.append(arena, "-isystem"); |
| 106 | try argv.append(arena, include_path); |
| 107 | } |
| 108 | |
| 109 | if (!options.ignore_include_env_var) { |
| 110 | const INCLUDE = include_env_value orelse ""; |
| 111 | |
| 112 | // The only precedence here is llvm-rc which also uses the platform-specific |
| 113 | // delimiter. There's no precedence set by `rc.exe` since it's Windows-only. |
| 114 | const delimiter = switch (builtin.os.tag) { |
| 115 | .windows => ';', |
| 116 | else => ':', |
| 117 | }; |
| 118 | var it = std.mem.tokenizeScalar(u8, INCLUDE, delimiter); |
| 119 | while (it.next()) |include_path| { |
| 120 | try argv.append(arena, "-isystem"); |
| 121 | try argv.append(arena, include_path); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | var symbol_it = options.symbols.iterator(); |
| 126 | while (symbol_it.next()) |entry| { |
| 127 | switch (entry.value_ptr.*) { |
| 128 | .define => |value| { |
| 129 | try argv.append(arena, "-D"); |
| 130 | const define_arg = try std.fmt.allocPrint(arena, "{s}={s}", .{ entry.key_ptr.*, value }); |
| 131 | try argv.append(arena, define_arg); |
| 132 | }, |
| 133 | .undefine => { |
| 134 | try argv.append(arena, "-U"); |
| 135 | try argv.append(arena, entry.key_ptr.*); |
| 136 | }, |
| 137 | } |
| 138 | } |
| 139 | } |