From 4e7bb71decc89bbfb09a515272acefd5808a3d3a Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Wed, 1 May 2024 14:54:13 -0700 Subject: [PATCH] [CMake] Add option to set the target namespace Each target name is prefixed with SWIFTSYNTAX_TARGET_NAMESPACE Also, add SWIFTSYNTAX_EMIT_MODULE setting; 'true' for building library-evolution enabled modules with .swiftinterface, 'false' for building fragile modules, undefined is 'true' --- .../SwiftLibraryPluginProvider/CMakeLists.txt | 2 +- Sources/SwiftSyntaxBuilder/CMakeLists.txt | 2 +- Sources/VersionMarkerModules/CMakeLists.txt | 6 +- .../CMakeLists.txt | 9 +- Sources/_SwiftSyntaxCShims/CMakeLists.txt | 9 +- cmake/modules/AddSwiftHostLibrary.cmake | 146 ++++++++++-------- 6 files changed, 98 insertions(+), 76 deletions(-) diff --git a/Sources/SwiftLibraryPluginProvider/CMakeLists.txt b/Sources/SwiftLibraryPluginProvider/CMakeLists.txt index fc3d24c086b..7d05d703cff 100644 --- a/Sources/SwiftLibraryPluginProvider/CMakeLists.txt +++ b/Sources/SwiftLibraryPluginProvider/CMakeLists.txt @@ -9,7 +9,7 @@ add_swift_syntax_library(SwiftLibraryPluginProvider LibraryPluginProvider.swift ) -target_link_libraries(SwiftLibraryPluginProvider PRIVATE +target_link_swift_syntax_libraries(SwiftLibraryPluginProvider PRIVATE _SwiftLibraryPluginProviderCShims ) target_link_swift_syntax_libraries(SwiftLibraryPluginProvider PUBLIC diff --git a/Sources/SwiftSyntaxBuilder/CMakeLists.txt b/Sources/SwiftSyntaxBuilder/CMakeLists.txt index 5aa56b1164b..a05ff828483 100644 --- a/Sources/SwiftSyntaxBuilder/CMakeLists.txt +++ b/Sources/SwiftSyntaxBuilder/CMakeLists.txt @@ -28,7 +28,7 @@ add_swift_syntax_library(SwiftSyntaxBuilder # Don't depend on OSLog when we are building for the compiler. # In that case we don't want any dependencies on the SDK. -target_compile_options(SwiftSyntaxBuilder PRIVATE +target_compile_options(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntaxBuilder PRIVATE $<$:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>) target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC diff --git a/Sources/VersionMarkerModules/CMakeLists.txt b/Sources/VersionMarkerModules/CMakeLists.txt index e9550336a03..52457d79b36 100644 --- a/Sources/VersionMarkerModules/CMakeLists.txt +++ b/Sources/VersionMarkerModules/CMakeLists.txt @@ -8,9 +8,9 @@ # Version Marker Modules. # See the 'Macro Versioning.md' document for more details. -add_library(SwiftSyntax509 STATIC +add_library(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntax509 STATIC SwiftSyntax509/Empty.swift) -add_library(SwiftSyntax510 STATIC +add_library(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntax510 STATIC SwiftSyntax509/Empty.swift) -add_library(SwiftSyntax600 STATIC +add_library(${SWIFTSYNTAX_TARGET_NAMESPACE}SwiftSyntax600 STATIC SwiftSyntax509/Empty.swift) diff --git a/Sources/_SwiftLibraryPluginProviderCShims/CMakeLists.txt b/Sources/_SwiftLibraryPluginProviderCShims/CMakeLists.txt index 6efd0094d41..243ea73d1a6 100644 --- a/Sources/_SwiftLibraryPluginProviderCShims/CMakeLists.txt +++ b/Sources/_SwiftLibraryPluginProviderCShims/CMakeLists.txt @@ -1,6 +1,7 @@ -add_library(_SwiftLibraryPluginProviderCShims STATIC +set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}_SwiftLibraryPluginProviderCShims) +add_library(${target} STATIC LoadLibrary.c ) -target_include_directories(_SwiftLibraryPluginProviderCShims PUBLIC "include") -set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS _SwiftLibraryPluginProviderCShims) -install(TARGETS _SwiftLibraryPluginProviderCShims EXPORT SwiftSyntaxTargets) +target_include_directories(${target} PUBLIC "include") +set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target}) +install(TARGETS ${target} EXPORT SwiftSyntaxTargets) diff --git a/Sources/_SwiftSyntaxCShims/CMakeLists.txt b/Sources/_SwiftSyntaxCShims/CMakeLists.txt index b85340a7456..39cfa9294c2 100644 --- a/Sources/_SwiftSyntaxCShims/CMakeLists.txt +++ b/Sources/_SwiftSyntaxCShims/CMakeLists.txt @@ -1,4 +1,5 @@ -add_library(_SwiftSyntaxCShims INTERFACE) -target_include_directories(_SwiftSyntaxCShims INTERFACE "include") -set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS _SwiftSyntaxCShims) -install(TARGETS _SwiftSyntaxCShims EXPORT SwiftSyntaxTargets) +set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}_SwiftSyntaxCShims) +add_library(${target} INTERFACE) +target_include_directories(${target} INTERFACE "include") +set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target}) +install(TARGETS ${target} EXPORT SwiftSyntaxTargets) diff --git a/cmake/modules/AddSwiftHostLibrary.cmake b/cmake/modules/AddSwiftHostLibrary.cmake index 459d0f3cb4f..15b1a076e91 100644 --- a/cmake/modules/AddSwiftHostLibrary.cmake +++ b/cmake/modules/AddSwiftHostLibrary.cmake @@ -6,21 +6,33 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -# cmake generation for Swift adds an order only dependency, which matches how C-family languages -# works. In that case, however, ninja (and presumably other generators) will rebuild on header -# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the -# ABI/API of B changes. -# -# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as -# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't -# seem to be being tracked. -# -# Remove once rdar://102202478 is fixed. function(target_link_swift_syntax_libraries TARGET) - target_link_libraries(${TARGET} ${ARGN}) - cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN}) - foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS}) + set(link_type) + if(ARGS_PUBLIC) + set(link_type PUBLIC) + elseif(ARGS_PRIVATE) + set(link_type PRIVATE) + elseif(ARGS_INTERFACE) + set(link_type INTERFACE) + endif() + + string(PREPEND TARGET ${SWIFTSYNTAX_TARGET_NAMESPACE}) + list(TRANSFORM ARGS_UNPARSED_ARGUMENTS PREPEND "${SWIFTSYNTAX_TARGET_NAMESPACE}" OUTPUT_VARIABLE dependencies) + + target_link_libraries(${TARGET} ${link_type} ${dependencies}) + + # cmake generation for Swift adds an order only dependency, which matches how C-family languages + # works. In that case, however, ninja (and presumably other generators) will rebuild on header + # changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the + # ABI/API of B changes. + # + # For now workaround this by touching a file whenever B is rebuilt and then compiling that file as + # part of A. Ideally this file would be generated by each of the targets, but that dependency didn't + # seem to be being tracked. + # + # Remove once rdar://102202478 is fixed. + foreach(DEPENDENCY ${dependencies}) string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY}) add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift @@ -36,103 +48,111 @@ endfunction() function(add_swift_syntax_library name) set(ASHL_SOURCES ${ARGN}) - # Create the library target. - add_library(${name} ${ASHL_SOURCES}) - - # Determine where Swift modules will be built and installed. - set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) - set(module_base "${module_dir}/${name}.swiftmodule") - set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule") - set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface") - set(module_private_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.private.swiftinterface") - set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo") + set(target ${SWIFTSYNTAX_TARGET_NAMESPACE}${name}) - # Add a custom target to create the module directory. - add_custom_command( - TARGET ${name} + # Create the library target. + add_library(${target} ${ASHL_SOURCES}) + + if(NOT DEFINED SWIFTSYNTAX_EMIT_MODULE OR SWIFTSYNTAX_EMIT_MODULE) + # Determine where Swift modules will be built and installed. + set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + set(module_base "${module_dir}/${name}.swiftmodule") + set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule") + set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface") + set(module_private_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.private.swiftinterface") + set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo") + + # Add a custom target to create the module directory. + add_custom_command( + TARGET ${target} PRE_BUILD COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base} - COMMENT "Generating module directory for ${name}") + COMMENT "Generating module directory for ${target}") + + # Configure the emission of the Swift module files. + target_compile_options("${target}" PRIVATE + $<$: + -DRESILIENT_LIBRARIES; + -enable-library-evolution; + -emit-module-path;${module_file}; + -emit-module-source-info-path;${module_sourceinfo_file}; + -emit-module-interface-path;${module_interface_file}; + -emit-private-module-interface-path;${module_private_interface_file} + >) + else() + set(module_dir ${CMAKE_CURRENT_BINARY_DIR}) + set(module_base "${module_dir}/${name}.swiftmodule") + set(module_file "${module_file}") + endif() # Touch the library and objects to workaround their mtime not being updated # when there are no real changes (eg. a file was updated with a comment). # Ideally this should be done in the driver, which could only update the # files that have changed. add_custom_command( - TARGET ${name} - POST_BUILD - COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $ $ "${module_base}" - COMMAND_EXPAND_LISTS - COMMENT "Update mtime of library outputs workaround") - - # Install the Swift module into the appropriate location. - set_target_properties(${name} - PROPERTIES Swift_MODULE_DIRECTORY ${module_dir} + TARGET ${target} + POST_BUILD + COMMAND "${CMAKE_COMMAND}" -E touch_nocreate $ $ "${module_base}" + COMMAND_EXPAND_LISTS + COMMENT "Update mtime of library outputs workaround") + + set_target_properties(${target} PROPERTIES + Swift_MODULE_NAME ${name} + Swift_MODULE_DIRECTORY ${module_dir} + INTERFACE_INCLUDE_DIRECTORIES ${module_dir} ) # Configure the emission of the Swift module files. - target_compile_options("${name}" PRIVATE + target_compile_options("${target}" PRIVATE $<$: - -DRESILIENT_LIBRARIES; - -module-name;${name}; - -enable-library-evolution; - -emit-module-path;${module_file}; - -emit-module-source-info-path;${module_sourceinfo_file}; - -emit-module-interface-path;${module_interface_file}; - -emit-private-module-interface-path;${module_private_interface_file} + "SHELL:-module-name ${name}" + "SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name}" >) - if(SWIFT_MODULE_ABI_NAME_PREFIX) - # ABI name prefix. this can be used to avoid name conflicts. - target_compile_options("${name}" PRIVATE - $<$: - "SHELL:-Xfrontend -module-abi-name -Xfrontend ${SWIFT_MODULE_ABI_NAME_PREFIX}${name}" - >) - endif() if(CMAKE_VERSION VERSION_LESS 3.26.0 AND SWIFT_SYNTAX_ENABLE_WMO_PRE_3_26) - target_compile_options(${name} PRIVATE + target_compile_options(${target} PRIVATE $<$:-wmo>) endif() - target_compile_options(${name} PRIVATE + target_compile_options(${target} PRIVATE $<$:-color-diagnostics> ) if(LLVM_USE_LINKER) - target_link_options(${name} PRIVATE + target_link_options(${target} PRIVATE "-use-ld=${LLVM_USE_LINKER}" ) endif() # NOTE: workaround for CMake not setting up include flags yet - set_target_properties(${name} PROPERTIES + set_target_properties(${target} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${module_dir} ) - set_target_properties(${name} PROPERTIES + set_target_properties(${target} PROPERTIES BUILD_WITH_INSTALL_RPATH YES ) if(SWIFT_HOST_LIBRARIES_RPATH) # Don't add builder's stdlib RPATH automatically. - target_compile_options(${name} PRIVATE -no-toolchain-stdlib-rpath) - set_property(TARGET ${name} + target_compile_options(${target} PRIVATE -no-toolchain-stdlib-rpath) + set_property(TARGET ${target} PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}" ) endif() - get_target_property(lib_type ${name} TYPE) + get_target_property(lib_type ${target} TYPE) if(lib_type STREQUAL SHARED_LIBRARY) if (CMAKE_SYSTEM_NAME STREQUAL Darwin) # Allow install_name_tool to update paths (for rdar://109473564) - set_property(TARGET ${name} APPEND_STRING PROPERTY + set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -Xlinker -headerpad_max_install_names") endif() endif() if(PROJECT_IS_TOP_LEVEL OR SWIFT_SYNTAX_INSTALL_TARGETS) # Install this target - install(TARGETS ${name} + install(TARGETS ${target} EXPORT SwiftSyntaxTargets ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} @@ -146,7 +166,7 @@ function(add_swift_syntax_library name) FILES_MATCHING PATTERN "*.swiftinterface" ) else() - set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) + set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${target}) endif() - add_library(SwiftSyntax::${name} ALIAS ${name}) + add_library(SwiftSyntax::${target} ALIAS ${target}) endfunction()