Skip to content

Commit 0804fa5

Browse files
committed
bootstrap.c: Port to Windows.
1 parent 2a6acc8 commit 0804fa5

File tree

1 file changed

+159
-89
lines changed

1 file changed

+159
-89
lines changed

bootstrap.c

Lines changed: 159 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,92 @@
1+
#include <stdbool.h>
12
#include <stdio.h>
2-
#include <string.h>
33
#include <stdlib.h>
4-
#include <stdbool.h>
4+
#include <string.h>
5+
6+
#if defined(_WIN32)
7+
#include <process.h>
8+
#else
9+
#include <errno.h>
10+
#include <sys/wait.h>
11+
#include <unistd.h>
12+
#endif
513

614
static const char *get_c_compiler(void) {
715
const char *cc = getenv("CC");
8-
return (cc == NULL) ? "cc" : cc;
16+
if (cc != NULL) return cc;
17+
#if defined(_MSC_VER)
18+
return "cl";
19+
#else
20+
return "cc";
21+
#endif
922
}
1023

1124
static void panic(const char *reason) {
1225
fprintf(stderr, "%s\n", reason);
1326
abort();
1427
}
1528

16-
#if defined(__WIN32__)
17-
#error TODO write the functionality for executing child process into this build script
18-
#else
29+
static void error(const char *function) {
30+
perror(function);
31+
exit(1);
32+
}
1933

20-
#include <unistd.h>
21-
#include <errno.h>
22-
#include <sys/wait.h>
34+
static void fatal(const char *message) {
35+
fprintf(stderr, "%s\n", message);
36+
exit(1);
37+
}
38+
39+
static void usage(const char *message) {
40+
fprintf(stderr, "%s\n", message);
41+
exit(2);
42+
}
2343

24-
static void run(char **argv) {
44+
static void run(const char *const *argv) {
45+
#if defined(_WIN32)
46+
intptr_t status = _spawnvp(_P_WAIT, argv[0], argv);
47+
48+
if (status == -1)
49+
error("_spawnv()");
50+
51+
if (status != 0)
52+
fatal("child process failed");
53+
#else
2554
pid_t pid = fork();
26-
if (pid == -1)
27-
panic("fork failed");
28-
if (pid == 0) {
29-
// child
30-
execvp(argv[0], argv);
31-
exit(1);
55+
switch (pid) {
56+
case -1: error("fork()"); break;
57+
case 0: {
58+
// child
59+
execvp(argv[0], (char *const *)argv);
60+
panic("execve() returned");
61+
}
3262
}
3363

3464
// parent
35-
3665
int status;
37-
waitpid(pid, &status, 0);
66+
if (waitpid(pid, &status, 0) == -1)
67+
error("waitpid(child, &status, 0)");
3868

3969
if (!WIFEXITED(status))
40-
panic("child process crashed");
70+
fatal("child process crashed");
4171

4272
if (WEXITSTATUS(status) != 0)
43-
panic("child process failed");
44-
}
73+
fatal("child process failed");
4574
#endif
75+
}
4676

47-
static void print_and_run(const char **argv) {
77+
static void print_and_run(const char *const *argv) {
4878
fprintf(stderr, "%s", argv[0]);
49-
for (const char **arg = argv + 1; *arg; arg += 1) {
79+
for (const char *const *arg = argv + 1; *arg; arg++) {
5080
fprintf(stderr, " %s", *arg);
5181
}
5282
fprintf(stderr, "\n");
53-
run((char **)argv);
83+
run(argv);
5484
}
5585

5686
static const char *get_host_os(void) {
5787
const char *host_os = getenv("ZIG_HOST_TARGET_OS");
5888
if (host_os != NULL) return host_os;
59-
#if defined(__WIN32__)
89+
#if defined(_WIN32)
6090
return "windows";
6191
#elif defined(__APPLE__)
6292
return "macos";
@@ -67,25 +97,30 @@ static const char *get_host_os(void) {
6797
#elif defined(__HAIKU__)
6898
return "haiku";
6999
#else
70-
panic("unknown host os, specify with ZIG_HOST_TARGET_OS");
100+
usage("unknown host OS; specify with environment variable ZIG_HOST_TARGET_OS");
71101
#endif
72102
}
73103

74104
static const char *get_host_arch(void) {
75105
const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
76106
if (host_arch != NULL) return host_arch;
77-
#if defined(__x86_64__ )
107+
#if defined(__x86_64__) || defined(_M_X64)
78108
return "x86_64";
79-
#elif defined(__aarch64__)
109+
#elif defined(__aarch64__) || defined(_M_ARM64)
80110
return "aarch64";
81111
#else
82-
panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
112+
usage("unknown host architecture; specify with environment variable ZIG_HOST_TARGET_ARCH");
83113
#endif
84114
}
85115

86116
static const char *get_host_abi(void) {
87117
const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
88-
return (host_abi == NULL) ? "" : host_abi;
118+
if (host_abi != NULL) return host_abi;
119+
#if defined(_MSC_VER)
120+
return "-msvc";
121+
#else
122+
return "";
123+
#endif
89124
}
90125

91126
static const char *get_host_triple(void) {
@@ -100,28 +135,50 @@ int main(int argc, char **argv) {
100135
const char *cc = get_c_compiler();
101136
const char *host_triple = get_host_triple();
102137

103-
{
104-
const char *child_argv[] = {
105-
cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
106-
};
107-
print_and_run(child_argv);
108-
}
109-
{
110-
const char *child_argv[] = {
111-
"./zig-wasm2c", "stage1/zig1.wasm", "zig1.c", NULL,
112-
};
113-
print_and_run(child_argv);
114-
}
115-
{
116-
const char *child_argv[] = {
117-
cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
118-
};
119-
print_and_run(child_argv);
120-
}
138+
print_and_run((const char *[]) {
139+
cc,
140+
#if defined(_MSC_VER)
141+
"stage1/wasm2c.c",
142+
"/O2",
143+
"/link",
144+
"/OUT:zig-wasm2c.exe",
145+
#else
146+
"stage1/wasm2c.c",
147+
"-std=c99",
148+
"-O2",
149+
"-o", "zig-wasm2c",
150+
#endif
151+
NULL,
152+
});
153+
154+
print_and_run((const char *[]) {
155+
"./zig-wasm2c", "stage1/zig1.wasm", "zig1.c",
156+
NULL,
157+
});
158+
159+
print_and_run((const char *[]) {
160+
cc,
161+
#if defined(_MSC_VER)
162+
"zig1.c",
163+
"stage1/wasi.c",
164+
"/Os",
165+
"/link",
166+
"/OUT:zig1.exe",
167+
#else
168+
"zig1.c",
169+
"stage1/wasi.c",
170+
"-std=c99",
171+
"-Os",
172+
"-lm",
173+
"-o", "zig1",
174+
#endif
175+
NULL,
176+
});
177+
121178
{
122179
FILE *f = fopen("config.zig", "wb");
123180
if (f == NULL)
124-
panic("unable to open config.zig for writing");
181+
error("fopen(\"config.zig\", \"wb\")");
125182

126183
const char *zig_version = "0.14.0-dev.bootstrap";
127184

@@ -145,52 +202,65 @@ int main(int argc, char **argv) {
145202
if (written < 100)
146203
panic("unable to write to config.zig file");
147204
if (fclose(f) != 0)
148-
panic("unable to finish writing to config.zig file");
205+
error("fclose(\"config.zig\")");
149206
}
150207

151-
{
152-
const char *child_argv[] = {
153-
"./zig1", "lib", "build-exe",
154-
"-ofmt=c", "-lc", "-OReleaseSmall",
155-
"--name", "zig2", "-femit-bin=zig2.c",
156-
"-target", host_triple,
157-
"--dep", "build_options",
158-
"--dep", "aro",
159-
"-Mroot=src/main.zig",
160-
"-Mbuild_options=config.zig",
161-
"-Maro=lib/compiler/aro/aro.zig",
162-
NULL,
163-
};
164-
print_and_run(child_argv);
165-
}
208+
print_and_run((const char *[]) {
209+
"./zig1", "lib", "build-exe",
210+
"-OReleaseSmall",
211+
"-target", host_triple,
212+
"-lc",
213+
"-ofmt=c",
214+
"-femit-bin=zig2.c",
215+
"--name", "zig2",
216+
"--dep", "build_options",
217+
"--dep", "aro",
218+
"-Mroot=src/main.zig",
219+
"-Mbuild_options=config.zig",
220+
"-Maro=lib/compiler/aro/aro.zig",
221+
NULL,
222+
});
166223

167-
{
168-
const char *child_argv[] = {
169-
"./zig1", "lib", "build-obj",
170-
"-ofmt=c", "-OReleaseSmall",
171-
"--name", "compiler_rt", "-femit-bin=compiler_rt.c",
172-
"-target", host_triple,
173-
"-Mroot=lib/compiler_rt.zig",
174-
NULL,
175-
};
176-
print_and_run(child_argv);
177-
}
224+
print_and_run((const char *[]) {
225+
"./zig1", "lib", "build-obj",
226+
"-OReleaseSmall",
227+
"-target", host_triple,
228+
"-ofmt=c",
229+
"-femit-bin=compiler_rt.c",
230+
"--name", "compiler_rt",
231+
"-Mroot=lib/compiler_rt.zig",
232+
NULL,
233+
});
178234

179-
{
180-
const char *child_argv[] = {
181-
cc, "-o", "zig2", "zig2.c", "compiler_rt.c",
182-
"-std=c99", "-O2", "-fno-stack-protector",
183-
"-Istage1",
235+
print_and_run((const char *[]) {
236+
cc,
237+
#if defined(_MSC_VER)
238+
"zig2.c",
239+
"compiler_rt.c",
240+
"/Istage1",
241+
"/O2",
242+
"/GS-",
243+
"/Gs0x10000000",
244+
"/link",
245+
"/STACK:0x10000000,0x10000000",
246+
"/OUT:zig2.exe",
247+
#else
248+
"zig2.c",
249+
"compiler_rt.c",
250+
"-Istage1",
251+
"-std=c99",
252+
#if defined(__GNUC__)
253+
"-pthread",
254+
#endif
255+
"-O2",
256+
"-fno-stack-protector",
184257
#if defined(__APPLE__)
185-
"-Wl,-stack_size,0x10000000",
258+
"-Wl,-stack_size,0x10000000",
186259
#else
187-
"-Wl,-z,stack-size=0x10000000",
260+
"-Wl,-z,stack-size=0x10000000",
188261
#endif
189-
#if defined(__GNUC__)
190-
"-pthread",
262+
"-o", "zig2",
191263
#endif
192-
NULL,
193-
};
194-
print_and_run(child_argv);
195-
}
264+
NULL,
265+
});
196266
}

0 commit comments

Comments
 (0)