zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

bootstrap.c (5575B) - Raw


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <stdbool.h>
      5 
      6 static const char *get_c_compiler(void) {
      7     const char *cc = getenv("CC");
      8     return (cc == NULL) ? "cc" : cc;
      9 }
     10 
     11 static void panic(const char *reason) {
     12     fprintf(stderr, "%s\n", reason);
     13     abort();
     14 }
     15 
     16 #if defined(__WIN32__)
     17 #error TODO write the functionality for executing child process into this build script
     18 #else
     19 
     20 #include <unistd.h>
     21 #include <errno.h>
     22 #include <sys/wait.h>
     23 
     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);
     32     }
     33 
     34     // parent
     35 
     36     int status;
     37     waitpid(pid, &status, 0);
     38 
     39     if (!WIFEXITED(status))
     40         panic("child process crashed");
     41 
     42     if (WEXITSTATUS(status) != 0)
     43         panic("child process failed");
     44 }
     45 #endif
     46 
     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);
     51     }
     52     fprintf(stderr, "\n");
     53     run((char **)argv);
     54 }
     55 
     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(__DragonFly__)
     68     return "dragonfly";
     69 #elif defined(__HAIKU__)
     70     return "haiku";
     71 #else
     72     panic("unknown host os, specify with ZIG_HOST_TARGET_OS");
     73 #endif
     74 }
     75 
     76 static const char *get_host_arch(void) {
     77     const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
     78     if (host_arch != NULL) return host_arch;
     79 #if defined(__x86_64__ )
     80     return "x86_64";
     81 #elif defined(__aarch64__)
     82     return "aarch64";
     83 #else
     84     panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
     85 #endif
     86 }
     87 
     88 static const char *get_host_abi(void) {
     89     const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
     90     return (host_abi == NULL) ? "" : host_abi;
     91 }
     92 
     93 static const char *get_host_triple(void) {
     94     const char *host_triple = getenv("ZIG_HOST_TARGET_TRIPLE");
     95     if (host_triple != NULL) return host_triple;
     96     static char global_buffer[100];
     97     sprintf(global_buffer, "%s-%s%s", get_host_arch(), get_host_os(), get_host_abi());
     98     return global_buffer;
     99 }
    100 
    101 int main(int argc, char **argv) {
    102     const char *cc = get_c_compiler();
    103     const char *host_triple = get_host_triple();
    104 
    105     {
    106         const char *child_argv[] = {
    107             cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
    108         };
    109         print_and_run(child_argv);
    110     }
    111     {
    112         const char *child_argv[] = {
    113             "./zig-wasm2c", "stage1/zig1.wasm", "zig1.c", NULL,
    114         };
    115         print_and_run(child_argv);
    116     }
    117     {
    118         const char *child_argv[] = {
    119             cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
    120         };
    121         print_and_run(child_argv);
    122     }
    123     {
    124         FILE *f = fopen("config.zig", "wb");
    125         if (f == NULL)
    126             panic("unable to open config.zig for writing");
    127 
    128         const char *zig_version = "0.14.0-dev.bootstrap";
    129 
    130         int written = fprintf(f,
    131             "pub const have_llvm = false;\n"
    132             "pub const llvm_has_m68k = false;\n"
    133             "pub const llvm_has_csky = false;\n"
    134             "pub const llvm_has_arc = false;\n"
    135             "pub const llvm_has_xtensa = false;\n"
    136             "pub const version: [:0]const u8 = \"%s\";\n"
    137             "pub const semver = @import(\"std\").SemanticVersion.parse(version) catch unreachable;\n"
    138             "pub const enable_debug_extensions = false;\n"
    139             "pub const enable_logging = false;\n"
    140             "pub const enable_link_snapshots = false;\n"
    141             "pub const enable_tracy = false;\n"
    142             "pub const value_tracing = false;\n"
    143             "pub const skip_non_native = false;\n"
    144             "pub const debug_gpa = false;\n"
    145             "pub const dev = .core;\n"
    146             "pub const value_interpret_mode = .direct;\n"
    147         , zig_version);
    148         if (written < 100)
    149             panic("unable to write to config.zig file");
    150         if (fclose(f) != 0)
    151             panic("unable to finish writing to config.zig file");
    152     }
    153 
    154     {
    155         const char *child_argv[] = {
    156             "./zig1", "lib", "build-exe",
    157             "-ofmt=c", "-lc", "-OReleaseSmall",
    158             "--name", "zig2", "-femit-bin=zig2.c",
    159             "-target", host_triple,
    160             "--dep", "build_options",
    161             "--dep", "aro",
    162             "-Mroot=src/main.zig",
    163             "-Mbuild_options=config.zig",
    164             "-Maro=lib/compiler/aro/aro.zig",
    165             NULL,
    166         };
    167         print_and_run(child_argv);
    168     }
    169 
    170     {
    171         const char *child_argv[] = {
    172             "./zig1", "lib", "build-obj",
    173             "-ofmt=c", "-OReleaseSmall",
    174             "--name", "compiler_rt", "-femit-bin=compiler_rt.c",
    175             "-target", host_triple,
    176             "-Mroot=lib/compiler_rt.zig",
    177             NULL,
    178         };
    179         print_and_run(child_argv);
    180     }
    181 
    182     {
    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,
    196         };
    197         print_and_run(child_argv);
    198     }
    199 }