authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-13 12:10:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-13 12:10:21-04:00
logf3db3b3c1319e2e86bf5eabd2c2eaef58e4dd783
tree0df5ac1ae3cb5c7cddf30391666128a7a4cae264
parentc1793d61061a112dc3d58c33ac0d3a1ebc8fb35e
parenta038ef3570caed01815173d95089e016fab3a050
signature Commit is signed but in an unrecognized format.

Merge branch 'asm-cc' of https://github.com/LemonBoy/zig into LemonBoy-asm-cc


5 files changed, 40 insertions(+), 69 deletions(-)

src/cache_hash.cpp-3
...@@ -470,9 +470,6 @@ Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {...@@ -470,9 +470,6 @@ Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
470 Error err;470 Error err;
471 Buf *contents = buf_alloc();471 Buf *contents = buf_alloc();
472 if ((err = os_fetch_file_path(dep_file_path, contents))) {472 if ((err = os_fetch_file_path(dep_file_path, contents))) {
473 if (verbose) {
474 fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
475 }
476 return ErrorReadingDepFile;473 return ErrorReadingDepFile;
477 }474 }
478 SplitIterator it = memSplit(buf_to_slice(contents), str("\r\n"));475 SplitIterator it = memSplit(buf_to_slice(contents), str("\r\n"));
src/codegen.cpp+7-26
...@@ -8334,8 +8334,6 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa...@@ -8334,8 +8334,6 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
8334 for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) {8334 for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) {
8335 args.append(g->clang_argv[arg_i]);8335 args.append(g->clang_argv[arg_i]);
8336 }8336 }
8337
8338
8339}8337}
83408338
8341void codegen_translate_c(CodeGen *g, Buf *full_path, FILE *out_file, bool use_userland_implementation) {8339void 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) {...@@ -8582,24 +8580,6 @@ static void gen_root_source(CodeGen *g) {
85828580
8583}8581}
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
8603static void print_zig_cc_cmd(const char *zig_exe, ZigList<const char *> *args) {8583static void print_zig_cc_cmd(const char *zig_exe, ZigList<const char *> *args) {
8604 fprintf(stderr, "%s", zig_exe);8584 fprintf(stderr, "%s", zig_exe);
8605 for (size_t arg_i = 0; arg_i < args->length; arg_i += 1) {8585 for (size_t arg_i = 0; arg_i < args->length; arg_i += 1) {
...@@ -8750,8 +8730,12 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -8750,8 +8730,12 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
87508730
8751 // add the files depended on to the cache system8731 // add the files depended on to the cache system
8752 if ((err = cache_add_dep_file(cache_hash, out_dep_path, true))) {8732 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));8733 // Don't treat the absence of the .d file as a fatal error, the
8754 exit(1);8734 // compiler may not produce one eg. when compiling .s files
8735 if (err != ErrorReadingDepFile) {
8736 fprintf(stderr, "Failed to add C source dependencies to cache: %s\n", err_str(err));
8737 exit(1);
8738 }
8755 }8739 }
8756 os_delete_file(out_dep_path);8740 os_delete_file(out_dep_path);
87578741
...@@ -9330,8 +9314,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -9330,8 +9314,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
9330 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);9314 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
9331 cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);9315 cache_list_of_buf(ch, g->rpath_list.items, g->rpath_list.length);
9332 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);9316 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);
9335 cache_int(ch, g->build_mode);9317 cache_int(ch, g->build_mode);
9336 cache_int(ch, g->out_type);9318 cache_int(ch, g->out_type);
9337 cache_bool(ch, g->zig_target->is_native);9319 cache_bool(ch, g->zig_target->is_native);
...@@ -9392,7 +9374,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -9392,7 +9374,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
9392}9374}
93939375
9394static bool need_llvm_module(CodeGen *g) {9376static bool need_llvm_module(CodeGen *g) {
9395 return g->assembly_files.length != 0 || buf_len(&g->root_package->root_src_path) != 0;9377 return buf_len(&g->root_package->root_src_path) != 0;
9396}9378}
93979379
9398static void resolve_out_paths(CodeGen *g) {9380static void resolve_out_paths(CodeGen *g) {
...@@ -9499,7 +9481,6 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9499,7 +9481,6 @@ void codegen_build_and_link(CodeGen *g) {
94999481
9500 codegen_add_time_event(g, "Semantic Analysis");9482 codegen_add_time_event(g, "Semantic Analysis");
95019483
9502 gen_global_asm(g);
9503 gen_root_source(g);9484 gen_root_source(g);
95049485
9505 }9486 }
src/link.cpp+31-30
...@@ -59,14 +59,6 @@ static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFil...@@ -59,14 +59,6 @@ static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFil
59 return buf_ptr(&child_gen->output_file_path);59 return buf_ptr(&child_gen->output_file_path);
60}60}
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
70static const char *path_from_zig_lib(CodeGen *g, const char *dir, const char *subpath) {62static const char *path_from_zig_lib(CodeGen *g, const char *dir, const char *subpath) {
71 Buf *dir1 = buf_alloc();63 Buf *dir1 = buf_alloc();
72 os_path_join(g->zig_lib_dir, buf_create_from_str(dir), dir1);64 os_path_join(g->zig_lib_dir, buf_create_from_str(dir), dir1);
...@@ -140,6 +132,7 @@ static const char *build_libunwind(CodeGen *parent) {...@@ -140,6 +132,7 @@ static const char *build_libunwind(CodeGen *parent) {
140 c_file->args.append(path_from_libunwind(parent, "include"));132 c_file->args.append(path_from_libunwind(parent, "include"));
141 c_file->args.append("-fPIC");133 c_file->args.append("-fPIC");
142 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");134 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
135 c_file->args.append("-Wa,--noexecstack");
143 if (parent->zig_target->is_native) {136 if (parent->zig_target->is_native) {
144 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");137 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
145 }138 }
...@@ -406,12 +399,13 @@ static const char *musl_arch_name(const ZigTarget *target) {...@@ -406,12 +399,13 @@ static const char *musl_arch_name(const ZigTarget *target) {
406 }399 }
407}400}
408401
409static Buf *musl_start_asm_path(CodeGen *parent, const char *file) {402static const char *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",403 Buf *result = 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);404 buf_ptr(parent->zig_lib_dir), musl_arch_name(parent->zig_target), file);
405 return buf_ptr(result);
412}406}
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) {
415 c_file->args.append("-std=c99");409 c_file->args.append("-std=c99");
416 c_file->args.append("-ffreestanding");410 c_file->args.append("-ffreestanding");
417 // Musl adds these args to builds with gcc but clang does not support them. 411 // 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) {...@@ -448,7 +442,11 @@ static void musl_add_cc_args(CodeGen *parent, CFile *c_file) {
448 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "include" OS_SEP "generic-musl",442 c_file->args.append(buf_ptr(buf_sprintf("%s" OS_SEP "libc" OS_SEP "include" OS_SEP "generic-musl",
449 buf_ptr(parent->zig_lib_dir))));443 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
452 c_file->args.append("-fomit-frame-pointer");450 c_file->args.append("-fomit-frame-pointer");
453 c_file->args.append("-fno-unwind-tables");451 c_file->args.append("-fno-unwind-tables");
454 c_file->args.append("-fno-asynchronous-unwind-tables");452 c_file->args.append("-fno-asynchronous-unwind-tables");
...@@ -574,19 +572,14 @@ static const char *build_musl(CodeGen *parent) {...@@ -574,19 +572,14 @@ static const char *build_musl(CodeGen *parent) {
574 Buf *full_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "%s",572 Buf *full_path = buf_sprintf("%s" OS_SEP "libc" OS_SEP "%s",
575 buf_ptr(parent->zig_lib_dir), buf_ptr(src_file));573 buf_ptr(parent->zig_lib_dir), buf_ptr(src_file));
576574
577 if (src_kind == MuslSrcAsm) {575 CFile *c_file = allocate<CFile>(1);
578 codegen_add_assembly(child_gen, full_path);576 c_file->source_path = buf_ptr(full_path);
579 } else {577
580 CFile *c_file = allocate<CFile>(1);578 musl_add_cc_args(parent, c_file, src_kind == MuslSrcO3);
581 c_file->source_path = buf_ptr(full_path);579 c_file->args.append("-Qunused-arguments");
582 musl_add_cc_args(parent, c_file);580 c_file->args.append("-w"); // disable all warnings
583 c_file->args.append("-Qunused-arguments");581
584 c_file->args.append("-w"); // disable all warnings582 c_source_files.append(c_file);
585 if (src_kind == MuslSrcO3) {
586 c_file->args.append("-O3");
587 }
588 c_source_files.append(c_file);
589 }
590 }583 }
591584
592 child_gen->c_source_files = c_source_files;585 child_gen->c_source_files = c_source_files;
...@@ -744,20 +737,28 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {...@@ -744,20 +737,28 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) {
744 }737 }
745 } else if (parent->libc == nullptr && target_is_musl(parent->zig_target)) {738 } else if (parent->libc == nullptr && target_is_musl(parent->zig_target)) {
746 if (strcmp(file, "crti.o") == 0) {739 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);
748 } else if (strcmp(file, "crtn.o") == 0) {745 } 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);
750 } else if (strcmp(file, "crt1.o") == 0) {751 } else if (strcmp(file, "crt1.o") == 0) {
751 CFile *c_file = allocate<CFile>(1);752 CFile *c_file = allocate<CFile>(1);
752 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "crt1.c");753 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);
754 c_file->args.append("-fno-stack-protector");755 c_file->args.append("-fno-stack-protector");
755 c_file->args.append("-DCRT");756 c_file->args.append("-DCRT");
756 return build_libc_object(parent, "crt1", c_file);757 return build_libc_object(parent, "crt1", c_file);
757 } else if (strcmp(file, "Scrt1.o") == 0) {758 } else if (strcmp(file, "Scrt1.o") == 0) {
758 CFile *c_file = allocate<CFile>(1);759 CFile *c_file = allocate<CFile>(1);
759 c_file->source_path = path_from_libc(parent, "musl" OS_SEP "crt" OS_SEP "Scrt1.c");760 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);
761 c_file->args.append("-fPIC");762 c_file->args.append("-fPIC");
762 c_file->args.append("-fno-stack-protector");763 c_file->args.append("-fno-stack-protector");
763 c_file->args.append("-DCRT");764 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) {...@@ -48,7 +48,6 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
48 " zen print zen of zig and exit\n"48 " zen print zen of zig and exit\n"
49 "\n"49 "\n"
50 "Compile Options:\n"50 "Compile Options:\n"
51 " --assembly [source] add assembly file to build\n"
52 " --c-source [options] [file] compile C source code\n"51 " --c-source [options] [file] compile C source code\n"
53 " --cache-dir [path] override the local cache directory\n"52 " --cache-dir [path] override the local cache directory\n"
54 " --cache [auto|off|on] build in cache, print output path to stdout\n"53 " --cache [auto|off|on] build in cache, print output path to stdout\n"
...@@ -428,7 +427,6 @@ int main(int argc, char **argv) {...@@ -428,7 +427,6 @@ int main(int argc, char **argv) {
428 bool each_lib_rpath = false;427 bool each_lib_rpath = false;
429 ZigList<const char *> objects = {0};428 ZigList<const char *> objects = {0};
430 ZigList<CFile *> c_source_files = {0};429 ZigList<CFile *> c_source_files = {0};
431 ZigList<const char *> asm_files = {0};
432 const char *test_filter = nullptr;430 const char *test_filter = nullptr;
433 const char *test_name_prefix = nullptr;431 const char *test_name_prefix = nullptr;
434 size_t ver_major = 0;432 size_t ver_major = 0;
...@@ -774,8 +772,6 @@ int main(int argc, char **argv) {...@@ -774,8 +772,6 @@ int main(int argc, char **argv) {
774 break;772 break;
775 }773 }
776 }774 }
777 } else if (strcmp(arg, "--assembly") == 0) {
778 asm_files.append(argv[i]);
779 } else if (strcmp(arg, "--cache-dir") == 0) {775 } else if (strcmp(arg, "--cache-dir") == 0) {
780 cache_dir = argv[i];776 cache_dir = argv[i];
781 } else if (strcmp(arg, "-target") == 0) {777 } else if (strcmp(arg, "-target") == 0) {
...@@ -971,14 +967,13 @@ int main(int argc, char **argv) {...@@ -971,14 +967,13 @@ int main(int argc, char **argv) {
971 case CmdTranslateCUserland:967 case CmdTranslateCUserland:
972 case CmdTest:968 case CmdTest:
973 {969 {
974 if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0 &&970 if (cmd == CmdBuild && !in_file && objects.length == 0 &&
975 c_source_files.length == 0)971 c_source_files.length == 0)
976 {972 {
977 fprintf(stderr,973 fprintf(stderr,
978 "Expected at least one of these things:\n"974 "Expected at least one of these things:\n"
979 " * Zig root source file argument\n"975 " * Zig root source file argument\n"
980 " * --object argument\n"976 " * --object argument\n"
981 " * --assembly argument\n"
982 " * --c-source argument\n");977 " * --c-source argument\n");
983 return print_error_usage(arg0);978 return print_error_usage(arg0);
984 } else if ((cmd == CmdTranslateC || cmd == CmdTranslateCUserland ||979 } else if ((cmd == CmdTranslateC || cmd == CmdTranslateCUserland ||
...@@ -1130,9 +1125,6 @@ int main(int argc, char **argv) {...@@ -1130,9 +1125,6 @@ int main(int argc, char **argv) {
1130 for (size_t i = 0; i < objects.length; i += 1) {1125 for (size_t i = 0; i < objects.length; i += 1) {
1131 codegen_add_object(g, buf_create_from_str(objects.at(i)));1126 codegen_add_object(g, buf_create_from_str(objects.at(i)));
1132 }1127 }
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 }
1136 }1128 }
11371129
11381130
std/build.zig+1-1
...@@ -1376,7 +1376,7 @@ pub const LibExeObjStep = struct {...@@ -1376,7 +1376,7 @@ pub const LibExeObjStep = struct {
1376 try zig_args.append(name);1376 try zig_args.append(name);
1377 },1377 },
1378 LinkObject.AssemblyFile => |asm_file| {1378 LinkObject.AssemblyFile => |asm_file| {
1379 try zig_args.append("--assembly");1379 try zig_args.append("--c-source");
1380 try zig_args.append(builder.pathFromRoot(asm_file));1380 try zig_args.append(builder.pathFromRoot(asm_file));
1381 },1381 },
1382 LinkObject.CSourceFile => |c_source_file| {1382 LinkObject.CSourceFile => |c_source_file| {