std.process.Child: Mitigate arbitrary command execution vulnerability on Windows...
[zig.git] / bootstrap.c
blob529d3f737af3f438a3cd1a75f743220a68b10fe1
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
6 static const char *get_c_compiler(void) {
7 const char *cc = getenv("CC");
8 return (cc == NULL) ? "cc" : cc;
11 static void panic(const char *reason) {
12 fprintf(stderr, "%s\n", reason);
13 abort();
16 #if defined(__WIN32__)
17 #error TODO write the functionality for executing child process into this build script
18 #else
20 #include <unistd.h>
21 #include <errno.h>
22 #include <sys/wait.h>
24 static void run(char **argv) {
25 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);
34 // parent
36 int status;
37 waitpid(pid, &status, 0);
39 if (!WIFEXITED(status))
40 panic("child process crashed");
42 if (WEXITSTATUS(status) != 0)
43 panic("child process failed");
45 #endif
47 static void print_and_run(const char **argv) {
48 fprintf(stderr, "%s", argv[0]);
49 for (const char **arg = argv + 1; *arg; arg += 1) {
50 fprintf(stderr, " %s", *arg);
52 fprintf(stderr, "\n");
53 run((char **)argv);
56 static const char *get_host_os(void) {
57 const char *host_os = getenv("ZIG_HOST_TARGET_OS");
58 if (host_os != NULL) return host_os;
59 #if defined(__WIN32__)
60 return "windows";
61 #elif defined(__APPLE__)
62 return "macos";
63 #elif defined(__linux__)
64 return "linux";
65 #elif defined(__FreeBSD__)
66 return "freebsd";
67 #elif defined(__HAIKU__)
68 return "haiku";
69 #else
70 panic("unknown host os, specify with ZIG_HOST_TARGET_OS");
71 #endif
74 static const char *get_host_arch(void) {
75 const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
76 if (host_arch != NULL) return host_arch;
77 #if defined(__x86_64__ )
78 return "x86_64";
79 #elif defined(__aarch64__)
80 return "aarch64";
81 #else
82 panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
83 #endif
86 static const char *get_host_abi(void) {
87 const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
88 return (host_abi == NULL) ? "" : host_abi;
91 static const char *get_host_triple(void) {
92 const char *host_triple = getenv("ZIG_HOST_TARGET_TRIPLE");
93 if (host_triple != NULL) return host_triple;
94 static char global_buffer[100];
95 sprintf(global_buffer, "%s-%s%s", get_host_arch(), get_host_os(), get_host_abi());
96 return global_buffer;
99 int main(int argc, char **argv) {
100 const char *cc = get_c_compiler();
101 const char *host_triple = get_host_triple();
104 const char *child_argv[] = {
105 cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
107 print_and_run(child_argv);
110 const char *child_argv[] = {
111 "./zig-wasm2c", "stage1/zig1.wasm", "zig1.c", NULL,
113 print_and_run(child_argv);
116 const char *child_argv[] = {
117 cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
119 print_and_run(child_argv);
122 FILE *f = fopen("config.zig", "wb");
123 if (f == NULL)
124 panic("unable to open config.zig for writing");
126 const char *zig_version = "0.12.0-dev.bootstrap";
128 int written = fprintf(f,
129 "pub const have_llvm = false;\n"
130 "pub const llvm_has_m68k = false;\n"
131 "pub const llvm_has_csky = false;\n"
132 "pub const llvm_has_arc = false;\n"
133 "pub const llvm_has_xtensa = false;\n"
134 "pub const version: [:0]const u8 = \"%s\";\n"
135 "pub const semver = @import(\"std\").SemanticVersion.parse(version) catch unreachable;\n"
136 "pub const enable_debug_extensions = false;\n"
137 "pub const enable_logging = false;\n"
138 "pub const enable_link_snapshots = false;\n"
139 "pub const enable_tracy = false;\n"
140 "pub const value_tracing = false;\n"
141 "pub const skip_non_native = false;\n"
142 "pub const force_gpa = false;\n"
143 "pub const only_c = false;\n"
144 "pub const only_core_functionality = true;\n"
145 , zig_version);
146 if (written < 100)
147 panic("unable to write to config.zig file");
148 if (fclose(f) != 0)
149 panic("unable to finish writing to config.zig file");
153 const char *child_argv[] = {
154 "./zig1", "lib", "build-exe",
155 "-ofmt=c", "-lc", "-OReleaseSmall",
156 "--name", "zig2", "-femit-bin=zig2.c",
157 "-target", host_triple,
158 "--dep", "build_options",
159 "--dep", "aro",
160 "--mod", "root", "src/main.zig",
161 "--mod", "build_options", "config.zig",
162 "--mod", "aro", "lib/compiler/aro/aro.zig",
163 NULL,
165 print_and_run(child_argv);
169 const char *child_argv[] = {
170 "./zig1", "lib", "build-obj",
171 "-ofmt=c", "-OReleaseSmall",
172 "--name", "compiler_rt", "-femit-bin=compiler_rt.c",
173 "-target", host_triple,
174 "--dep", "build_options",
175 "--mod", "root", "lib/compiler_rt.zig",
176 "--mod", "build_options", "config.zig",
177 NULL,
179 print_and_run(child_argv);
183 const char *child_argv[] = {
184 cc, "-o", "zig2", "zig2.c", "compiler_rt.c",
185 "-std=c99", "-O2", "-fno-stack-protector",
186 "-Istage1",
187 #if defined(__APPLE__)
188 "-Wl,-stack_size,0x10000000",
189 #else
190 "-Wl,-z,stack-size=0x10000000",
191 #endif
192 #if defined(__GNUC__)
193 "-pthread",
194 #endif
195 NULL,
197 print_and_run(child_argv);