Commit 33e3db11fe

Andrew Kelley <andrew@ziglang.org>
2022-11-16 07:19:24
zig1.c: autodetect host target triple
instead of assuming x8_64-linux in CMake
1 parent 34e9bbb
Changed files (2)
stage1/zig1.c
@@ -22,6 +22,38 @@
 
 #include <zstd.h>
 
+#if defined(__APPLE__)
+#define ZIG_TRIPLE_OS "macos"
+#elif defined(_WIN32)
+#define ZIG_TRIPLE_OS "windows"
+#elif defined(__linux__)
+#define ZIG_TRIPLE_OS "linux"
+#elif defined(__FreeBSD__)
+#define ZIG_TRIPLE_OS "freebsd"
+#elif defined(__NetBSD__)
+#define ZIG_TRIPLE_OS "netbsd"
+#elif defined(__DragonFly__)
+#define ZIG_TRIPLE_OS "dragonfly"
+#elif defined(__OpenBSD__)
+#define ZIG_TRIPLE_OS "openbsd"
+#elif defined(__HAIKU__)
+#define ZIG_TRIPLE_OS "haiku"
+#elif defined(__sun)
+#define ZIG_TRIPLE_OS "solaris"
+#else
+#error please add more os definitions above this line
+#endif
+
+#if defined(__x86_64__)
+#define ZIG_TRIPLE_ARCH "x86_64"
+#elif defined(__aarch64__)
+#define ZIG_TRIPLE_ARCH "aarch64"
+#elif defined(__ARM_EABI__)
+#define ZIG_TRIPLE_ARCH "arm"
+#else
+#error please add more arch definitions above this line
+#endif
+
 enum wasi_errno_t {
     WASI_ESUCCESS = 0,
     WASI_E2BIG = 1,
@@ -4114,6 +4146,14 @@ int main(int argc, char **argv) {
         new_argv_i += 1;
     }
 
+    {
+        new_argv[new_argv_i] = "-target";
+        new_argv_i += 1;
+
+        new_argv[new_argv_i] = ZIG_TRIPLE_ARCH "-" ZIG_TRIPLE_OS;
+        new_argv_i += 1;
+    }
+
     if (isatty(STDERR_FILENO) != 0) {
         new_argv[new_argv_i] = "--color";
         new_argv_i += 1;
CMakeLists.txt
@@ -731,7 +731,6 @@ set(BUILD_ZIG2_ARGS
   zig2
   "${CMAKE_SOURCE_DIR}/stage1/zig1.wasm.zst"
   build-exe src/main.zig -ofmt=c -lc
-  -target x86_64-linux-musl # TODO: autodetect in zig1.c
   -OReleaseFast
 )
  
@@ -750,7 +749,6 @@ set(BUILD_COMPILER_RT_ARGS
   compiler_rt
   "${CMAKE_SOURCE_DIR}/stage1/zig1.wasm.zst"
   build-obj lib/compiler_rt.zig -ofmt=c
-  -target x86_64-linux-musl # TODO: autodetect in zig1.c
   -OReleaseFast
 )