#include "target.h" #include "tier0/commandline.h" #include "tier1/utlstring.h" //----------------------------------------------------------------------------- // Generates triplet suitable for most compilers. //----------------------------------------------------------------------------- CUtlString Target_t::GetTriplet() { CUtlString triplet = ""; if ( cpu == TARGET_CPU_AMD64 ) triplet.AppendTail("x86_64"); if ( cpu == TARGET_CPU_80386 ) triplet.AppendTail("i386"); if ( cpu == TARGET_CPU_80486 ) triplet.AppendTail("i486"); if ( cpu == TARGET_CPU_80586 ) triplet.AppendTail("i586"); if ( cpu == TARGET_CPU_80686 ) triplet.AppendTail("i686"); if ( cpu == TARGET_CPU_AARCH64 ) triplet.AppendTail("aarch64"); if ( cpu == TARGET_CPU_WASM32 ) triplet.AppendTail("wasm32"); triplet.AppendTail("-"); if ( kernel == TARGET_KERNEL_UNKNOWN ) triplet.AppendTail("unknown-unknown"); if ( kernel == TARGET_KERNEL_LINUX ) triplet.AppendTail("unknown-linux"); if ( kernel == TARGET_KERNEL_WINDOWS ) triplet.AppendTail("pc-windows"); if ( kernel == TARGET_KERNEL_DARWIN ) triplet.AppendTail("apple-darwin"); if ( kernel == TARGET_KERNEL_IOS ) triplet.AppendTail("apple-ios"); if ( kernel == TARGET_KERNEL_ANDROID ) triplet.AppendTail("linux-android"); if ( kernel == TARGET_KERNEL_WASI ) triplet.AppendTail("unknown-wasi"); if ( kernel == TARGET_KERNEL_EMSCRIPTEN ) triplet.AppendTail("unknown-emscripten"); if ( abi != TARGET_ABI_DEFAULT ) triplet.AppendTail("-"); if ( abi == TARGET_ABI_GNU ) triplet.AppendTail("gnu"); if ( abi == TARGET_ABI_MUSL ) triplet.AppendTail("musl"); return triplet; } const char *Target_t::GetExecutableFileFormat() { switch(kernel) { case TARGET_KERNEL_LINUX: case TARGET_KERNEL_ANDROID: case TARGET_KERNEL_DARWIN: case TARGET_KERNEL_IOS: return "%s"; break; case TARGET_KERNEL_WINDOWS: return "%s.exe"; default: break; } return NULL; } const char *Target_t::GetStaticLibraryFileFormat() { switch(kernel) { case TARGET_KERNEL_LINUX: case TARGET_KERNEL_ANDROID: case TARGET_KERNEL_DARWIN: case TARGET_KERNEL_IOS: return "lib%s.a"; case TARGET_KERNEL_WINDOWS: { switch(abi) { case TARGET_ABI_MSVC: return "%s.lib"; default: return "lib%s.a"; } } default: break; } return NULL; } const char *Target_t::GetDynamicLibraryFileFormat() { switch(kernel) { case TARGET_KERNEL_LINUX: case TARGET_KERNEL_ANDROID: return "lib%s.so"; case TARGET_KERNEL_DARWIN: return "lib%s.dylib"; case TARGET_KERNEL_WINDOWS: return "%s.dll"; default: break; } return NULL; } //---------------------------------------------------------------------------- // Returns target on which fpc is being run //---------------------------------------------------------------------------- Target_t Target_t::HostTarget() { ETargetKernel kernel = #if defined(__linux__) TARGET_KERNEL_LINUX #elif defined(__APPLE__) TARGET_KERNEL_DARWIN #endif ; ETargetABI abi = #if defined(__GLIBC__) #endif TARGET_ABI_GNU #if defined(__MUSL__) TARGET_ABI_MUSL #endif ; ETargetCPU cpu = #if defined(__x86_64__) TARGET_CPU_AMD64 #endif #if defined(__i386__) TARGET_CPU_80386 #endif #if defined(__i486__) TARGET_CPU_80486 #endif #if defined(__i586__) TARGET_CPU_80586 #endif #if defined(__i686__) TARGET_CPU_80686 #endif ; return { .kernel = kernel, .cpu = cpu, .optimization = TARGET_DEBUG, }; }; //----------------------------------------------------------------------------- // Returns default target for build, by default it will be host target //----------------------------------------------------------------------------- Target_t Target_t::DefaultTarget() { CUtlString szDevice = CommandLine()->ParamValue("-device"); CUtlString szOS = CommandLine()->ParamValue("-os"); CUtlString szArch = CommandLine()->ParamValue("-arch"); CUtlString szAbi = CommandLine()->ParamValue("-abi"); ETargetKernel kernel = HostTarget().kernel; ETargetCPU cpu = HostTarget().cpu; ETargetABI abi = HostTarget().abi; if ( szArch == "x86_64" ) cpu = TARGET_CPU_AMD64; else if ( szArch == "aarch64" ) cpu = TARGET_CPU_AARCH64; else if ( szArch == "wasm32" ) cpu = TARGET_CPU_WASM32; if ( szOS == "unknown" ) kernel = TARGET_KERNEL_UNKNOWN; else if ( szOS == "windows" ) kernel = TARGET_KERNEL_WINDOWS; else if ( szOS == "linux" ) kernel = TARGET_KERNEL_LINUX; else if ( szOS == "macos" ) kernel = TARGET_KERNEL_DARWIN; else if ( szOS == "ios" ) kernel = TARGET_KERNEL_IOS; else if ( szOS == "android" ) kernel = TARGET_KERNEL_ANDROID; else if ( szOS != 0 ) V_printf("Unknown OS: %s\n", szOS.GetString()); return { .kernel = kernel, .cpu = cpu, .abi = abi, .optimization = TARGET_DEBUG, }; }