55#
66# See https://swift.org/LICENSE.txt for license information
77
8- include (CheckCompilerFlag)
9-
10- # Generate bridging header from Swift to C++
11- # NOTE: This logic will eventually be upstreamed into CMake
12- function (_swift_generate_cxx_header_target target module header)
13- cmake_parse_arguments (ARG "" "" "SOURCES;SEARCH_PATHS;DEPENDS" ${ARGN} )
14- if (NOT ARG_SOURCES)
15- message (FATAL_ERROR "No sources provided to 'swift_generate_cxx_header_target'" )
8+
9+ # Generate the bridging header from Swift to C++
10+ #
11+ # target: the name of the target to generate headers for.
12+ # This target must build swift source files.
13+ # header: the name of the header file to generate.
14+ #
15+ # NOTE: This logic will eventually be unstreamed into CMake.
16+ function (_swift_generate_cxx_header target header)
17+ if (NOT TARGET ${target} )
18+ message (FATAL_ERROR "Target ${target} not defined." )
19+ endif ()
20+
21+ if (NOT DEFINED CMAKE_Swift_COMPILER)
22+ message (WARNING "Swift not enabled in project. Cannot generate headers for Swift files." )
23+ return ()
24+ endif ()
25+
26+ cmake_parse_arguments (ARG "" "" "SEARCH_PATHS;MODULE_NAME" ${ARGN} )
27+
28+ if (NOT ARG_MODULE_NAME)
29+ set (target_module_name $<TARGET_PROPERTY:${target} ,Swift_MODULE_NAME>)
30+ set (ARG_MODULE_NAME $<IF:$<BOOL :${target_module_name} >,${target_module_name} ,${target} >)
1631 endif ()
1732
1833 if (ARG_SEARCH_PATHS)
@@ -23,27 +38,36 @@ function(_swift_generate_cxx_header_target target module header)
2338 set (SDK_FLAGS "-sdk" "${CMAKE_OSX_SYSROOT} " )
2439 elseif (WIN32 )
2540 set (SDK_FLAGS "-sdk" "$ENV{SDKROOT} " )
41+ elseif (DEFINED ${CMAKE_SYSROOT} )
42+ set (SDK_FLAGS "-sdk" "${CMAKE_SYSROOT} " )
2643 endif ()
2744
28- add_custom_command (
29- OUTPUT
30- "${header} "
45+ cmake_path(APPEND CMAKE_CURRENT_BINARY_DIR include
46+ OUTPUT_VARIABLE base_path)
47+
48+ cmake_path(APPEND base_path ${header}
49+ OUTPUT_VARIABLE header_path)
50+
51+ set (_AllSources $<TARGET_PROPERTY:${target} ,SOURCES >)
52+ set (_SwiftSources $<FILTER :${_AllSources} ,INCLUDE ,\\.swift$>)
53+ add_custom_command (OUTPUT ${header_path}
54+ DEPENDS ${_SwiftSources}
55+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
3156 COMMAND
3257 ${CMAKE_Swift_COMPILER} -frontend -typecheck
3358 ${ARG_SEARCH_PATHS}
34- ${ARG_SOURCES }
59+ ${_SwiftSources }
3560 ${SDK_FLAGS}
36- -module-name "${module } "
61+ -module-name "${ARG_MODULE_NAME } "
3762 -cxx-interoperability-mode=default
38- -emit-clang-header-path "${header} "
39- DEPENDS
40- ${ARG_DEPENDS}
63+ -emit-clang-header-path ${header_path}
4164 COMMENT
42- "Generating '${header } '"
43- )
65+ "Generating '${header_path } '"
66+ COMMAND_EXPAND_LISTS )
4467
45- add_custom_target ("${target} "
46- DEPENDS
47- "${header} "
48- )
68+ # Added to public interface for dependees to find.
69+ target_include_directories (${target} PUBLIC ${base_path} )
70+ # Added to the target to ensure target rebuilds if header changes and is used
71+ # by sources in the target.
72+ target_sources (${target} PRIVATE ${header_path} )
4973endfunction ()
0 commit comments