diff --git a/CMakeLists.txt b/CMakeLists.txt index ed579ae5579..62fe07de47c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,14 +15,16 @@ project(SwiftSyntax LANGUAGES C Swift) set(SWIFT_VERSION 5) set(CMAKE_Swift_LANGUAGE_VERSION ${SWIFT_VERSION}) -set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) +# 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) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +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_MACOSX_RPATH YES) +include(AddSwiftHostLibrary) + # Ensure that we do not link the _StringProcessing module. But we can # only pass this flag for new-enough compilers that support it. file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift" "") @@ -42,6 +44,24 @@ if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT) $<$:-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") + else() + message(FATAL_ERROR "Unrecognized architecture for Windows host") + endif() +endif() + +message(STATUS "Module triple: ${SWIFT_MODULE_TRIPLE}") + # Force single-threaded-only syntax trees to eliminate the Darwin # dependency in the compiler. add_compile_definitions( diff --git a/Sources/IDEUtils/CMakeLists.txt b/Sources/IDEUtils/CMakeLists.txt index 08e5c3ab8bb..ef2da4abb44 100644 --- a/Sources/IDEUtils/CMakeLists.txt +++ b/Sources/IDEUtils/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_library(IDEUtils STATIC +add_swift_host_library(IDEUtils gyb_generated/SyntaxClassification.swift Syntax+Classifications.swift SyntaxClassifier.swift @@ -14,15 +14,3 @@ add_library(IDEUtils STATIC target_link_libraries(IDEUtils PUBLIC SwiftSyntax) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS IDEUtils) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(IDEUtils PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) - -install(TARGETS IDEUtils - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftBasicFormat/CMakeLists.txt b/Sources/SwiftBasicFormat/CMakeLists.txt index df6c4679bbb..1b01786b5f9 100644 --- a/Sources/SwiftBasicFormat/CMakeLists.txt +++ b/Sources/SwiftBasicFormat/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_library(SwiftBasicFormat STATIC +add_swift_host_library(SwiftBasicFormat generated/BasicFormat.swift SyntaxProtocol+Formatted.swift Trivia+Indented.swift @@ -14,15 +14,3 @@ add_library(SwiftBasicFormat STATIC target_link_libraries(SwiftBasicFormat PUBLIC SwiftSyntax) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftBasicFormat) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftBasicFormat PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) - -install(TARGETS SwiftBasicFormat - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftCompilerSupport/CMakeLists.txt b/Sources/SwiftCompilerSupport/CMakeLists.txt index 48e1e9e6097..c3d028c6329 100644 --- a/Sources/SwiftCompilerSupport/CMakeLists.txt +++ b/Sources/SwiftCompilerSupport/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_library(SwiftCompilerSupport STATIC +add_swift_host_library(SwiftCompilerSupport ConsistencyCheck.swift ) @@ -17,16 +17,3 @@ target_link_libraries(SwiftCompilerSupport PUBLIC SwiftParserDiagnostics SwiftOperators ) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftCompilerSupport) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftCompilerSupport PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES - "${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}") - -install(TARGETS SwiftCompilerSupport - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftDiagnostics/CMakeLists.txt b/Sources/SwiftDiagnostics/CMakeLists.txt index 57b281c8a39..a095a986c58 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_library(SwiftDiagnostics STATIC +add_swift_host_library(SwiftDiagnostics Diagnostic.swift DiagnosticsFormatter.swift FixIt.swift @@ -16,15 +16,3 @@ add_library(SwiftDiagnostics STATIC target_link_libraries(SwiftDiagnostics PUBLIC SwiftSyntax) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftDiagnostics) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftDiagnostics PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) - -install(TARGETS SwiftDiagnostics - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftOperators/CMakeLists.txt b/Sources/SwiftOperators/CMakeLists.txt index 7e1749ea816..b6c802991f2 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_library(SwiftOperators STATIC +add_swift_host_library(SwiftOperators Operator.swift OperatorError+Diagnostics.swift OperatorError.swift @@ -23,16 +23,3 @@ target_link_libraries(SwiftOperators PUBLIC SwiftSyntax SwiftDiagnostics SwiftParser) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftOperators) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftOperators PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES - "${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}") - -install(TARGETS SwiftOperators - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftParser/CMakeLists.txt b/Sources/SwiftParser/CMakeLists.txt index bcfccfc84ee..cef72c047a1 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_library(SwiftParser STATIC +add_swift_host_library(SwiftParser Attributes.swift Availability.swift CharacterInfo.swift @@ -40,16 +40,3 @@ add_library(SwiftParser STATIC target_link_libraries(SwiftParser PUBLIC SwiftSyntax SwiftDiagnostics) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftParser) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftParser PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES - "${CMAKE_Swift_MODULE_DIRECTORY}") - -install(TARGETS SwiftParser - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftParserDiagnostics/CMakeLists.txt b/Sources/SwiftParserDiagnostics/CMakeLists.txt index 2dd2820444d..b498dbf81d0 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_library(SwiftParserDiagnostics STATIC +add_swift_host_library(SwiftParserDiagnostics DiagnosticExtensions.swift MissingNodesError.swift ParserDiagnosticMessages.swift @@ -20,16 +20,3 @@ target_link_libraries(SwiftParserDiagnostics PUBLIC SwiftDiagnostics SwiftParser SwiftSyntax) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftParserDiagnostics) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftParserDiagnostics PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES - "${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}") - -install(TARGETS SwiftParserDiagnostics - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftSyntax/CMakeLists.txt b/Sources/SwiftSyntax/CMakeLists.txt index 15a940a0a62..174eef08f8c 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_library(SwiftSyntax STATIC +add_swift_host_library(SwiftSyntax AbsolutePosition.swift BumpPtrAllocator.swift CommonAncestor.swift @@ -54,15 +54,3 @@ add_library(SwiftSyntax STATIC gyb_generated/syntax_nodes/SyntaxStmtNodes.swift gyb_generated/syntax_nodes/SyntaxTypeNodes.swift ) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftSyntax) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftSyntax PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) - -install(TARGETS SwiftSyntax - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/SwiftSyntaxBuilder/CMakeLists.txt b/Sources/SwiftSyntaxBuilder/CMakeLists.txt index 5cfa05a2fb3..78cccaf6b6e 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_library(SwiftSyntaxBuilder STATIC +add_swift_host_library(SwiftSyntaxBuilder ConvenienceInitializers.swift HasTrailingComma.swift Indenter.swift @@ -29,16 +29,3 @@ target_link_libraries(SwiftSyntaxBuilder PUBLIC SwiftParserDiagnostics SwiftSyntax ) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftSyntaxBuilder) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(SwiftSyntaxBuilder PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES - "${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}") - -install(TARGETS SwiftSyntaxBuilder - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/Sources/_SwiftSyntaxMacros/CMakeLists.txt b/Sources/_SwiftSyntaxMacros/CMakeLists.txt index d581f78abdc..0b5112f7934 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_library(_SwiftSyntaxMacros STATIC +add_swift_host_library(_SwiftSyntaxMacros ExpressionMacro.swift Macro.swift MacroEvaluationContext.swift @@ -21,16 +21,3 @@ target_link_libraries(_SwiftSyntaxMacros PUBLIC SwiftParser SwiftSyntaxBuilder ) - -set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS _SwiftSyntaxMacros) - -# NOTE: workaround for CMake not setting up include flags yet -set_target_properties(_SwiftSyntaxMacros PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES - "${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}") - -install(TARGETS _SwiftSyntaxMacros - EXPORT SwiftSyntaxTargets - ARCHIVE DESTINATION lib - LIBRARY DESTINATION lib - RUNTIME DESTINATION bin) diff --git a/cmake/modules/AddSwiftHostLibrary.cmake b/cmake/modules/AddSwiftHostLibrary.cmake new file mode 100644 index 00000000000..c26cfbf4dbf --- /dev/null +++ b/cmake/modules/AddSwiftHostLibrary.cmake @@ -0,0 +1,65 @@ +# This source file is part of the Swift.org open source project +# +# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See http://swift.org/LICENSE.txt for license information +# See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +# Add a new host library with the given name. +function(add_swift_host_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") + + # Add a custom target to create the module directory. + add_custom_command( + TARGET ${name} + PRE_BUILD + COMMAND "${CMAKE_COMMAND}" -E make_directory ${module_base} + COMMENT "Generating module directory for ${name}") + + # Install the Swift module into the appropriate location. + set_target_properties(${name} + PROPERTIES Swift_MODULE_DIRECTORY ${module_base} + ) + + # Configure the emission of the Swift module files. + target_compile_options("${name}" PRIVATE + $<$: + -module-name;${name}; + -emit-module-path;${module_file}; + -emit-module-source-info-path;${module_sourceinfo_file} + >) + + # NOTE: workaround for CMake not setting up include flags yet + set_target_properties(${name} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${module_dir} + ) + + # 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 "*.swift*" + ) +endfunction()