33#include <stdlib.h>
44#include <stdbool.h>
55
6+ #if defined(_WIN32 )
7+
8+ #include <process.h>
9+ #include <windows.h>
10+
11+ #else
12+
13+ #include <unistd.h>
14+ #include <errno.h>
15+ #include <sys/wait.h>
16+
17+ #endif
18+
619static const char * get_c_compiler (void ) {
720 const char * cc = getenv ("CC" );
8- return (cc == NULL ) ? "cc" : cc ;
21+ if (cc != NULL ) return cc ;
22+ #if defined(_MSC_VER )
23+ return "cl" ;
24+ #else
25+ return "cc" ;
26+ #endif
927}
1028
1129static void panic (const char * reason ) {
12- fprintf (stderr , "%s\n" , reason );
30+ fprintf (
31+ stderr ,
32+ "%s (%d)\n" ,
33+ reason ,
34+ #if defined(_WIN32 )
35+ (int )GetLastError ()
36+ #else
37+ errno
38+ #endif
39+ );
1340 abort ();
1441}
1542
16- #if defined(__WIN32__ )
17- #error TODO write the functionality for executing child process into this build script
18- #else
43+ #if defined(_WIN32 )
1944
20- #include <unistd.h>
21- #include <errno.h>
22- #include <sys/wait.h>
45+ static void run (const char * const * argv ) {
46+ intptr_t status = _spawnv (_P_WAIT , argv [0 ], argv );
47+
48+ if (status == -1 )
49+ panic ("_spawnv failed" );
50+
51+ if (status != 0 )
52+ panic ("child process failed" );
53+ }
54+
55+ #else
2356
24- static void run (char * * argv ) {
57+ static void run (const char * const * argv ) {
2558 pid_t pid = fork ();
2659 if (pid == -1 )
2760 panic ("fork failed" );
2861 if (pid == 0 ) {
2962 // child
30- execvp (argv [0 ], argv );
63+ execvp (argv [0 ], ( char * const * ) argv );
3164 exit (1 );
3265 }
3366
@@ -44,19 +77,19 @@ static void run(char **argv) {
4477}
4578#endif
4679
47- static void print_and_run (const char * * argv ) {
80+ static void print_and_run (const char * const * argv ) {
4881 fprintf (stderr , "%s" , argv [0 ]);
49- for (const char * * arg = argv + 1 ; * arg ; arg += 1 ) {
82+ for (const char * const * arg = argv + 1 ; * arg ; arg += 1 ) {
5083 fprintf (stderr , " %s" , * arg );
5184 }
5285 fprintf (stderr , "\n" );
53- run (( char * * ) argv );
86+ run (argv );
5487}
5588
5689static const char * get_host_os (void ) {
5790 const char * host_os = getenv ("ZIG_HOST_TARGET_OS" );
5891 if (host_os != NULL ) return host_os ;
59- #if defined(__WIN32__ )
92+ #if defined(_WIN32 )
6093 return "windows" ;
6194#elif defined(__APPLE__ )
6295 return "macos" ;
@@ -74,9 +107,9 @@ static const char *get_host_os(void) {
74107static const char * get_host_arch (void ) {
75108 const char * host_arch = getenv ("ZIG_HOST_TARGET_ARCH" );
76109 if (host_arch != NULL ) return host_arch ;
77- #if defined(__x86_64__ )
110+ #if defined(__x86_64__ ) || defined( _M_X64 )
78111 return "x86_64" ;
79- #elif defined(__aarch64__ )
112+ #elif defined(__aarch64__ ) || defined( _M_ARM64 )
80113 return "aarch64" ;
81114#else
82115 panic ("unknown host arch, specify with ZIG_HOST_TARGET_ARCH" );
@@ -85,7 +118,12 @@ static const char *get_host_arch(void) {
85118
86119static const char * get_host_abi (void ) {
87120 const char * host_abi = getenv ("ZIG_HOST_TARGET_ABI" );
88- return (host_abi == NULL ) ? "" : host_abi ;
121+ if (host_abi != NULL ) return host_abi ;
122+ #if defined(_MSC_VER )
123+ return "-msvc" ;
124+ #else
125+ return "" ;
126+ #endif
89127}
90128
91129static const char * get_host_triple (void ) {
@@ -102,7 +140,19 @@ int main(int argc, char **argv) {
102140
103141 {
104142 const char * child_argv [] = {
105- cc , "-o" , "zig-wasm2c" , "stage1/wasm2c.c" , "-O2" , "-std=c99" , NULL ,
143+ cc ,
144+ #if defined(_MSC_VER )
145+ "stage1/wasm2c.c" ,
146+ "/O2" ,
147+ "/link" ,
148+ "/out:zig-wasm2c.exe" ,
149+ #else
150+ "-std=c99" ,
151+ "stage1/wasm2c.c" ,
152+ "-O2" ,
153+ "-o" , "zig-wasm2c" ,
154+ #endif
155+ NULL ,
106156 };
107157 print_and_run (child_argv );
108158 }
@@ -114,7 +164,22 @@ int main(int argc, char **argv) {
114164 }
115165 {
116166 const char * child_argv [] = {
117- cc , "-o" , "zig1" , "zig1.c" , "stage1/wasi.c" , "-std=c99" , "-Os" , "-lm" , NULL ,
167+ cc ,
168+ #if defined(_MSC_VER )
169+ "zig1.c" ,
170+ "stage1/wasi.c" ,
171+ "/Os" ,
172+ "/link" ,
173+ "/out:zig1.exe" ,
174+ #else
175+ "-std=c99" ,
176+ "zig1.c" ,
177+ "stage1/wasi.c" ,
178+ "-Os" ,
179+ "-lm" ,
180+ "-o" , "zig1" ,
181+ #endif
182+ NULL ,
118183 };
119184 print_and_run (child_argv );
120185 }
@@ -151,14 +216,17 @@ int main(int argc, char **argv) {
151216 {
152217 const char * child_argv [] = {
153218 "./zig1" , "lib" , "build-exe" ,
154- "-ofmt=c" , "-lc" , "-OReleaseSmall" ,
155- "--name" , "zig2" , "-femit-bin=zig2.c" ,
156- "-target" , host_triple ,
219+ "--name" , "zig2" ,
157220 "--dep" , "build_options" ,
158221 "--dep" , "aro" ,
159222 "-Mroot=src/main.zig" ,
160223 "-Mbuild_options=config.zig" ,
161224 "-Maro=lib/compiler/aro/aro.zig" ,
225+ "-OReleaseSmall" ,
226+ "-target" , host_triple ,
227+ "-lc" ,
228+ "-ofmt=c" ,
229+ "-femit-bin=zig2.c" ,
162230 NULL ,
163231 };
164232 print_and_run (child_argv );
@@ -167,27 +235,46 @@ int main(int argc, char **argv) {
167235 {
168236 const char * child_argv [] = {
169237 "./zig1" , "lib" , "build-obj" ,
170- "-ofmt=c" , "-OReleaseSmall" ,
171- "--name" , "compiler_rt" , "-femit-bin=compiler_rt.c" ,
172- "-target" , host_triple ,
238+ "--name" , "compiler_rt" ,
173239 "-Mroot=lib/compiler_rt.zig" ,
240+ "-OReleaseSmall" ,
241+ "-target" , host_triple ,
242+ "-ofmt=c" ,
243+ "-femit-bin=compiler_rt.c" ,
174244 NULL ,
175245 };
176246 print_and_run (child_argv );
177247 }
178248
179249 {
180250 const char * child_argv [] = {
181- cc , "-o" , "zig2" , "zig2.c" , "compiler_rt.c" ,
182- "-std=c99" , "-O2" , "-fno-stack-protector" ,
251+ cc ,
252+ #if defined(_MSC_VER )
253+ "/Istage1" ,
254+ "zig2.c" ,
255+ "compiler_rt.c" ,
256+ "/O2" ,
257+ "/GS-" ,
258+ "/Gs0x10000000" ,
259+ "/link" ,
260+ "/stack:0x10000000,0x10000000" ,
261+ "/out:zig2.exe" ,
262+ #else
183263 "-Istage1" ,
264+ "-std=c99" ,
265+ #if defined(__GNUC__ )
266+ "-pthread" ,
267+ #endif
268+ "zig2.c" ,
269+ "compiler_rt.c" ,
270+ "-O2" ,
271+ "-fno-stack-protector" ,
184272#if defined(__APPLE__ )
185273 "-Wl,-stack_size,0x10000000" ,
186274#else
187275 "-Wl,-z,stack-size=0x10000000" ,
188276#endif
189- #if defined(__GNUC__ )
190- "-pthread" ,
277+ "-o" , "zig2" ,
191278#endif
192279 NULL ,
193280 };
0 commit comments