Skip to content

Commit

Permalink
bootstrap.c: Port to Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrp committed Nov 23, 2024
1 parent 6e63458 commit 78774bf
Showing 1 changed file with 116 additions and 29 deletions.
145 changes: 116 additions & 29 deletions bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,64 @@
#include <stdlib.h>
#include <stdbool.h>

#if defined(_WIN32)

#include <process.h>
#include <windows.h>

#else

#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>

#endif

static const char *get_c_compiler(void) {
const char *cc = getenv("CC");
return (cc == NULL) ? "cc" : cc;
if (cc != NULL) return cc;
#if defined(_MSC_VER)
return "cl";
#else
return "cc";
#endif
}

static void panic(const char *reason) {
fprintf(stderr, "%s\n", reason);
fprintf(
stderr,
"%s (%d)\n",
reason,
#if defined(_WIN32)
(int)GetLastError()
#else
errno
#endif
);
abort();
}

#if defined(__WIN32__)
#error TODO write the functionality for executing child process into this build script
#else
#if defined(_WIN32)

#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
static void run(const char *const *argv) {
intptr_t status = _spawnv(_P_WAIT, argv[0], argv);

if (status == -1)
panic("_spawnv failed");

if (status != 0)
panic("child process failed");
}

#else

static void run(char **argv) {
static void run(const char *const *argv) {
pid_t pid = fork();
if (pid == -1)
panic("fork failed");
if (pid == 0) {
// child
execvp(argv[0], argv);
execvp(argv[0], (char *const *)argv);
exit(1);
}

Expand All @@ -44,19 +77,19 @@ static void run(char **argv) {
}
#endif

static void print_and_run(const char **argv) {
static void print_and_run(const char *const *argv) {
fprintf(stderr, "%s", argv[0]);
for (const char **arg = argv + 1; *arg; arg += 1) {
for (const char *const *arg = argv + 1; *arg; arg += 1) {
fprintf(stderr, " %s", *arg);
}
fprintf(stderr, "\n");
run((char **)argv);
run(argv);
}

static const char *get_host_os(void) {
const char *host_os = getenv("ZIG_HOST_TARGET_OS");
if (host_os != NULL) return host_os;
#if defined(__WIN32__)
#if defined(_WIN32)
return "windows";
#elif defined(__APPLE__)
return "macos";
Expand All @@ -74,9 +107,9 @@ static const char *get_host_os(void) {
static const char *get_host_arch(void) {
const char *host_arch = getenv("ZIG_HOST_TARGET_ARCH");
if (host_arch != NULL) return host_arch;
#if defined(__x86_64__ )
#if defined(__x86_64__) || defined(_M_X64)
return "x86_64";
#elif defined(__aarch64__)
#elif defined(__aarch64__) || defined(_M_ARM64)
return "aarch64";
#else
panic("unknown host arch, specify with ZIG_HOST_TARGET_ARCH");
Expand All @@ -85,7 +118,12 @@ static const char *get_host_arch(void) {

static const char *get_host_abi(void) {
const char *host_abi = getenv("ZIG_HOST_TARGET_ABI");
return (host_abi == NULL) ? "" : host_abi;
if (host_abi != NULL) return host_abi;
#if defined(_MSC_VER)
return "-msvc";
#else
return "";
#endif
}

static const char *get_host_triple(void) {
Expand All @@ -102,7 +140,19 @@ int main(int argc, char **argv) {

{
const char *child_argv[] = {
cc, "-o", "zig-wasm2c", "stage1/wasm2c.c", "-O2", "-std=c99", NULL,
cc,
#if defined(_MSC_VER)
"stage1/wasm2c.c",
"/O2",
"/link",
"/out:zig-wasm2c.exe",
#else
"-std=c99",
"stage1/wasm2c.c",
"-O2",
"-o", "zig-wasm2c",
#endif
NULL,
};
print_and_run(child_argv);
}
Expand All @@ -114,7 +164,22 @@ int main(int argc, char **argv) {
}
{
const char *child_argv[] = {
cc, "-o", "zig1", "zig1.c", "stage1/wasi.c", "-std=c99", "-Os", "-lm", NULL,
cc,
#if defined(_MSC_VER)
"zig1.c",
"stage1/wasi.c",
"/Os",
"/link",
"/out:zig1.exe",
#else
"-std=c99",
"zig1.c",
"stage1/wasi.c",
"-Os",
"-lm",
"-o", "zig1",
#endif
NULL,
};
print_and_run(child_argv);
}
Expand Down Expand Up @@ -151,14 +216,17 @@ int main(int argc, char **argv) {
{
const char *child_argv[] = {
"./zig1", "lib", "build-exe",
"-ofmt=c", "-lc", "-OReleaseSmall",
"--name", "zig2", "-femit-bin=zig2.c",
"-target", host_triple,
"--name", "zig2",
"--dep", "build_options",
"--dep", "aro",
"-Mroot=src/main.zig",
"-Mbuild_options=config.zig",
"-Maro=lib/compiler/aro/aro.zig",
"-OReleaseSmall",
"-target", host_triple,
"-lc",
"-ofmt=c",
"-femit-bin=zig2.c",
NULL,
};
print_and_run(child_argv);
Expand All @@ -167,27 +235,46 @@ int main(int argc, char **argv) {
{
const char *child_argv[] = {
"./zig1", "lib", "build-obj",
"-ofmt=c", "-OReleaseSmall",
"--name", "compiler_rt", "-femit-bin=compiler_rt.c",
"-target", host_triple,
"--name", "compiler_rt",
"-Mroot=lib/compiler_rt.zig",
"-OReleaseSmall",
"-target", host_triple,
"-ofmt=c",
"-femit-bin=compiler_rt.c",
NULL,
};
print_and_run(child_argv);
}

{
const char *child_argv[] = {
cc, "-o", "zig2", "zig2.c", "compiler_rt.c",
"-std=c99", "-O2", "-fno-stack-protector",
cc,
#if defined(_MSC_VER)
"/Istage1",
"zig2.c",
"compiler_rt.c",
"/O2",
"/GS-",
"/Gs0x10000000",
"/link",
"/stack:0x10000000,0x10000000",
"/out:zig2.exe",
#else
"-Istage1",
"-std=c99",
#if defined(__GNUC__)
"-pthread",
#endif
"zig2.c",
"compiler_rt.c",
"-O2",
"-fno-stack-protector",
#if defined(__APPLE__)
"-Wl,-stack_size,0x10000000",
#else
"-Wl,-z,stack-size=0x10000000",
#endif
#if defined(__GNUC__)
"-pthread",
"-o", "zig2",
#endif
NULL,
};
Expand Down

0 comments on commit 78774bf

Please sign in to comment.