Skip to content

Commit dd60161

Browse files
committed
cmake: Add cross-compiling support
To configure CMake for cross-compiling, use `--toolchain depends/${HOST}/share/toolchain.cmake` command-line option.
1 parent d8cba32 commit dd60161

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

CMakeLists.txt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
5252
set(CMAKE_CXX_EXTENSIONS OFF)
5353

5454
set(configure_warnings)
55-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
56-
include(CheckPIESupported)
57-
check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX)
58-
if(NOT CMAKE_CXX_LINK_PIE_SUPPORTED)
59-
list(APPEND configure_warnings "PIE link options are not supported for executable targets: ${check_pie_output}.")
55+
if(NOT CMAKE_CROSSCOMPILING)
56+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.14)
57+
include(CheckPIESupported)
58+
check_pie_supported(OUTPUT_VARIABLE check_pie_output LANGUAGES CXX)
59+
if(NOT CMAKE_CXX_LINK_PIE_SUPPORTED)
60+
list(APPEND configure_warnings "PIE link options are not supported for executable targets: ${check_pie_output}.")
61+
endif()
62+
else()
63+
list(APPEND configure_warnings "No PIE options will be passed to a linker for executable targets.")
6064
endif()
61-
else()
62-
list(APPEND configure_warnings "No PIE options will be passed to a linker for executable targets.")
6365
endif()
6466
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
6567

@@ -98,6 +100,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
98100
add_compile_definitions(MAC_OSX)
99101
endif()
100102

103+
if(CMAKE_CROSSCOMPILING AND DEPENDS_ALLOW_HOST_PACKAGES)
104+
list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_SYSTEM_PREFIX_PATH}")
105+
endif()
106+
101107
include(AddThreadsIfNeeded)
102108
add_threads_if_needed()
103109

@@ -127,6 +133,12 @@ message("=================")
127133
message("Executables:")
128134
message(" bitcoind ............................ ${BUILD_DAEMON}")
129135
message("")
136+
if(CMAKE_CROSSCOMPILING)
137+
set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}")
138+
else()
139+
set(cross_status "FALSE")
140+
endif()
141+
message("Cross compiling ....................... ${cross_status}")
130142
get_directory_property(definitions COMPILE_DEFINITIONS)
131143
string(REPLACE ";" " " definitions "${definitions}")
132144
message("Preprocessor defined macros ........... ${definitions}")

cmake/module/AddLibeventIfNeeded.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ function(add_libevent_if_needed)
3737
return()
3838
endif()
3939

40-
find_package(PkgConfig)
41-
pkg_check_modules(libevent REQUIRED libevent>=${libevent_minimum_version} IMPORTED_TARGET GLOBAL)
40+
include(CrossPkgConfig)
41+
cross_pkg_check_modules(libevent REQUIRED libevent>=${libevent_minimum_version} IMPORTED_TARGET GLOBAL)
4242
check_evhttp_connection_get_peer(PkgConfig::libevent)
4343
target_link_libraries(PkgConfig::libevent INTERFACE
4444
$<$<BOOL:${MINGW}>:iphlpapi;ws2_32>
4545
)
4646
add_library(libevent::libevent ALIAS PkgConfig::libevent)
4747

4848
if(NOT WIN32)
49-
pkg_check_modules(libevent_pthreads REQUIRED libevent_pthreads>=${libevent_minimum_version} IMPORTED_TARGET)
49+
cross_pkg_check_modules(libevent_pthreads REQUIRED libevent_pthreads>=${libevent_minimum_version} IMPORTED_TARGET)
5050
endif()
5151
endfunction()

cmake/module/CrossPkgConfig.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2023 The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
find_package(PkgConfig REQUIRED)
6+
7+
macro(cross_pkg_check_modules)
8+
if(CMAKE_CROSSCOMPILING)
9+
set(pkg_config_path_saved "$ENV{PKG_CONFIG_PATH}")
10+
set(pkg_config_libdir_saved "$ENV{PKG_CONFIG_LIBDIR}")
11+
set(ENV{PKG_CONFIG_PATH} ${PKG_CONFIG_PATH})
12+
set(ENV{PKG_CONFIG_LIBDIR} ${PKG_CONFIG_LIBDIR})
13+
pkg_check_modules(${ARGV})
14+
set(ENV{PKG_CONFIG_PATH} ${pkg_config_path_saved})
15+
set(ENV{PKG_CONFIG_LIBDIR} ${pkg_config_libdir_saved})
16+
else()
17+
pkg_check_modules(${ARGV})
18+
endif()
19+
endmacro()

0 commit comments

Comments
 (0)