621e89a8
Andrew Kelley
#include <stdio.h>
621e89a8
Andrew Kelley
#include <string.h>
621e89a8
Andrew Kelley
#include <stdlib.h>
621e89a8
Andrew Kelley
#include <stdbool.h>
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
static const char *get_c_compiler(void) {
621e89a8
Andrew Kelley
const char *cc = getenv("CC");
621e89a8
Andrew Kelley
return (cc == NULL) ? "cc" : cc;
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
static void panic(const char *reason) {
621e89a8
Andrew Kelley
fprintf(stderr, "%s\n", reason);
621e89a8
Andrew Kelley
abort();
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
#if defined(__WIN32__)
621e89a8
Andrew Kelley
#error TODO write the functionality for executing child process into this build script
621e89a8
Andrew Kelley
#else
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
#include <unistd.h>
621e89a8
Andrew Kelley
#include <errno.h>
621e89a8
Andrew Kelley
#include <sys/wait.h>
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
static void run(char **argv) {
621e89a8
Andrew Kelley
pid_t pid = fork();
621e89a8
Andrew Kelley
if (pid == -1)
621e89a8
Andrew Kelley
panic("fork failed");
621e89a8
Andrew Kelley
if (pid == 0) {
621e89a8
Andrew Kelley
// child
621e89a8
Andrew Kelley
execvp(argv[0], argv);
621e89a8
Andrew Kelley
exit(1);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
// parent
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
int status;
621e89a8
Andrew Kelley
waitpid(pid, &status, 0);
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
if (!WIFEXITED(status))
621e89a8
Andrew Kelley
panic("child process crashed");
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
if (WEXITSTATUS(status) != 0)
621e89a8
Andrew Kelley
panic("child process failed");
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
#endif
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
static void print_and_run(const char **argv) {
621e89a8
Andrew Kelley
fprintf(stderr, "%s", argv[0]);
621e89a8
Andrew Kelley
for (const char **arg = argv + 1; *arg; arg += 1) {
621e89a8
Andrew Kelley
fprintf(stderr, " %s", *arg);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
fprintf(stderr, "\n");
621e89a8
Andrew Kelley
run((char **)argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
static const char *get_host_os(void) {
5b803aec
Jacob Young
const char *host_os = getenv("ZIG_HOST_TARGET_OS");
5b803aec
Jacob Young
if (host_os != NULL) return host_os;
621e89a8
Andrew Kelley
#if defined(__WIN32__)
621e89a8
Andrew Kelley
return "windows";
621e89a8
Andrew Kelley
#elif defined(__APPLE__)
621e89a8
Andrew Kelley
return "macos";
621e89a8
Andrew Kelley
#elif defined(__linux__)
621e89a8
Andrew Kelley
return "linux";
f36ac227
Lateef Jackson
#elif defined(__FreeBSD__)
f36ac227
Lateef Jackson
return "freebsd";
2dd74cd3
Jacob Young
#elif defined(__HAIKU__)
2dd74cd3
Jacob Young
return "haiku";
621e89a8
Andrew Kelley
#else
5b803aec
Jacob Young
panic("unknown host os, specify with ZIG_HOST_TARGET_OS");
621e89a8
Andrew Kelley
#endif
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
static const char *get_host_arch(void) {
5b803aec
Jacob Young
const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
5b803aec
Jacob Young
if (host_arch != NULL) return host_arch;
621e89a8
Andrew Kelley
#if defined(__x86_64__ )
621e89a8
Andrew Kelley
return "x86_64";
621e89a8
Andrew Kelley
#elif defined(__aarch64__)
621e89a8
Andrew Kelley
return "aarch64";
621e89a8
Andrew Kelley
#else
5b803aec
Jacob Young
panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
621e89a8
Andrew Kelley
#endif
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
5b803aec
Jacob Young
static const char *get_host_abi(void) {
5b803aec
Jacob Young
const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
5b803aec
Jacob Young
return (host_abi == NULL) ? "" : host_abi;
5b803aec
Jacob Young
}
5b803aec
Jacob Young
621e89a8
Andrew Kelley
static const char *get_host_triple(void) {
5b803aec
Jacob Young
const char *host_triple = getenv("ZIG_HOST_TARGET_TRIPLE");
5b803aec
Jacob Young
if (host_triple != NULL) return host_triple;
621e89a8
Andrew Kelley
static char global_buffer[100];
5b803aec
Jacob Young
sprintf(global_buffer, "%s-%s%s", get_host_arch(), get_host_os(), get_host_abi());
621e89a8
Andrew Kelley
return global_buffer;
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
int main(int argc, char **argv) {
621e89a8
Andrew Kelley
const char *cc = get_c_compiler();
621e89a8
Andrew Kelley
const char *host_triple = get_host_triple();
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
const char *child_argv[] = {
621e89a8
Andrew Kelley
cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
621e89a8
Andrew Kelley
};
621e89a8
Andrew Kelley
print_and_run(child_argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
const char *child_argv[] = {
621e89a8
Andrew Kelley
"./zig-wasm2c", "stage1/zig1.wasm", "zig1.c", NULL,
621e89a8
Andrew Kelley
};
621e89a8
Andrew Kelley
print_and_run(child_argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
const char *child_argv[] = {
621e89a8
Andrew Kelley
cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
621e89a8
Andrew Kelley
};
621e89a8
Andrew Kelley
print_and_run(child_argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
FILE *f = fopen("config.zig", "wb");
621e89a8
Andrew Kelley
if (f == NULL)
621e89a8
Andrew Kelley
panic("unable to open config.zig for writing");
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
const char *zig_version = "0.12.0-dev.bootstrap";
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
int written = fprintf(f,
621e89a8
Andrew Kelley
"pub const have_llvm = false;\n"
621e89a8
Andrew Kelley
"pub const llvm_has_m68k = false;\n"
621e89a8
Andrew Kelley
"pub const llvm_has_csky = false;\n"
621e89a8
Andrew Kelley
"pub const llvm_has_arc = false;\n"
621e89a8
Andrew Kelley
"pub const llvm_has_xtensa = false;\n"
621e89a8
Andrew Kelley
"pub const version: [:0]const u8 = \"%s\";\n"
621e89a8
Andrew Kelley
"pub const semver = @import(\"std\").SemanticVersion.parse(version) catch unreachable;\n"
b60fc16b
Jacob Young
"pub const enable_debug_extensions = false;\n"
b60fc16b
Jacob Young
"pub const enable_logging = false;\n"
b60fc16b
Jacob Young
"pub const enable_link_snapshots = false;\n"
621e89a8
Andrew Kelley
"pub const enable_tracy = false;\n"
621e89a8
Andrew Kelley
"pub const value_tracing = false;\n"
621e89a8
Andrew Kelley
"pub const skip_non_native = false;\n"
621e89a8
Andrew Kelley
"pub const force_gpa = false;\n"
b60fc16b
Jacob Young
"pub const only_c = false;\n"
621e89a8
Andrew Kelley
"pub const only_core_functionality = true;\n"
621e89a8
Andrew Kelley
, zig_version);
621e89a8
Andrew Kelley
if (written < 100)
621e89a8
Andrew Kelley
panic("unable to write to config.zig file");
621e89a8
Andrew Kelley
if (fclose(f) != 0)
621e89a8
Andrew Kelley
panic("unable to finish writing to config.zig file");
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
const char *child_argv[] = {
8cf2cfc5
Andrew Kelley
"./zig1", "lib", "build-exe",
621e89a8
Andrew Kelley
"-ofmt=c", "-lc", "-OReleaseSmall",
621e89a8
Andrew Kelley
"--name", "zig2", "-femit-bin=zig2.c",
621e89a8
Andrew Kelley
"-target", host_triple,
8cf2cfc5
Andrew Kelley
"--dep", "build_options",
8cf2cfc5
Andrew Kelley
"--dep", "aro",
8cf2cfc5
Andrew Kelley
"--mod", "root", "src/main.zig",
8cf2cfc5
Andrew Kelley
"--mod", "build_options", "config.zig",
240d0b68
Andrew Kelley
"--mod", "aro", "lib/compiler/aro/aro.zig",
621e89a8
Andrew Kelley
NULL,
621e89a8
Andrew Kelley
};
621e89a8
Andrew Kelley
print_and_run(child_argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
const char *child_argv[] = {
8cf2cfc5
Andrew Kelley
"./zig1", "lib", "build-obj",
621e89a8
Andrew Kelley
"-ofmt=c", "-OReleaseSmall",
621e89a8
Andrew Kelley
"--name", "compiler_rt", "-femit-bin=compiler_rt.c",
621e89a8
Andrew Kelley
"-target", host_triple,
8cf2cfc5
Andrew Kelley
"--dep", "build_options",
8cf2cfc5
Andrew Kelley
"--mod", "root", "lib/compiler_rt.zig",
8cf2cfc5
Andrew Kelley
"--mod", "build_options", "config.zig",
621e89a8
Andrew Kelley
NULL,
621e89a8
Andrew Kelley
};
621e89a8
Andrew Kelley
print_and_run(child_argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
621e89a8
Andrew Kelley
{
621e89a8
Andrew Kelley
const char *child_argv[] = {
621e89a8
Andrew Kelley
cc, "-o", "zig2", "zig2.c", "compiler_rt.c",
621e89a8
Andrew Kelley
"-std=c99", "-O2", "-fno-stack-protector",
557cb646
Andrew Kelley
"-Istage1",
557cb646
Andrew Kelley
#if defined(__APPLE__)
557cb646
Andrew Kelley
"-Wl,-stack_size,0x10000000",
557cb646
Andrew Kelley
#else
557cb646
Andrew Kelley
"-Wl,-z,stack-size=0x10000000",
557cb646
Andrew Kelley
#endif
557cb646
Andrew Kelley
#if defined(__GNUC__)
557cb646
Andrew Kelley
"-pthread",
557cb646
Andrew Kelley
#endif
557cb646
Andrew Kelley
NULL,
621e89a8
Andrew Kelley
};
621e89a8
Andrew Kelley
print_and_run(child_argv);
621e89a8
Andrew Kelley
}
621e89a8
Andrew Kelley
}