Skip to content

Commit e312ede

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 6c5ccd9 commit e312ede

File tree

9 files changed

+403
-0
lines changed

9 files changed

+403
-0
lines changed

CMakeLists.txt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#[[
2+
This source file is part of the swift-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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+
79+
if(NOT TARGET SwiftMarkdown::Markdown)
80+
add_library(SwiftMarkdown::Markdown ALIAS Markdown)
81+
endif()
82+
83+
if(NOT TARGET SwiftSyntax::SwiftSyntax)
84+
add_library(SwiftSyntax::SwiftSyntax ALIAS SwiftSyntax)
85+
add_library(SwiftSyntax::SwiftSyntaxBuilder ALIAS SwiftSyntaxBuilder)
86+
add_library(SwiftSyntax::SwiftOperators ALIAS SwiftOperators)
87+
add_library(SwiftSyntax::SwiftParser ALIAS SwiftParser)
88+
add_library(SwiftSyntax::SwiftParserDiagnostics ALIAS SwiftParserDiagnostics)
89+
endif()
90+
endif()
91+
92+
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-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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+
}

Sources/swift-format/CMakeLists.txt

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-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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-format open source project
3+
4+
Copyright (c) 2024 Apple Inc. and the swift-format 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()

0 commit comments

Comments
 (0)