Skip to content

[CMake] Updates to allow inclusion using FetchContent #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 28, 2023
54 changes: 32 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow this. This is meant to be a user controlled option, what were you expecting? Or is this is a bug CMake that we should also try to get fixed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is just what was already there, I don't think this is a CMake bug. For non-Windows it's all being set via build-script.

Copy link
Contributor

@bnbarham bnbarham Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to expand on this... The reason we need the module triple at all is to output the module in the subfolder format rather than the flat format. Ideally we'll get that fixed at some point in CMake itself 😅. CMAKE_Swift_COMPILER_TARGET is being set in build-script, but I assume that if this was added, it wasn't set for Windows somewhere (in PR testing?). SWIFT_HOST_MODULE_TRIPLE is being passed from the Swift build, so this path would only be taken in the standalone. Not ideal, but also not really anything to do with the fetchcontent change itself.

# 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.
Expand All @@ -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)
25 changes: 0 additions & 25 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftBasicFormat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftDiagnostics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,5 +16,5 @@ add_swift_host_library(SwiftDiagnostics
Note.swift
)

target_link_libraries(SwiftDiagnostics PUBLIC
target_link_swift_syntax_libraries(SwiftDiagnostics PUBLIC
SwiftSyntax)
4 changes: 2 additions & 2 deletions Sources/SwiftIDEUtils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions Sources/SwiftOperators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
4 changes: 2 additions & 2 deletions Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions Sources/SwiftParserDiagnostics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntaxBuilder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +30,7 @@ add_swift_host_library(SwiftSyntaxBuilder
target_compile_options(SwiftSyntaxBuilder PRIVATE
$<$<COMPILE_LANGUAGE:Swift>:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>)

target_link_libraries(SwiftSyntaxBuilder PUBLIC
target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC
SwiftBasicFormat
SwiftParser
SwiftParserDiagnostics
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_swift_host_library(SwiftSyntaxMacroExpansion
add_swift_syntax_library(SwiftSyntaxMacroExpansion
BasicMacroExpansionContext.swift
FunctionParameterUtils.swift
IndentationUtils.swift
Expand All @@ -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
)
4 changes: 2 additions & 2 deletions Sources/SwiftSyntaxMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +24,6 @@ add_swift_host_library(SwiftSyntaxMacros
MacroExpansionContext.swift
)

target_link_libraries(SwiftSyntaxMacros PUBLIC
target_link_swift_syntax_libraries(SwiftSyntaxMacros PUBLIC
SwiftSyntaxBuilder
)
Loading