Skip to content

Commit 7fa02ad

Browse files
committed
Generate native glibc.modulemap when cross-compiling
1 parent 8dfdbd1 commit 7fa02ad

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

stdlib/public/Platform/CMakeLists.txt

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,24 @@ foreach(sdk ${SWIFT_SDKS})
3838
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
3939
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
4040

41-
# Determine the location of glibc based on the target.
41+
# Determine the location of glibc headers based on the target.
42+
set(GLIBC_NATIVE_INCLUDE_PATH "/usr/include")
43+
set(GLIBC_NATIVE_ARCH_INCLUDE_PATH ${GLIBC_NATIVE_INCLUDE_PATH})
44+
# Some SDKs place their headers in architecture-specific subfolders.
45+
if((${sdk} STREQUAL "LINUX" OR ${sdk} STREQUAL "FREEBSD") AND CMAKE_LIBRARY_ARCHITECTURE)
46+
set(GLIBC_NATIVE_ARCH_INCLUDE_PATH "${GLIBC_NATIVE_ARCH_INCLUDE_PATH}/${CMAKE_LIBRARY_ARCHITECTURE}")
47+
endif()
48+
49+
# Android uses a different sysroot variable, although there are pending PRs to refactor this
4250
if(${sdk} STREQUAL "ANDROID")
43-
set(GLIBC_INCLUDE_PATH "${SWIFT_ANDROID_SDK_PATH}/usr/include")
44-
set(GLIBC_ARCH_INCLUDE_PATH ${GLIBC_INCLUDE_PATH})
51+
set(GLIBC_SYSTEM_ROOT "${SWIFT_ANDROID_SDK_PATH}")
4552
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()
53+
set(GLIBC_SYSTEM_ROOT "${CMAKE_SYSROOT}")
5654
endif()
5755

56+
set(GLIBC_INCLUDE_PATH "${GLIBC_SYSTEM_ROOT}/${GLIBC_NATIVE_INCLUDE_PATH}")
57+
set(GLIBC_ARCH_INCLUDE_PATH ${GLIBC_SYSTEM_ROOT}/${GLIBC_NATIVE_ARCH_INCLUDE_PATH})
58+
5859
set(glibc_modulemap_source "glibc.modulemap.gyb")
5960
set(glibc_modulemap_out "${module_dir}/glibc.modulemap")
6061

@@ -68,11 +69,28 @@ foreach(sdk ${SWIFT_SDKS})
6869
"-DGLIBC_INCLUDE_PATH=${GLIBC_INCLUDE_PATH}"
6970
"-DGLIBC_ARCH_INCLUDE_PATH=${GLIBC_ARCH_INCLUDE_PATH}")
7071

72+
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target})
73+
74+
# If cross-compiling, generate a native modulemap without the sysroot prefix
75+
# The prefixed one is still the 'glibc.modulemap' which we'll look at during the build process
76+
if(CMAKE_CROSSCOMPILING)
77+
set(glibc_native_modulemap_out "${module_dir}/native-substitutes/glibc.modulemap")
78+
handle_gyb_source_single(glibc_modulemap_native_target
79+
SOURCE "${glibc_modulemap_source}"
80+
OUTPUT "${glibc_native_modulemap_out}"
81+
FLAGS
82+
"-DCMAKE_SDK=${sdk}"
83+
"-DGLIBC_INCLUDE_PATH=${GLIBC_NATIVE_INCLUDE_PATH}"
84+
"-DGLIBC_ARCH_INCLUDE_PATH=${GLIBC_NATIVE_ARCH_INCLUDE_PATH}")
85+
86+
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_native_target})
87+
set(glibc_modulemap_out ${glibc_native_modulemap_out})
88+
endif()
89+
7190
swift_install_in_component(sdk-overlay
7291
FILES "${glibc_modulemap_out}"
7392
DESTINATION "lib/swift/${arch_subdir}")
7493

75-
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target})
7694
endforeach()
7795
endif()
7896
endforeach()

0 commit comments

Comments
 (0)