Skip to content

Commit

Permalink
zig build: organize build artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Apr 30, 2017
1 parent 38a04a2 commit 363d903
Show file tree
Hide file tree
Showing 16 changed files with 357 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
zig-cache/
build/
build-release/
/.cproject
/.project
/.settings/
/test_artifacts/
19 changes: 8 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ pub fn build(b: &Builder) {
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
const test_step = b.step("test", "Run all the tests");

const cleanup = b.addRemoveDirTree("test_artifacts");
test_step.dependOn(&cleanup.step);

cleanup.step.dependOn(tests.addPkgTests(b, test_filter,
test_step.dependOn(tests.addPkgTests(b, test_filter,
"test/behavior.zig", "behavior", "Run the behavior tests"));

cleanup.step.dependOn(tests.addPkgTests(b, test_filter,
test_step.dependOn(tests.addPkgTests(b, test_filter,
"std/index.zig", "std", "Run the standard library tests"));

cleanup.step.dependOn(tests.addCompareOutputTests(b, test_filter));
cleanup.step.dependOn(tests.addBuildExampleTests(b, test_filter));
cleanup.step.dependOn(tests.addCompileErrorTests(b, test_filter));
cleanup.step.dependOn(tests.addAssembleAndLinkTests(b, test_filter));
cleanup.step.dependOn(tests.addDebugSafetyTests(b, test_filter));
cleanup.step.dependOn(tests.addParseHTests(b, test_filter));
test_step.dependOn(tests.addCompareOutputTests(b, test_filter));
test_step.dependOn(tests.addBuildExampleTests(b, test_filter));
test_step.dependOn(tests.addCompileErrorTests(b, test_filter));
test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter));
test_step.dependOn(tests.addDebugSafetyTests(b, test_filter));
test_step.dependOn(tests.addParseHTests(b, test_filter));
}
2 changes: 1 addition & 1 deletion example/mix_o_files/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn build(b: &Builder) {

b.default_step.dependOn(&exe.step);

const run_cmd = b.addCommand(b.out_dir, b.env_map, "./test", [][]const u8{});
const run_cmd = b.addCommand(b.cache_root, b.env_map, "./test", [][]const u8{});
run_cmd.step.dependOn(&exe.step);

const test_step = b.step("test", "Test the program");
Expand Down
2 changes: 1 addition & 1 deletion example/shared_library/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn build(b: &Builder) {

b.default_step.dependOn(&exe.step);

const run_cmd = b.addCommand(b.out_dir, b.env_map, "./test", [][]const u8{});
const run_cmd = b.addCommand(b.cache_root, b.env_map, "./test", [][]const u8{});
run_cmd.step.dependOn(&exe.step);

const test_step = b.step("test", "Test the program");
Expand Down
1 change: 1 addition & 0 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ struct CodeGen {
ZigList<TimeEvent> timing_events;

Buf *cache_dir;
Buf *out_h_path;
};

enum VarLinkage {
Expand Down
48 changes: 29 additions & 19 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_pa
return entry;
}

CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target) {
CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type) {
CodeGen *g = allocate<CodeGen>(1);

codegen_add_time_event(g, "Initialize");

g->out_type = out_type;
g->import_table.init(32);
g->builtin_fn_table.init(32);
g->primitive_type_table.init(32);
Expand All @@ -74,7 +75,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target) {
g->external_prototypes.init(8);
g->is_release_build = false;
g->is_test_build = false;
g->want_h_file = true;
g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);

buf_resize(&g->global_asm, 0);

Expand Down Expand Up @@ -145,6 +146,10 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target) {
return g;
}

void codegen_set_output_h_path(CodeGen *g, Buf *h_path) {
g->out_h_path = h_path;
}

void codegen_set_clang_argv(CodeGen *g, const char **args, size_t len) {
g->clang_argv = args;
g->clang_argv_len = len;
Expand Down Expand Up @@ -196,10 +201,6 @@ void codegen_set_strip(CodeGen *g, bool strip) {
g->strip_debug_symbols = strip;
}

void codegen_set_out_type(CodeGen *g, OutType out_type) {
g->out_type = out_type;
}

void codegen_set_out_name(CodeGen *g, Buf *out_name) {
g->root_out_name = out_name;
}
Expand Down Expand Up @@ -4808,15 +4809,6 @@ static void gen_global_asm(CodeGen *g) {
}
}

void codegen_build(CodeGen *g) {
assert(g->out_type != OutTypeUnknown);
init(g);

gen_global_asm(g);
gen_root_source(g);
do_code_gen(g);
}

void codegen_add_object(CodeGen *g, Buf *object_path) {
g->link_objects.append(object_path);
}
Expand Down Expand Up @@ -4946,13 +4938,21 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
}
}

void codegen_generate_h_file(CodeGen *g) {
static void gen_h_file(CodeGen *g) {
if (!g->want_h_file)
return;

codegen_add_time_event(g, "Generate .h");

assert(!g->is_test_build);

Buf *h_file_out_path = buf_sprintf("%s.h", buf_ptr(g->root_out_name));
FILE *out_h = fopen(buf_ptr(h_file_out_path), "wb");
if (!g->out_h_path) {
g->out_h_path = buf_sprintf("%s.h", buf_ptr(g->root_out_name));
}

FILE *out_h = fopen(buf_ptr(g->out_h_path), "wb");
if (!out_h)
zig_panic("unable to open %s: %s", buf_ptr(h_file_out_path), strerror(errno));
zig_panic("unable to open %s: %s", buf_ptr(g->out_h_path), strerror(errno));

Buf *export_macro = buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name));
buf_upcase(export_macro);
Expand Down Expand Up @@ -5056,3 +5056,13 @@ void codegen_print_timing_report(CodeGen *g, FILE *f) {
void codegen_add_time_event(CodeGen *g, const char *name) {
g->timing_events.append({os_get_time(), name});
}

void codegen_build(CodeGen *g) {
assert(g->out_type != OutTypeUnknown);
init(g);

gen_global_asm(g);
gen_root_source(g);
do_code_gen(g);
gen_h_file(g);
}
5 changes: 2 additions & 3 deletions src/codegen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include <stdio.h>

CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target);
CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type);

void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
void codegen_set_is_release(CodeGen *codegen, bool is_release);
Expand All @@ -25,7 +25,6 @@ void codegen_set_is_static(CodeGen *codegen, bool is_static);
void codegen_set_strip(CodeGen *codegen, bool strip);
void codegen_set_verbose(CodeGen *codegen, bool verbose);
void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);
void codegen_set_out_type(CodeGen *codegen, OutType out_type);
void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
void codegen_set_libc_lib_dir(CodeGen *codegen, Buf *libc_lib_dir);
void codegen_set_libc_static_lib_dir(CodeGen *g, Buf *libc_static_lib_dir);
Expand All @@ -48,6 +47,7 @@ void codegen_set_test_filter(CodeGen *g, Buf *filter);
void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);
void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch);
void codegen_set_cache_dir(CodeGen *g, Buf *cache_dir);
void codegen_set_output_h_path(CodeGen *g, Buf *h_path);
void codegen_add_time_event(CodeGen *g, const char *name);
void codegen_print_timing_report(CodeGen *g, FILE *f);
void codegen_build(CodeGen *g);
Expand All @@ -59,6 +59,5 @@ void codegen_add_object(CodeGen *g, Buf *object_path);
void codegen_parseh(CodeGen *g, Buf *path);
void codegen_render_ast(CodeGen *g, FILE *f, int indent_size);

void codegen_generate_h_file(CodeGen *g);

#endif
20 changes: 5 additions & 15 deletions src/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static Buf *build_o(CodeGen *parent_gen, const char *oname) {
os_path_join(parent_gen->zig_std_special_dir, source_basename, full_path);

ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
CodeGen *child_gen = codegen_create(full_path, child_target);
CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj);
child_gen->link_libc = parent_gen->link_libc;

child_gen->link_libs.resize(parent_gen->link_libs.length);
Expand All @@ -55,7 +55,6 @@ static Buf *build_o(CodeGen *parent_gen, const char *oname) {
codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);
codegen_set_is_static(child_gen, parent_gen->is_static);

codegen_set_out_type(child_gen, OutTypeObj);
codegen_set_out_name(child_gen, buf_create_from_str(oname));

codegen_set_verbose(child_gen, parent_gen->verbose);
Expand Down Expand Up @@ -186,9 +185,10 @@ static void construct_linker_job_elf(LinkJob *lj) {
} else if (shared) {
lj->args.append("-shared");

buf_resize(&lj->out_file, 0);
buf_appendf(&lj->out_file, "lib%s.so.%zu.%zu.%zu",
buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);
if (buf_len(&lj->out_file) == 0) {
buf_appendf(&lj->out_file, "lib%s.so.%zu.%zu.%zu",
buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);
}
soname = buf_sprintf("lib%s.so.%zu", buf_ptr(g->root_out_name), g->version_major);
}

Expand Down Expand Up @@ -752,9 +752,6 @@ void codegen_link(CodeGen *g, const char *out_file) {
}

if (g->out_type == OutTypeObj) {
if (g->want_h_file) {
codegen_generate_h_file(g);
}
if (override_out_file) {
assert(g->link_objects.length == 1);
Buf *o_file_path = g->link_objects.at(0);
Expand Down Expand Up @@ -798,13 +795,6 @@ void codegen_link(CodeGen *g, const char *out_file) {
fprintf(stderr, "%s\n", buf_ptr(&diag));
exit(1);
}
codegen_add_time_event(g, "Generate .h");

if (g->out_type == OutTypeLib ||
g->out_type == OutTypeObj)
{
codegen_generate_h_file(g);
}

codegen_add_time_event(g, "Done");

Expand Down
33 changes: 22 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static int usage(const char *arg0) {
" --libc-include-dir [path] directory where libc stdlib.h resides\n"
" --name [name] override output name\n"
" --output [file] override destination path\n"
" --output-h [file] override generated header file path\n"
" --release build with optimizations on and debug protection off\n"
" --static output will be statically linked\n"
" --strip exclude debug symbols\n"
Expand Down Expand Up @@ -118,13 +119,16 @@ enum Cmd {
CmdTargets,
};

static const char *default_zig_cache_name = "zig-cache";

int main(int argc, char **argv) {
os_init();

char *arg0 = argv[0];
Cmd cmd = CmdInvalid;
const char *in_file = nullptr;
const char *out_file = nullptr;
const char *out_file_h = nullptr;
bool is_release_build = false;
bool strip = false;
bool is_static = false;
Expand Down Expand Up @@ -163,7 +167,7 @@ int main(int argc, char **argv) {
size_t ver_minor = 0;
size_t ver_patch = 0;
bool timing_info = false;
const char *cache_dir = "zig-cache";
const char *cache_dir = nullptr;

if (argc >= 2 && strcmp(argv[1], "build") == 0) {
const char *zig_exe_path = arg0;
Expand Down Expand Up @@ -200,9 +204,8 @@ int main(int argc, char **argv) {
}
}

CodeGen *g = codegen_create(build_runner_path, nullptr);
CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe);
codegen_set_out_name(g, buf_create_from_str("build"));
codegen_set_out_type(g, OutTypeExe);
codegen_set_verbose(g, verbose);

Buf build_file_abs = BUF_INIT;
Expand All @@ -212,7 +215,12 @@ int main(int argc, char **argv) {
os_path_split(&build_file_abs, &build_file_dirname, &build_file_basename);

Buf *full_cache_dir = buf_alloc();
os_path_resolve(buf_create_from_str("."), buf_create_from_str(cache_dir), full_cache_dir);
if (cache_dir == nullptr) {
os_path_join(&build_file_dirname, buf_create_from_str(default_zig_cache_name), full_cache_dir);
} else {
os_path_resolve(buf_create_from_str("."), buf_create_from_str(cache_dir), full_cache_dir);
}

Buf *path_to_build_exe = buf_alloc();
os_path_join(full_cache_dir, buf_create_from_str("build"), path_to_build_exe);
codegen_set_cache_dir(g, full_cache_dir);
Expand Down Expand Up @@ -309,6 +317,8 @@ int main(int argc, char **argv) {
return usage(arg0);
} else if (strcmp(arg, "--output") == 0) {
out_file = argv[i];
} else if (strcmp(arg, "--output-h") == 0) {
out_file_h = argv[i];
} else if (strcmp(arg, "--color") == 0) {
if (strcmp(argv[i], "auto") == 0) {
color = ErrColorAuto;
Expand Down Expand Up @@ -396,6 +406,7 @@ int main(int argc, char **argv) {
cmd = CmdParseH;
} else if (strcmp(arg, "test") == 0) {
cmd = CmdTest;
out_type = OutTypeExe;
} else if (strcmp(arg, "targets") == 0) {
cmd = CmdTargets;
} else {
Expand Down Expand Up @@ -495,9 +506,11 @@ int main(int argc, char **argv) {
Buf *zig_root_source_file = (cmd == CmdParseH) ? nullptr : in_file_buf;

Buf *full_cache_dir = buf_alloc();
os_path_resolve(buf_create_from_str("."), buf_create_from_str(cache_dir), full_cache_dir);
os_path_resolve(buf_create_from_str("."),
buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
full_cache_dir);

CodeGen *g = codegen_create(zig_root_source_file, target);
CodeGen *g = codegen_create(zig_root_source_file, target, out_type);
codegen_set_out_name(g, buf_out_name);
codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
codegen_set_is_release(g, is_release_build);
Expand All @@ -510,11 +523,6 @@ int main(int argc, char **argv) {
codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);
codegen_set_strip(g, strip);
codegen_set_is_static(g, is_static);
if (out_type != OutTypeUnknown) {
codegen_set_out_type(g, out_type);
} else if (cmd == CmdTest) {
codegen_set_out_type(g, OutTypeExe);
}
if (libc_lib_dir)
codegen_set_libc_lib_dir(g, buf_create_from_str(libc_lib_dir));
if (libc_static_lib_dir)
Expand Down Expand Up @@ -568,6 +576,9 @@ int main(int argc, char **argv) {
codegen_set_test_name_prefix(g, buf_create_from_str(test_name_prefix));
}

if (out_file_h)
codegen_set_output_h_path(g, buf_create_from_str(out_file_h));

if (cmd == CmdBuild) {
for (size_t i = 0; i < objects.length; i += 1) {
codegen_add_object(g, buf_create_from_str(objects.at(i)));
Expand Down
Loading

0 comments on commit 363d903

Please sign in to comment.