authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-24 03:54:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-24 03:55:45-04:00
logba41be67f02158df964543d21b30c83f1b17550d
tree79532b92c9309e669fc7c96420e1efe9b7dd1f44
parent41b588547cd49673ff632ed0fc3210c76ff48b42

windows gui hello world


6 files changed, 43 insertions(+), 24 deletions(-)

example/hello_world/hello_windows.zig created+6
......@@ -0,0 +1,6 @@
1use @import("std").os.windows;
2
3export fn WinMain(hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: PWSTR, nCmdShow: INT) -> INT {
4 _ = MessageBoxA(null, c"hello", c"title", 0);
5 return 0;
6}
src/all_types.hpp+1
......@@ -1451,6 +1451,7 @@ struct CodeGen {
14511451 bool want_h_file;
14521452 bool have_pub_main;
14531453 bool have_c_main;
1454 bool have_winmain;
14541455 bool have_pub_panic;
14551456 Buf *libc_lib_dir;
14561457 Buf *libc_static_lib_dir;
src/analyze.cpp+6
......@@ -3200,6 +3200,12 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a
32003200 g->have_c_main = true;
32013201 g->windows_subsystem_windows = false;
32023202 g->windows_subsystem_console = true;
3203 } else if (proto_node->data.fn_proto.visib_mod == VisibModExport && buf_eql_str(proto_name, "WinMain") &&
3204 g->zig_target.os == ZigLLVM_Win32)
3205 {
3206 g->have_winmain = true;
3207 g->windows_subsystem_windows = true;
3208 g->windows_subsystem_console = false;
32033209 }
32043210
32053211 }
src/codegen.cpp+1-1
......@@ -5157,7 +5157,7 @@ static void gen_root_source(CodeGen *g) {
51575157 assert(g->root_out_name);
51585158 assert(g->out_type != OutTypeUnknown);
51595159
5160 if (!g->is_test_build && g->zig_target.os != ZigLLVM_UnknownOS && !g->have_c_main &&
5160 if (!g->is_test_build && g->zig_target.os != ZigLLVM_UnknownOS && !g->have_c_main && !g->have_winmain &&
51615161 ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe))
51625162 {
51635163 g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig");
src/link.cpp+23-22
......@@ -398,7 +398,11 @@ static void construct_linker_job_coff(LinkJob *lj) {
398398 //lj->args.append(get_libc_static_file(g, "crtbegin.o"));
399399 } else {
400400 lj->args.append("-NODEFAULTLIB");
401 lj->args.append("-ENTRY:_start");
401 if (g->have_winmain) {
402 lj->args.append("-ENTRY:WinMain");
403 } else {
404 lj->args.append("-ENTRY:_start");
405 }
402406 }
403407
404408 for (size_t i = 0; i < g->lib_dirs.length; i += 1) {
......@@ -424,6 +428,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
424428 }
425429
426430 Buf *def_contents = buf_alloc();
431 ZigList<const char *> gen_lib_args = {0};
427432 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
428433 LinkLib *link_lib = g->link_libs_list.at(lib_i);
429434 if (buf_eql_str(link_lib->name, "c")) {
......@@ -433,38 +438,34 @@ static void construct_linker_job_coff(LinkJob *lj) {
433438 Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name));
434439 lj->args.append(buf_ptr(arg));
435440 } else {
441 buf_resize(def_contents, 0);
436442 buf_appendf(def_contents, "LIBRARY %s\nEXPORTS\n", buf_ptr(link_lib->name));
437443 for (size_t exp_i = 0; exp_i < link_lib->symbols.length; exp_i += 1) {
438444 Buf *symbol_name = link_lib->symbols.at(exp_i);
439445 buf_appendf(def_contents, "%s\n", buf_ptr(symbol_name));
440446 }
441447 buf_appendf(def_contents, "\n");
442 }
443 }
444 if (buf_len(def_contents) != 0) {
445 Buf *def_path = buf_alloc();
446 os_path_join(g->cache_dir, buf_create_from_str("all.def"), def_path);
447 os_write_file(def_path, def_contents);
448448
449 Buf *all_lib_path = buf_alloc();
450 os_path_join(g->cache_dir, buf_create_from_str("all.lib"), all_lib_path);
449 Buf *def_path = buf_alloc();
450 os_path_join(g->cache_dir, buf_sprintf("%s.def", buf_ptr(link_lib->name)), def_path);
451 os_write_file(def_path, def_contents);
451452
452 //Buf *dll_path = buf_alloc();
453 //os_path_join(g->cache_dir, buf_create_from_str("all.dll"), dll_path);
453 Buf *generated_lib_path = buf_alloc();
454 os_path_join(g->cache_dir, buf_sprintf("%s.lib", buf_ptr(link_lib->name)), generated_lib_path);
454455
455 ZigList<const char *> args = {0};
456 args.append("link");
456 gen_lib_args.resize(0);
457 gen_lib_args.append("link");
457458
458 coff_append_machine_arg(g, &args);
459 args.append(buf_ptr(buf_sprintf("-DEF:%s", buf_ptr(def_path))));
460 args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(all_lib_path))));
461 Buf diag = BUF_INIT;
462 if (!ZigLLDLink(g->zig_target.oformat, args.items, args.length, &diag)) {
463 fprintf(stderr, "%s\n", buf_ptr(&diag));
464 exit(1);
459 coff_append_machine_arg(g, &gen_lib_args);
460 gen_lib_args.append(buf_ptr(buf_sprintf("-DEF:%s", buf_ptr(def_path))));
461 gen_lib_args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(generated_lib_path))));
462 Buf diag = BUF_INIT;
463 if (!ZigLLDLink(g->zig_target.oformat, gen_lib_args.items, gen_lib_args.length, &diag)) {
464 fprintf(stderr, "%s\n", buf_ptr(&diag));
465 exit(1);
466 }
467 lj->args.append(buf_ptr(generated_lib_path));
465468 }
466
467 lj->args.append(buf_ptr(all_lib_path));
468469 }
469470
470471 if (g->libc_link_lib != nullptr) {
std/os/windows/index.zig+6-1
......@@ -39,6 +39,9 @@ pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &con
3939
4040pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD);
4141
42
43pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) -> c_int;
44
4245pub const PROV_RSA_FULL = 1;
4346
4447pub const UNICODE = false;
......@@ -46,13 +49,14 @@ pub const LPTSTR = if (unicode) LPWSTR else LPSTR;
4649pub const LPWSTR = &WCHAR;
4750pub const LPSTR = &CHAR;
4851pub const CHAR = u8;
49
52pub const PWSTR = &WCHAR;
5053
5154pub const BOOL = bool;
5255pub const BYTE = u8;
5356pub const DWORD = u32;
5457pub const FLOAT = f32;
5558pub const HANDLE = &c_void;
59pub const HINSTANCE = &@OpaqueType();
5660pub const HCRYPTPROV = ULONG_PTR;
5761pub const LPCTSTR = &const TCHAR;
5862pub const LPDWORD = &DWORD;
......@@ -60,6 +64,7 @@ pub const LPVOID = &c_void;
6064pub const PVOID = &c_void;
6165pub const TCHAR = if (UNICODE) WCHAR else u8;
6266pub const UINT = c_uint;
67pub const INT = c_int;
6368pub const ULONG_PTR = usize;
6469pub const WCHAR = u16;
6570pub const LPCVOID = &const c_void;