Skip to content

Commit

Permalink
cmake: Add cross-compiling support
Browse files Browse the repository at this point in the history
To configure CMake for cross-compiling, use
`--toolchain depends/${HOST}/share/toolchain.cmake` command-line option.
  • Loading branch information
hebasto committed Apr 2, 2023
1 parent ccc0e49 commit 5b25bcd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_compile_definitions(MAC_OSX)
endif()

if(CMAKE_CROSSCOMPILING AND DEPENDS_ALLOW_HOST_PACKAGES)
list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_SYSTEM_PREFIX_PATH}")
endif()

include(AddThreadsIfNeeded)
add_threads_if_needed()

Expand Down Expand Up @@ -130,6 +134,12 @@ message("=================")
message("Executables:")
message(" bitcoind ............................ ${BUILD_DAEMON}")
message("")
if(CMAKE_CROSSCOMPILING)
set(cross_status "TRUE, for ${CMAKE_SYSTEM_NAME}, ${CMAKE_SYSTEM_PROCESSOR}")
else()
set(cross_status "FALSE")
endif()
message("Cross compiling ....................... ${cross_status}")
get_directory_property(definitions COMPILE_DEFINITIONS)
string(REPLACE ";" " " definitions "${definitions}")
message("Preprocessor defined macros ........... ${definitions}")
Expand Down
6 changes: 3 additions & 3 deletions cmake/module/AddLibeventIfNeeded.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ function(add_libevent_if_needed)
return()
endif()

find_package(PkgConfig)
pkg_check_modules(libevent REQUIRED libevent>=${libevent_minimum_version} IMPORTED_TARGET GLOBAL)
include(CrossPkgConfig)
cross_pkg_check_modules(libevent REQUIRED libevent>=${libevent_minimum_version} IMPORTED_TARGET GLOBAL)
check_evhttp_connection_get_peer(PkgConfig::libevent)
target_link_libraries(PkgConfig::libevent INTERFACE
$<$<BOOL:${MINGW}>:iphlpapi;ssp;ws2_32>
)
add_library(libevent::libevent ALIAS PkgConfig::libevent)

if(NOT WIN32)
pkg_check_modules(libevent_pthreads REQUIRED libevent_pthreads>=${libevent_minimum_version} IMPORTED_TARGET)
cross_pkg_check_modules(libevent_pthreads REQUIRED libevent_pthreads>=${libevent_minimum_version} IMPORTED_TARGET)
endif()
endfunction()
19 changes: 19 additions & 0 deletions cmake/module/CrossPkgConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2023 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.

find_package(PkgConfig REQUIRED)

macro(cross_pkg_check_modules)
if(CMAKE_CROSSCOMPILING)
set(pkg_config_path_saved "$ENV{PKG_CONFIG_PATH}")
set(pkg_config_libdir_saved "$ENV{PKG_CONFIG_LIBDIR}")
set(ENV{PKG_CONFIG_PATH} ${PKG_CONFIG_PATH})
set(ENV{PKG_CONFIG_LIBDIR} ${PKG_CONFIG_LIBDIR})
pkg_check_modules(${ARGV})
set(ENV{PKG_CONFIG_PATH} ${pkg_config_path_saved})
set(ENV{PKG_CONFIG_LIBDIR} ${pkg_config_libdir_saved})
else()
pkg_check_modules(${ARGV})
endif()
endmacro()

0 comments on commit 5b25bcd

Please sign in to comment.