Commit: 371a04e929cc6f8cd609cc22cbcc7e346dbc68b3
Parent: 51ea75ce373adbf12ce6952f010fe35e5cd9a603
Author: Randy Palamar
Date: Tue, 20 May 2025 21:01:23 -0600
core: add compiler.h for compiler/os/arch detection
Diffstat:
6 files changed, 93 insertions(+), 48 deletions(-)
diff --git a/build.c b/build.c
@@ -19,26 +19,13 @@
#include <stdarg.h>
#include <stdio.h>
-#define is_aarch64 0
-#define is_amd64 0
-#define is_unix 0
-#define is_w32 0
-#define is_clang 0
-
-#ifdef __ARM_ARCH_ISA_A64
-#undef is_aarch64
-#define is_aarch64 1
-#elif __x86_64__
-#undef is_amd64
-#define is_amd64 1
-#else
-#error unsupported architecture
-#endif
+#define is_aarch64 ARCH_ARM64
+#define is_amd64 ARCH_X64
+#define is_unix OS_LINUX
+#define is_w32 OS_WINDOWS
+#define is_clang COMPILER_CLANG
-#if defined(__linux__)
-
- #undef is_unix
- #define is_unix 1
+#if OS_LINUX
#include <errno.h>
#include <string.h>
@@ -50,10 +37,7 @@
#define OS_SHARED_LIB(s) s ".so"
#define OS_MAIN "main_linux.c"
-#elif defined(_WIN32)
-
- #undef is_w32
- #define is_w32 1
+#elif OS_WINDOWS
#include "os_win32.c"
@@ -64,12 +48,12 @@
#error Unsupported Platform
#endif
-#ifdef __clang__
-#undef is_clang
-#define is_clang 1
-#define COMPILER "clang"
+#if COMPILER_CLANG
+ #define COMPILER "clang"
+#elif COMPILER_MSVC
+ #define COMPILER "cl"
#else
-#define COMPILER "cc"
+ #define COMPILER "cc"
#endif
#define shift(list, count) ((count)--, *(list)++)
@@ -135,7 +119,7 @@ stream_push_command(Stream *s, CommandList *c)
}
}
-#if defined(__unix__)
+#if OS_LINUX
function b32
os_rename_file(char *name, char *new)
@@ -211,7 +195,7 @@ os_wait_close_process(iptr handle)
return result;
}
-#elif defined(_WIN32)
+#elif OS_WINDOWS
enum {
MOVEFILE_REPLACE_EXISTING = 0x01,
diff --git a/compiler.h b/compiler.h
@@ -0,0 +1,63 @@
+/* See LICENSE for license details. */
+#ifndef COMPILER_H
+#define COMPILER_H
+
+#if defined(__linux__)
+ #define OS_LINUX 1
+#elif defined(_WIN32)
+ #define OS_WINDOWS 1
+#else
+ #error Unsupported Operating System
+#endif
+
+#ifdef __clang__
+ #define COMPILER_CLANG 1
+#elif _MSC_VER
+ #define COMPILER_MSVC 1
+#elif __GNUC__
+ #define COMPILER_GCC 1
+#else
+ #error Unsupported Compiler
+#endif
+
+#if COMPILER_MSVC
+ #if defined(_M_AMD64)
+ #define ARCH_X64 1
+ #elif defined(_M_ARM64)
+ #define ARCH_ARM64 1
+ #else
+ #error Unsupported Architecture
+ #endif
+#else
+ #if defined(__x86_64__)
+ #define ARCH_X64 1
+ #elif defined(__aarch64__)
+ #define ARCH_ARM64 1
+ #else
+ #error Unsupported Architecture
+ #endif
+#endif
+
+#if !defined(OS_WINDOWS)
+ #define OS_WINDOWS 0
+#endif
+#if !defined(OS_LINUX)
+ #define OS_LINUX 0
+#endif
+#if !defined(COMPILER_CLANG)
+ #define COMPILER_CLANG 0
+#endif
+#if !defined(COMPILER_MSVC)
+ #define COMPILER_MSVC 0
+#endif
+#if !defined(COMPILER_GCC)
+ #define COMPILER_GCC 0
+#endif
+#if !defined(ARCH_X64)
+ #define ARCH_X64 0
+#endif
+#if !defined(ARCH_ARM64)
+ #define ARCH_ARM64 0
+#endif
+
+#endif /* COMPILER_H */
diff --git a/helpers/ogl_beamformer_lib.c b/helpers/ogl_beamformer_lib.c
@@ -1,4 +1,6 @@
/* See LICENSE for license details. */
+#include "../compiler.h"
+
#include "../util.h"
#include "../beamformer_parameters.h"
#include "ogl_beamformer_lib_base.h"
@@ -9,9 +11,9 @@
global BeamformerSharedMemory *g_bp;
global BeamformerLibErrorKind g_lib_last_error;
-#if defined(__linux__)
+#if OS_LINUX
#include "../os_linux.c"
-#elif defined(_WIN32)
+#elif OS_WINDOWS
#include "../os_win32.c"
#define PIPE_TYPE_BYTE 0x00
@@ -33,7 +35,7 @@ W32(void) Sleep(u32);
#error Unsupported Platform
#endif
-#if defined(__linux__)
+#if OS_LINUX
function Pipe
os_open_read_pipe(char *name)
@@ -84,7 +86,7 @@ os_open_shared_memory_area(char *name)
return result;
}
-#elif defined(_WIN32)
+#elif OS_WINDOWS
/* TODO(rnp): temporary workaround */
function OS_WAIT_ON_VALUE_FN(os_wait_on_value_stub)
diff --git a/intrinsics.c b/intrinsics.c
@@ -1,13 +1,5 @@
/* See LICENSE for license details. */
-#ifdef __clang__
- #define COMPILER_CLANG 1
-#elif _MSC_VER
- #define COMPILER_MSVC 1
-#elif __GNUC__
- #define COMPILER_GCC 1
-#else
- #error Unsupported Compiler
-#endif
+#include "compiler.h"
#if COMPILER_CLANG || COMPILER_GCC
#define force_inline inline __attribute__((always_inline))
@@ -15,7 +7,7 @@
#define force_inline __forceinline
#endif
-#if COMPILER_MSVC || (COMPILER_CLANG && _WIN32)
+#if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS)
#pragma section(".rdata$", read)
#define read_only __declspec(allocate(".rdata$"))
#elif COMPILER_CLANG
@@ -54,7 +46,7 @@ ctz_u32(u32 a)
return result;
}
-#ifdef __ARM_ARCH_ISA_A64
+#if ARCH_ARM64
/* TODO? debuggers just loop here forever and need a manual PC increment (step over) */
#define debugbreak() asm volatile ("brk 0xf000")
@@ -79,7 +71,7 @@ typedef int32x4_t i32x4;
#define store_f32x4(a, o) vst1q_f32(o, a)
#define store_i32x4(a, o) vst1q_s32(o, a)
-#elif __x86_64__
+#elif ARCH_X64
#include <immintrin.h>
typedef __m128 f32x4;
typedef __m128i i32x4;
diff --git a/main_linux.c b/main_linux.c
@@ -1,5 +1,7 @@
/* See LICENSE for license details. */
-#ifndef __linux__
+#include "compiler.h"
+
+#if !OS_LINUX
#error This file is only meant to be compiled for Linux
#endif
diff --git a/main_w32.c b/main_w32.c
@@ -1,5 +1,7 @@
/* See LICENSE for license details. */
-#ifndef _WIN32
+#include "compiler.h"
+
+#if !OS_WINDOWS
#error This file is only meant to be compiled for Win32
#endif