activate start code when pub main exists

and rename LinkType->LinkMode, OutType->OutputMode
This commit is contained in:
Andrew Kelley 2019-12-03 12:29:55 -05:00
parent ffd21c586d
commit 6a046c1bcd
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
4 changed files with 34 additions and 45 deletions

View File

@ -350,8 +350,7 @@ pub const Endian = enum {
/// This data structure is used by the Zig language code generation and /// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation. /// therefore must be kept in sync with the compiler implementation.
pub const OutType = enum { pub const OutputMode = enum {
Unknown,
Exe, Exe,
Lib, Lib,
Obj, Obj,
@ -359,7 +358,7 @@ pub const OutType = enum {
/// This data structure is used by the Zig language code generation and /// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation. /// therefore must be kept in sync with the compiler implementation.
pub const LinkType = enum { pub const LinkMode = enum {
Static, Static,
Dynamic, Dynamic,
}; };

View File

@ -19,15 +19,15 @@ const is_mips = switch (builtin.arch) {
}; };
comptime { comptime {
switch (builtin.output_type) { if (builtin.output_mode == .Lib and builtin.link_mode == .Dynamic) {
.Unknown => unreachable, if (builtin.os == .windows and !@hasDecl(root, "_DllMainCRTStartup")) {
.Exe => { @export("_DllMainCRTStartup", _DllMainCRTStartup, .Strong);
if (builtin.link_libc) { }
if (@hasDecl(root, "main") and } else if (builtin.output_mode == .Exe or @hasDecl(root, "main")) {
if (builtin.link_libc and @hasDecl(root, "main") and
@typeInfo(@typeOf(root.main)).Fn.calling_convention != .C) @typeInfo(@typeOf(root.main)).Fn.calling_convention != .C)
{ {
@export("main", main, .Weak); @export("main", main, .Weak);
}
} else if (builtin.os == .windows) { } else if (builtin.os == .windows) {
if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup")) { if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup")) {
@export("WinMainCRTStartup", WinMainCRTStartup, .Strong); @export("WinMainCRTStartup", WinMainCRTStartup, .Strong);
@ -41,15 +41,6 @@ comptime {
} else { } else {
if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong); if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong);
} }
},
.Lib => {
if (builtin.os == .windows and builtin.link_type == .Dynamic and
!@hasDecl(root, "_DllMainCRTStartup"))
{
@export("_DllMainCRTStartup", _DllMainCRTStartup, .Strong);
}
},
.Obj => {},
} }
} }

View File

@ -8378,8 +8378,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
const char *out_type = nullptr; const char *out_type = nullptr;
switch (g->out_type) { switch (g->out_type) {
case OutTypeUnknown: case OutTypeUnknown:
out_type = "Unknown"; zig_unreachable();
break;
case OutTypeExe: case OutTypeExe:
out_type = "Exe"; out_type = "Exe";
break; break;
@ -8390,9 +8389,9 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
out_type = "Obj"; out_type = "Obj";
break; break;
} }
buf_appendf(contents, "pub const output_type = OutType.%s;\n", out_type); buf_appendf(contents, "pub const output_mode = OutputMode.%s;\n", out_type);
const char *link_type = g->is_dynamic ? "Dynamic" : "Static"; const char *link_type = g->is_dynamic ? "Dynamic" : "Static";
buf_appendf(contents, "pub const link_type = LinkType.%s;\n", link_type); buf_appendf(contents, "pub const link_mode = LinkMode.%s;\n", link_type);
buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build)); buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));
buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded)); buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded));
buf_appendf(contents, "pub const os = Os.%s;\n", cur_os); buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);

View File

@ -156,7 +156,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:9:27: error: @atomicRmw on enum only works with .Xchg", "tmp.zig:9:27: error: @atomicRmw on enum only works with .Xchg",
); );
cases.addExe( cases.add(
"disallow coercion from non-null-terminated pointer to null-terminated pointer", "disallow coercion from non-null-terminated pointer to null-terminated pointer",
\\extern fn puts(s: [*:0]const u8) c_int; \\extern fn puts(s: [*:0]const u8) c_int;
\\pub fn main() void { \\pub fn main() void {
@ -876,7 +876,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:3:32: note: cast discards const qualifier", "tmp.zig:3:32: note: cast discards const qualifier",
); );
cases.addExe( cases.add(
"overflow in enum value allocation", "overflow in enum value allocation",
\\const Moo = enum(u8) { \\const Moo = enum(u8) {
\\ Last = 255, \\ Last = 255,
@ -3000,14 +3000,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:3:9: error: encountered @panic at compile-time", "tmp.zig:3:9: error: encountered @panic at compile-time",
); );
cases.addExe( cases.add(
"wrong return type for main", "wrong return type for main",
\\pub fn main() f32 { } \\pub fn main() f32 { }
, ,
"error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'", "error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'",
); );
cases.addExe( cases.add(
"double ?? on main return value", "double ?? on main return value",
\\pub fn main() ??void { \\pub fn main() ??void {
\\} \\}
@ -4900,7 +4900,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:3:13: error: cannot assign to constant", "tmp.zig:3:13: error: cannot assign to constant",
); );
cases.addExe( cases.add(
"main function with bogus args type", "main function with bogus args type",
\\pub fn main(args: [][]bogus) !void {} \\pub fn main(args: [][]bogus) !void {}
, ,
@ -5718,7 +5718,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:4:13: error: cannot continue out of defer expression", "tmp.zig:4:13: error: cannot continue out of defer expression",
); );
cases.addExe( cases.add(
"calling a var args function only known at runtime", "calling a var args function only known at runtime",
\\var foos = [_]fn(...) void { foo1, foo2 }; \\var foos = [_]fn(...) void { foo1, foo2 };
\\ \\
@ -5732,7 +5732,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:7:9: error: calling a generic function requires compile-time known function value", "tmp.zig:7:9: error: calling a generic function requires compile-time known function value",
); );
cases.addExe( cases.add(
"calling a generic function only known at runtime", "calling a generic function only known at runtime",
\\var foos = [_]fn(var) void { foo1, foo2 }; \\var foos = [_]fn(var) void { foo1, foo2 };
\\ \\
@ -6850,7 +6850,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:3:23: error: vector element type must be integer, float, bool, or pointer; 'comptime_int' is invalid", "tmp.zig:3:23: error: vector element type must be integer, float, bool, or pointer; 'comptime_int' is invalid",
); );
cases.addExe("compileLog of tagged enum doesn't crash the compiler", cases.add("compileLog of tagged enum doesn't crash the compiler",
\\const Bar = union(enum(u32)) { \\const Bar = union(enum(u32)) {
\\ X: i32 = 1 \\ X: i32 = 1
\\}; \\};