Skip to content

Commit f67e87e

Browse files
committed
bootstrap.c: Port to Windows.
1 parent 6e63458 commit f67e87e

File tree

1 file changed

+98
-24
lines changed

1 file changed

+98
-24
lines changed

bootstrap.c

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,49 @@
55

66
static const char *get_c_compiler(void) {
77
const char *cc = getenv("CC");
8-
return (cc == NULL) ? "cc" : cc;
8+
if (cc != NULL) return cc;
9+
#if defined(_MSC_VER)
10+
return "cl";
11+
#else
12+
return "cc";
13+
#endif
914
}
1015

1116
static void panic(const char *reason) {
1217
fprintf(stderr, "%s\n", reason);
1318
abort();
1419
}
1520

16-
#if defined(__WIN32__)
17-
#error TODO write the functionality for executing child process into this build script
21+
#if defined(_WIN32)
22+
23+
#include <process.h>
24+
#include <windows.h>
25+
26+
static void run(const char *const *argv) {
27+
intptr_t status = _spawnv(_P_WAIT, argv[0], argv);
28+
29+
if (status == -1)
30+
panic("_spawnv failed");
31+
32+
printf("%d\n", GetLastError());
33+
34+
if (status != 0)
35+
panic("child process failed");
36+
}
37+
1838
#else
1939

2040
#include <unistd.h>
2141
#include <errno.h>
2242
#include <sys/wait.h>
2343

24-
static void run(char **argv) {
44+
static void run(const char *const *argv) {
2545
pid_t pid = fork();
2646
if (pid == -1)
2747
panic("fork failed");
2848
if (pid == 0) {
2949
// child
30-
execvp(argv[0], argv);
50+
execvp(argv[0], (char *const *)argv);
3151
exit(1);
3252
}
3353

@@ -44,19 +64,19 @@ static void run(char **argv) {
4464
}
4565
#endif
4666

47-
static void print_and_run(const char **argv) {
67+
static void print_and_run(const char *const *argv) {
4868
fprintf(stderr, "%s", argv[0]);
49-
for (const char **arg = argv + 1; *arg; arg += 1) {
69+
for (const char *const *arg = argv + 1; *arg; arg += 1) {
5070
fprintf(stderr, " %s", *arg);
5171
}
5272
fprintf(stderr, "\n");
53-
run((char **)argv);
73+
run(argv);
5474
}
5575

5676
static const char *get_host_os(void) {
5777
const char *host_os = getenv("ZIG_HOST_TARGET_OS");
5878
if (host_os != NULL) return host_os;
59-
#if defined(__WIN32__)
79+
#if defined(_WIN32)
6080
return "windows";
6181
#elif defined(__APPLE__)
6282
return "macos";
@@ -74,9 +94,9 @@ static const char *get_host_os(void) {
7494
static const char *get_host_arch(void) {
7595
const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
7696
if (host_arch != NULL) return host_arch;
77-
#if defined(__x86_64__ )
97+
#if defined(__x86_64__) || defined(_M_X64)
7898
return "x86_64";
79-
#elif defined(__aarch64__)
99+
#elif defined(__aarch64__) || defined(_M_ARM64)
80100
return "aarch64";
81101
#else
82102
panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
@@ -85,7 +105,12 @@ static const char *get_host_arch(void) {
85105

86106
static const char *get_host_abi(void) {
87107
const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
88-
return (host_abi == NULL) ? "" : host_abi;
108+
if (host_abi != NULL) return host_abi;
109+
#if defined(_MSC_VER)
110+
return "-msvc";
111+
#else
112+
return "";
113+
#endif
89114
}
90115

91116
static const char *get_host_triple(void) {
@@ -102,7 +127,19 @@ int main(int argc, char **argv) {
102127

103128
{
104129
const char *child_argv[] = {
105-
cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
130+
cc,
131+
#if defined(_MSC_VER)
132+
"stage1/wasm2c.c",
133+
"/O2",
134+
"/link",
135+
"/out:zig-wasm2c.exe",
136+
#else
137+
"-std=c99",
138+
"stage1/wasm2c.c",
139+
"-O2",
140+
"-o", "zig-wasm2c",
141+
#endif
142+
NULL,
106143
};
107144
print_and_run(child_argv);
108145
}
@@ -114,7 +151,22 @@ int main(int argc, char **argv) {
114151
}
115152
{
116153
const char *child_argv[] = {
117-
cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
154+
cc,
155+
#if defined(_MSC_VER)
156+
"zig1.c",
157+
"stage1/wasi.c",
158+
"/Os",
159+
"/link",
160+
"/out:zig1.exe",
161+
#else
162+
"-std=c99",
163+
"zig1.c",
164+
"stage1/wasi.c",
165+
"-Os",
166+
"-lm",
167+
"-o", "zig1",
168+
#endif
169+
NULL,
118170
};
119171
print_and_run(child_argv);
120172
}
@@ -151,14 +203,17 @@ int main(int argc, char **argv) {
151203
{
152204
const char *child_argv[] = {
153205
"./zig1", "lib", "build-exe",
154-
"-ofmt=c", "-lc", "-OReleaseSmall",
155-
"--name", "zig2", "-femit-bin=zig2.c",
156-
"-target", host_triple,
206+
"--name", "zig2",
157207
"--dep", "build_options",
158208
"--dep", "aro",
159209
"-Mroot=src/main.zig",
160210
"-Mbuild_options=config.zig",
161211
"-Maro=lib/compiler/aro/aro.zig",
212+
"-OReleaseSmall",
213+
"-target", host_triple,
214+
"-lc",
215+
"-ofmt=c",
216+
"-femit-bin=zig2.c",
162217
NULL,
163218
};
164219
print_and_run(child_argv);
@@ -167,27 +222,46 @@ int main(int argc, char **argv) {
167222
{
168223
const char *child_argv[] = {
169224
"./zig1", "lib", "build-obj",
170-
"-ofmt=c", "-OReleaseSmall",
171-
"--name", "compiler_rt", "-femit-bin=compiler_rt.c",
172-
"-target", host_triple,
225+
"--name", "compiler_rt",
173226
"-Mroot=lib/compiler_rt.zig",
227+
"-OReleaseSmall",
228+
"-target", host_triple,
229+
"-ofmt=c",
230+
"-femit-bin=compiler_rt.c",
174231
NULL,
175232
};
176233
print_and_run(child_argv);
177234
}
178235

179236
{
180237
const char *child_argv[] = {
181-
cc, "-o", "zig2", "zig2.c", "compiler_rt.c",
182-
"-std=c99", "-O2", "-fno-stack-protector",
238+
cc,
239+
#if defined(_MSC_VER)
240+
"/Istage1",
241+
"zig2.c",
242+
"compiler_rt.c",
243+
"/O2",
244+
"/GS-",
245+
"/Gs0x10000000",
246+
"/link",
247+
"/stack:0x10000000,0x10000000",
248+
"/out:zig2.exe",
249+
#else
183250
"-Istage1",
251+
"-std=c99",
252+
#if defined(__GNUC__)
253+
"-pthread",
254+
#endif
255+
"zig2.c",
256+
"compiler_rt.c",
257+
"-O2",
258+
"-fno-stack-protector",
184259
#if defined(__APPLE__)
185260
"-Wl,-stack_size,0x10000000",
186261
#else
187262
"-Wl,-z,stack-size=0x10000000",
188263
#endif
189-
#if defined(__GNUC__)
190-
"-pthread",
264+
"-o", "zig2",
191265
#endif
192266
NULL,
193267
};

0 commit comments

Comments
 (0)