authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-21 20:32:48-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-21 20:32:48-04:00
log4b0ddb817bb5d4effd8cd2dd0844ac278e35e1d5
treeea0112fc930bf6a51b62136f10a60560c3c96bd9
parent28ad78cb7f4a567ce6a595b8ff466e90b311f3e4
signature Commit is signed but in an unrecognized format.

zig cc: better support for the preprocessor option (-E)


9 files changed, 115 insertions(+), 14 deletions(-)

src-self-hosted/clang_options_data.zig+4-4
...@@ -7,7 +7,7 @@ flagpd1("CC"),...@@ -7,7 +7,7 @@ flagpd1("CC"),
7.{7.{
8 .name = "E",8 .name = "E",
9 .syntax = .flag,9 .syntax = .flag,
10 .zig_equivalent = .driver_punt,10 .zig_equivalent = .preprocess,
11 .pd1 = true,11 .pd1 = true,
12 .pd2 = false,12 .pd2 = false,
13 .psl = false,13 .psl = false,
...@@ -133,7 +133,7 @@ flagpd1("###"),...@@ -133,7 +133,7 @@ flagpd1("###"),
133.{133.{
134 .name = "E",134 .name = "E",
135 .syntax = .flag,135 .syntax = .flag,
136 .zig_equivalent = .driver_punt,136 .zig_equivalent = .preprocess,
137 .pd1 = true,137 .pd1 = true,
138 .pd2 = false,138 .pd2 = false,
139 .psl = true,139 .psl = true,
...@@ -1421,7 +1421,7 @@ flagpd1("###"),...@@ -1421,7 +1421,7 @@ flagpd1("###"),
1421.{1421.{
1422 .name = "assemble",1422 .name = "assemble",
1423 .syntax = .flag,1423 .syntax = .flag,
1424 .zig_equivalent = .other,1424 .zig_equivalent = .driver_punt,
1425 .pd1 = false,1425 .pd1 = false,
1426 .pd2 = true,1426 .pd2 = true,
1427 .psl = false,1427 .psl = false,
...@@ -1749,7 +1749,7 @@ flagpd1("###"),...@@ -1749,7 +1749,7 @@ flagpd1("###"),
1749.{1749.{
1750 .name = "preprocess",1750 .name = "preprocess",
1751 .syntax = .flag,1751 .syntax = .flag,
1752 .zig_equivalent = .other,1752 .zig_equivalent = .preprocess,
1753 .pd1 = false,1753 .pd1 = false,
1754 .pd2 = true,1754 .pd2 = true,
1755 .psl = false,1755 .psl = false,
src-self-hosted/stage2.zig+1
...@@ -1247,6 +1247,7 @@ pub const ClangArgIterator = extern struct {...@@ -1247,6 +1247,7 @@ pub const ClangArgIterator = extern struct {
1247 shared,1247 shared,
1248 rdynamic,1248 rdynamic,
1249 wl,1249 wl,
1250 preprocess,
1250 };1251 };
12511252
1252 fn init(argv: []const [*:0]const u8) ClangArgIterator {1253 fn init(argv: []const [*:0]const u8) ClangArgIterator {
src/all_types.hpp+2
...@@ -2003,6 +2003,7 @@ enum WantCSanitize {...@@ -2003,6 +2003,7 @@ enum WantCSanitize {
2003struct CFile {2003struct CFile {
2004 ZigList<const char *> args;2004 ZigList<const char *> args;
2005 const char *source_path;2005 const char *source_path;
2006 const char *preprocessor_only_basename;
2006};2007};
20072008
2008// When adding fields, check if they should be added to the hash computation in build_with_cache2009// When adding fields, check if they should be added to the hash computation in build_with_cache
...@@ -2147,6 +2148,7 @@ struct CodeGen {...@@ -2147,6 +2148,7 @@ struct CodeGen {
2147 // As an input parameter, mutually exclusive with enable_cache. But it gets2148 // As an input parameter, mutually exclusive with enable_cache. But it gets
2148 // populated in codegen_build_and_link.2149 // populated in codegen_build_and_link.
2149 Buf *output_dir;2150 Buf *output_dir;
2151 Buf *c_artifact_dir;
2150 const char **libc_include_dir_list;2152 const char **libc_include_dir_list;
2151 size_t libc_include_dir_len;2153 size_t libc_include_dir_len;
21522154
src/codegen.cpp+16-6
...@@ -9723,13 +9723,17 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -9723,13 +9723,17 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
9723 buf_len(c_source_basename), 0);9723 buf_len(c_source_basename), 0);
97249724
9725 Buf *final_o_basename = buf_alloc();9725 Buf *final_o_basename = buf_alloc();
9726 // We special case when doing build-obj for just one C file9726 if (c_file->preprocessor_only_basename == nullptr) {
9727 if (main_output_dir_is_just_one_c_object_pre(g)) {9727 // We special case when doing build-obj for just one C file
9728 buf_init_from_buf(final_o_basename, g->root_out_name);9728 if (main_output_dir_is_just_one_c_object_pre(g)) {
9729 buf_init_from_buf(final_o_basename, g->root_out_name);
9730 } else {
9731 os_path_extname(c_source_basename, final_o_basename, nullptr);
9732 }
9733 buf_append_str(final_o_basename, target_o_file_ext(g->zig_target));
9729 } else {9734 } else {
9730 os_path_extname(c_source_basename, final_o_basename, nullptr);9735 buf_init_from_str(final_o_basename, c_file->preprocessor_only_basename);
9731 }9736 }
9732 buf_append_str(final_o_basename, target_o_file_ext(g->zig_target));
97339737
9734 CacheHash *cache_hash;9738 CacheHash *cache_hash;
9735 if ((err = create_c_object_cache(g, &cache_hash, true))) {9739 if ((err = create_c_object_cache(g, &cache_hash, true))) {
...@@ -9780,13 +9784,18 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -9780,13 +9784,18 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
9780 args.append(buf_ptr(self_exe_path));9784 args.append(buf_ptr(self_exe_path));
9781 args.append("clang");9785 args.append("clang");
97829786
9787 if (c_file->preprocessor_only_basename != nullptr) {
9788 args.append("-E");
9789 } else {
9790 args.append("-c");
9791 }
9792
9783 Buf *out_dep_path = buf_sprintf("%s.d", buf_ptr(out_obj_path));9793 Buf *out_dep_path = buf_sprintf("%s.d", buf_ptr(out_obj_path));
9784 add_cc_args(g, args, buf_ptr(out_dep_path), false);9794 add_cc_args(g, args, buf_ptr(out_dep_path), false);
97859795
9786 args.append("-o");9796 args.append("-o");
9787 args.append(buf_ptr(out_obj_path));9797 args.append(buf_ptr(out_obj_path));
97889798
9789 args.append("-c");
9790 args.append(buf_ptr(c_source_file));9799 args.append(buf_ptr(c_source_file));
97919800
9792 for (size_t arg_i = 0; arg_i < c_file->args.length; arg_i += 1) {9801 for (size_t arg_i = 0; arg_i < c_file->args.length; arg_i += 1) {
...@@ -9841,6 +9850,7 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -9841,6 +9850,7 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
9841 os_path_join(artifact_dir, final_o_basename, o_final_path);9850 os_path_join(artifact_dir, final_o_basename, o_final_path);
9842 }9851 }
98439852
9853 g->c_artifact_dir = artifact_dir;
9844 g->link_objects.append(o_final_path);9854 g->link_objects.append(o_final_path);
9845 g->caches_to_release.append(cache_hash);9855 g->caches_to_release.append(cache_hash);
98469856
src/main.cpp+57-3
...@@ -453,6 +453,7 @@ static int main0(int argc, char **argv) {...@@ -453,6 +453,7 @@ static int main0(int argc, char **argv) {
453 const char *mcpu = nullptr;453 const char *mcpu = nullptr;
454 CodeModel code_model = CodeModelDefault;454 CodeModel code_model = CodeModelDefault;
455 const char *override_soname = nullptr;455 const char *override_soname = nullptr;
456 bool only_preprocess = false;
456457
457 ZigList<const char *> llvm_argv = {0};458 ZigList<const char *> llvm_argv = {0};
458 llvm_argv.append("zig (LLVM option parsing)");459 llvm_argv.append("zig (LLVM option parsing)");
...@@ -660,6 +661,9 @@ static int main0(int argc, char **argv) {...@@ -660,6 +661,9 @@ static int main0(int argc, char **argv) {
660 }661 }
661 break;662 break;
662 }663 }
664 case Stage2ClangArgPreprocess:
665 only_preprocess = true;
666 break;
663 }667 }
664 }668 }
665 // Parse linker args669 // Parse linker args
...@@ -715,7 +719,28 @@ static int main0(int argc, char **argv) {...@@ -715,7 +719,28 @@ static int main0(int argc, char **argv) {
715 have_libc = true;719 have_libc = true;
716 link_libs.append("c");720 link_libs.append("c");
717 }721 }
718 if (!c_arg) {722 if (only_preprocess) {
723 cmd = CmdBuild;
724 out_type = OutTypeObj;
725 emit_bin = false;
726 // Transfer "objects" into c_source_files
727 for (size_t i = 0; i < objects.length; i += 1) {
728 CFile *c_file = heap::c_allocator.create<CFile>();
729 c_file->source_path = objects.at(i);
730 c_source_files.append(c_file);
731 }
732 for (size_t i = 0; i < c_source_files.length; i += 1) {
733 Buf *src_path;
734 if (emit_bin_override_path != nullptr) {
735 src_path = buf_create_from_str(emit_bin_override_path);
736 } else {
737 src_path = buf_create_from_str(c_source_files.at(i)->source_path);
738 }
739 Buf basename = BUF_INIT;
740 os_path_split(src_path, nullptr, &basename);
741 c_source_files.at(i)->preprocessor_only_basename = buf_ptr(&basename);
742 }
743 } else if (!c_arg) {
719 cmd = CmdBuild;744 cmd = CmdBuild;
720 if (is_shared_lib) {745 if (is_shared_lib) {
721 out_type = OutTypeLib;746 out_type = OutTypeLib;
...@@ -1464,12 +1489,41 @@ static int main0(int argc, char **argv) {...@@ -1464,12 +1489,41 @@ static int main0(int argc, char **argv) {
1464 return term.code;1489 return term.code;
1465 } else if (cmd == CmdBuild) {1490 } else if (cmd == CmdBuild) {
1466 if (emit_bin_override_path != nullptr) {1491 if (emit_bin_override_path != nullptr) {
1492#if defined(ZIG_OS_WINDOWS)
1493 buf_replace(g->output_dir, '/', '\\');
1494#endif
1467 Buf *dest_path = buf_create_from_str(emit_bin_override_path);1495 Buf *dest_path = buf_create_from_str(emit_bin_override_path);
1468 if ((err = os_update_file(&g->bin_file_output_path, dest_path))) {1496 Buf *source_path;
1469 fprintf(stderr, "unable to copy %s to %s: %s\n", buf_ptr(&g->bin_file_output_path),1497 if (only_preprocess) {
1498 source_path = buf_alloc();
1499 Buf *pp_only_basename = buf_create_from_str(
1500 c_source_files.at(0)->preprocessor_only_basename);
1501 os_path_join(g->output_dir, pp_only_basename, source_path);
1502
1503 } else {
1504 source_path = &g->bin_file_output_path;
1505 }
1506 if ((err = os_update_file(source_path, dest_path))) {
1507 fprintf(stderr, "unable to copy %s to %s: %s\n", buf_ptr(source_path),
1470 buf_ptr(dest_path), err_str(err));1508 buf_ptr(dest_path), err_str(err));
1471 return main_exit(root_progress_node, EXIT_FAILURE);1509 return main_exit(root_progress_node, EXIT_FAILURE);
1472 }1510 }
1511 } else if (only_preprocess) {
1512#if defined(ZIG_OS_WINDOWS)
1513 buf_replace(g->c_artifact_dir, '/', '\\');
1514#endif
1515 // dump the preprocessed output to stdout
1516 for (size_t i = 0; i < c_source_files.length; i += 1) {
1517 Buf *source_path = buf_alloc();
1518 Buf *pp_only_basename = buf_create_from_str(
1519 c_source_files.at(i)->preprocessor_only_basename);
1520 os_path_join(g->c_artifact_dir, pp_only_basename, source_path);
1521 if ((err = os_dump_file(source_path, stdout))) {
1522 fprintf(stderr, "unable to read %s: %s\n", buf_ptr(source_path),
1523 err_str(err));
1524 return main_exit(root_progress_node, EXIT_FAILURE);
1525 }
1526 }
1473 } else if (g->enable_cache) {1527 } else if (g->enable_cache) {
1474#if defined(ZIG_OS_WINDOWS)1528#if defined(ZIG_OS_WINDOWS)
1475 buf_replace(&g->bin_file_output_path, '/', '\\');1529 buf_replace(&g->bin_file_output_path, '/', '\\');
src/os.cpp+24
...@@ -1051,6 +1051,30 @@ static Error copy_open_files(FILE *src_f, FILE *dest_f) {...@@ -1051,6 +1051,30 @@ static Error copy_open_files(FILE *src_f, FILE *dest_f) {
1051 }1051 }
1052}1052}
10531053
1054Error os_dump_file(Buf *src_path, FILE *dest_file) {
1055 Error err;
1056
1057 FILE *src_f = fopen(buf_ptr(src_path), "rb");
1058 if (!src_f) {
1059 int err = errno;
1060 if (err == ENOENT) {
1061 return ErrorFileNotFound;
1062 } else if (err == EACCES || err == EPERM) {
1063 return ErrorAccess;
1064 } else {
1065 return ErrorFileSystem;
1066 }
1067 }
1068 copy_open_files(src_f, dest_file);
1069 if ((err = copy_open_files(src_f, dest_file))) {
1070 fclose(src_f);
1071 return err;
1072 }
1073
1074 fclose(src_f);
1075 return ErrorNone;
1076}
1077
1054#if defined(ZIG_OS_WINDOWS)1078#if defined(ZIG_OS_WINDOWS)
1055static void windows_filetime_to_os_timestamp(FILETIME *ft, OsTimeStamp *mtime) {1079static void windows_filetime_to_os_timestamp(FILETIME *ft, OsTimeStamp *mtime) {
1056 mtime->sec = (((ULONGLONG) ft->dwHighDateTime) << 32) + ft->dwLowDateTime;1080 mtime->sec = (((ULONGLONG) ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
src/os.hpp+1
...@@ -129,6 +129,7 @@ void os_file_close(OsFile *file);...@@ -129,6 +129,7 @@ void os_file_close(OsFile *file);
129Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);129Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);
130Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);130Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);
131Error ATTRIBUTE_MUST_USE os_update_file(Buf *src_path, Buf *dest_path);131Error ATTRIBUTE_MUST_USE os_update_file(Buf *src_path, Buf *dest_path);
132Error ATTRIBUTE_MUST_USE os_dump_file(Buf *src_path, FILE *dest_file);
132133
133Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents);134Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents);
134Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);135Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);
src/stage2.h+1
...@@ -333,6 +333,7 @@ enum Stage2ClangArg {...@@ -333,6 +333,7 @@ enum Stage2ClangArg {
333 Stage2ClangArgShared,333 Stage2ClangArgShared,
334 Stage2ClangArgRDynamic,334 Stage2ClangArgRDynamic,
335 Stage2ClangArgWL,335 Stage2ClangArgWL,
336 Stage2ClangArgPreprocess,
336};337};
337338
338// ABI warning339// ABI warning
tools/update_clang_options.zig+9-1
...@@ -76,12 +76,20 @@ const known_options = [_]KnownOpt{...@@ -76,12 +76,20 @@ const known_options = [_]KnownOpt{
76 },76 },
77 .{77 .{
78 .name = "E",78 .name = "E",
79 .ident = "driver_punt",79 .ident = "preprocess",
80 },
81 .{
82 .name = "preprocess",
83 .ident = "preprocess",
80 },84 },
81 .{85 .{
82 .name = "S",86 .name = "S",
83 .ident = "driver_punt",87 .ident = "driver_punt",
84 },88 },
89 .{
90 .name = "assemble",
91 .ident = "driver_punt",
92 },
85};93};
8694
87const blacklisted_options = [_][]const u8{};95const blacklisted_options = [_][]const u8{};