Skip to content

Commit

Permalink
make aro-based translate-c lazily built from source
Browse files Browse the repository at this point in the history
Part of #19063.

Primarily, this moves Aro from deps/ to lib/compiler/ so that it can be
lazily compiled from source. src/aro_translate_c.zig is moved to
lib/compiler/aro_translate_c.zig and some of Zig CLI logic moved to a
main() function there.

aro_translate_c.zig becomes the "common" import for clang-based
translate-c.

Not all of the compiler was able to be detangled from Aro, however, so
it still, for now, remains being compiled with the main compiler
sources due to the clang-based translate-c depending on it. Once
aro-based translate-c achieves feature parity with the clang-based
translate-c implementation, the clang-based one can be removed from Zig.

Aro made it unnecessarily difficult to depend on with these .def files
and all these Zig module requirements. I looked at the .def files and
made these observations:

- The canonical source is llvm .def files.
- Therefore there is an update process to sync with llvm that involves
  regenerating the .def files in Aro.
- Therefore you might as well just regenerate the .zig files directly
  and check those into Aro.
- Also with a small amount of tinkering, the file size on disk of these
  generated .zig files can be made many times smaller, without
  compromising type safety in the usage of the data.

This would make things much easier on Zig as downstream project,
particularly we could remove those pesky stubs when bootstrapping.

I have gone ahead with these changes since they unblock me and I will
have a chat with Vexu to see what he thinks.
  • Loading branch information
andrewrk committed Feb 28, 2024
1 parent 9410b11 commit 240d0b6
Show file tree
Hide file tree
Showing 68 changed files with 16,575 additions and 22,667 deletions.
16 changes: 1 addition & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,8 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/target.zig"
"${CMAKE_SOURCE_DIR}/src/tracy.zig"
"${CMAKE_SOURCE_DIR}/src/translate_c.zig"
"${CMAKE_SOURCE_DIR}/src/translate_c/ast.zig"
"${CMAKE_SOURCE_DIR}/src/type.zig"
"${CMAKE_SOURCE_DIR}/src/wasi_libc.zig"
"${CMAKE_SOURCE_DIR}/src/stubs/aro_builtins.zig"
"${CMAKE_SOURCE_DIR}/src/stubs/aro_names.zig"
)

if(MSVC)
Expand Down Expand Up @@ -822,18 +819,7 @@ set(BUILD_ZIG2_ARGS
--dep "aro"
--mod "root" "src/main.zig"
--mod "build_options" "${ZIG_CONFIG_ZIG_OUT}"
--mod "aro_options" "src/stubs/aro_options.zig"
--mod "Builtins/Builtin.def" "src/stubs/aro_builtins.zig"
--mod "Attribute/names.def" "src/stubs/aro_names.zig"
--mod "Diagnostics/messages.def" "src/stubs/aro_messages.zig"
--dep "build_options=aro_options"
--mod "aro_backend" "deps/aro/backend.zig"
--dep "Builtins/Builtin.def"
--dep "Attribute/names.def"
--dep "Diagnostics/messages.def"
--dep "build_options=aro_options"
--dep "backend=aro_backend"
--mod "aro" "deps/aro/aro.zig"
--mod "aro" "lib/compiler/aro/aro.zig"
)

add_custom_command(
Expand Down
16 changes: 1 addition & 15 deletions bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,8 @@ int main(int argc, char **argv) {
"--dep", "build_options",
"--dep", "aro",
"--mod", "root", "src/main.zig",

"--mod", "build_options", "config.zig",
"--mod", "aro_options", "src/stubs/aro_options.zig",
"--mod", "Builtins/Builtin.def", "src/stubs/aro_builtins.zig",
"--mod", "Attribute/names.def", "src/stubs/aro_names.zig",
"--mod", "Diagnostics/messages.def", "src/stubs/aro_messages.zig",

"--dep", "build_options=aro_options",
"--mod", "aro_backend", "deps/aro/backend.zig",

"--dep", "Builtins/Builtin.def",
"--dep", "Attribute/names.def",
"--dep", "Diagnostics/messages.def",
"--dep", "build_options=aro_options",
"--dep", "backend=aro_backend",
"--mod", "aro", "deps/aro/aro.zig",
"--mod", "aro", "lib/compiler/aro/aro.zig",
NULL,
};
print_and_run(child_argv);
Expand Down
29 changes: 8 additions & 21 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const io = std.io;
const fs = std.fs;
const InstallDirectoryOptions = std.Build.InstallDirectoryOptions;
const assert = std.debug.assert;
const GenerateDef = @import("deps/aro/build/GenerateDef.zig");

const zig_version = std.SemanticVersion{ .major = 0, .minor = 12, .patch = 0 };
const stack_size = 32 * 1024 * 1024;
Expand Down Expand Up @@ -636,34 +635,22 @@ fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.St
});
exe.stack_size = stack_size;

const aro_options = b.addOptions();
aro_options.addOption([]const u8, "version_str", "aro-zig");
const aro_options_module = aro_options.createModule();
const aro_backend = b.createModule(.{
.root_source_file = .{ .path = "deps/aro/backend.zig" },
.imports = &.{.{
.name = "build_options",
.module = aro_options_module,
}},
});
const aro_module = b.createModule(.{
.root_source_file = .{ .path = "deps/aro/aro.zig" },
.root_source_file = .{ .path = "lib/compiler/aro/aro.zig" },
});

const aro_translate_c_module = b.createModule(.{
.root_source_file = .{ .path = "lib/compiler/aro_translate_c.zig" },
.imports = &.{
.{
.name = "build_options",
.module = aro_options_module,
},
.{
.name = "backend",
.module = aro_backend,
.name = "aro",
.module = aro_module,
},
GenerateDef.create(b, .{ .name = "Builtins/Builtin.def", .src_prefix = "deps/aro/aro" }),
GenerateDef.create(b, .{ .name = "Attribute/names.def", .src_prefix = "deps/aro/aro" }),
GenerateDef.create(b, .{ .name = "Diagnostics/messages.def", .src_prefix = "deps/aro/aro", .kind = .named }),
},
});

exe.root_module.addImport("aro", aro_module);
exe.root_module.addImport("aro_translate_c", aro_translate_c_module);
return exe;
}

Expand Down
Loading

0 comments on commit 240d0b6

Please sign in to comment.