Skip to content

Commit f7353a7

Browse files
committed
Merge pull request #2486 from karwa/cross-compiling-modmap
Generate native glibc.modulemap when cross-compiling
2 parents 260e3bf + ca24fd5 commit f7353a7

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ function(_add_variant_swift_compile_flags
180180

181181
list(APPEND result
182182
"-sdk" "${SWIFT_SDK_${sdk}_PATH}"
183-
"-target" "${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}")
183+
"-target" "${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}"
184+
"-resource-dir" "${SWIFTLIB_DIR}")
184185

185186
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
186187
list(APPEND result

stdlib/public/Platform/CMakeLists.txt

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,26 @@ foreach(sdk ${SWIFT_SDKS})
3737
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
3838
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
3939
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
40-
41-
# Determine the location of glibc based on the target.
42-
if(${sdk} STREQUAL "ANDROID")
43-
set(GLIBC_INCLUDE_PATH "${SWIFT_ANDROID_SDK_PATH}/usr/include")
44-
set(GLIBC_ARCH_INCLUDE_PATH ${GLIBC_INCLUDE_PATH})
45-
else()
46-
set(GLIBC_INCLUDE_PATH "/usr/include")
47-
set(GLIBC_ARCH_INCLUDE_PATH ${GLIBC_INCLUDE_PATH})
48-
if((${sdk} STREQUAL "LINUX" OR ${sdk} STREQUAL "FREEBSD") AND CMAKE_LIBRARY_ARCHITECTURE)
49-
# FIXME: Some distributions install headers in
50-
# "/usr/include/x86_64-linux-gnu/sys/...". Here we use the host
51-
# machine's path, regardless of the SDK target we're building for.
52-
# This will break if cross-compiling from a distro that uses the
53-
# architecture as part of the path to a distro that does not.
54-
set(GLIBC_ARCH_INCLUDE_PATH "${GLIBC_INCLUDE_PATH}/${CMAKE_LIBRARY_ARCHITECTURE}")
55-
endif()
40+
41+
# Determine the location of glibc headers based on the target.
42+
set(GLIBC_SYSROOT_RELATIVE_INCLUDE_PATH "/usr/include")
43+
set(GLIBC_SYSROOT_REALTIVE_ARCH_INCLUDE_PATH ${GLIBC_SYSROOT_RELATIVE_INCLUDE_PATH})
44+
45+
# Some SDKs place their headers in architecture-specific subfolders.
46+
if((${sdk} STREQUAL "LINUX" OR ${sdk} STREQUAL "FREEBSD") AND CMAKE_LIBRARY_ARCHITECTURE)
47+
set(GLIBC_SYSROOT_REALTIVE_ARCH_INCLUDE_PATH "${GLIBC_SYSROOT_REALTIVE_ARCH_INCLUDE_PATH}/${CMAKE_LIBRARY_ARCHITECTURE}")
5648
endif()
5749

50+
set(GLIBC_INCLUDE_PATH "${SWIFT_SDK_${sdk}_PATH}/${GLIBC_SYSROOT_RELATIVE_INCLUDE_PATH}")
51+
set(GLIBC_ARCH_INCLUDE_PATH "${SWIFT_SDK_${sdk}_PATH}/${GLIBC_SYSROOT_REALTIVE_ARCH_INCLUDE_PATH}")
52+
5853
set(glibc_modulemap_source "glibc.modulemap.gyb")
5954
set(glibc_modulemap_out "${module_dir}/glibc.modulemap")
6055

6156
# Configure the module map based on the target. Each platform needs to
6257
# reference different headers, based on what's available in their glibc.
58+
# This is the 'glibc.modulemap' in the 'resource-dir', so
59+
# it's the one we'll look at during the build process.
6360
handle_gyb_source_single(glibc_modulemap_target
6461
SOURCE "${glibc_modulemap_source}"
6562
OUTPUT "${glibc_modulemap_out}"
@@ -68,11 +65,34 @@ foreach(sdk ${SWIFT_SDKS})
6865
"-DGLIBC_INCLUDE_PATH=${GLIBC_INCLUDE_PATH}"
6966
"-DGLIBC_ARCH_INCLUDE_PATH=${GLIBC_ARCH_INCLUDE_PATH}")
7067

68+
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target})
69+
70+
# If this SDK is a target for a non-native host, create a native modulemap
71+
# without a sysroot prefix. This is the one we'll install instead.
72+
if(NOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_PATH}" STREQUAL "/")
73+
74+
set(glibc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/glibc.modulemap")
75+
handle_gyb_source_single(glibc_modulemap_native_target
76+
SOURCE "${glibc_modulemap_source}"
77+
OUTPUT "${glibc_sysroot_relative_modulemap_out}"
78+
FLAGS
79+
"-DCMAKE_SDK=${sdk}"
80+
"-DGLIBC_INCLUDE_PATH=${GLIBC_SYSROOT_RELATIVE_INCLUDE_PATH}"
81+
"-DGLIBC_ARCH_INCLUDE_PATH=${GLIBC_SYSROOT_REALTIVE_ARCH_INCLUDE_PATH}")
82+
83+
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_native_target})
84+
set(glibc_modulemap_out ${glibc_sysroot_relative_modulemap_out})
85+
endif()
86+
87+
# FIXME: When SDK is a cross-compile target (SDK != Host), the generated
88+
# modulemap will be relative to the Host, with hardcoded paths.
89+
# It is not relocatable to the target platform itself.
90+
# This only affects ANDROID right now, but could affect cross-compiled LINUX targets
91+
7192
swift_install_in_component(sdk-overlay
7293
FILES "${glibc_modulemap_out}"
7394
DESTINATION "lib/swift/${arch_subdir}")
7495

75-
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target})
7696
endforeach()
7797
endif()
7898
endforeach()

0 commit comments

Comments
 (0)