authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-18 13:57:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-18 14:04:48-04:00
log237dfdbdc6f83071cff88489cc66cb83a2d65b00
tree6aade70d5b9fa2ec5706dcd7c82e4c63822a6b2f
parent6f0f8a92ec7cf7eac8c05469a46398f47885614e

error when building exe with no entry point

closes #30

5 files changed, 58 insertions(+), 3 deletions(-)

src/all_types.hpp+1
......@@ -1362,6 +1362,7 @@ struct CodeGen {
13621362 bool strip_debug_symbols;
13631363 bool want_h_file;
13641364 bool have_pub_main;
1365 bool have_c_main;
13651366 bool have_pub_panic;
13661367 bool link_libc;
13671368 Buf *libc_lib_dir;
src/analyze.cpp+5
......@@ -2911,7 +2911,12 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package,
29112911 } else if (buf_eql_str(proto_name, "panic")) {
29122912 g->have_pub_panic = true;
29132913 }
2914 } else if (proto_node->data.fn_proto.visib_mod == VisibModExport && buf_eql_str(proto_name, "main") &&
2915 g->link_libc)
2916 {
2917 g->have_c_main = true;
29142918 }
2919
29152920 }
29162921 }
29172922
src/codegen.cpp+3-1
......@@ -4726,7 +4726,9 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou
47264726 assert(g->root_out_name);
47274727 assert(g->out_type != OutTypeUnknown);
47284728
4729 if (!g->is_test_build && g->have_pub_main && (g->out_type == OutTypeObj || g->out_type == OutTypeExe)) {
4729 if (!g->is_test_build && g->zig_target.os != ZigLLVM_UnknownOS && !g->have_c_main &&
4730 ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe))
4731 {
47304732 g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g), "bootstrap.zig");
47314733 }
47324734 if (!g->omit_zigrt) {
std/special/bootstrap.zig+2-2
......@@ -12,8 +12,8 @@ const exit = std.os.posix.exit;
1212var argc_ptr: &usize = undefined;
1313
1414export nakedcc fn _start() -> noreturn {
15 @setGlobalLinkage(_start, if (want_start_symbol) GlobalLinkage.Strong else GlobalLinkage.Internal);
1615 if (!want_start_symbol) {
16 @setGlobalLinkage(_start, GlobalLinkage.Internal);
1717 unreachable;
1818 }
1919
......@@ -48,8 +48,8 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
4848}
4949
5050export fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 {
51 @setGlobalLinkage(main, if (want_main_symbol) GlobalLinkage.Strong else GlobalLinkage.Internal);
5251 if (!want_main_symbol) {
52 @setGlobalLinkage(main, GlobalLinkage.Internal);
5353 unreachable;
5454 }
5555
test/run_tests.cpp+47
......@@ -161,6 +161,38 @@ static TestCase *add_compile_fail_case(const char *case_name, const char *source
161161 return test_case;
162162}
163163
164static TestCase *add_compile_fail_case_exe(const char *case_name, const char *source, size_t count, ...) {
165 va_list ap;
166 va_start(ap, count);
167
168 TestCase *test_case = allocate<TestCase>(1);
169 test_case->case_name = case_name;
170 test_case->source_files.resize(1);
171 test_case->source_files.at(0).relative_path = tmp_source_path;
172 test_case->source_files.at(0).source_code = source;
173
174 for (size_t i = 0; i < count; i += 1) {
175 const char *arg = va_arg(ap, const char *);
176 test_case->compile_errors.append(arg);
177 }
178
179 test_case->compiler_args.append("build_exe");
180 test_case->compiler_args.append(tmp_source_path);
181
182 test_case->compiler_args.append("--name");
183 test_case->compiler_args.append("test");
184
185 test_case->compiler_args.append("--output");
186 test_case->compiler_args.append(tmp_exe_path);
187
188 test_case->compiler_args.append("--release");
189 test_case->compiler_args.append("--strip");
190
191 test_cases.append(test_case);
192
193 return test_case;
194}
195
164196static void add_debug_safety_case(const char *case_name, const char *source) {
165197 TestCase *test_case = allocate<TestCase>(1);
166198 test_case->is_debug_safety = true;
......@@ -220,6 +252,11 @@ static TestCase *add_example_compile_extra(const char *root_source_file, bool li
220252 test_case->compiler_args.append("build_exe");
221253 test_case->compiler_args.append(buf_ptr(buf_sprintf("../%s", root_source_file)));
222254
255 if (libc) {
256 test_case->compiler_args.append("--library");
257 test_case->compiler_args.append("c");
258 }
259
223260 test_cases.append(test_case);
224261
225262 return test_case;
......@@ -1952,6 +1989,16 @@ comptime {
19521989 const another_foo_ptr = @fieldParentPtr(Foo, "b", &foo.a);
19531990}
19541991 )SOURCE", 1, ".tmp_source.zig:9:29: error: field 'b' has index 1 but pointer value is index 0 of struct 'Foo'");
1992
1993 add_compile_fail_case_exe("missing main fn in executable", R"SOURCE(
1994 )SOURCE", 1, "error: no member named 'main' in '");
1995
1996 add_compile_fail_case_exe("private main fn", R"SOURCE(
1997fn main() {}
1998 )SOURCE", 2,
1999 "error: 'main' is private",
2000 ".tmp_source.zig:2:1: note: declared here");
2001
19552002}
19562003
19572004//////////////////////////////////////////////////////////////////////////////