66# See http://swift.org/LICENSE.txt for license information
77# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88
9- # cmake generation for Swift adds an order only dependency, which matches how C-family languages
10- # works. In that case, however, ninja (and presumably other generators) will rebuild on header
11- # changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
12- # ABI/API of B changes.
13- #
14- # For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
15- # part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
16- # seem to be being tracked.
17- #
18- # Remove once rdar://102202478 is fixed.
199function (target_link_swift_syntax_libraries TARGET )
20- target_link_libraries (${TARGET} ${ARGN} )
21-
2210 cmake_parse_arguments (ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN} )
23- foreach (DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS} )
11+ set (link_type)
12+ if (ARGS_PUBLIC)
13+ set (link_type PUBLIC )
14+ elseif (ARGS_PRIVATE)
15+ set (link_type PRIVATE )
16+ elseif (ARGS_INTERFACE)
17+ set (link_type INTERFACE )
18+ endif ()
19+
20+ string (PREPEND TARGET ${SWIFTSYNTAX_TARGET_NAMESPACE} )
21+ list (TRANSFORM ARGS_UNPARSED_ARGUMENTS PREPEND "${SWIFTSYNTAX_TARGET_NAMESPACE} " OUTPUT_VARIABLE dependencies)
22+
23+ target_link_libraries (${TARGET} ${link_type} ${dependencies} )
24+
25+ # cmake generation for Swift adds an order only dependency, which matches how C-family languages
26+ # works. In that case, however, ninja (and presumably other generators) will rebuild on header
27+ # changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the
28+ # ABI/API of B changes.
29+ #
30+ # For now workaround this by touching a file whenever B is rebuilt and then compiling that file as
31+ # part of A. Ideally this file would be generated by each of the targets, but that dependency didn't
32+ # seem to be being tracked.
33+ #
34+ # Remove once rdar://102202478 is fixed.
35+ foreach (DEPENDENCY ${dependencies} )
2436 string (REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY} )
2537 add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /forced-${sanitized} -dep.swift
2638 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR} /forced-${sanitized} -dep.swift
@@ -36,103 +48,111 @@ endfunction()
3648function (add_swift_syntax_library name )
3749 set (ASHL_SOURCES ${ARGN} )
3850
39- # Create the library target.
40- add_library (${name} ${ASHL_SOURCES} )
41-
42- # Determine where Swift modules will be built and installed.
43- set (module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
44- set (module_base "${module_dir} /${name} .swiftmodule" )
45- set (module_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .swiftmodule" )
46- set (module_interface_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .swiftinterface" )
47- set (module_private_interface_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .private.swiftinterface" )
48- set (module_sourceinfo_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .swiftsourceinfo" )
51+ set (target ${SWIFTSYNTAX_TARGET_NAMESPACE}${name} )
4952
50- # Add a custom target to create the module directory.
51- add_custom_command (
52- TARGET ${name}
53+ # Create the library target.
54+ add_library (${target} ${ASHL_SOURCES} )
55+
56+ if (NOT DEFINED SWIFTSYNTAX_EMIT_MODULE OR SWIFTSYNTAX_EMIT_MODULE)
57+ # Determine where Swift modules will be built and installed.
58+ set (module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
59+ set (module_base "${module_dir} /${name} .swiftmodule" )
60+ set (module_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .swiftmodule" )
61+ set (module_interface_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .swiftinterface" )
62+ set (module_private_interface_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .private.swiftinterface" )
63+ set (module_sourceinfo_file "${module_base} /${SWIFT_HOST_MODULE_TRIPLE} .swiftsourceinfo" )
64+
65+ # Add a custom target to create the module directory.
66+ add_custom_command (
67+ TARGET ${target}
5368 PRE_BUILD
5469 COMMAND "${CMAKE_COMMAND} " -E make_directory ${module_base}
55- COMMENT "Generating module directory for ${name} " )
70+ COMMENT "Generating module directory for ${target} " )
71+
72+ # Configure the emission of the Swift module files.
73+ target_compile_options ("${target} " PRIVATE
74+ $<$<COMPILE_LANGUAGE:Swift>:
75+ -DRESILIENT_LIBRARIES;
76+ -enable-library-evolution;
77+ -emit-module-path ;${module_file} ;
78+ -emit-module-source -info-path ;${module_sourceinfo_file} ;
79+ -emit-module-interface -path ;${module_interface_file} ;
80+ -emit-private -module-interface -path ;${module_private_interface_file}
81+ >)
82+ else ()
83+ set (module_dir ${CMAKE_CURRENT_BINARY_DIR} )
84+ set (module_base "${module_dir} /${name} .swiftmodule" )
85+ set (module_file "${module_file} " )
86+ endif ()
5687
5788 # Touch the library and objects to workaround their mtime not being updated
5889 # when there are no real changes (eg. a file was updated with a comment).
5990 # Ideally this should be done in the driver, which could only update the
6091 # files that have changed.
6192 add_custom_command (
62- TARGET ${name}
63- POST_BUILD
64- COMMAND "${CMAKE_COMMAND} " -E touch_nocreate $<TARGET_FILE:${name} > $<TARGET_OBJECTS:${name} > "${module_base} "
65- COMMAND_EXPAND_LISTS
66- COMMENT "Update mtime of library outputs workaround" )
67-
68- # Install the Swift module into the appropriate location.
69- set_target_properties (${name}
70- PROPERTIES Swift_MODULE_DIRECTORY ${module_dir}
93+ TARGET ${target}
94+ POST_BUILD
95+ COMMAND "${CMAKE_COMMAND} " -E touch_nocreate $<TARGET_FILE:${target} > $<TARGET_OBJECTS:${target} > "${module_base} "
96+ COMMAND_EXPAND_LISTS
97+ COMMENT "Update mtime of library outputs workaround" )
98+
99+ set_target_properties (${target} PROPERTIES
100+ Swift_MODULE_NAME ${name}
101+ Swift_MODULE_DIRECTORY ${module_dir}
102+ INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
71103 )
72104
73105 # Configure the emission of the Swift module files.
74- target_compile_options ("${name } " PRIVATE
106+ target_compile_options ("${target } " PRIVATE
75107 $<$<COMPILE_LANGUAGE:Swift>:
76- -DRESILIENT_LIBRARIES;
77- -module-name ;${name} ;
78- -enable-library-evolution;
79- -emit-module-path ;${module_file} ;
80- -emit-module-source -info-path ;${module_sourceinfo_file} ;
81- -emit-module-interface -path ;${module_interface_file} ;
82- -emit-private -module-interface -path ;${module_private_interface_file}
108+ "SHELL:-module-name ${name} "
109+ "SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name} "
83110 >)
84- if (SWIFT_MODULE_ABI_NAME_PREFIX)
85- # ABI name prefix. this can be used to avoid name conflicts.
86- target_compile_options ("${name} " PRIVATE
87- $<$<COMPILE_LANGUAGE:Swift>:
88- "SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name} "
89- >)
90- endif ()
91111
92112 if (CMAKE_VERSION VERSION_LESS 3.26.0 AND SWIFT_SYNTAX_ENABLE_WMO_PRE_3_26)
93- target_compile_options (${name } PRIVATE
113+ target_compile_options (${target } PRIVATE
94114 $<$<COMPILE_LANGUAGE:Swift>:-wmo>)
95115 endif ()
96116
97- target_compile_options (${name } PRIVATE
117+ target_compile_options (${target } PRIVATE
98118 $<$<COMPILE_LANGUAGE:Swift>:-color-diagnostics>
99119 )
100120
101121 if (LLVM_USE_LINKER)
102- target_link_options (${name } PRIVATE
122+ target_link_options (${target } PRIVATE
103123 "-use-ld=${LLVM_USE_LINKER} "
104124 )
105125 endif ()
106126
107127 # NOTE: workaround for CMake not setting up include flags yet
108- set_target_properties (${name } PROPERTIES
128+ set_target_properties (${target } PROPERTIES
109129 INTERFACE_INCLUDE_DIRECTORIES ${module_dir}
110130 )
111131
112- set_target_properties (${name } PROPERTIES
132+ set_target_properties (${target } PROPERTIES
113133 BUILD_WITH_INSTALL_RPATH YES
114134 )
115135
116136 if (SWIFT_HOST_LIBRARIES_RPATH)
117137 # Don't add builder's stdlib RPATH automatically.
118- target_compile_options (${name } PRIVATE -no -toolchain-stdlib-rpath)
119- set_property (TARGET ${name }
138+ target_compile_options (${target } PRIVATE -no -toolchain-stdlib-rpath)
139+ set_property (TARGET ${target }
120140 PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH} "
121141 )
122142 endif ()
123143
124- get_target_property (lib_type ${name } TYPE )
144+ get_target_property (lib_type ${target } TYPE )
125145 if (lib_type STREQUAL SHARED_LIBRARY)
126146 if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
127147 # Allow install_name_tool to update paths (for rdar://109473564)
128- set_property (TARGET ${name } APPEND_STRING PROPERTY
148+ set_property (TARGET ${target } APPEND_STRING PROPERTY
129149 LINK_FLAGS " -Xlinker -headerpad_max_install_names" )
130150 endif ()
131151 endif ()
132152
133153 if (PROJECT_IS_TOP_LEVEL OR SWIFT_SYNTAX_INSTALL_TARGETS)
134154 # Install this target
135- install (TARGETS ${name }
155+ install (TARGETS ${target }
136156 EXPORT SwiftSyntaxTargets
137157 ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
138158 LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}
@@ -146,7 +166,7 @@ function(add_swift_syntax_library name)
146166 FILES_MATCHING PATTERN "*.swiftinterface"
147167 )
148168 else ()
149- set_property (GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name } )
169+ set_property (GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target } )
150170 endif ()
151- add_library (SwiftSyntax::${name } ALIAS ${name } )
171+ add_library (SwiftSyntax::${target } ALIAS ${target } )
152172endfunction ()
0 commit comments