Skip to content

Commit e34a624

Browse files
committed
build: introduce a CMake based build for swift-format
This is in preparation to use SwiftFormat from SourceKit-LSP which is distributed as part of the toolchain. On Windows, we are now able to build swift-format against the shared Swift Syntax package, yielding an overall size reduction: SPM swift-format.exe: 75,683,840 b CMake swift-format.exe: 830,464 b SwiftFormat.dll: 7,818,240 b Net Savings: 67,035,136 b
1 parent bbb3abc commit e34a624

File tree

9 files changed

+387
-0
lines changed

9 files changed

+387
-0
lines changed

CMakeLists.txt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
cmake_minimum_required(VERSION 3.19.0)
11+
12+
if(POLICY CMP0077)
13+
cmake_policy(SET CMP0077 NEW)
14+
endif()
15+
if(POLICY CMP0091)
16+
cmake_policy(SET CMP0091 NEW)
17+
endif()
18+
19+
project(SwiftFormat
20+
LANGUAGES C Swift)
21+
22+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
23+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
24+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
25+
26+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
27+
set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
28+
29+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
30+
31+
include(FetchContent)
32+
include(GNUInstallDirs)
33+
include(SwiftSupport)
34+
35+
find_package(Foundation CONFIG)
36+
37+
set(_SF_VENDOR_DEPENDENCIES)
38+
39+
set(BUILD_EXAMPLES NO)
40+
set(BUILD_TESTING NO)
41+
42+
find_package(ArgumentParser CONFIG)
43+
if(NOT ArgumentParser_FOUND)
44+
FetchContent_Declare(ArgumentParser
45+
GIT_REPOSITORY https://github.com/apple/swift-argument-parser
46+
GIT_TAG 1.2.3)
47+
list(APPEND _SF_VENDOR_DEPENDENCIES ArgumentParser)
48+
endif()
49+
50+
find_package(cmark-gfm CONFIG)
51+
if(NOT cmark-gfm_FOUND)
52+
FetchContent_Declare(cmark-gfm
53+
GIT_REPOSITORY https://github.com/apple/swift-cmark
54+
GIT_TAG gfm)
55+
list(APPEND _SF_VENDOR_DEPENDENCIES cmark-gfm)
56+
endif()
57+
58+
find_package(SwiftMarkdown CONFIG)
59+
if(NOT SwiftMarkdown_FOUND)
60+
# TODO(compnerd) we need a latest version for now as we need the CMake support
61+
# which is untagged.
62+
FetchContent_Declare(Markdown
63+
GIT_REPOSITORY https://github.com/apple/swift-markdown
64+
GIT_TAG main)
65+
list(APPEND _SF_VENDOR_DEPENDENCIES Markdown)
66+
endif()
67+
68+
find_package(SwiftSyntax CONFIG)
69+
if(NOT SwiftSyntax_FOUND)
70+
FetchContent_Declare(Syntax
71+
GIT_REPOSITORY https://github.com/apple/swift-syntax
72+
GIT_TAG main)
73+
list(APPEND _SF_VENDOR_DEPENDENCIES Syntax)
74+
endif()
75+
76+
if(_SF_VENDOR_DEPENDENCIES)
77+
FetchContent_MakeAvailable(${_SF_VENDOR_DEPENDENCIES})
78+
endif()
79+
80+
add_subdirectory(Sources)

Sources/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_subdirectory(SwiftFormat)
11+
add_subdirectory(_SwiftFormatInstructionCounter)
12+
add_subdirectory(swift-format)

Sources/SwiftFormat/CMakeLists.txt

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(SwiftFormat
11+
API/Configuration+Default.swift
12+
API/Configuration.swift
13+
API/DebugOptions.swift
14+
API/Finding.swift
15+
API/FindingCategorizing.swift
16+
API/Indent.swift
17+
API/SwiftFormatError.swift
18+
API/SwiftFormatter.swift
19+
API/SwiftLinter.swift
20+
Core/Context.swift
21+
Core/DocumentationComment.swift
22+
Core/DocumentationCommentText.swift
23+
Core/Finding+Convenience.swift
24+
Core/FindingEmitter.swift
25+
Core/FormatPipeline.swift
26+
Core/FunctionDeclSyntax+Convenience.swift
27+
Core/ImportsXCTestVisitor.swift
28+
Core/LazySplitSequence.swift
29+
Core/LintPipeline.swift
30+
Core/ModifierListSyntax+Convenience.swift
31+
Core/Parsing.swift
32+
Core/Pipelines+Generated.swift
33+
Core/RememberingIterator.swift
34+
Core/Rule.swift
35+
Core/RuleBasedFindingCategory.swift
36+
Core/RuleMask.swift
37+
Core/RuleNameCache+Generated.swift
38+
Core/RuleRegistry+Generated.swift
39+
Core/RuleState.swift
40+
Core/SyntaxFormatRule.swift
41+
Core/SyntaxLintRule.swift
42+
Core/SyntaxProtocol+Convenience.swift
43+
Core/Trivia+Convenience.swift
44+
Core/WithSemicolonSyntax.swift
45+
PrettyPrint/Comment.swift
46+
PrettyPrint/Indent+Length.swift
47+
PrettyPrint/PrettyPrint.swift
48+
PrettyPrint/PrettyPrintFindingCategory.swift
49+
PrettyPrint/Token.swift
50+
PrettyPrint/TokenStreamCreator.swift
51+
PrettyPrint/Verbatim.swift
52+
PrettyPrint/WhitespaceFindingCategory.swift
53+
PrettyPrint/WhitespaceLinter.swift
54+
Rules/AllPublicDeclarationsHaveDocumentation.swift
55+
Rules/AlwaysUseLiteralForEmptyCollectionInit.swift
56+
Rules/AlwaysUseLowerCamelCase.swift
57+
Rules/AmbiguousTrailingClosureOverload.swift
58+
Rules/BeginDocumentationCommentWithOneLineSummary.swift
59+
Rules/DoNotUseSemicolons.swift
60+
Rules/DontRepeatTypeInStaticProperties.swift
61+
Rules/FileScopedDeclarationPrivacy.swift
62+
Rules/FullyIndirectEnum.swift
63+
Rules/GroupNumericLiterals.swift
64+
Rules/IdentifiersMustBeASCII.swift
65+
Rules/NeverForceUnwrap.swift
66+
Rules/NeverUseForceTry.swift
67+
Rules/NeverUseImplicitlyUnwrappedOptionals.swift
68+
Rules/NoAccessLevelOnExtensionDeclaration.swift
69+
Rules/NoAssignmentInExpressions.swift
70+
Rules/NoBlockComments.swift
71+
Rules/NoCasesWithOnlyFallthrough.swift
72+
Rules/NoEmptyTrailingClosureParentheses.swift
73+
Rules/NoLabelsInCasePatterns.swift
74+
Rules/NoLeadingUnderscores.swift
75+
Rules/NoParensAroundConditions.swift
76+
Rules/NoPlaygroundLiterals.swift
77+
Rules/NoVoidReturnOnFunctionSignature.swift
78+
Rules/OmitExplicitReturns.swift
79+
Rules/OneCasePerLine.swift
80+
Rules/OneVariableDeclarationPerLine.swift
81+
Rules/OnlyOneTrailingClosureArgument.swift
82+
Rules/OrderedImports.swift
83+
Rules/ReplaceForEachWithForLoop.swift
84+
Rules/ReturnVoidInsteadOfEmptyTuple.swift
85+
Rules/TypeNamesShouldBeCapitalized.swift
86+
Rules/UseEarlyExits.swift
87+
Rules/UseExplicitNilCheckInConditions.swift
88+
Rules/UseLetInEveryBoundCaseVariable.swift
89+
Rules/UseShorthandTypeNames.swift
90+
Rules/UseSingleLinePropertyGetter.swift
91+
Rules/UseSynthesizedInitializer.swift
92+
Rules/UseTripleSlashForDocumentationComments.swift
93+
Rules/UseWhereClausesInForLoops.swift
94+
Rules/ValidateDocumentationComments.swift)
95+
target_link_libraries(SwiftFormat PUBLIC
96+
SwiftMarkdown::Markdown
97+
SwiftSyntax::SwiftSyntax
98+
SwiftSyntax::SwiftSyntaxBuilder
99+
SwiftSyntax::SwiftOperators
100+
SwiftSyntax::SwiftParser
101+
SwiftSyntax::SwiftParserDiagnostics
102+
libcmark-gfm
103+
libcmark-gfm-extensions)
104+
105+
_install_target(SwiftFormat)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(_SwiftFormatInstructionCounter STATIC
11+
src/InstructionsExecuted.c)
12+
target_include_directories(_SwiftFormatInstructionCounter PUBLIC
13+
include)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module _SwiftFormatInstructionCounter {
2+
header "InstructionsExecuted.h"
3+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_executable(swift-format
11+
PrintVersion.swift
12+
SwiftFormatCommand.swift
13+
VersionOptions.swift
14+
Frontend/ConfigurationLoader.swift
15+
Frontend/FormatFrontend.swift
16+
Frontend/Frontend.swift
17+
Frontend/LintFrontend.swift
18+
Subcommands/DumpConfiguration.swift
19+
Subcommands/Format.swift
20+
Subcommands/Lint.swift
21+
Subcommands/LintFormatOptions.swift
22+
Subcommands/PerformanceMeasurement.swift
23+
Utilities/Diagnostic.swift
24+
Utilities/DiagnosticsEngine.swift
25+
Utilities/FileHandleTextOutputStream.swift
26+
Utilities/FileIterator.swift
27+
Utilities/FormatError.swift
28+
Utilities/StderrDiagnosticPrinter.swift
29+
Utilities/TTY.swift)
30+
target_link_libraries(swift-format PRIVATE
31+
_SwiftFormatInstructionCounter
32+
ArgumentParser
33+
SwiftFormat
34+
SwiftParser
35+
SwiftSyntax)
36+
37+
_install_target(swift-format)

cmake/modules/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
set(SWIFT_FORMAT_EXPORTS_FILE
11+
${CMAKE_CURRENT_BINARY_DIR}/SwiftFormatExports.cmake)
12+
13+
configure_file(SwiftFormatConfig.cmake.in
14+
${CMAKE_CURRENT_BINARY_DIR}/SwiftFormatConfig.cmake)
15+
16+
get_property(SWIFT_FORMAT_EXPORTS GLOBAL PROPERTY SWIFT_FORMAT_EXPORTS)
17+
export(TARGETS ${SWIFT_FORMAT_EXPORTS}
18+
NAMESPACE SwiftFormat::
19+
FILE ${SWIFT_FORMAT_EXPORTS_FILE}
20+
EXPORT_LINK_INTERFACE_LIBRARIES)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
if(NOT TARGET SwiftFormat)
11+
include("@SWIFT_FORMAT_EXPORTS_FILE@")
12+
endif()

cmake/modules/SwiftSupport.cmake

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#[[
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
# Returns the current architecture name in a variable
11+
#
12+
# Usage:
13+
# get_swift_host_arch(result_var_name)
14+
#
15+
# If the current architecture is supported by Swift, sets ${result_var_name}
16+
# with the sanitized host architecture name derived from CMAKE_SYSTEM_PROCESSOR.
17+
function(get_swift_host_arch result_var_name)
18+
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
19+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
20+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
21+
set("${result_var_name}" "aarch64" PARENT_SCOPE)
22+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
23+
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
24+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
25+
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
26+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
27+
set("${result_var_name}" "s390x" PARENT_SCOPE)
28+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
29+
set("${result_var_name}" "armv6" PARENT_SCOPE)
30+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
31+
set("${result_var_name}" "armv7" PARENT_SCOPE)
32+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
33+
set("${result_var_name}" "armv7" PARENT_SCOPE)
34+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "amd64")
35+
set("${result_var_name}" "amd64" PARENT_SCOPE)
36+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
37+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
38+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
39+
set("${result_var_name}" "itanium" PARENT_SCOPE)
40+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
41+
set("${result_var_name}" "i686" PARENT_SCOPE)
42+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
43+
set("${result_var_name}" "i686" PARENT_SCOPE)
44+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32")
45+
set("${result_var_name}" "wasm32" PARENT_SCOPE)
46+
else()
47+
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
48+
endif()
49+
endfunction()
50+
51+
# Returns the os name in a variable
52+
#
53+
# Usage:
54+
# get_swift_host_os(result_var_name)
55+
#
56+
#
57+
# Sets ${result_var_name} with the converted OS name derived from
58+
# CMAKE_SYSTEM_NAME.
59+
function(get_swift_host_os result_var_name)
60+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
61+
set(${result_var_name} macosx PARENT_SCOPE)
62+
else()
63+
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
64+
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
65+
endif()
66+
endfunction()
67+
68+
function(_install_target module)
69+
get_swift_host_os(swift_os)
70+
get_target_property(type ${module} TYPE)
71+
72+
if(type STREQUAL STATIC_LIBRARY)
73+
set(swift swift_static)
74+
else()
75+
set(swift swift)
76+
endif()
77+
78+
install(TARGETS ${module}
79+
ARCHIVE DESTINATION lib/${swift}/${swift_os}
80+
LIBRARY DESTINATION lib/${swift}/${swift_os}
81+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
82+
if(type STREQUAL EXECUTABLE)
83+
return()
84+
endif()
85+
86+
get_swift_host_arch(swift_arch)
87+
get_target_property(module_name ${module} Swift_MODULE_NAME)
88+
if(NOT module_name)
89+
set(module_name ${module})
90+
endif()
91+
92+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
93+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
94+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
95+
RENAME ${swift_arch}.swiftdoc)
96+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
97+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
98+
RENAME ${swift_arch}.swiftmodule)
99+
else()
100+
install(FILES
101+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
102+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
103+
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
104+
endif()
105+
endfunction()

0 commit comments

Comments
 (0)