-
Notifications
You must be signed in to change notification settings - Fork 9
/
linux.cmake
executable file
·113 lines (103 loc) · 4.4 KB
/
linux.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# This file is part of the cmake-tools project. It was retrieved from
# https://github.com/wang-bin/cmake-tools
#
# The cmake-tools project is licensed under the new MIT license.
#
# Copyright (c) 2017-2024, Wang Bin
#
#cross build apps for linux
#
# LINUX_FLAGS: flags for both compiler and linker, e.g. --target=arm-rpi-linux-gnueabihf ...
# CMAKE_SYSTEM_PROCESSOR: REQUIRED
# vars: CXXCONFIG_H_DIR, LIBSTDCXX_SO
option(USE_STDCXX "libstdc++ version to use, MUST >= 4.8. default is 0, selected by compiler" 0)
if(NOT OS)
set(OS Linux)
endif()
set(CMAKE_SYSTEM_NAME Linux) # assume host build if not set, host flags will be used, e.g. apple clang flags are added on macOS
if(NOT CMAKE_SYSTEM_PROCESSOR)
message("CMAKE_SYSTEM_PROCESSOR for target is not set. Must be aarch64(arm64), armv7(arm), x86(i386,i686), x64(x86_64). Assumeme build for host arch: ${CMAKE_HOST_SYSTEM_PROCESSOR}.")
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "[aA].*[rR].*64") # arm64, aarch64
set(TRIPLE_ARCH aarch64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm.*hf") # armhf, armv7hf, armv6kzhf
string(REPLACE "hf" "" __MARCH "${CMAKE_SYSTEM_PROCESSOR}")
if(NOT ${__MARCH} STREQUAL "arm")
add_compile_options(-march=${__MARCH})
endif()
set(TRIPLE_ARCH arm) # will affect lib dir search, e.g. /lib/${TRIPLE_ARCH}-linux-gnueabihf
set(TRIPLE_ABI eabihf) # armhf: -mfloat-abi=hard
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
set(TRIPLE_ARCH arm) # will affect lib dir search, e.g. /lib/${TRIPLE_ARCH}-linux-gnueabihf
set(TRIPLE_ABI eabi) # armel: -mfloat-abi=soft -mfloat-abi=softfp
if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "arm" AND NOT LINUX_FLAGS MATCHES "-march=")
add_compile_options(-march=${CMAKE_SYSTEM_PROCESSOR})
endif()
if(LINUX_FLAGS MATCHES "-mfloat-abi=hard")
set(TRIPLE_ABI eabihf) # TARGET_TRIPPLE will affect lib dir search
endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "64")
set(TRIPLE_ARCH x86_64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "86")
set(TRIPLE_ARCH i386)
endif()
if(NOT DEFINED USE_CRT) # can be gnu, musl
set(USE_CRT gnu)
endif()
if(NOT "${USE_CRT}" STREQUAL "")
set(TARGET_ABI "-${USE_CRT}${TRIPLE_ABI}")
endif()
# arch[sub][-vendor]-sys[-abi]
set(TARGET_TRIPPLE ${TRIPLE_ARCH}${TARGET_VENDOR}-linux${TARGET_ABI})
set(CMAKE_LIBRARY_ARCHITECTURE ${TARGET_TRIPPLE}) # FIND_LIBRARY search subdir
# Export configurable variables for the try_compile() command. Or set env var like llvm
set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
CMAKE_SYSTEM_PROCESSOR
CMAKE_C_COMPILER # find_program only once
LINUX_FLAGS
#LINUX_SYSROOT
)
# Sysroot.
#message("CMAKE_SYSROOT_COMPILE: ${CMAKE_SYSROOT_COMPILE}, ${CMAKE_CROSSCOMPILING}")
if(EXISTS "${LINUX_SYSROOT}")
set(CMAKE_SYSROOT ${LINUX_SYSROOT})
# CMake 3.9 tries to use CMAKE_SYSROOT_COMPILE before it gets set from CMAKE_SYSROOT, which leads to using the system's /usr/include. Set this manually.