authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-28 20:54:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-28 20:54:11-07:00
log0afb5b2ec6de494efe0605b2270f6a01b95237af
treeb43c1faae430a6a0ac3f5801a278d36a0a13ed04
parente248de93a07fd6a7f48581ce0d9f6f77a69c76de

stage2: add `zig ar` subcommand

The same entrypoint supports the following commands: * ar * ranlib * dlltool * lib For now, our strategy is to bundle the (renamed) `main()` function of llvm-ar, same as our strategy for `zig clang`. However, as Zig matures, a goal will be to replace the dependency on LLVM with our own implementation of this tool, so that it is available in builds of zig that do not have LLVM extensions enabled. This commit also categorizes the subcommands into categories in the --help menu.

3 files changed, 41 insertions(+), 8 deletions(-)

CMakeLists.txt+1
......@@ -319,6 +319,7 @@ set(OPTIMIZED_C_SOURCES
319319set(ZIG_CPP_SOURCES
320320 # These are planned to stay even when we are self-hosted.
321321 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
322 "${CMAKE_SOURCE_DIR}/src/zig_llvm-ar.cpp"
322323 "${CMAKE_SOURCE_DIR}/src/zig_clang.cpp"
323324 "${CMAKE_SOURCE_DIR}/src/zig_clang_driver.cpp"
324325 "${CMAKE_SOURCE_DIR}/src/zig_clang_cc1_main.cpp"
src/main.zig+38-7
......@@ -43,22 +43,29 @@ const normal_usage =
4343 \\Commands:
4444 \\
4545 \\ build Build project from build.zig
46 \\ init-exe Initialize a `zig build` application in the cwd
47 \\ init-lib Initialize a `zig build` library in the cwd
48 \\
49 \\ ast-check Look for simple compile errors in any set of files
4650 \\ build-exe Create executable from source or object files
4751 \\ build-lib Create library from source or object files
4852 \\ build-obj Create object from source or object files
53 \\ fmt Reformat Zig source into canonical form
54 \\ run Create executable and run immediately
55 \\ test Create and run a test build
56 \\ translate-c Convert C code to Zig code
57 \\
58 \\ ar Use Zig as a drop-in archiver
4959 \\ cc Use Zig as a drop-in C compiler
5060 \\ c++ Use Zig as a drop-in C++ compiler
61 \\ dlltool Use Zig as a drop-in dlltool.exe
62 \\ lib Use Zig as a drop-in lib.exe
63 \\ ranlib Use Zig as a drop-in ranlib
64 \\
5165 \\ env Print lib path, std path, cache directory, and version
52 \\ fmt Reformat Zig source into canonical form
53 \\ ast-check Look for simple compile errors in any set of files
5466 \\ help Print this help and exit
55 \\ init-exe Initialize a `zig build` application in the cwd
56 \\ init-lib Initialize a `zig build` library in the cwd
5767 \\ libc Display native libc paths file or validate one
58 \\ run Create executable and run immediately
59 \\ translate-c Convert C code to Zig code
6068 \\ targets List available compilation targets
61 \\ test Create and run a test build
6269 \\ version Print version number and exit
6370 \\ zen Print Zen of Zig and exit
6471 \\
......@@ -201,6 +208,12 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
201208 return buildOutputType(gpa, arena, args, .zig_test);
202209 } else if (mem.eql(u8, cmd, "run")) {
203210 return buildOutputType(gpa, arena, args, .run);
211 } else if (mem.eql(u8, cmd, "dlltool") or
212 mem.eql(u8, cmd, "ranlib") or
213 mem.eql(u8, cmd, "lib") or
214 mem.eql(u8, cmd, "ar"))
215 {
216 return punt_to_llvm_ar(arena, args);
204217 } else if (mem.eql(u8, cmd, "cc")) {
205218 return buildOutputType(gpa, arena, args, .cc);
206219 } else if (mem.eql(u8, cmd, "c++")) {
......@@ -3197,6 +3210,7 @@ pub const info_zen =
31973210;
31983211
31993212extern "c" fn ZigClang_main(argc: c_int, argv: [*:null]?[*:0]u8) c_int;
3213extern "c" fn ZigLlvmAr_main(argc: c_int, argv: [*:null]?[*:0]u8) c_int;
32003214
32013215/// TODO https://github.com/ziglang/zig/issues/3257
32023216fn punt_to_clang(arena: *Allocator, args: []const []const u8) error{OutOfMemory} {
......@@ -3212,6 +3226,23 @@ fn punt_to_clang(arena: *Allocator, args: []const []const u8) error{OutOfMemory}
32123226 process.exit(@bitCast(u8, @truncate(i8, exit_code)));
32133227}
32143228
3229/// TODO https://github.com/ziglang/zig/issues/3257
3230fn punt_to_llvm_ar(arena: *Allocator, args: []const []const u8) error{OutOfMemory} {
3231 if (!build_options.have_llvm)
3232 fatal("`zig ar`, `zig dlltool`, `zig ranlib', and `zig lib` unavailable: compiler built without LLVM extensions", .{});
3233
3234 // Convert the args to the format llvm-ar expects.
3235 // We subtract 1 to shave off the zig binary from args[0].
3236 const argv = try arena.allocSentinel(?[*:0]u8, args.len - 1, null);
3237 for (args[1..]) |arg, i| {
3238 // TODO If there was an argsAllocZ we could avoid this allocation.
3239 argv[i] = try arena.dupeZ(u8, arg);
3240 }
3241 const argc = @intCast(c_int, argv.len);
3242 const exit_code = ZigLlvmAr_main(argc, argv.ptr);
3243 process.exit(@bitCast(u8, @truncate(i8, exit_code)));
3244}
3245
32153246/// The first argument determines which backend is invoked. The options are:
32163247/// * `ld.lld` - ELF
32173248/// * `ld64.lld` - Mach-O
src/zig_llvm-ar.cpp+2-1
......@@ -1260,7 +1260,8 @@ static int ranlib_main(int argc, char **argv) {
12601260 return performOperation(CreateSymTab, nullptr);
12611261}
12621262
1263int main(int argc, char **argv) {
1263extern "C" int ZigLlvmAr_main(int argc, char **argv);
1264int ZigLlvmAr_main(int argc, char **argv) {
12641265 InitLLVM X(argc, argv);
12651266 ToolName = argv[0];
12661267