|
| 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 PRESET_TARGET to a supported platform, after |
| 10 | +# which the appropriate TARGET_SYSTEM is set and a target triple is |
| 11 | +# 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 | +macro(check_for_android_ndk) |
| 18 | + if(NOT DEFINED ENV{NDK}) |
| 19 | + message(FATAL_ERROR "No NDK found! Set the 'NDK' environment variable to the path of your NDK, for example, export NDK=/home/martin/android-ndk-r15c") |
| 20 | + elseif(NOT EXISTS $ENV{NDK}) |
| 21 | + message(FATAL_ERROR "Could not find path set in environment variable NDK: $ENV{NDK}.") |
| 22 | + endif() |
| 23 | +endmacro() |
| 24 | + |
| 25 | +if(NOT "${PRESET_TARGET}" STREQUAL "AUTO") |
| 26 | + # This initial RT_CFLAGS/LD_FLAGS configuration for Android is a |
| 27 | + # convenience for natively compiling, because CMake cannot detect |
| 28 | + # Android as a separate platform from Linux. |
| 29 | + set(ANDROID_API "21") |
| 30 | + if(RT_CFLAGS STREQUAL "" AND "${PRESET_TARGET}" MATCHES "Android") |
| 31 | + set(RT_CFLAGS_UNCONFIGURED True) |
| 32 | + if("${PRESET_TARGET}" MATCHES "arm") |
| 33 | + set(RT_CFLAGS "-ffunction-sections -funwind-tables -fstack-protector-strong -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -fno-integrated-as -g -target armv7-none-linux-androideabi${ANDROID_API} -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Os -DNDEBUG -DANDROID -D__ANDROID_API__=${ANDROID_API} -Wa,--noexecstack -Wformat -Werror=format-security -fpie") |
| 34 | + endif() |
| 35 | + endif() |
| 36 | + |
| 37 | + if(LD_FLAGS STREQUAL "" AND "${PRESET_TARGET}" MATCHES "Android") |
| 38 | + set(LD_FLAGS_UNCONFIGURED True) |
| 39 | + if("${PRESET_TARGET}" MATCHES "arm") |
| 40 | + set(LD_FLAGS "-Wl,--gc-sections -Wl,-z,nocopyreloc -lgcc -Wl,--exclude-libs,libgcc.a -no-canonical-prefixes -target armv7-none-linux-androideabi${ANDROID_API} -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -fpie -pie") |
| 41 | + endif() |
| 42 | + endif() |
| 43 | + |
| 44 | + if("${PRESET_TARGET}" MATCHES "Windows") |
| 45 | + set(TARGET_SYSTEM "Windows;MSVC") |
| 46 | + if("${PRESET_TARGET}" MATCHES "x64") |
| 47 | + # stub example, fill in with the rest |
| 48 | + list(APPEND D_FLAGS "-mtriple=x86_64-pc-windows-msvc") |
| 49 | + endif() |
| 50 | + elseif("${PRESET_TARGET}" MATCHES "Android") |
| 51 | + set(TARGET_SYSTEM "Android;Linux;UNIX") |
| 52 | + |
| 53 | + if("${PRESET_TARGET}" MATCHES "arm") |
| 54 | + list(APPEND D_FLAGS "-mtriple=armv7-none-linux-android") |
| 55 | + if (RT_CFLAGS_UNCONFIGURED AND NOT CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm") |
| 56 | + check_for_android_ndk() |
| 57 | + append("-gcc-toolchain $ENV{NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot $ENV{NDK}/sysroot -isystem $ENV{NDK}/sysroot/usr/include/arm-linux-androideabi${ANDROID_API}" RT_CFLAGS) |
| 58 | + endif() |
| 59 | + if (LD_FLAGS_UNCONFIGURED AND NOT CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm") |
| 60 | + check_for_android_ndk() |
| 61 | + append("--sysroot=$ENV{NDK}/platforms/android-${ANDROID_API}/arch-arm -gcc-toolchain $ENV{NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -fuse-ld=bfd" LD_FLAGS) |
| 62 | + endif() |
| 63 | + endif() |
| 64 | + endif() |
| 65 | +endif() |
0 commit comments