2017-03-31 02:48:15 -07:00
|
|
|
const root = @import("@build");
|
|
|
|
const std = @import("std");
|
|
|
|
const io = std.io;
|
2017-04-06 02:34:04 -07:00
|
|
|
const fmt = std.fmt;
|
2017-04-03 21:17:24 -07:00
|
|
|
const os = std.os;
|
2017-03-31 02:48:15 -07:00
|
|
|
const Builder = std.build.Builder;
|
|
|
|
const mem = std.mem;
|
2017-05-04 11:05:06 -07:00
|
|
|
const ArrayList = std.ArrayList;
|
2017-03-31 02:48:15 -07:00
|
|
|
|
2017-04-02 17:44:04 -07:00
|
|
|
error InvalidArgs;
|
|
|
|
|
2017-04-03 21:17:24 -07:00
|
|
|
pub fn main() -> %void {
|
2017-10-11 07:16:13 -07:00
|
|
|
var arg_it = os.args();
|
2017-04-27 23:22:12 -07:00
|
|
|
|
2017-03-31 02:48:15 -07:00
|
|
|
// TODO use a more general purpose allocator here
|
2017-09-14 17:28:07 -07:00
|
|
|
var inc_allocator = %%mem.IncrementingAllocator.init(20 * 1024 * 1024);
|
2017-03-31 02:48:15 -07:00
|
|
|
defer inc_allocator.deinit();
|
|
|
|
|
2017-04-06 02:34:04 -07:00
|
|
|
const allocator = &inc_allocator.allocator;
|
|
|
|
|
2017-10-11 07:16:13 -07:00
|
|
|
|
|
|
|
// skip my own exe name
|
|
|
|
_ = arg_it.skip();
|
|
|
|
|
|
|
|
const zig_exe = %return unwrapArg(arg_it.next(allocator) ?? {
|
|
|
|
%%io.stderr.printf("Expected first argument to be path to zig compiler\n");
|
|
|
|
return error.InvalidArgs;
|
|
|
|
});
|
|
|
|
const build_root = %return unwrapArg(arg_it.next(allocator) ?? {
|
|
|
|
%%io.stderr.printf("Expected second argument to be build root directory path\n");
|
|
|
|
return error.InvalidArgs;
|
|
|
|
});
|
|
|
|
const cache_root = %return unwrapArg(arg_it.next(allocator) ?? {
|
|
|
|
%%io.stderr.printf("Expected third argument to be cache root directory path\n");
|
|
|
|
return error.InvalidArgs;
|
|
|
|
});
|
|
|
|
|
2017-04-27 23:22:12 -07:00
|
|
|
var builder = Builder.init(allocator, zig_exe, build_root, cache_root);
|
2017-04-03 22:52:20 -07:00
|
|
|
defer builder.deinit();
|
2017-04-06 02:34:04 -07:00
|
|
|
|
2017-05-04 11:05:06 -07:00
|
|
|
var targets = ArrayList([]const u8).init(allocator);
|
2017-04-06 02:34:04 -07:00
|
|
|
|
2017-04-13 14:21:00 -07:00
|
|
|
var prefix: ?[]const u8 = null;
|
|
|
|
|
2017-10-11 07:16:13 -07:00
|
|
|
while (arg_it.next(allocator)) |err_or_arg| {
|
|
|
|
const arg = %return unwrapArg(err_or_arg);
|
2017-04-06 10:59:11 -07:00
|
|
|
if (mem.startsWith(u8, arg, "-D")) {
|
2017-05-19 07:39:59 -07:00
|
|
|
const option_contents = arg[2..];
|
2017-04-06 02:34:04 -07:00
|
|
|
if (option_contents.len == 0) {
|
2017-04-06 10:59:11 -07:00
|
|
|
%%io.stderr.printf("Expected option name after '-D'\n\n");
|
2017-04-18 22:13:15 -07:00
|
|
|
return usage(&builder, false, &io.stderr);
|
2017-04-06 02:34:04 -07:00
|
|
|
}
|
2017-05-03 14:23:11 -07:00
|
|
|
if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
|
2017-05-19 07:39:59 -07:00
|
|
|
const option_name = option_contents[0..name_end];
|
|
|
|
const option_value = option_contents[name_end + 1..];
|
2017-04-06 02:34:04 -07:00
|
|
|
if (builder.addUserInputOption(option_name, option_value))
|
2017-04-18 22:13:15 -07:00
|
|
|
return usage(&builder, false, &io.stderr);
|
2017-04-06 02:34:04 -07:00
|
|
|
} else {
|
|
|
|
if (builder.addUserInputFlag(option_contents))
|
2017-04-18 22:13:15 -07:00
|
|
|
return usage(&builder, false, &io.stderr);
|
2017-04-06 02:34:04 -07:00
|
|
|
}
|
|
|
|
} else if (mem.startsWith(u8, arg, "-")) {
|
|
|
|
if (mem.eql(u8, arg, "--verbose")) {
|
|
|
|
builder.verbose = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--help")) {
|
2017-10-11 07:16:13 -07:00
|
|
|
return usage(&builder, false, &io.stdout);
|
|
|
|
} else if (mem.eql(u8, arg, "--prefix")) {
|
|
|
|
prefix = %return unwrapArg(arg_it.next(allocator) ?? {
|
|
|
|
%%io.stderr.printf("Expected argument after --prefix\n\n");
|
|
|
|
return usage(&builder, false, &io.stderr);
|
|
|
|
});
|
2017-10-25 20:10:41 -07:00
|
|
|
} else if (mem.eql(u8, arg, "--verbose-tokenize")) {
|
|
|
|
builder.verbose_tokenize = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--verbose-ast")) {
|
|
|
|
builder.verbose_ast = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--verbose-link")) {
|
|
|
|
builder.verbose_link = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--verbose-ir")) {
|
|
|
|
builder.verbose_ir = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--verbose-llvm-ir")) {
|
|
|
|
builder.verbose_llvm_ir = true;
|
|
|
|
} else if (mem.eql(u8, arg, "--verbose-cimport")) {
|
|
|
|
builder.verbose_cimport = true;
|
2017-04-06 02:34:04 -07:00
|
|
|
} else {
|
|
|
|
%%io.stderr.printf("Unrecognized argument: {}\n\n", arg);
|
2017-04-18 22:13:15 -07:00
|
|
|
return usage(&builder, false, &io.stderr);
|
2017-04-06 02:34:04 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
%%targets.append(arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:21:00 -07:00
|
|
|
builder.setInstallPrefix(prefix);
|
2017-03-31 02:48:15 -07:00
|
|
|
root.build(&builder);
|
2017-04-06 02:34:04 -07:00
|
|
|
|
|
|
|
if (builder.validateUserInputDidItFail())
|
2017-04-18 22:13:15 -07:00
|
|
|
return usage(&builder, true, &io.stderr);
|
2017-04-06 02:34:04 -07:00
|
|
|
|
2017-04-30 10:01:35 -07:00
|
|
|
builder.make(targets.toSliceConst()) %% |err| {
|
|
|
|
if (err == error.InvalidStepName) {
|
|
|
|
return usage(&builder, true, &io.stderr);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
};
|
2017-04-06 02:34:04 -07:00
|
|
|
}
|
|
|
|
|
2017-04-18 22:13:15 -07:00
|
|
|
fn usage(builder: &Builder, already_ran_build: bool, out_stream: &io.OutStream) -> %void {
|
2017-04-06 02:34:04 -07:00
|
|
|
// run the build script to collect the options
|
|
|
|
if (!already_ran_build) {
|
2017-04-17 03:45:44 -07:00
|
|
|
builder.setInstallPrefix(null);
|
2017-04-06 02:34:04 -07:00
|
|
|
root.build(builder);
|
|
|
|
}
|
|
|
|
|
2017-04-11 03:14:46 -07:00
|
|
|
// This usage text has to be synchronized with src/main.cpp
|
2017-04-06 02:34:04 -07:00
|
|
|
%%out_stream.printf(
|
2017-04-13 14:21:00 -07:00
|
|
|
\\Usage: {} build [steps] [options]
|
|
|
|
\\
|
|
|
|
\\Steps:
|
|
|
|
\\
|
2017-04-18 22:13:15 -07:00
|
|
|
, builder.zig_exe);
|
2017-04-13 14:21:00 -07:00
|
|
|
|
|
|
|
const allocator = builder.allocator;
|
|
|
|
for (builder.top_level_steps.toSliceConst()) |top_level_step| {
|
|
|
|
%%out_stream.printf(" {s22} {}\n", top_level_step.step.name, top_level_step.description);
|
|
|
|
}
|
|
|
|
|
|
|
|
%%out_stream.write(
|
2017-04-06 02:34:04 -07:00
|
|
|
\\
|
|
|
|
\\General Options:
|
2017-04-13 14:21:00 -07:00
|
|
|
\\ --help Print this help and exit
|
|
|
|
\\ --verbose Print commands before executing them
|
2017-10-25 20:10:41 -07:00
|
|
|
\\ --prefix $path Override default install prefix
|
2017-04-06 02:34:04 -07:00
|
|
|
\\
|
|
|
|
\\Project-Specific Options:
|
|
|
|
\\
|
2017-04-13 14:21:00 -07:00
|
|
|
);
|
2017-04-06 02:34:04 -07:00
|
|
|
|
|
|
|
if (builder.available_options_list.len == 0) {
|
2017-10-25 20:10:41 -07:00
|
|
|
%%out_stream.print(" (none)\n");
|
2017-04-06 02:34:04 -07:00
|
|
|
} else {
|
|
|
|
for (builder.available_options_list.toSliceConst()) |option| {
|
|
|
|
const name = %%fmt.allocPrint(allocator,
|
2017-10-25 20:10:41 -07:00
|
|
|
" -D{}=${}", option.name, Builder.typeIdName(option.type_id));
|
2017-04-06 02:34:04 -07:00
|
|
|
defer allocator.free(name);
|
2017-10-25 20:10:41 -07:00
|
|
|
%%out_stream.print("{s24} {}\n", name, option.description);
|
2017-04-06 02:34:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:10:41 -07:00
|
|
|
%%out_stream.write(
|
|
|
|
\\
|
|
|
|
\\Advanced Options:
|
|
|
|
\\ --build-file $file Override path to build.zig
|
|
|
|
\\ --cache-dir $path Override path to zig cache directory
|
|
|
|
\\ --verbose-tokenize Enable compiler debug output for tokenization
|
|
|
|
\\ --verbose-ast Enable compiler debug output for parsing into an AST
|
|
|
|
\\ --verbose-link Enable compiler debug output for linking
|
|
|
|
\\ --verbose-ir Enable compiler debug output for Zig IR
|
|
|
|
\\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
|
|
|
|
\\ --verbose-cimport Enable compiler debug output for C imports
|
|
|
|
\\
|
|
|
|
);
|
|
|
|
|
|
|
|
%%out_stream.flush();
|
|
|
|
|
2017-04-06 02:34:04 -07:00
|
|
|
if (out_stream == &io.stderr)
|
|
|
|
return error.InvalidArgs;
|
2017-03-31 02:48:15 -07:00
|
|
|
}
|
2017-10-11 07:16:13 -07:00
|
|
|
|
|
|
|
fn unwrapArg(arg: %[]u8) -> %[]u8 {
|
|
|
|
return arg %% |err| {
|
|
|
|
%%io.stderr.printf("Unable to parse command line: {}\n", err);
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
}
|