zig

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

blob fdcae4e1 (5610B) - 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 #else
     68     panic("unknown host os, specify with ZIG_HOST_TARGET_OS");
     69 #endif
     70 }
     71 
     72 static const char *get_host_arch(void) {
     73     const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
     74     if (host_arch != NULL) return host_arch;
     75 #if defined(__x86_64__ )
     76     return "x86_64";
     77 #elif defined(__aarch64__)
     78     return "aarch64";
     79 #else
     80     panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
     81 #endif
     82 }
     83 
     84 static const char *get_host_abi(void) {
     85     const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
     86     return (host_abi == NULL) ? "" : host_abi;
     87 }
     88 
     89 static const char *get_host_triple(void) {
     90     const char *host_triple = getenv("ZIG_HOST_TARGET_TRIPLE");
     91     if (host_triple != NULL) return host_triple;
     92     static char global_buffer[100];
     93     sprintf(global_buffer, "%s-%s%s", get_host_arch(), get_host_os(), get_host_abi());
     94     return global_buffer;
     95 }
     96 
     97 int main(int argc, char **argv) {
     98     const char *cc = get_c_compiler();
     99     const char *host_triple = get_host_triple();
    100 
    101     {
    102         const char *child_argv[] = {
    103             cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
    104         };
    105         print_and_run(child_argv);
    106     }
    107     {
    108         const char *child_argv[] = {
    109             "./zig-wasm2c", "stage1/zig1.wasm", "zig1.c", NULL,
    110         };
    111         print_and_run(child_argv);
    112     }
    113     {
    114         const char *child_argv[] = {
    115             cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
    116         };
    117         print_and_run(child_argv);
    118     }
    119     {
    120         FILE *f = fopen("config.zig", "wb");
    121         if (f == NULL)
    122             panic("unable to open config.zig for writing");
    123 
    124         const char *zig_version = "0.12.0-dev.bootstrap";
    125 
    126         int written = fprintf(f,
    127             "pub const have_llvm = false;\n"
    128             "pub const llvm_has_m68k = false;\n"
    129             "pub const llvm_has_csky = false;\n"
    130             "pub const llvm_has_arc = false;\n"
    131             "pub const llvm_has_xtensa = false;\n"
    132             "pub const version: [:0]const u8 = \"%s\";\n"
    133             "pub const semver = @import(\"std\").SemanticVersion.parse(version) catch unreachable;\n"
    134             "pub const enable_debug_extensions = false;\n"
    135             "pub const enable_logging = false;\n"
    136             "pub const enable_link_snapshots = false;\n"
    137             "pub const enable_tracy = false;\n"
    138             "pub const value_tracing = false;\n"
    139             "pub const skip_non_native = false;\n"
    140             "pub const force_gpa = false;\n"
    141             "pub const only_c = false;\n"
    142             "pub const only_core_functionality = true;\n"
    143         , zig_version);
    144         if (written < 100)
    145             panic("unable to write to config.zig file");
    146         if (fclose(f) != 0)
    147             panic("unable to finish writing to config.zig file");
    148     }
    149 
    150     {
    151         const char *child_argv[] = {
    152             "./zig1", "lib", "build-exe",
    153             "-ofmt=c", "-lc", "-OReleaseSmall",
    154             "--name", "zig2", "-femit-bin=zig2.c",
    155             "-target", host_triple,
    156             "--dep", "build_options",
    157             "--dep", "aro",
    158             "--mod", "root", "src/main.zig",
    159             "--mod", "build_options", "config.zig",
    160             "--mod", "aro", "lib/compiler/aro/aro.zig",
    161             NULL,
    162         };
    163         print_and_run(child_argv);
    164     }
    165 
    166     {
    167         const char *child_argv[] = {
    168             "./zig1", "lib", "build-obj",
    169             "-ofmt=c", "-OReleaseSmall",
    170             "--name", "compiler_rt", "-femit-bin=compiler_rt.c",
    171             "-target", host_triple,
    172             "--dep", "build_options",
    173             "--mod", "root", "lib/compiler_rt.zig",
    174             "--mod", "build_options", "config.zig",
    175             NULL,
    176         };
    177         print_and_run(child_argv);
    178     }
    179 
    180     {
    181         const char *child_argv[] = {
    182             cc, "-o", "zig2", "zig2.c", "compiler_rt.c",
    183             "-std=c99", "-O2", "-fno-stack-protector",
    184             "-Istage1",
    185 #if defined(__APPLE__)
    186             "-Wl,-stack_size,0x10000000",
    187 #else
    188             "-Wl,-z,stack-size=0x10000000",
    189 #endif
    190 #if defined(__GNUC__)
    191             "-pthread",
    192 #endif
    193             NULL,
    194         };
    195         print_and_run(child_argv);
    196     }
    197 }