Skip to content

Commit acb02f2

Browse files
committed
Add preset configurations for cross-compilation targets supported by the ldc developers, such as C, D, and linker flags, starting with Android-arm.
1 parent c746d27 commit acb02f2

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 2.8)
22
project(ldc)
33

4-
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")
4+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules" "${CMAKE_SOURCE_DIR}/runtime")
55

66
include(FindDCompiler)
77
include(CheckIncludeFile)

runtime/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ project(runtime)
22

33
cmake_minimum_required(VERSION 2.8.9)
44

5+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR})
6+
57
# Verify required variables if this CMake project is NOT embedded in the LDC CMake project.
68
if(NOT LDC_EXE)
79
if(NOT LDC_EXE_FULL)
@@ -40,12 +42,14 @@ set(COMPILE_ALL_D_FILES_AT_ONCE ON CACHE BOOL "Comp
4042
set(RT_CFLAGS "" CACHE STRING "Runtime extra C compiler flags, separated by ' '")
4143
set(LD_FLAGS "" CACHE STRING "Runtime extra C linker flags, separated by ' '")
4244
set(C_SYSTEM_LIBS AUTO CACHE STRING "C system libraries for linking shared libraries and test runners, separated by ';'")
43-
set(TARGET_SYSTEM AUTO CACHE STRING "Targeted platform for cross-compilation (e.g., 'Linux;UNIX', 'Darwin;APPLE;UNIX', 'Windows;MSVC')")
45+
set(TARGET_SYSTEM AUTO CACHE STRING "Target OS/toolchain for cross-compilation (e.g., 'Linux;UNIX', 'Darwin;APPLE;UNIX', 'Windows;MSVC')")
46+
set(PRESET_TARGET AUTO CACHE STRING "Use preset target configuration for cross-compilation, by passing format OS-arch (e.g., 'Linux-arm', 'Mac-x64', 'Windows-aarch64', 'Android-x86')")
4447

4548
set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX})
4649

4750
# Auto-detect TARGET_SYSTEM
48-
if("${TARGET_SYSTEM}" STREQUAL "AUTO")
51+
52+
if("${PRESET_TARGET}" STREQUAL "AUTO" AND "${TARGET_SYSTEM}" STREQUAL "AUTO")
4953
set(TARGET_SYSTEM ${CMAKE_SYSTEM_NAME})
5054
# Additionally add MSVC, APPLE and/or UNIX
5155
if(MSVC)
@@ -59,6 +63,8 @@ if("${TARGET_SYSTEM}" STREQUAL "AUTO")
5963
endif()
6064
endif()
6165

66+
include(PresetRuntimeConfiguration)
67+
6268
if("${TARGET_SYSTEM}" MATCHES "UNIX")
6369
ENABLE_LANGUAGE(ASM)
6470
elseif("${TARGET_SYSTEM}" MATCHES "MSVC")
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)