platform_cracker.h (11193B)
1 ////////////////////////////////////////////////////////////////// 2 // platform_cracker.h 3 4 ////////////////////////////////////////////////////////////////// 5 // Sets if not previously defined: 6 // RV_DEBUG = 1 7 // _RV_DEFINE_TRUE_FALSE = 1 8 // RV_GLOBAL = RV_INTERNAL 9 // RV_ARENA_FREE_LIST = 1 10 // RV_RENDER_OPENGL = 1 11 // 12 // Defines: 13 // Codebase Keywords: 14 // RV_INTERNAL 15 // RV_LOCAL_PERSIST 16 // 17 // Constructs: 18 // RV_C_LINKAGE_BEGIN 19 // RV_C_LINKAGE_END 20 // RV_THREAD_LOCAL 21 // RV_FORCE_INLINE 22 // RV_READ_ONLY 23 // rv_assert(...) 24 // rv_unreachable() = rv_assert(false) 25 // rv_alignof(T) 26 // true=1, false=0 (C only, optional) 27 // 28 // Address sanitizer: 29 // RV_ASAN_ENABLED 30 // RV_NO_ASAN 31 // RV_ASAN_POISON(ptr, sz) 32 // RV_ASAN_UNPOISON(ptr, sz) 33 // 34 // Language: 35 // RV_LANG_C 36 // RV_LANG_CPP 37 // RV_CPP_VERSION 38 // 39 // Operating System: 40 // RV_OS_MAC 41 // RV_OS_LINUX 42 // RV_OS_WINDOWS 43 // 44 // Window System: 45 // RV_WIN_ENABLED 46 // RV_WIN_X11 (default on linux) 47 // RV_WIN_WINDOWS (not implemented) 48 // RV_WIN_WAYLAND (not implemented) 49 // RV_WIN_GLFW (default otherwise) 50 // 51 // Compiler: 52 // RV_COMPILER_GCC 53 // RV_COMPILER_CLANG 54 // RV_COMPILER_CL 55 // RV_COMPILER_CL_YEAR 56 // 57 // Architecture: 58 // RV_ARCH_X64 59 // RV_ARCH_X86 60 // RV_ARCH_ARM64 61 // RV_ARCH_ARM32 62 // 63 // Register size: 64 // RV_ARCH_64BIT 65 // RV_ARCH_32BIT 66 67 ////////////////////////////////////////////////////////////////// 68 // Sets if not previously defined 69 70 #if !defined(_RV_DEFINE_TRUE_FALSE) 71 #define _RV_DEFINE_TRUE_FALSE 1 72 #endif 73 74 #if !defined(RV_DEBUG) 75 #define RV_DEBUG 1 76 #endif 77 78 #if !defined(RV_ARENA_FREE_LIST) 79 #define RV_ARENA_FREE_LIST 1 80 #endif 81 82 #if !defined(RV_RENDER_OPENGL) 83 #define RV_RENDER_OPENGL 1 84 #endif 85 86 ////////////////////////////////////////////////////////////////// 87 // Defines 88 89 #define RV_INTERNAL static 90 #define RV_LOCAL_PERSIST static 91 #if !defined(RV_GLOBAL) 92 #define RV_GLOBAL RV_INTERNAL 93 #endif 94 95 96 #if defined(__clang__) 97 98 #define RV_COMPILER_CLANG 1 99 100 #if defined(__APPLE__) && defined(__MACH__) 101 #define RV_OS_MAC 1 102 #elif defined(__gnu_linux__) 103 #define RV_OS_LINUX 1 104 #elif defined(_WIN32) 105 #define RV_OS_WINDOWS 1 106 #else 107 #error This compiler/platform combo is not supported yet 108 #endif 109 110 #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) 111 #define RV_ARCH_X64 1 112 #elif defined(i386) || defined(__i386) || defined(__i386__) 113 #define RV_ARCH_X86 1 114 #elif defined(__aarch64__) 115 #define RV_ARCH_ARM64 1 116 #elif defined(__arm__) 117 #define RV_ARCH_ARM32 1 118 #else 119 #error architecture not supported yet 120 #endif 121 122 #elif defined(_MSC_VER) 123 124 #define RV_COMPILER_CL 1 125 126 #if defined(_WIN32) 127 #define RV_OS_WINDOWS 1 128 #else 129 #error This compiler/platform combo is not supported yet 130 #endif 131 132 #if defined(_M_AMD64) 133 #define RV_ARCH_X64 1 134 #elif defined(_M_IX86) 135 #define RV_ARCH_X86 1 136 #elif defined(_M_ARM64) 137 #define RV_ARCH_ARM64 1 138 #elif defined(_M_ARM) 139 #define RV_ARCH_ARM32 1 140 #else 141 #error architecture not supported yet 142 #endif 143 144 #if _MSC_VER >= 1920 145 #define RV_COMPILER_CL_YEAR 2019 146 #elif _MSC_VER >= 1910 147 #define RV_COMPILER_CL_YEAR 2017 148 #elif _MSC_VER >= 1900 149 #define RV_COMPILER_CL_YEAR 2015 150 #elif _MSC_VER >= 1800 151 #define RV_COMPILER_CL_YEAR 2013 152 #elif _MSC_VER >= 1700 153 #define RV_COMPILER_CL_YEAR 2012 154 #elif _MSC_VER >= 1600 155 #define RV_COMPILER_CL_YEAR 2010 156 #elif _MSC_VER >= 1500 157 #define RV_COMPILER_CL_YEAR 2008 158 #elif _MSC_VER >= 1400 159 #define RV_COMPILER_CL_YEAR 2005 160 #else 161 #define RV_COMPILER_CL_YEAR 0 162 #endif 163 164 #elif defined(__GNUC__) || defined(__GNUG__) 165 166 #define RV_COMPILER_GCC 1 167 168 #if defined(__gnu_linux__) 169 #define RV_OS_LINUX 1 170 #else 171 #error This compiler/platform combo is not supported yet 172 #endif 173 174 #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) 175 #define RV_ARCH_X64 1 176 #elif defined(i386) || defined(__i386) || defined(__i386__) 177 #define RV_ARCH_X86 1 178 #elif defined(__aarch64__) 179 #define RV_ARCH_ARM64 1 180 #elif defined(__arm__) 181 #define RV_ARCH_ARM32 1 182 #else 183 #error architecture not supported yet 184 #endif 185 186 #else 187 #error This compiler is not supported yet 188 #endif 189 190 #if defined(RV_ARCH_X64) 191 #define RV_ARCH_64BIT 1 192 #elif defined(RV_ARCH_X86) 193 #define RV_ARCH_32BIT 1 194 #endif 195 196 #if defined(__cplusplus) 197 #define RV_LANG_CPP 1 198 199 // We can't get this 100% correct thanks to Microsoft's compiler. 200 // So this check lets us pre-define RV_CPP_VERSION if we have to. 201 #if !defined(RV_CPP_VERSION) 202 #if defined(RV_COMPILER_CL) 203 // CL is annoying and didn't update __cplusplus over time 204 // If it is available _MSVC_LANG serves the same role 205 #if defined(_MSVC_LANG) 206 #if _MSVC_LANG <= 199711L 207 #define RV_CPP_VERSION 98 208 #elif _MSVC_LANG <= 201103L 209 #define RV_CPP_VERSION 11 210 #elif _MSVC_LANG <= 201402L 211 #define RV_CPP_VERSION 14 212 #elif _MSVC_LANG <= 201703L 213 #define RV_CPP_VERSION 17 214 #elif _MSVC_LANG <= 202002L 215 #define RV_CPP_VERSION 20 216 #else 217 #define RV_CPP_VERSION 23 218 #endif 219 // If we don't have _MSVC_LANG we can guess from the compiler version 220 #else 221 #if RV_COMPILER_CL_YEAR <= 2010 222 #define RV_CPP_VERSION 98 223 #elif RV_COMPILER_CL_YEAR <= 2015 224 #define RV_CPP_VERSION 11 225 #else 226 #define RV_CPP_VERSION 17 227 # endif 228 #endif 229 #else 230 // Other compilers use __cplusplus correctly 231 #if __cplusplus <= 199711L 232 #define RV_CPP_VERSION 98 233 #elif __cplusplus <= 201103L 234 #define RV_CPP_VERSION 11 235 #elif __cplusplus <= 201402L 236 #define RV_CPP_VERSION 14 237 #elif __cplusplus <= 201703L 238 #define RV_CPP_VERSION 17 239 #elif __cplusplus <= 202002L 240 #define RV_CPP_VERSION 20 241 #else 242 #define RV_CPP_VERSION 23 243 #endif 244 #endif 245 #endif 246 247 #else 248 #define RV_LANG_C 1 249 #endif 250 251 #if COMPILER_CLANG 252 #if defined(__SANITIZE_ADDRESS__) 253 #define RV_ASAN_ENABLED 1 254 #define RV_NO_ASAN __declspec(no_sanitize_address) 255 #else 256 #define RV_NO_ASAN 257 #endif 258 #elif RV_COMPILER_CLANG || RV_COMPILER_GCC 259 #if defined(__has_feature) 260 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 261 #define RV_ASAN_ENABLED 1 262 #endif 263 #endif 264 #define RV_NO_ASAN __attribute__((no_sanitize("address"))) 265 #else 266 #define RV_NO_ASAN 267 #endif 268 269 // Zeroify 270 271 #if !defined(RV_ASAN_ENABLED) 272 #define RV_ASAN_ENABLED 0 273 #endif 274 #if !defined(RV_ARCH_32BIT) 275 #define RV_ARCH_32BIT 0 276 #endif 277 #if !defined(RV_ARCH_64BIT) 278 #define RV_ARCH_64BIT 0 279 #endif 280 #if !defined(RV_ARCH_X64) 281 #define RV_ARCH_X64 0 282 #endif 283 #if !defined(RV_ARCH_X86) 284 #define RV_ARCH_X86 0 285 #endif 286 #if !defined(RV_ARCH_ARM64) 287 #define RV_ARCH_ARM64 0 288 #endif 289 #if !defined(RV_ARCH_ARM32) 290 #define RV_ARCH_ARM32 0 291 #endif 292 #if !defined(RV_COMPILER_CL) 293 #define RV_COMPILER_CL 0 294 #endif 295 #if !defined(RV_COMPILER_GCC) 296 #define RV_COMPILER_GCC 0 297 #endif 298 #if !defined(RV_COMPILER_CLANG) 299 #define RV_COMPILER_CLANG 0 300 #endif 301 #if !defined(RV_OS_WINDOWS) 302 #define RV_OS_WINDOWS 0 303 #endif 304 #if !defined(RV_OS_LINUX) 305 #define RV_OS_LINUX 0 306 #endif 307 #if !defined(RV_OS_MAC) 308 #define RV_OS_MAC 0 309 #endif 310 #if !defined(RV_LANG_C) 311 #define RV_LANG_C 0 312 #endif 313 #if !defined(RV_LANG_CPP) 314 #define RV_LANG_CPP 0 315 #endif 316 #if !defined(RV_CPP_VERSION) 317 #define RV_CPP_VERSION 0 318 #endif 319 320 // windowing 321 322 #if !defined(RV_WIN_WAYLAND) 323 #define RV_WIN_WAYLAND 0 // wayland is only set externally, x11 is default 324 #endif 325 326 #if RV_OS_LINUX || RV_OS_MAC 327 #if !defined(RV_WIN_X11) 328 #if RV_WIN_WAYLAND 329 #define RV_WIN_X11 0 330 #else 331 #define RV_WIN_X11 1 332 #endif 333 #endif 334 #endif 335 336 #if RV_OS_WINDOWS 337 #if !defined(RV_WIN_WINDOWS) 338 #define RV_WIN_WINDOWS 1 339 #endif 340 #endif 341 342 #if !defined(RV_WIN_X11) 343 #define RV_WIN_X11 0 344 #endif 345 #if !defined(RV_WIN_WINDOWS) 346 #define RV_WIN_WINDOWS 0 347 #endif 348 349 #if !RV_WIN_X11 && !RV_WIN_WAYLAND && !RV_WIN_WINDOWS && !defined(RV_WIN_GLFW) 350 #define RV_WIN_GLFW 1 351 #endif 352 353 #if RV_WIN_X11 || RV_WIN_WAYLAND || RV_WIN_WINDOWS || RV_WIN_GLFW 354 #define RV_WIN_ENABLED 1 355 #else 356 #define RV_WIN_ENABLED 0 357 #endif 358 359 // language constructs 360 361 #if RV_LANG_C 362 #define RV_C_LINKAGE_BEGIN 363 #define RV_C_LINKAGE_END 364 #else 365 #define RV_C_LINKAGE_BEGIN extern "C"{ 366 #define RV_C_LINKAGE_END } 367 #endif 368 369 #if RV_LANG_C && _RV_DEFINE_TRUE_FALSE 370 #define true ((bool32)1) 371 #define false ((bool32)0) 372 #endif 373 374 #if RV_COMPILER_CL 375 #define RV_THREAD_LOCAL __declspec(thread) 376 #elif RV_COMPILER_GCC || RV_COMPILER_CLANG 377 #define RV_THREAD_LOCAL __thread 378 #endif 379 380 #if RV_COMPILER_CL || (RV_COMPILER_CLANG && RV_OS_WINDOWS) 381 #pragma section(".rdata$", read) 382 #define RV_READ_ONLY __declspec(allocate(".rdata$")) 383 #elif (RV_COMPILER_CLANG) 384 #define RV_READ_ONLY __attribute__((section(".rodata"))) 385 #elif (RV_COMPILER_GCC) 386 #define RV_READ_ONLY __attribute__ ((section (".text#"))) 387 #else 388 #define RV_READ_ONLY 389 #endif 390 391 #if RV_COMPILER_CL 392 #define RV_FORCE_INLINE __forceinline 393 #elif RV_COMPILER_CLANG || RV_COMPILER_GCC 394 #define RV_FORCE_INLINE __attribute__((always_inline)) 395 #endif 396 397 #if RV_DEBUG 398 #if RV_COMPILER_CLANG || RV_COMPILER_GCC 399 #define rv_assert(...) if (!(__VA_ARGS__)) __builtin_trap() 400 #elif RV_COMPILER_CL 401 #define rv_assert(...) if (!(__VA_ARGS__)) __debugbreak() 402 #else 403 #define rv_assert(...) if (!(__VA_ARGS__)) *(volatile int *)0 = 0 404 #endif 405 #else 406 #define rv_assert(...) 407 #endif 408 409 #define rv_unreachable() rv_assert(false) 410 411 #if RV_ASAN_ENABLED 412 #include <sanitizer/asan_interface.h> 413 #define RV_ASAN_POISON(ptr, sz) ASAN_POISON_MEMORY_REGION(ptr, sz) 414 #define RV_ASAN_UNPOISON(ptr, sz) ASAN_UNPOISON_MEMORY_REGION(ptr, sz) 415 #else 416 #define RV_ASAN_POISON(ptr, sz) ((void)(ptr), (void)(sz)) 417 #define RV_ASAN_UNPOISON(ptr, sz) ((void)(ptr), (void)(sz)) 418 #endif 419 420 #if RV_COMPILER_CL 421 #define rv_alignof(T) __alignof(T) 422 #elif RV_COMPILER_CLANG 423 #define rv_alignof(T) __alignof(T) 424 #elif RV_COMPILER_GCC 425 #define rv_alignof(T) __alignof__(T) 426 #else 427 #if !defined(rv_alignof) 428 #define rv_alignof(T) offsetof(struct {char _; T z;}, z) 429 #warning alignof not defined for this compiler, try doing '#define rv_alignof(T) _Alignof(T)'? 430 #endif 431 #endif 432 433