add support to use zig as a linker driver

closes #243

I also added --grep to ./run_tests if you want to single out
some specific tests
This commit is contained in:
Andrew Kelley
2017-04-05 07:46:50 -04:00
parent 8c10b6dcbd
commit d65cd73a8b
9 changed files with 237 additions and 92 deletions

View File

@@ -16,7 +16,6 @@ struct LinkJob {
Buf out_file;
ZigList<const char *> args;
bool link_in_crt;
Buf out_file_o;
HashMap<Buf *, bool, buf_hash, buf_eql_buf> rpath_table;
};
@@ -32,14 +31,6 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {
return buf_ptr(out_buf);
}
static const char *get_o_file_extension(CodeGen *g) {
if (g->zig_target.env_type == ZigLLVM_MSVC) {
return ".obj";
} else {
return ".o";
}
}
static Buf *build_o(CodeGen *parent_gen, const char *oname) {
Buf *source_basename = buf_sprintf("%s.zig", oname);
@@ -78,7 +69,7 @@ static Buf *build_o(CodeGen *parent_gen, const char *oname) {
}
codegen_add_root_code(child_gen, parent_gen->zig_std_special_dir, source_basename, &source_code);
const char *o_ext = get_o_file_extension(child_gen);
const char *o_ext = target_o_file_ext(&child_gen->zig_target);
Buf *o_out = buf_sprintf("%s%s", oname, o_ext);
codegen_link(child_gen, buf_ptr(o_out));
@@ -274,7 +265,9 @@ static void construct_linker_job_elf(LinkJob *lj) {
}
// .o files
lj->args.append((const char *)buf_ptr(&lj->out_file_o));
for (size_t i = 0; i < g->link_objects.length; i += 1) {
lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
}
if (g->is_test_build) {
Buf *test_runner_o_path = build_o(g, "test_runner");
@@ -417,7 +410,9 @@ static void construct_linker_job_coff(LinkJob *lj) {
lj->args.append(buf_ptr(g->libc_static_lib_dir));
}
lj->args.append((const char *)buf_ptr(&lj->out_file_o));
for (size_t i = 0; i < g->link_objects.length; i += 1) {
lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
}
if (g->is_test_build) {
Buf *test_runner_o_path = build_o(g, "test_runner");
@@ -681,7 +676,9 @@ static void construct_linker_job_macho(LinkJob *lj) {
lj->args.append(lib_dir);
}
lj->args.append((const char *)buf_ptr(&lj->out_file_o));
for (size_t i = 0; i < g->link_objects.length; i += 1) {
lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
}
if (g->is_test_build) {
Buf *test_runner_o_path = build_o(g, "test_runner");
@@ -776,19 +773,6 @@ void codegen_link(CodeGen *g, const char *out_file) {
buf_append_str(&lj.out_file, get_exe_file_extension(g));
}
}
buf_init_from_buf(&lj.out_file_o, &lj.out_file);
if (g->out_type != OutTypeObj || !override_out_file) {
const char *o_ext = get_o_file_extension(g);
buf_append_str(&lj.out_file_o, o_ext);
}
char *err_msg = nullptr;
if (LLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(&lj.out_file_o),
LLVMObjectFile, &err_msg))
{
zig_panic("unable to write object file: %s", err_msg);
}
if (g->out_type == OutTypeObj) {
if (g->want_h_file) {