Skip to content

Commit 2b65dc1

Browse files
committed
zig cc: detect optimization and debug flags
1 parent 4b0ddb8 commit 2b65dc1

File tree

7 files changed

+159
-24
lines changed

7 files changed

+159
-24
lines changed

src-self-hosted/clang_options.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,23 @@ pub const CliArg = struct {
4646
multi_arg: u8,
4747
};
4848

49-
pub fn matchEql(self: CliArg, arg: []const u8) bool {
49+
pub fn matchEql(self: CliArg, arg: []const u8) u2 {
5050
if (self.pd1 and arg.len >= self.name.len + 1 and
5151
mem.startsWith(u8, arg, "-") and mem.eql(u8, arg[1..], self.name))
5252
{
53-
return true;
53+
return 1;
5454
}
5555
if (self.pd2 and arg.len >= self.name.len + 2 and
5656
mem.startsWith(u8, arg, "--") and mem.eql(u8, arg[2..], self.name))
5757
{
58-
return true;
58+
return 2;
5959
}
6060
if (self.psl and arg.len >= self.name.len + 1 and
6161
mem.startsWith(u8, arg, "/") and mem.eql(u8, arg[1..], self.name))
6262
{
63-
return true;
63+
return 1;
6464
}
65-
return false;
65+
return 0;
6666
}
6767

6868
pub fn matchStartsWith(self: CliArg, arg: []const u8) usize {

src-self-hosted/clang_options_data.zig

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ flagpd1("MV"),
3535
flagpd1("Mach"),
3636
flagpd1("O0"),
3737
flagpd1("O4"),
38-
flagpd1("O"),
38+
.{
39+
.name = "O",
40+
.syntax = .flag,
41+
.zig_equivalent = .optimize,
42+
.pd1 = true,
43+
.pd2 = false,
44+
.psl = false,
45+
},
3946
flagpd1("ObjC"),
4047
flagpd1("ObjC++"),
4148
flagpd1("P"),
@@ -1485,7 +1492,7 @@ flagpd1("###"),
14851492
.{
14861493
.name = "debug",
14871494
.syntax = .flag,
1488-
.zig_equivalent = .other,
1495+
.zig_equivalent = .debug,
14891496
.pd1 = false,
14901497
.pd2 = true,
14911498
.psl = false,
@@ -1701,7 +1708,7 @@ flagpd1("###"),
17011708
.{
17021709
.name = "optimize",
17031710
.syntax = .flag,
1704-
.zig_equivalent = .other,
1711+
.zig_equivalent = .optimize,
17051712
.pd1 = false,
17061713
.pd2 = true,
17071714
.psl = false,
@@ -2034,15 +2041,15 @@ flagpd1("fno-semantic-interposition"),
20342041
.{
20352042
.name = "O1",
20362043
.syntax = .flag,
2037-
.zig_equivalent = .other,
2044+
.zig_equivalent = .optimize,
20382045
.pd1 = true,
20392046
.pd2 = false,
20402047
.psl = true,
20412048
},
20422049
.{
20432050
.name = "O2",
20442051
.syntax = .flag,
2045-
.zig_equivalent = .other,
2052+
.zig_equivalent = .optimize,
20462053
.pd1 = true,
20472054
.pd2 = false,
20482055
.psl = true,
@@ -2083,7 +2090,7 @@ flagpd1("fno-ident"),
20832090
.{
20842091
.name = "Og",
20852092
.syntax = .flag,
2086-
.zig_equivalent = .other,
2093+
.zig_equivalent = .optimize,
20872094
.pd1 = true,
20882095
.pd2 = false,
20892096
.psl = true,
@@ -3088,7 +3095,14 @@ flagpd1("g0"),
30883095
flagpd1("g1"),
30893096
flagpd1("g2"),
30903097
flagpd1("g3"),
3091-
flagpd1("g"),
3098+
.{
3099+
.name = "g",
3100+
.syntax = .flag,
3101+
.zig_equivalent = .debug,
3102+
.pd1 = true,
3103+
.pd2 = false,
3104+
.psl = false,
3105+
},
30923106
sepd1("gcc-toolchain"),
30933107
flagpd1("gcodeview"),
30943108
flagpd1("gcodeview-ghash"),
@@ -4954,7 +4968,7 @@ joinpd1("flto-jobs="),
49544968
.{
49554969
.name = "fsanitize=",
49564970
.syntax = .comma_joined,
4957-
.zig_equivalent = .other,
4971+
.zig_equivalent = .sanitize,
49584972
.pd1 = true,
49594973
.pd2 = false,
49604974
.psl = false,
@@ -4997,7 +5011,7 @@ joinpd1("segs_read_"),
49975011
.{
49985012
.name = "optimize=",
49995013
.syntax = .joined,
5000-
.zig_equivalent = .other,
5014+
.zig_equivalent = .optimize,
50015015
.pd1 = false,
50025016
.pd2 = true,
50035017
.psl = false,
@@ -5187,7 +5201,7 @@ joinpd1("Rpass="),
51875201
.{
51885202
.name = "debug=",
51895203
.syntax = .joined,
5190-
.zig_equivalent = .other,
5204+
.zig_equivalent = .debug,
51915205
.pd1 = false,
51925206
.pd2 = true,
51935207
.psl = false,
@@ -5231,7 +5245,14 @@ joinpd1("mtune="),
52315245
.psl = false,
52325246
},
52335247
joinpd1("weak-l"),
5234-
joinpd1("Ofast"),
5248+
.{
5249+
.name = "Ofast",
5250+
.syntax = .joined,
5251+
.zig_equivalent = .optimize,
5252+
.pd1 = true,
5253+
.pd2 = false,
5254+
.psl = false,
5255+
},
52355256
jspd1("Tdata"),
52365257
jspd1("Ttext"),
52375258
.{
@@ -5584,7 +5605,14 @@ jspd1("G"),
55845605
jspd1("I"),
55855606
jspd1("J"),
55865607
jspd1("L"),
5587-
joinpd1("O"),
5608+
.{
5609+
.name = "O",
5610+
.syntax = .joined,
5611+
.zig_equivalent = .optimize,
5612+
.pd1 = true,
5613+
.pd2 = false,
5614+
.psl = false,
5615+
},
55885616
joinpd1("R"),
55895617
jspd1("T"),
55905618
jspd1("U"),
@@ -5619,7 +5647,7 @@ joinpd1("Z"),
56195647
.{
56205648
.name = "O",
56215649
.syntax = .joined,
5622-
.zig_equivalent = .other,
5650+
.zig_equivalent = .optimize,
56235651
.pd1 = true,
56245652
.pd2 = false,
56255653
.psl = true,

src-self-hosted/stage2.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,9 @@ pub const ClangArgIterator = extern struct {
12481248
rdynamic,
12491249
wl,
12501250
preprocess,
1251+
optimize,
1252+
debug,
1253+
sanitize,
12511254
};
12521255

12531256
fn init(argv: []const [*:0]const u8) ClangArgIterator {
@@ -1284,11 +1287,14 @@ pub const ClangArgIterator = extern struct {
12841287
}
12851288

12861289
find_clang_arg: for (clang_args) |clang_arg| switch (clang_arg.syntax) {
1287-
.flag => if (clang_arg.matchEql(arg)) {
1288-
self.zig_equivalent = clang_arg.zig_equivalent;
1289-
self.only_arg = arg.ptr;
1290+
.flag => {
1291+
const prefix_len = clang_arg.matchEql(arg);
1292+
if (prefix_len > 0) {
1293+
self.zig_equivalent = clang_arg.zig_equivalent;
1294+
self.only_arg = arg.ptr + prefix_len;
12901295

1291-
break :find_clang_arg;
1296+
break :find_clang_arg;
1297+
}
12921298
},
12931299
.joined, .comma_joined => {
12941300
// joined example: --target=foo
@@ -1338,7 +1344,7 @@ pub const ClangArgIterator = extern struct {
13381344
break :find_clang_arg;
13391345
}
13401346
},
1341-
.separate => if (clang_arg.matchEql(arg)) {
1347+
.separate => if (clang_arg.matchEql(arg) > 0) {
13421348
if (self.next_index >= self.argv_len) {
13431349
std.debug.warn("Expected parameter after '{}'\n", .{arg});
13441350
process.exit(1);
@@ -1355,7 +1361,7 @@ pub const ClangArgIterator = extern struct {
13551361
@panic("TODO");
13561362
}
13571363
},
1358-
.multi_arg => if (clang_arg.matchEql(arg)) {
1364+
.multi_arg => if (clang_arg.matchEql(arg) > 0) {
13591365
@panic("TODO");
13601366
},
13611367
}

src/codegen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9269,6 +9269,7 @@ void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_pa
92699269
case BuildModeDebug:
92709270
// windows c runtime requires -D_DEBUG if using debug libraries
92719271
args.append("-D_DEBUG");
9272+
args.append("-Og");
92729273

92739274
if (g->libc_link_lib != nullptr) {
92749275
args.append("-fstack-protector-strong");

src/main.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ static int main0(int argc, char **argv) {
580580
return stage2_fmt(argc, argv);
581581
} else if (argc >= 2 && strcmp(argv[1], "cc") == 0) {
582582
emit_h = false;
583+
strip = true;
583584

584585
bool c_arg = false;
585586
Stage2ClangArgIterator it;
@@ -664,6 +665,42 @@ static int main0(int argc, char **argv) {
664665
case Stage2ClangArgPreprocess:
665666
only_preprocess = true;
666667
break;
668+
case Stage2ClangArgOptimize:
669+
// alright what release mode do they want?
670+
if (strcmp(it.only_arg, "Os") == 0) {
671+
build_mode = BuildModeSmallRelease;
672+
} else if (strcmp(it.only_arg, "O2") == 0 ||
673+
strcmp(it.only_arg, "O3") == 0 ||
674+
strcmp(it.only_arg, "O4") == 0)
675+
{
676+
build_mode = BuildModeFastRelease;
677+
} else if (strcmp(it.only_arg, "Og") == 0) {
678+
build_mode = BuildModeDebug;
679+
} else {
680+
for (size_t i = 0; i < it.other_args_len; i += 1) {
681+
clang_argv.append(it.other_args_ptr[i]);
682+
}
683+
}
684+
break;
685+
case Stage2ClangArgDebug:
686+
strip = false;
687+
if (strcmp(it.only_arg, "-g") == 0) {
688+
// we handled with strip = false above
689+
} else {
690+
for (size_t i = 0; i < it.other_args_len; i += 1) {
691+
clang_argv.append(it.other_args_ptr[i]);
692+
}
693+
}
694+
break;
695+
case Stage2ClangArgSanitize:
696+
if (strcmp(it.only_arg, "undefined") == 0) {
697+
want_sanitize_c = WantCSanitizeEnabled;
698+
} else {
699+
for (size_t i = 0; i < it.other_args_len; i += 1) {
700+
clang_argv.append(it.other_args_ptr[i]);
701+
}
702+
}
703+
break;
667704
}
668705
}
669706
// Parse linker args
@@ -715,6 +752,10 @@ static int main0(int argc, char **argv) {
715752
}
716753
}
717754

755+
if (want_sanitize_c == WantCSanitizeEnabled && build_mode == BuildModeFastRelease) {
756+
build_mode = BuildModeSafeRelease;
757+
}
758+
718759
if (!nostdlib && !have_libc) {
719760
have_libc = true;
720761
link_libs.append("c");

src/stage2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ enum Stage2ClangArg {
334334
Stage2ClangArgRDynamic,
335335
Stage2ClangArgWL,
336336
Stage2ClangArgPreprocess,
337+
Stage2ClangArgOptimize,
338+
Stage2ClangArgDebug,
339+
Stage2ClangArgSanitize,
337340
};
338341

339342
// ABI warning

tools/update_clang_options.zig

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,62 @@ const known_options = [_]KnownOpt{
9090
.name = "assemble",
9191
.ident = "driver_punt",
9292
},
93+
.{
94+
.name = "O1",
95+
.ident = "optimize",
96+
},
97+
.{
98+
.name = "O2",
99+
.ident = "optimize",
100+
},
101+
.{
102+
.name = "Og",
103+
.ident = "optimize",
104+
},
105+
.{
106+
.name = "O",
107+
.ident = "optimize",
108+
},
109+
.{
110+
.name = "Ofast",
111+
.ident = "optimize",
112+
},
113+
.{
114+
.name = "optimize",
115+
.ident = "optimize",
116+
},
117+
.{
118+
.name = "g",
119+
.ident = "debug",
120+
},
121+
.{
122+
.name = "debug",
123+
.ident = "debug",
124+
},
125+
.{
126+
.name = "g-dwarf",
127+
.ident = "debug",
128+
},
129+
.{
130+
.name = "g-dwarf-2",
131+
.ident = "debug",
132+
},
133+
.{
134+
.name = "g-dwarf-3",
135+
.ident = "debug",
136+
},
137+
.{
138+
.name = "g-dwarf-4",
139+
.ident = "debug",
140+
},
141+
.{
142+
.name = "g-dwarf-5",
143+
.ident = "debug",
144+
},
145+
.{
146+
.name = "fsanitize",
147+
.ident = "sanitize",
148+
},
93149
};
94150

95151
const blacklisted_options = [_][]const u8{};

0 commit comments

Comments
 (0)