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
signaturelock-open 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"),
77.{
88 .name = "E",
99 .syntax = .flag,
10 .zig_equivalent = .driver_punt,
10 .zig_equivalent = .preprocess,
1111 .pd1 = true,
1212 .pd2 = false,
1313 .psl = false,
......@@ -133,7 +133,7 @@ flagpd1("###"),
133133.{
134134 .name = "E",
135135 .syntax = .flag,
136 .zig_equivalent = .driver_punt,
136 .zig_equivalent = .preprocess,
137137 .pd1 = true,
138138 .pd2 = false,
139139 .psl = true,
......@@ -1421,7 +1421,7 @@ flagpd1("###"),
14211421.{
14221422 .name = "assemble",
14231423 .syntax = .flag,
1424 .zig_equivalent = .other,
1424 .zig_equivalent = .driver_punt,
14251425 .pd1 = false,
14261426 .pd2 = true,
14271427 .psl = false,
......@@ -1749,7 +1749,7 @@ flagpd1("###"),
17491749.{
17501750 .name = "preprocess",
17511751 .syntax = .flag,
1752 .zig_equivalent = .other,
1752 .zig_equivalent = .preprocess,
17531753 .pd1 = false,
17541754 .pd2 = true,
17551755 .psl = false,
src-self-hosted/stage2.zig+1
......@@ -1247,6 +1247,7 @@ pub const ClangArgIterator = extern struct {
12471247 shared,
12481248 rdynamic,
12491249 wl,
1250 preprocess,
12501251 };
12511252
12521253 fn init(argv: []const [*:0]const u8) ClangArgIterator {
src/all_types.hpp+2
......@@ -2003,6 +2003,7 @@ enum WantCSanitize {
20032003struct CFile {
20042004 ZigList<const char *> args;
20052005 const char *source_path;
2006 const char *preprocessor_only_basename;
20062007};
20072008
20082009// When adding fields, check if they should be added to the hash computation in build_with_cache
......@@ -2147,6 +2148,7 @@ struct CodeGen {
21472148 // As an input parameter, mutually exclusive with enable_cache. But it gets
21482149 // populated in codegen_build_and_link.
21492150 Buf *output_dir;
2151 Buf *c_artifact_dir;
21502152 const char **libc_include_dir_list;
21512153 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) {
97239723 buf_len(c_source_basename), 0);
97249724
97259725 Buf *final_o_basename = buf_alloc();
9726 // We special case when doing build-obj for just one C file
9727 if (main_output_dir_is_just_one_c_object_pre(g)) {
9728 buf_init_from_buf(final_o_basename, g->root_out_name);
9726 if (c_file->preprocessor_only_basename == nullptr) {
9727 // We special case when doing build-obj for just one C file
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));
97299734 } 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);
97319736 }
9732 buf_append_str(final_o_basename, target_o_file_ext(g->zig_target));
97339737
97349738 CacheHash *cache_hash;
97359739 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) {
97809784 args.append(buf_ptr(self_exe_path));
97819785 args.append("clang");
97829786
9787 if (c_file->preprocessor_only_basename != nullptr) {
9788 args.append("-E");
9789 } else {
9790 args.append("-c");
9791 }
9792
97839793 Buf *out_dep_path = buf_sprintf("%s.d", buf_ptr(out_obj_path));
97849794 add_cc_args(g, args, buf_ptr(out_dep_path), false);
97859795
97869796 args.append("-o");
97879797 args.append(buf_ptr(out_obj_path));
97889798
9789 args.append("-c");
97909799 args.append(buf_ptr(c_source_file));
97919800
97929801 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) {
98419850 os_path_join(artifact_dir, final_o_basename, o_final_path);
98429851 }
98439852
9853 g->c_artifact_dir = artifact_dir;
98449854 g->link_objects.append(o_final_path);
98459855 g->caches_to_release.append(cache_hash);
98469856
src/main.cpp+57-3
......@@ -453,6 +453,7 @@ static int main0(int argc, char **argv) {
453453 const char *mcpu = nullptr;
454454 CodeModel code_model = CodeModelDefault;
455455 const char *override_soname = nullptr;
456 bool only_preprocess = false;
456457
457458 ZigList<const char *> llvm_argv = {0};
458459 llvm_argv.append("zig (LLVM option parsing)");
......@@ -660,6 +661,9 @@ static int main0(int argc, char **argv) {
660661 }
661662 break;
662663 }
664 case Stage2ClangArgPreprocess:
665 only_preprocess = true;
666 break;
663667 }
664668 }
665669 // Parse linker args
......@@ -715,7 +719,28 @@ static int main0(int argc, char **argv) {
715719 have_libc = true;
716720 link_libs.append("c");
717721 }
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) {
719744 cmd = CmdBuild;
720745 if (is_shared_lib) {
721746 out_type = OutTypeLib;
......@@ -1464,12 +1489,41 @@ static int main0(int argc, char **argv) {
14641489 return term.code;
14651490 } else if (cmd == CmdBuild) {
14661491 if (emit_bin_override_path != nullptr) {
1492#if defined(ZIG_OS_WINDOWS)
1493 buf_replace(g->output_dir, '/', '\\');
1494#endif
14671495 Buf *dest_path = buf_create_from_str(emit_bin_override_path);
1468 if ((err = os_update_file(&g->bin_file_output_path, dest_path))) {
1469 fprintf(stderr, "unable to copy %s to %s: %s\n", buf_ptr(&g->bin_file_output_path),
1496 Buf *source_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),
14701508 buf_ptr(dest_path), err_str(err));
14711509 return main_exit(root_progress_node, EXIT_FAILURE);
14721510 }
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 }
14731527 } else if (g->enable_cache) {
14741528#if defined(ZIG_OS_WINDOWS)
14751529 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) {
10511051 }
10521052}
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
10541078#if defined(ZIG_OS_WINDOWS)
10551079static void windows_filetime_to_os_timestamp(FILETIME *ft, OsTimeStamp *mtime) {
10561080 mtime->sec = (((ULONGLONG) ft->dwHighDateTime) << 32) + ft->dwLowDateTime;
src/os.hpp+1
......@@ -129,6 +129,7 @@ void os_file_close(OsFile *file);
129129Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);
130130Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);
131131Error 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
133134Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents);
134135Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);
src/stage2.h+1
......@@ -333,6 +333,7 @@ enum Stage2ClangArg {
333333 Stage2ClangArgShared,
334334 Stage2ClangArgRDynamic,
335335 Stage2ClangArgWL,
336 Stage2ClangArgPreprocess,
336337};
337338
338339// ABI warning
tools/update_clang_options.zig+9-1
......@@ -76,12 +76,20 @@ const known_options = [_]KnownOpt{
7676 },
7777 .{
7878 .name = "E",
79 .ident = "driver_punt",
79 .ident = "preprocess",
80 },
81 .{
82 .name = "preprocess",
83 .ident = "preprocess",
8084 },
8185 .{
8286 .name = "S",
8387 .ident = "driver_punt",
8488 },
89 .{
90 .name = "assemble",
91 .ident = "driver_punt",
92 },
8593};
8694
8795const blacklisted_options = [_][]const u8{};