Skip to content

Commit c35ff05

Browse files
committed
[CMake] Build modules into a proper ".swiftmodule" directory
1 parent 8ac6d59 commit c35ff05

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

cmake/modules/AddSwiftHostLibrary.cmake

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,48 @@
1010
function(add_swift_host_library name)
1111
set(ASHL_SOURCES ${ARGN})
1212

13+
# Create the library target.
1314
add_library(${name} ${ASHL_SOURCES})
1415

1516
# Add this to the list of exported targets.
1617
set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS ${name})
1718

19+
# Determine the module triple based on the target triple.
20+
# FIXME: We would prefer to have build-script tell us.
21+
string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" module_triple
22+
${CMAKE_Swift_COMPILER_TARGET})
23+
24+
# Determine where Swift modules will be built and installed.
25+
set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
26+
set(module_base "${module_dir}/${name}.swiftmodule")
27+
set(module_file "${module_base}/${module_triple}.swiftmodule")
28+
set(module_interface_file "${module_base}/${module_triple}.swiftinterface")
29+
set(module_sourceinfo_file "${module_base}/${module_triple}.swiftsourceinfo")
30+
31+
# Add a custom target to create the module directory.
32+
add_custom_command(
33+
TARGET ${name}
34+
PRE_BUILD
35+
COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base}
36+
COMMENT "Generating module directory for ${name}")
37+
38+
# Install the Swift module into the appropriate location.
39+
set_target_properties(${name}
40+
PROPERTIES Swift_MODULE_DIRECTORY ${module_base}
41+
)
42+
43+
# Configure the emission of the Swift module files.
44+
target_compile_options("${name}" PRIVATE
45+
$<$<COMPILE_LANGUAGE:Swift>:
46+
-module-name;${name};
47+
-emit-module-path;${module_file};
48+
-emit-module-source-info-path;${module_sourceinfo_file}
49+
>)
50+
1851
# NOTE: workaround for CMake not setting up include flags yet
1952
set_target_properties(${name} PROPERTIES
20-
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
53+
INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
54+
)
2155

2256
# Install this target
2357
install(TARGETS ${name}

0 commit comments

Comments
 (0)