authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-13 12:16:17-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-13 12:16:17-04:00
log66d86eccbe43e01500bd57dba13a4ce68aba8921
tree5b695c1cc8f10d1d153411d5ee6ff0ddf7e56dc2
parentc1793d61061a112dc3d58c33ac0d3a1ebc8fb35e
parent7330a6102f7a8108ee364b9de79efe4a70049167
signature Commit is signed but in an unrecognized format.

Merge branch 'LemonBoy-asm-cc'


5 files changed, 45 insertions(+), 67 deletions(-)

src/cache_hash.cpp+2
......@@ -470,6 +470,8 @@ Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
470470 Error err;
471471 Buf *contents = buf_alloc();
472472 if ((err = os_fetch_file_path(dep_file_path, contents))) {
473 if (err == ErrorFileNotFound)
474 return err;
473475 if (verbose) {
474476 fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
475477 }
src/codegen.cpp+10-27
......@@ -8334,8 +8334,6 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
83348334 for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) {
83358335 args.append(g->clang_argv[arg_i]);
83368336 }
8337
8338
83398337}
83408338
83418339void codegen_translate_c(CodeGen *g, Buf *full_path, FILE *out_file, bool use_userland_implementation) {
......@@ -8582,24 +8580,6 @@ static void gen_root_source(CodeGen *g) {
85828580
85838581}
85848582
8585void codegen_add_assembly(CodeGen *g, Buf *path) {
8586 g->assembly_files.append(path);
8587}
8588
8589static void gen_global_asm(CodeGen *g) {
8590 Buf contents = BUF_INIT;
8591 Error err;
8592 for (size_t i = 0; i < g->assembly_files.length; i += 1) {
8593 Buf *asm_file = g->assembly_files.at(i);
8594 // No need to use the caching system for these fetches because they
8595 // are handled separately.
8596 if ((err = os_fetch_file_path(asm_file, &contents))) {
8597 zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
8598 }
8599 buf_append_buf(&g->global_asm, &contents);
8600 }
8601}
8602
86038583static void print_zig_cc_cmd(const char *zig_exe, ZigList<const char *> *args) {
86048584 fprintf(stderr, "%s", zig_exe);
86058585 for (size_t arg_i = 0; arg_i < args->length; arg_i += 1) {
......@@ -8750,10 +8730,16 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
87508730
87518731 // add the files depended on to the cache system
87528732 if ((err = cache_add_dep_file(cache_hash, out_dep_path, true))) {
8753 fprintf(stderr, "Failed to add C source dependencies to cache: %s\n", err_str(err));
8754 exit(1);
8733 // Don't treat the absence of the .d file as a fatal error, the
8734 // compiler may not produce one eg. when compiling .s files
8735 if (err != ErrorFileNotFound) {
8736 fprintf(stderr, "Failed to add C source dependencies to cache: %s\n", err_str(err));
8737 exit(1);
8738 }
8739 }
8740 if (err != ErrorFileNotFound) {
8741 os_delete_file(out_dep_path);
87558742 }
8756 os_delete_file(out_dep_path);
87578743
87588744 if ((err = cache_final(cache_hash, &digest))) {
87598745 fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));
......@@ -9330,8 +9316,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
93309316 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
93319317 cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);
93329318 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);
9333 cache_list_of_file(ch, g->assembly_files.items, g->assembly_files.length);
9334 cache_int(ch, g->emit_file_type);
93359319 cache_int(ch, g->build_mode);
93369320 cache_int(ch, g->out_type);
93379321 cache_bool(ch, g->zig_target->is_native);
......@@ -9392,7 +9376,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
93929376}
93939377
93949378static bool need_llvm_module(CodeGen *g) {
9395 return g->assembly_files.length != 0 || buf_len(&g->root_package->root_src_path) != 0;
9379 return buf_len(&g->root_package->root_src_path) != 0;
93969380}
93979381
93989382static void resolve_out_paths(CodeGen *g) {
......@@ -9499,7 +9483,6 @@ void codegen_build_and_link(CodeGen *g) {
94999483
95009484 codegen_add_time_event(g, "Semantic Analysis");
95019485
9502 gen_global_asm(g);
95039486 gen_root_source(g);
95049487
95059488 }
src/link.cpp+31-30
......@@ -59,14 +59,6 @@ static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFil
5959 return buf_ptr(&child_gen->output_file_path);
6060}
6161
62static const char *build_asm_object(CodeGen *parent_gen, const char *name, Buf *file) {
63 CodeGen *child_gen = create_child_codegen(parent_gen, nullptr, OutTypeObj, nullptr);
64 codegen_set_out_name(child_gen, buf_create_from_str(name));
65 codegen_add_assembly(child_gen, file);
66 codegen_build_and_link(child_gen);
67 return buf_ptr(&child_gen->output_file_path);
68}
69
7062static const char *path_from_zig_lib(CodeGen *g, const char *dir, const char *subpath) {
7163 Buf *dir1 = buf_alloc();
7264 os_path_join(g->zig_lib_dir, buf_create_from_str(dir), dir1);
......@@ -140,6 +132,7 @@ static const char *build_libunwind(CodeGen *parent) {
140132 c_file->args.append(path_from_libunwind(parent, "include"));
141133 c_file->args.append("-fPIC");
142134 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
135 c_file->args.append("-Wa,--noexecstack");
143136 if (parent->zig_target->is_native) {
144137 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
145138 }
......@@ -406,12 +399,13 @@ static const char *musl_arch_name(const ZigTarget *target) {
406399 }
407400}
408401
409static Buf *musl_start_asm_path(CodeGen *parent, const char *file) {
410 return buf_sprintf("%s" OS_SEP "libc" OS_SEP "musl" OS_SEP "crt" OS_SEP "%s" OS_SEP "%s",
411 buf_ptr(parent->zig_lib_dir), musl_arch_name(parent->zig_target), file);
402static const char *musl_start_asm_path(CodeGen *parent, const char *file) {
403 Buf *result = buf_sprintf("%s" OS_SEP "libc" OS_SEP "musl" OS_SEP "crt" OS_SEP "%s" OS_SEP "%s",
404 buf_ptr(parent->zig_lib_dir), musl_arch_name(parent->zig_target), file);
405 return buf_ptr(result);
412406}
413407
414static void musl_add_cc_args(CodeGen *parent, CFile *c_file) {
408static void musl_add_cc_args(CodeGen *parent, CFile *c_file, bool want_O3) {
415409 c_file->args.append("-std=c99");
416410 c_file->args.append("-ffreestanding");
417411 // Musl adds these args to builds with gcc but clang does not support them.
......@@ -448,7 +442,11 @@ static void musl_add_cc_args(CodeGen *parent, CFile *c_file) {
448442 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "include" OS_SEP "generic-musl",
449443 buf_ptr(parent->zig_lib_dir))));
450444
451 c_file->args.append("-Os");
445 if (want_O3)
446 c_file->args.append("-O3");
447 else
448 c_file->args.append("-Os");
449
452450 c_file->args.append("-fomit-frame-pointer");
453451 c_file->args.append("-fno-unwind-tables");
454452 c_file->args.append("-fno-asynchronous-unwind-tables");
......@@ -574,19 +572,14 @@ static const char *build_musl(CodeGen *parent) {
574572 Buf *full_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "%s",
575573 buf_ptr(parent->zig_lib_dir), buf_ptr(src_file));
576574
577 if (src_kind == MuslSrcAsm) {
578 codegen_add_assembly(child_gen, full_path);
579 } else {
580 CFile *c_file = allocate<CFile>(1);
581 c_file->source_path = buf_ptr(full_path);
582 musl_add_cc_args(parent, c_file);
583 c_file->args.append("-Qunused-arguments");
584 c_file->args.append("-w"); // disable all warnings
585 if (src_kind == MuslSrcO3) {
586 c_file->args.append("-O3");
587 }
588 c_source_files.append(c_file);
589 }
575 CFile *c_file = allocate<CFile>(1);
576 c_file->source_path = buf_ptr(full_path);
577
578 musl_add_cc_args(parent, c_file, src_kind == MuslSrcO3);
579 c_file->args.append("-Qunused-arguments");
580 c_file->args.append("-w"); // disable all warnings
581
582 c_source_files.append(c_file);
590583 }
591584
592585 child_gen->c_source_files = c_source_files;
......@@ -744,20 +737,28 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
744737 }
745738 } else if (parent->libc == nullptr && target_is_musl(parent->zig_target)) {
746739 if (strcmp(file, "crti.o") == 0) {
747 return build_asm_object(parent, "crti", musl_start_asm_path(parent, "crti.s"));
740 CFile *c_file = allocate<CFile>(1);
741 c_file->source_path = musl_start_asm_path(parent, "crti.s");
742 musl_add_cc_args(parent, c_file, false);
743 c_file->args.append("-Qunused-arguments");
744 return build_libc_object(parent, "crti", c_file);
748745 } else if (strcmp(file, "crtn.o") == 0) {
749 return build_asm_object(parent, "crtn", musl_start_asm_path(parent, "crtn.s"));
746 CFile *c_file = allocate<CFile>(1);
747 c_file->source_path = musl_start_asm_path(parent, "crtn.s");
748 c_file->args.append("-Qunused-arguments");
749 musl_add_cc_args(parent, c_file, false);
750 return build_libc_object(parent, "crtn", c_file);
750751 } else if (strcmp(file, "crt1.o") == 0) {
751752 CFile *c_file = allocate<CFile>(1);
752753 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "crt1.c");
753 musl_add_cc_args(parent, c_file);
754 musl_add_cc_args(parent, c_file, false);
754755 c_file->args.append("-fno-stack-protector");
755756 c_file->args.append("-DCRT");
756757 return build_libc_object(parent, "crt1", c_file);
757758 } else if (strcmp(file, "Scrt1.o") == 0) {
758759 CFile *c_file = allocate<CFile>(1);
759760 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "Scrt1.c");
760 musl_add_cc_args(parent, c_file);
761 musl_add_cc_args(parent, c_file, false);
761762 c_file->args.append("-fPIC");
762763 c_file->args.append("-fno-stack-protector");
763764 c_file->args.append("-DCRT");
src/main.cpp+1-9
......@@ -48,7 +48,6 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
4848 " zen print zen of zig and exit\n"
4949 "\n"
5050 "Compile Options:\n"
51 " --assembly [source] add assembly file to build\n"
5251 " --c-source [options] [file] compile C source code\n"
5352 " --cache-dir [path] override the local cache directory\n"
5453 " --cache [auto|off|on] build in cache, print output path to stdout\n"
......@@ -428,7 +427,6 @@ int main(int argc, char **argv) {
428427 bool each_lib_rpath = false;
429428 ZigList<const char *> objects = {0};
430429 ZigList<CFile *> c_source_files = {0};
431 ZigList<const char *> asm_files = {0};
432430 const char *test_filter = nullptr;
433431 const char *test_name_prefix = nullptr;
434432 size_t ver_major = 0;
......@@ -774,8 +772,6 @@ int main(int argc, char **argv) {
774772 break;
775773 }
776774 }
777 } else if (strcmp(arg, "--assembly") == 0) {
778 asm_files.append(argv[i]);
779775 } else if (strcmp(arg, "--cache-dir") == 0) {
780776 cache_dir = argv[i];
781777 } else if (strcmp(arg, "-target") == 0) {
......@@ -971,14 +967,13 @@ int main(int argc, char **argv) {
971967 case CmdTranslateCUserland:
972968 case CmdTest:
973969 {
974 if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0 &&
970 if (cmd == CmdBuild && !in_file && objects.length == 0 &&
975971 c_source_files.length == 0)
976972 {
977973 fprintf(stderr,
978974 "Expected at least one of these things:\n"
979975 " * Zig root source file argument\n"
980976 " * --object argument\n"
981 " * --assembly argument\n"
982977 " * --c-source argument\n");
983978 return print_error_usage(arg0);
984979 } else if ((cmd == CmdTranslateC || cmd == CmdTranslateCUserland ||
......@@ -1130,9 +1125,6 @@ int main(int argc, char **argv) {
11301125 for (size_t i = 0; i < objects.length; i += 1) {
11311126 codegen_add_object(g, buf_create_from_str(objects.at(i)));
11321127 }
1133 for (size_t i = 0; i < asm_files.length; i += 1) {
1134 codegen_add_assembly(g, buf_create_from_str(asm_files.at(i)));
1135 }
11361128 }
11371129
11381130
std/build.zig+1-1
......@@ -1376,7 +1376,7 @@ pub const LibExeObjStep = struct {
13761376 try zig_args.append(name);
13771377 },
13781378 LinkObject.AssemblyFile => |asm_file| {
1379 try zig_args.append("--assembly");
1379 try zig_args.append("--c-source");
13801380 try zig_args.append(builder.pathFromRoot(asm_file));
13811381 },
13821382 LinkObject.CSourceFile => |c_source_file| {