|
| 1 | +# - Preset cross-compilation configurations for C/ASM and D compilation |
| 2 | +# and linking, for supported targets |
| 3 | +# |
| 4 | +# This module sets compiler flags for a few C and assembly files in |
| 5 | +# DRuntime and Phobos, and linker flags to link the standard library as |
| 6 | +# a shared library and build the test runners, for various |
| 7 | +# cross-compilation targets that the LDC developers have tried out. |
| 8 | +# |
| 9 | +# It is enabled by setting LDC_TARGET_PRESET to a supported platform, |
| 10 | +# after which the appropriate TARGET_SYSTEM is set and a target triple |
| 11 | +# is appended to D_FLAGS. |
| 12 | +# |
| 13 | +# You can pass in custom RT_CFLAGS and LD_FLAGS of your choosing, but |
| 14 | +# if they're left unconfigured, they will also be set to sensible |
| 15 | +# defaults. |
| 16 | + |
| 17 | +if(NOT LDC_TARGET_PRESET STREQUAL "") |
| 18 | + if(LDC_TARGET_PRESET MATCHES "Android") |
| 19 | + set(ANDROID_API "21") |
| 20 | + endif() |
| 21 | + # This initial RT_CFLAGS/LD_FLAGS configuration for Android is a |
| 22 | + # convenience for natively compiling, because CMake cannot detect |
| 23 | + # Android as a separate platform from Linux. |
| 24 | + if(RT_CFLAGS STREQUAL "" AND LDC_TARGET_PRESET MATCHES "Android") |
| 25 | + set(RT_CFLAGS_UNCONFIGURED True) |
| 26 | + set(RT_CFLAGS "-ffunction-sections -funwind-tables -fstack-protector-strong -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -g -DNDEBUG -DANDROID -D__ANDROID_API__=${ANDROID_API} -Wa,--noexecstack -Wformat -Werror=format-security -fpie") |
| 27 | + |
| 28 | + if(LDC_TARGET_PRESET MATCHES "arm") |
| 29 | + append("-target armv7-none-linux-androideabi${ANDROID_API} -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Os" RT_CFLAGS) |
| 30 | + elseif(LDC_TARGET_PRESET MATCHES "aarch64") |
| 31 | + append("-target aarch64-none-linux-android -O2" RT_CFLAGS) |
| 32 | + endif() |
| 33 | + endif() |
| 34 | + |
| 35 | + if(LD_FLAGS STREQUAL "" AND LDC_TARGET_PRESET MATCHES "Android") |
| 36 | + set(LD_FLAGS_UNCONFIGURED True) |
| 37 | + set(LD_FLAGS "-Wl,--gc-sections -Wl,-z,nocopyreloc -no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -fpie -pie") |
| 38 | + |
| 39 | + if(LDC_TARGET_PRESET MATCHES "arm") |
| 40 | + append("-target armv7-none-linux-androideabi${ANDROID_API} -Wl,--fix-cortex-a8" LD_FLAGS) |
| 41 | + elseif(LDC_TARGET_PRESET MATCHES "aarch64") |
| 42 | + append("-target aarch64-none-linux-android" LD_FLAGS) |
| 43 | + endif() |
| 44 | + endif() |
| 45 | + |
| 46 | + if(LDC_TARGET_PRESET MATCHES "Windows") |
| 47 | + set(TARGET_SYSTEM "Windows;MSVC") |
| 48 | + if(LDC_TARGET_PRESET MATCHES "x64") |
| 49 | + # stub example, fill in with the rest |
| 50 | + list(APPEND D_FLAGS "-mtriple=x86_64-pc-windows-msvc") |
| 51 | + endif() |
| 52 | + elseif(LDC_TARGET_PRESET MATCHES "Android") |
| 53 | + set(TARGET_SYSTEM "Android;Linux;UNIX") |
| 54 | + |
| 55 | + # Check if we're using the NDK by looking for the toolchains |
| 56 | + # directory in CC |
| 57 | + if(CMAKE_C_COMPILER MATCHES "toolchains") |
| 58 | + # Extract the NDK path and platform from CC |
| 59 | + string(REGEX REPLACE ".toolchains.+" "" NDK_PATH ${CMAKE_C_COMPILER}) |
| 60 | + string(REGEX REPLACE ".+/prebuilt/([^/]+)/.+" "\\1" NDK_HOST_PLATFORM ${CMAKE_C_COMPILER}) |
| 61 | + |
| 62 | + if(LDC_TARGET_PRESET MATCHES "arm") |
| 63 | + set(TARGET_ARCH "arm") |
| 64 | + set(LLVM_TARGET_TRIPLE "armv7-none-linux-android") |
| 65 | + set(TOOLCHAIN_TARGET_TRIPLE "arm-linux-androideabi") |
| 66 | + elseif(LDC_TARGET_PRESET MATCHES "aarch64") |
| 67 | + set(TARGET_ARCH "arm64") |
| 68 | + set(LLVM_TARGET_TRIPLE "aarch64-none-linux-android") |
| 69 | + set(TOOLCHAIN_TARGET_TRIPLE "aarch64-linux-android") |
| 70 | + else() |
| 71 | + message(FATAL_ERROR "Android platform ${LDC_TARGET_PRESET} is not supported.") |
| 72 | + endif() |
| 73 | + list(APPEND D_FLAGS "-mtriple=${LLVM_TARGET_TRIPLE}") |
| 74 | + set(TOOLCHAIN_VERSION "4.9") |
| 75 | + |
| 76 | + if(RT_CFLAGS_UNCONFIGURED) |
| 77 | + append("-gcc-toolchain ${NDK_PATH}/toolchains/${TOOLCHAIN_TARGET_TRIPLE}-${TOOLCHAIN_VERSION}/prebuilt/${NDK_HOST_PLATFORM} --sysroot ${NDK_PATH}/sysroot -isystem ${NDK_PATH}/sysroot/usr/include/${TOOLCHAIN_TARGET_TRIPLE}" RT_CFLAGS) |
| 78 | + |
| 79 | + if(LDC_TARGET_PRESET MATCHES "arm") |
| 80 | + append("-fno-integrated-as" RT_CFLAGS) |
| 81 | + endif() |
| 82 | + endif() |
| 83 | + |
| 84 | + if(LD_FLAGS_UNCONFIGURED) |
| 85 | + set(LD_BFD "-fuse-ld=bfd") |
| 86 | + # work around Windows bug, android-ndk/ndk#75 |
| 87 | + if(NDK_HOST_PLATFORM MATCHES "windows") |
| 88 | + set(LD_BFD "${LD_BFD}.exe") |
| 89 | + endif() |
| 90 | + |
| 91 | + append("--sysroot=${NDK_PATH}/platforms/android-${ANDROID_API}/arch-${TARGET_ARCH} -gcc-toolchain ${NDK_PATH}/toolchains/${TOOLCHAIN_TARGET_TRIPLE}-${TOOLCHAIN_VERSION}/prebuilt/${NDK_HOST_PLATFORM} ${LD_BFD}" LD_FLAGS) |
| 92 | + endif() |
| 93 | + endif() |
| 94 | + else() |
| 95 | + message(FATAL_ERROR "LDC_TARGET_PRESET ${LDC_TARGET_PRESET} is not supported yet, pull requests to add common flags are welcome.") |
| 96 | + endif() |
| 97 | +endif() |
0 commit comments