Skip to content

Commit b09b385

Browse files
authored
Merge pull request swiftlang#59747 from apple/egorzhdan/cxx-split-overlay
[cxx-interop] Split C++ stdlib overlay into two modules
2 parents ec850fd + 82a90b9 commit b09b385

File tree

8 files changed

+166
-152
lines changed

8 files changed

+166
-152
lines changed

stdlib/public/Cxx/CMakeLists.txt

Lines changed: 7 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,13 @@
1-
set(libstdcxx_modulemap_target_list)
2-
foreach(sdk ${SWIFT_SDKS})
3-
if(NOT ${sdk} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS)
4-
continue()
5-
endif()
6-
7-
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
8-
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
9-
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
10-
11-
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
12-
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
13-
14-
set(libstdcxx_header "libstdcxx.h")
15-
set(libstdcxx_header_out "${module_dir}/libstdcxx.h")
16-
set(libstdcxx_header_out_static "${module_dir_static}/libstdcxx.h")
17-
set(libstdcxx_modulemap "libstdcxx.modulemap")
18-
set(libstdcxx_modulemap_out "${module_dir}/libstdcxx.modulemap")
19-
set(libstdcxx_modulemap_out_static "${module_dir_static}/libstdcxx.modulemap")
20-
21-
add_custom_command_target(
22-
copy_libstdcxx_modulemap
23-
COMMAND
24-
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
25-
COMMAND
26-
"${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out}"
27-
OUTPUT ${libstdcxx_modulemap_out}
28-
DEPENDS ${libstdcxx_modulemap}
29-
COMMENT "Copying libstdcxx modulemap to resources")
30-
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap})
31-
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap})
32-
33-
add_custom_command_target(
34-
copy_libstdcxx_header
35-
COMMAND
36-
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
37-
COMMAND
38-
"${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out}"
39-
OUTPUT ${libstdcxx_header_out}
40-
DEPENDS ${libstdcxx_header}
41-
COMMENT "Copying libstdcxx header to resources")
42-
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header})
43-
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header})
44-
45-
if(SWIFT_BUILD_STATIC_STDLIB)
46-
add_custom_command_target(
47-
copy_libstdcxx_modulemap_static
48-
COMMAND
49-
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
50-
COMMAND
51-
"${CMAKE_COMMAND}" "-E" "copy_if_different"
52-
"${libstdcxx_modulemap_out}" "${libstdcxx_modulemap_out_static}"
53-
OUTPUT ${libstdcxx_modulemap_out_static}
54-
DEPENDS ${copy_libstdcxx_modulemap}
55-
COMMENT "Copying libstdcxx modulemap to static resources")
56-
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap_static})
57-
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap_static})
58-
59-
add_custom_command_target(
60-
copy_libstdcxx_header_static
61-
COMMAND
62-
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
63-
COMMAND
64-
"${CMAKE_COMMAND}" "-E" "copy_if_different"
65-
"${libstdcxx_header_out}" "${libstdcxx_header_out_static}"
66-
OUTPUT ${libstdcxx_header_out_static}
67-
DEPENDS ${copy_libstdcxx_header}
68-
COMMENT "Copying libstdcxx header to static resources")
69-
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header_static})
70-
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header_static})
71-
endif()
72-
73-
swift_install_in_component(FILES "${libstdcxx_modulemap_out}"
74-
DESTINATION "lib/swift/${arch_subdir}"
75-
COMPONENT sdk-overlay)
76-
swift_install_in_component(FILES "${libstdcxx_header_out}"
77-
DESTINATION "lib/swift/${arch_subdir}"
78-
COMPONENT sdk-overlay)
79-
80-
if(SWIFT_BUILD_STATIC_STDLIB)
81-
swift_install_in_component(FILES "${libstdcxx_modulemap_out_static}"
82-
DESTINATION "lib/swift_static/${arch_subdir}"
83-
COMPONENT sdk-overlay)
84-
swift_install_in_component(FILES "${libstdcxx_header_out_static}"
85-
DESTINATION "lib/swift_static/${arch_subdir}"
86-
COMPONENT sdk-overlay)
87-
endif()
88-
89-
if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*")
90-
foreach(bootstrapping "0" "1")
91-
get_bootstrapping_path(bootstrapping_dir ${module_dir} ${bootstrapping})
92-
set(libstdcxx_modulemap_out_bootstrapping "${bootstrapping_dir}/libstdcxx.modulemap")
93-
set(libstdcxx_header_out_bootstrapping "${bootstrapping_dir}/libstdcxx.h")
94-
95-
add_custom_command_target(unused_var
96-
COMMAND
97-
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
98-
COMMAND
99-
"${CMAKE_COMMAND}" "-E" "copy_if_different"
100-
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out_bootstrapping}"
101-
102-
CUSTOM_TARGET_NAME "copy-libstdcxx-modulemap-bootstrapping${bootstrapping}"
103-
OUTPUT "${libstdcxx_modulemap_out_bootstrapping}"
104-
DEPENDS ${libstdcxx_modulemap}
105-
COMMENT "Copying libstdcxx modulemap to resources for bootstrapping${bootstrapping}")
106-
107-
add_custom_command_target(unused_var
108-
COMMAND
109-
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
110-
COMMAND
111-
"${CMAKE_COMMAND}" "-E" "copy_if_different"
112-
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out_bootstrapping}"
113-
114-
CUSTOM_TARGET_NAME "copy-libstdcxx-header-bootstrapping${bootstrapping}"
115-
OUTPUT "${libstdcxx_header_out_bootstrapping}"
116-
DEPENDS ${libstdcxx_header}
117-
COMMENT "Copying libstdcxx header to resources for bootstrapping${bootstrapping}")
118-
endforeach()
119-
endif()
120-
endforeach()
121-
endforeach()
122-
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})
123-
set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous")
124-
add_dependencies(sdk-overlay libstdcxx-modulemap)
125-
126-
127-
#
128-
# C++ Standard Library Overlay.
129-
#
130-
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
131-
std.swift
1+
add_swift_target_library(swiftCxx ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
1322
CxxSequence.swift
133-
String.swift
134-
135-
SWIFT_MODULE_DEPENDS_OSX Darwin
136-
SWIFT_MODULE_DEPENDS_IOS Darwin
137-
SWIFT_MODULE_DEPENDS_TVOS Darwin
138-
SWIFT_MODULE_DEPENDS_WATCHOS Darwin
139-
SWIFT_MODULE_DEPENDS_LINUX Glibc
1403

1414
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
1425
-Xfrontend -enable-experimental-cxx-interop
143-
-Xfrontend -module-interface-preserve-types-as-written
144-
145-
SWIFT_COMPILE_FLAGS_LINUX
146-
# GCC on Linux is usually located under `/usr`.
147-
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which does not include libstdc++.
148-
# Swift build scripts pass `--sysroot=/` to Clang. By default, Clang tries to find GCC installation under sysroot,
149-
# and if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it attempts to use the GCC without the
150-
# C++ stdlib, which causes a build failure. To fix that, we tell Clang explicitly to use GCC from `/usr`.
151-
-Xcc --gcc-toolchain=/usr
6+
# This module should not pull in the C++ standard library, so we disable it explicitly.
7+
# For functionality that depends on the C++ stdlib, use C++ stdlib overlay (`swiftstd` module).
8+
-Xcc -nostdinc++
1529

15310
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
154-
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
155-
INSTALL_IN_COMPONENT sdk-overlay
156-
DEPENDS libstdcxx-modulemap)
11+
INSTALL_IN_COMPONENT sdk-overlay)
12+
13+
add_subdirectory(std)

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
set(libstdcxx_modulemap_target_list)
2+
foreach(sdk ${SWIFT_SDKS})
3+
if(NOT ${sdk} IN_LIST SWIFT_LIBSTDCXX_PLATFORMS)
4+
continue()
5+
endif()
6+
7+
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
8+
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
9+
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
10+
11+
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
12+
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
13+
14+
set(libstdcxx_header "libstdcxx.h")
15+
set(libstdcxx_header_out "${module_dir}/libstdcxx.h")
16+
set(libstdcxx_header_out_static "${module_dir_static}/libstdcxx.h")
17+
set(libstdcxx_modulemap "libstdcxx.modulemap")
18+
set(libstdcxx_modulemap_out "${module_dir}/libstdcxx.modulemap")
19+
set(libstdcxx_modulemap_out_static "${module_dir_static}/libstdcxx.modulemap")
20+
21+
add_custom_command_target(
22+
copy_libstdcxx_modulemap
23+
COMMAND
24+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
25+
COMMAND
26+
"${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out}"
27+
OUTPUT ${libstdcxx_modulemap_out}
28+
DEPENDS ${libstdcxx_modulemap}
29+
COMMENT "Copying libstdcxx modulemap to resources")
30+
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap})
31+
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap})
32+
33+
add_custom_command_target(
34+
copy_libstdcxx_header
35+
COMMAND
36+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
37+
COMMAND
38+
"${CMAKE_COMMAND}" "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out}"
39+
OUTPUT ${libstdcxx_header_out}
40+
DEPENDS ${libstdcxx_header}
41+
COMMENT "Copying libstdcxx header to resources")
42+
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header})
43+
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header})
44+
45+
if(SWIFT_BUILD_STATIC_STDLIB)
46+
add_custom_command_target(
47+
copy_libstdcxx_modulemap_static
48+
COMMAND
49+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
50+
COMMAND
51+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
52+
"${libstdcxx_modulemap_out}" "${libstdcxx_modulemap_out_static}"
53+
OUTPUT ${libstdcxx_modulemap_out_static}
54+
DEPENDS ${copy_libstdcxx_modulemap}
55+
COMMENT "Copying libstdcxx modulemap to static resources")
56+
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_modulemap_static})
57+
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_modulemap_static})
58+
59+
add_custom_command_target(
60+
copy_libstdcxx_header_static
61+
COMMAND
62+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
63+
COMMAND
64+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
65+
"${libstdcxx_header_out}" "${libstdcxx_header_out_static}"
66+
OUTPUT ${libstdcxx_header_out_static}
67+
DEPENDS ${copy_libstdcxx_header}
68+
COMMENT "Copying libstdcxx header to static resources")
69+
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header_static})
70+
add_dependencies(swift-stdlib-${arch_suffix} ${copy_libstdcxx_header_static})
71+
endif()
72+
73+
swift_install_in_component(FILES "${libstdcxx_modulemap_out}"
74+
DESTINATION "lib/swift/${arch_subdir}"
75+
COMPONENT sdk-overlay)
76+
swift_install_in_component(FILES "${libstdcxx_header_out}"
77+
DESTINATION "lib/swift/${arch_subdir}"
78+
COMPONENT sdk-overlay)
79+
80+
if(SWIFT_BUILD_STATIC_STDLIB)
81+
swift_install_in_component(FILES "${libstdcxx_modulemap_out_static}"
82+
DESTINATION "lib/swift_static/${arch_subdir}"
83+
COMPONENT sdk-overlay)
84+
swift_install_in_component(FILES "${libstdcxx_header_out_static}"
85+
DESTINATION "lib/swift_static/${arch_subdir}"
86+
COMPONENT sdk-overlay)
87+
endif()
88+
89+
if(${BOOTSTRAPPING_MODE} MATCHES "BOOTSTRAPPING.*")
90+
foreach(bootstrapping "0" "1")
91+
get_bootstrapping_path(bootstrapping_dir ${module_dir} ${bootstrapping})
92+
set(libstdcxx_modulemap_out_bootstrapping "${bootstrapping_dir}/libstdcxx.modulemap")
93+
set(libstdcxx_header_out_bootstrapping "${bootstrapping_dir}/libstdcxx.h")
94+
95+
add_custom_command_target(unused_var
96+
COMMAND
97+
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
98+
COMMAND
99+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
100+
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_modulemap}" "${libstdcxx_modulemap_out_bootstrapping}"
101+
102+
CUSTOM_TARGET_NAME "copy-libstdcxx-modulemap-bootstrapping${bootstrapping}"
103+
OUTPUT "${libstdcxx_modulemap_out_bootstrapping}"
104+
DEPENDS ${libstdcxx_modulemap}
105+
COMMENT "Copying libstdcxx modulemap to resources for bootstrapping${bootstrapping}")
106+
107+
add_custom_command_target(unused_var
108+
COMMAND
109+
"${CMAKE_COMMAND}" "-E" "make_directory" "${bootstrapping_dir}"
110+
COMMAND
111+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
112+
"${CMAKE_CURRENT_SOURCE_DIR}/${libstdcxx_header}" "${libstdcxx_header_out_bootstrapping}"
113+
114+
CUSTOM_TARGET_NAME "copy-libstdcxx-header-bootstrapping${bootstrapping}"
115+
OUTPUT "${libstdcxx_header_out_bootstrapping}"
116+
DEPENDS ${libstdcxx_header}
117+
COMMENT "Copying libstdcxx header to resources for bootstrapping${bootstrapping}")
118+
endforeach()
119+
endif()
120+
endforeach()
121+
endforeach()
122+
add_custom_target(libstdcxx-modulemap DEPENDS ${libstdcxx_modulemap_target_list})
123+
set_property(TARGET libstdcxx-modulemap PROPERTY FOLDER "Miscellaneous")
124+
add_dependencies(sdk-overlay libstdcxx-modulemap)
125+
126+
127+
#
128+
# C++ Standard Library Overlay.
129+
#
130+
add_swift_target_library(swiftstd ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
131+
std.swift
132+
String.swift
133+
134+
SWIFT_MODULE_DEPENDS Cxx
135+
SWIFT_MODULE_DEPENDS_OSX Darwin
136+
SWIFT_MODULE_DEPENDS_IOS Darwin
137+
SWIFT_MODULE_DEPENDS_TVOS Darwin
138+
SWIFT_MODULE_DEPENDS_WATCHOS Darwin
139+
SWIFT_MODULE_DEPENDS_LINUX Glibc
140+
141+
SWIFT_COMPILE_FLAGS ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
142+
-Xfrontend -enable-experimental-cxx-interop
143+
-Xfrontend -module-interface-preserve-types-as-written
144+
145+
SWIFT_COMPILE_FLAGS_LINUX
146+
# GCC on Linux is usually located under `/usr`.
147+
# However, Ubuntu 20.04 ships with another GCC installation under `/`, which does not include libstdc++.
148+
# Swift build scripts pass `--sysroot=/` to Clang. By default, Clang tries to find GCC installation under sysroot,
149+
# and if it doesn't exist, under `{sysroot}/usr`. On Ubuntu 20.04 and newer, it attempts to use the GCC without the
150+
# C++ stdlib, which causes a build failure. To fix that, we tell Clang explicitly to use GCC from `/usr`.
151+
-Xcc --gcc-toolchain=/usr
152+
153+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
154+
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
155+
INSTALL_IN_COMPONENT sdk-overlay
156+
DEPENDS libstdcxx-modulemap)
File renamed without changes.
File renamed without changes.

stdlib/public/Cxx/std.swift renamed to stdlib/public/Cxx/std/std.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
@_exported import std // Clang module
14+
@_exported import Cxx // Swift module

test/Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// REQUIRES: OS=macosx || OS=linux-gnu
44

55
import CustomSequence
6-
import std
6+
import Cxx
77

88
// === SimpleSequence ===
99

test/Interop/Cxx/stdlib/overlay/custom-sequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import StdlibUnittest
77
import CustomSequence
8-
import std
8+
import Cxx
99

1010
var CxxSequenceTestSuite = TestSuite("CxxSequence")
1111

0 commit comments

Comments
 (0)