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
);
16 #if defined(__WIN32__)
17 #error TODO write the functionality for executing child process into this build script
24 static void run(char **argv
) {
30 execvp(argv
[0], argv
);
37 waitpid(pid
, &status
, 0);
39 if (!WIFEXITED(status
))
40 panic("child process crashed");
42 if (WEXITSTATUS(status
) != 0)
43 panic("child process failed");
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");
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__)
61 #elif defined(__APPLE__)
63 #elif defined(__linux__)
65 #elif defined(__FreeBSD__)
67 #elif defined(__HAIKU__)
70 panic("unknown host os, specify with ZIG_HOST_TARGET_OS");
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__ )
79 #elif defined(__aarch64__)
82 panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
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());
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");
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"
147 panic("unable to write to config.zig file");
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",
160 "--mod", "root", "src/main.zig",
161 "--mod", "build_options", "config.zig",
162 "--mod", "aro", "lib/compiler/aro/aro.zig",
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",
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",
187 #if defined(__APPLE__)
188 "-Wl,-stack_size,0x10000000",
190 "-Wl,-z,stack-size=0x10000000",
192 #if defined(__GNUC__)
197 print_and_run(child_argv
);