diff --git a/CMakeLists.txt b/CMakeLists.txt index aadf062ae56..30eb68d4ee6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,12 +15,25 @@ project(SwiftSyntax LANGUAGES C Swift) set(SWIFT_VERSION 5) set(CMAKE_Swift_LANGUAGE_VERSION ${SWIFT_VERSION}) +if(CMAKE_VERSION VERSION_LESS 3.21) + get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY) + if(NOT parent_dir) + set(PROJECT_IS_TOP_LEVEL TRUE) + endif() +endif() + # The subdirectory into which host libraries will be installed. set(SWIFT_HOST_LIBRARIES_SUBDIRECTORY "swift/host") -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +if(NOT CMAKE_ARCHIVE_OUTPUT_DIRECTORY) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") +endif() +if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") +endif() +if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +endif() set(CMAKE_MACOSX_RPATH YES) @@ -49,22 +62,25 @@ if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT) endif() # Determine the module triple. -# FIXME: This is a hack. It's all a hack. Windows isn't setting -# CMAKE_Swift_COMPILER_TARGET. -if(CMAKE_Swift_COMPILER_TARGET) - string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" SWIFT_MODULE_TRIPLE - ${CMAKE_Swift_COMPILER_TARGET}) -elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") - if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") - set(SWIFT_MODULE_TRIPLE "x86_64-unknown-windows-msvc") - elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64") - set(SWIFT_MODULE_TRIPLE "aarch64-unknown-windows-msvc") +if("${SWIFT_HOST_MODULE_TRIPLE}" STREQUAL "") + # FIXME: This is a hack. It's all a hack. Windows isn't setting + # CMAKE_Swift_COMPILER_TARGET. + if(CMAKE_Swift_COMPILER_TARGET) + string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" SWIFT_HOST_MODULE_TRIPLE + ${CMAKE_Swift_COMPILER_TARGET}) + elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + set(SWIFT_HOST_MODULE_TRIPLE "x86_64-unknown-windows-msvc") + elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64") + set(SWIFT_HOST_MODULE_TRIPLE "aarch64-unknown-windows-msvc") + else() + message(FATAL_ERROR "Unrecognized architecture for Windows host") + endif() else() - message(FATAL_ERROR "Unrecognized architecture for Windows host") + message(FATAL_ERROR "Host module triple required") endif() endif() - -message(STATUS "Module triple: ${SWIFT_MODULE_TRIPLE}") +message(STATUS "Module triple: ${SWIFT_HOST_MODULE_TRIPLE}") # Force single-threaded-only syntax trees to eliminate the Darwin # dependency in the compiler. @@ -78,10 +94,4 @@ if (SWIFTSYNTAX_ENABLE_ASSERTIONS) endif() add_subdirectory(Sources) - -export(EXPORT SwiftSyntaxTargets - FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake" - NAMESPACE SwiftSyntax:: -) - add_subdirectory(cmake/modules) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index 3b317b0891d..a6f6c9f5bc2 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -6,31 +6,6 @@ # 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_libraries TARGET) - cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN}) - - _target_link_libraries(${TARGET} PUBLIC ${ARGS_PUBLIC}) - foreach(DEPENDENCY ${ARGS_PUBLIC}) - add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift - COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift - DEPENDS ${DEPENDENCY} - ) - target_sources(${TARGET} PRIVATE - ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift - ) - endforeach() -endfunction() - add_subdirectory(SwiftBasicFormat) add_subdirectory(SwiftSyntax) add_subdirectory(SwiftDiagnostics) diff --git a/Sources/SwiftBasicFormat/CMakeLists.txt b/Sources/SwiftBasicFormat/CMakeLists.txt index 4c6c2dcb821..05859828e78 100644 --- a/Sources/SwiftBasicFormat/CMakeLists.txt +++ b/Sources/SwiftBasicFormat/CMakeLists.txt @@ -6,12 +6,12 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftBasicFormat +add_swift_syntax_library(SwiftBasicFormat BasicFormat.swift Syntax+Extensions.swift SyntaxProtocol+Formatted.swift Trivia+FormatExtensions.swift ) -target_link_libraries(SwiftBasicFormat PUBLIC +target_link_swift_syntax_libraries(SwiftBasicFormat PUBLIC SwiftSyntax) diff --git a/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt b/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt index c6e7c42432c..f01fc1f2f15 100644 --- a/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt +++ b/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftCompilerPluginMessageHandling +add_swift_syntax_library(SwiftCompilerPluginMessageHandling CompilerPluginMessageHandler.swift Diagnostics.swift Macros.swift @@ -15,7 +15,7 @@ add_swift_host_library(SwiftCompilerPluginMessageHandling PluginMessages.swift ) -target_link_libraries(SwiftCompilerPluginMessageHandling PUBLIC +target_link_swift_syntax_libraries(SwiftCompilerPluginMessageHandling PUBLIC SwiftSyntax SwiftBasicFormat SwiftDiagnostics diff --git a/Sources/SwiftDiagnostics/CMakeLists.txt b/Sources/SwiftDiagnostics/CMakeLists.txt index 05281ea0bf3..98070223502 100644 --- a/Sources/SwiftDiagnostics/CMakeLists.txt +++ b/Sources/SwiftDiagnostics/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftDiagnostics +add_swift_syntax_library(SwiftDiagnostics Convenience.swift Diagnostic.swift DiagnosticsFormatter.swift @@ -16,5 +16,5 @@ add_swift_host_library(SwiftDiagnostics Note.swift ) -target_link_libraries(SwiftDiagnostics PUBLIC +target_link_swift_syntax_libraries(SwiftDiagnostics PUBLIC SwiftSyntax) diff --git a/Sources/SwiftIDEUtils/CMakeLists.txt b/Sources/SwiftIDEUtils/CMakeLists.txt index 309fd3efa31..3909d293150 100644 --- a/Sources/SwiftIDEUtils/CMakeLists.txt +++ b/Sources/SwiftIDEUtils/CMakeLists.txt @@ -6,12 +6,12 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftIDEUtils +add_swift_syntax_library(SwiftIDEUtils SwiftIDEUtilsCompatibility.swift Syntax+Classifications.swift SyntaxClassification.swift SyntaxClassifier.swift ) -target_link_libraries(SwiftIDEUtils PUBLIC +target_link_swift_syntax_libraries(SwiftIDEUtils PUBLIC SwiftSyntax) diff --git a/Sources/SwiftOperators/CMakeLists.txt b/Sources/SwiftOperators/CMakeLists.txt index 886590411b1..998b51abb66 100644 --- a/Sources/SwiftOperators/CMakeLists.txt +++ b/Sources/SwiftOperators/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftOperators +add_swift_syntax_library(SwiftOperators Operator.swift OperatorError+Diagnostics.swift OperatorError.swift @@ -19,7 +19,7 @@ add_swift_host_library(SwiftOperators SyntaxSynthesis.swift ) -target_link_libraries(SwiftOperators PUBLIC +target_link_swift_syntax_libraries(SwiftOperators PUBLIC SwiftSyntax SwiftDiagnostics SwiftParser) diff --git a/Sources/SwiftParser/CMakeLists.txt b/Sources/SwiftParser/CMakeLists.txt index ae849ac9deb..c5cce15b45e 100644 --- a/Sources/SwiftParser/CMakeLists.txt +++ b/Sources/SwiftParser/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftParser +add_swift_syntax_library(SwiftParser Attributes.swift Availability.swift CharacterInfo.swift @@ -52,6 +52,6 @@ add_swift_host_library(SwiftParser Lexer/UnicodeScalarExtensions.swift ) -target_link_libraries(SwiftParser PUBLIC +target_link_swift_syntax_libraries(SwiftParser PUBLIC SwiftSyntax SwiftDiagnostics) diff --git a/Sources/SwiftParserDiagnostics/CMakeLists.txt b/Sources/SwiftParserDiagnostics/CMakeLists.txt index 91be323333a..d73590bb0f8 100644 --- a/Sources/SwiftParserDiagnostics/CMakeLists.txt +++ b/Sources/SwiftParserDiagnostics/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftParserDiagnostics +add_swift_syntax_library(SwiftParserDiagnostics DiagnosticExtensions.swift LexerDiagnosticMessages.swift MissingNodesError.swift @@ -23,7 +23,7 @@ add_swift_host_library(SwiftParserDiagnostics generated/TokenNameForDiagnostics.swift ) -target_link_libraries(SwiftParserDiagnostics PUBLIC +target_link_swift_syntax_libraries(SwiftParserDiagnostics PUBLIC SwiftBasicFormat SwiftDiagnostics SwiftParser diff --git a/Sources/SwiftSyntax/CMakeLists.txt b/Sources/SwiftSyntax/CMakeLists.txt index 90c10e098bf..5e59ce0e238 100644 --- a/Sources/SwiftSyntax/CMakeLists.txt +++ b/Sources/SwiftSyntax/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftSyntax +add_swift_syntax_library(SwiftSyntax AbsolutePosition.swift AbsoluteRawSyntax.swift AbsoluteSyntaxInfo.swift diff --git a/Sources/SwiftSyntaxBuilder/CMakeLists.txt b/Sources/SwiftSyntaxBuilder/CMakeLists.txt index 36f5f1c9008..38858cee6f4 100644 --- a/Sources/SwiftSyntaxBuilder/CMakeLists.txt +++ b/Sources/SwiftSyntaxBuilder/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftSyntaxBuilder +add_swift_syntax_library(SwiftSyntaxBuilder ConvenienceInitializers.swift DeclSyntaxParseable.swift Indenter.swift @@ -30,7 +30,7 @@ add_swift_host_library(SwiftSyntaxBuilder target_compile_options(SwiftSyntaxBuilder PRIVATE $<$:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>) -target_link_libraries(SwiftSyntaxBuilder PUBLIC +target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC SwiftBasicFormat SwiftParser SwiftParserDiagnostics diff --git a/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt b/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt index ad311fa5eee..d1a635f3370 100644 --- a/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt +++ b/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt @@ -1,4 +1,4 @@ -add_swift_host_library(SwiftSyntaxMacroExpansion +add_swift_syntax_library(SwiftSyntaxMacroExpansion BasicMacroExpansionContext.swift FunctionParameterUtils.swift IndentationUtils.swift @@ -8,7 +8,7 @@ add_swift_host_library(SwiftSyntaxMacroExpansion MacroSystem.swift ) -target_link_libraries(SwiftSyntaxMacroExpansion PUBLIC +target_link_swift_syntax_libraries(SwiftSyntaxMacroExpansion PUBLIC SwiftSyntax SwiftSyntaxMacros ) diff --git a/Sources/SwiftSyntaxMacros/CMakeLists.txt b/Sources/SwiftSyntaxMacros/CMakeLists.txt index 757b5eba93f..d4dd2270d2a 100644 --- a/Sources/SwiftSyntaxMacros/CMakeLists.txt +++ b/Sources/SwiftSyntaxMacros/CMakeLists.txt @@ -6,7 +6,7 @@ # See http://swift.org/LICENSE.txt for license information # See http://swift.org/CONTRIBUTORS.txt for Swift project authors -add_swift_host_library(SwiftSyntaxMacros +add_swift_syntax_library(SwiftSyntaxMacros MacroProtocols/AccessorMacro.swift MacroProtocols/AttachedMacro.swift MacroProtocols/CodeItemMacro.swift @@ -24,6 +24,6 @@ add_swift_host_library(SwiftSyntaxMacros MacroExpansionContext.swift ) -target_link_libraries(SwiftSyntaxMacros PUBLIC +target_link_swift_syntax_libraries(SwiftSyntaxMacros PUBLIC SwiftSyntaxBuilder ) diff --git a/cmake/modules/AddSwiftHostLibrary.cmake b/cmake/modules/AddSwiftHostLibrary.cmake index 6428f806383..7a4d22e47d7 100644 --- a/cmake/modules/AddSwiftHostLibrary.cmake +++ b/cmake/modules/AddSwiftHostLibrary.cmake @@ -6,22 +6,45 @@ # 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}) + 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 + DEPENDS ${DEPENDENCY} + ) + target_sources(${TARGET} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift + ) + endforeach() +endfunction() + # Add a new host library with the given name. -function(add_swift_host_library name) +function(add_swift_syntax_library name) set(ASHL_SOURCES ${ARGN}) # Create the library target. add_library(${name} ${ASHL_SOURCES}) - # Add this to the list of exported targets. - set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS ${name}) - # 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_MODULE_TRIPLE}.swiftmodule") - set(module_interface_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftinterface") - set(module_sourceinfo_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftsourceinfo") + set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule") + set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface") + set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo") # Add a custom target to create the module directory. add_custom_command( @@ -61,6 +84,12 @@ function(add_swift_host_library name) $<$:-wmo>) endif() + if(LLVM_USE_LINKER) + target_link_options(${name} PRIVATE + "-use-ld=${LLVM_USE_LINKER}" + ) + endif() + # NOTE: workaround for CMake not setting up include flags yet set_target_properties(${name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${module_dir} @@ -70,31 +99,39 @@ function(add_swift_host_library name) 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} + PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}" + ) + endif() + get_target_property(lib_type ${name} 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 LINK_FLAGS " -Xlinker -headerpad_max_install_names") - elseif (CMAKE_SYSTEM_NAME STREQUAL Linux) - # Make some room to update paths. - set_property(TARGET ${name} APPEND PROPERTY - INSTALL_RPATH ":::::::::::::::::::::::::::::::::::::::::::::::::::::::") endif() endif() - # Install this target - install(TARGETS ${name} - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} - LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} - RUNTIME DESTINATION bin - ) + if(PROJECT_IS_TOP_LEVEL) + # Install this target + install(TARGETS ${name} + EXPORT SwiftSyntaxTargets + ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} + LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} + RUNTIME DESTINATION bin + ) - # Install the module files. - install( - DIRECTORY ${module_base} - DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} - FILES_MATCHING PATTERN "*.swiftinterface" - ) + # Install the module files. + install( + DIRECTORY ${module_base} + DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} + FILES_MATCHING PATTERN "*.swiftinterface" + ) + else() + set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) + endif() endfunction() diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt index 069c64c0af3..bb98e4ba873 100644 --- a/cmake/modules/CMakeLists.txt +++ b/cmake/modules/CMakeLists.txt @@ -1,4 +1,5 @@ -get_property(SWIFTSYNTAX_EXPORTS GLOBAL PROPERTY SWIFTSYNTAX_EXPORTS) -export(TARGETS ${SWIFTSYNTAX_EXPORTS} - FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftSyntaxConfig.cmake - NAMESPACE SwiftSyntax::) +if(PROJECT_IS_TOP_LEVEL) + export(EXPORT SwiftSyntaxTargets + FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftSyntaxConfig.cmake + NAMESPACE SwiftSyntax::) +endif()