Skip to content

Commit

Permalink
build: introduce a CMake based build for swift-format
Browse files Browse the repository at this point in the history
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
  • Loading branch information
compnerd committed Jan 26, 2024
1 parent bbb3abc commit e5b594c
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 0 deletions.
92 changes: 92 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

cmake_minimum_required(VERSION 3.19.0)

if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()
if(POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif()

project(SwiftFormat
LANGUAGES C Swift)

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_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)

include(FetchContent)
include(GNUInstallDirs)
include(SwiftSupport)

find_package(Foundation CONFIG)

set(_SF_VENDOR_DEPENDENCIES)

set(BUILD_EXAMPLES NO)
set(BUILD_TESTING NO)

find_package(ArgumentParser CONFIG)
if(NOT ArgumentParser_FOUND)
FetchContent_Declare(ArgumentParser
GIT_REPOSITORY https://github.com/apple/swift-argument-parser
GIT_TAG 1.2.3)
list(APPEND _SF_VENDOR_DEPENDENCIES ArgumentParser)
endif()

find_package(cmark-gfm CONFIG)
if(NOT cmark-gfm_FOUND)
FetchContent_Declare(cmark-gfm
GIT_REPOSITORY https://github.com/apple/swift-cmark
GIT_TAG gfm)
list(APPEND _SF_VENDOR_DEPENDENCIES cmark-gfm)
endif()

find_package(SwiftMarkdown CONFIG)
if(NOT SwiftMarkdown_FOUND)
# TODO(compnerd) we need a latest version for now as we need the CMake support
# which is untagged.
FetchContent_Declare(Markdown
GIT_REPOSITORY https://github.com/apple/swift-markdown
GIT_TAG main)
list(APPEND _SF_VENDOR_DEPENDENCIES Markdown)
endif()

find_package(SwiftSyntax CONFIG)
if(NOT SwiftSyntax_FOUND)
FetchContent_Declare(Syntax
GIT_REPOSITORY https://github.com/apple/swift-syntax
GIT_TAG main)
list(APPEND _SF_VENDOR_DEPENDENCIES Syntax)
endif()

if(_SF_VENDOR_DEPENDENCIES)
FetchContent_MakeAvailable(${_SF_VENDOR_DEPENDENCIES})

if(NOT TARGET SwiftMarkdown::Markdown)
add_library(SwiftMarkdown::Markdown ALIAS Markdown)
endif()

if(NOT TARGET SwiftSyntax::SwiftSyntax)
add_library(SwiftSyntax::SwiftSyntax ALIAS SwiftSyntax)
add_library(SwiftSyntax::SwiftSyntaxBuilder ALIAS SwiftSyntaxBuilder)
add_library(SwiftSyntax::SwiftOperators ALIAS SwiftOperators)
add_library(SwiftSyntax::SwiftParser ALIAS SwiftParser)
add_library(SwiftSyntax::SwiftParserDiagnostics ALIAS SwiftParserDiagnostics)
endif()
endif()

add_subdirectory(Sources)
12 changes: 12 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

add_subdirectory(SwiftFormat)
add_subdirectory(_SwiftFormatInstructionCounter)
add_subdirectory(swift-format)
105 changes: 105 additions & 0 deletions Sources/SwiftFormat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

add_library(SwiftFormat
API/Configuration+Default.swift
API/Configuration.swift
API/DebugOptions.swift
API/Finding.swift
API/FindingCategorizing.swift
API/Indent.swift
API/SwiftFormatError.swift
API/SwiftFormatter.swift
API/SwiftLinter.swift
Core/Context.swift
Core/DocumentationComment.swift
Core/DocumentationCommentText.swift
Core/Finding+Convenience.swift
Core/FindingEmitter.swift
Core/FormatPipeline.swift
Core/FunctionDeclSyntax+Convenience.swift
Core/ImportsXCTestVisitor.swift
Core/LazySplitSequence.swift
Core/LintPipeline.swift
Core/ModifierListSyntax+Convenience.swift
Core/Parsing.swift
Core/Pipelines+Generated.swift
Core/RememberingIterator.swift
Core/Rule.swift
Core/RuleBasedFindingCategory.swift
Core/RuleMask.swift
Core/RuleNameCache+Generated.swift
Core/RuleRegistry+Generated.swift
Core/RuleState.swift
Core/SyntaxFormatRule.swift
Core/SyntaxLintRule.swift
Core/SyntaxProtocol+Convenience.swift
Core/Trivia+Convenience.swift
Core/WithSemicolonSyntax.swift
PrettyPrint/Comment.swift
PrettyPrint/Indent+Length.swift
PrettyPrint/PrettyPrint.swift
PrettyPrint/PrettyPrintFindingCategory.swift
PrettyPrint/Token.swift
PrettyPrint/TokenStreamCreator.swift
PrettyPrint/Verbatim.swift
PrettyPrint/WhitespaceFindingCategory.swift
PrettyPrint/WhitespaceLinter.swift
Rules/AllPublicDeclarationsHaveDocumentation.swift
Rules/AlwaysUseLiteralForEmptyCollectionInit.swift
Rules/AlwaysUseLowerCamelCase.swift
Rules/AmbiguousTrailingClosureOverload.swift
Rules/BeginDocumentationCommentWithOneLineSummary.swift
Rules/DoNotUseSemicolons.swift
Rules/DontRepeatTypeInStaticProperties.swift
Rules/FileScopedDeclarationPrivacy.swift
Rules/FullyIndirectEnum.swift
Rules/GroupNumericLiterals.swift
Rules/IdentifiersMustBeASCII.swift
Rules/NeverForceUnwrap.swift
Rules/NeverUseForceTry.swift
Rules/NeverUseImplicitlyUnwrappedOptionals.swift
Rules/NoAccessLevelOnExtensionDeclaration.swift
Rules/NoAssignmentInExpressions.swift
Rules/NoBlockComments.swift
Rules/NoCasesWithOnlyFallthrough.swift
Rules/NoEmptyTrailingClosureParentheses.swift
Rules/NoLabelsInCasePatterns.swift
Rules/NoLeadingUnderscores.swift
Rules/NoParensAroundConditions.swift
Rules/NoPlaygroundLiterals.swift
Rules/NoVoidReturnOnFunctionSignature.swift
Rules/OmitExplicitReturns.swift
Rules/OneCasePerLine.swift
Rules/OneVariableDeclarationPerLine.swift
Rules/OnlyOneTrailingClosureArgument.swift
Rules/OrderedImports.swift
Rules/ReplaceForEachWithForLoop.swift
Rules/ReturnVoidInsteadOfEmptyTuple.swift
Rules/TypeNamesShouldBeCapitalized.swift
Rules/UseEarlyExits.swift
Rules/UseExplicitNilCheckInConditions.swift
Rules/UseLetInEveryBoundCaseVariable.swift
Rules/UseShorthandTypeNames.swift
Rules/UseSingleLinePropertyGetter.swift
Rules/UseSynthesizedInitializer.swift
Rules/UseTripleSlashForDocumentationComments.swift
Rules/UseWhereClausesInForLoops.swift
Rules/ValidateDocumentationComments.swift)
target_link_libraries(SwiftFormat PUBLIC
SwiftMarkdown::Markdown
SwiftSyntax::SwiftSyntax
SwiftSyntax::SwiftSyntaxBuilder
SwiftSyntax::SwiftOperators
SwiftSyntax::SwiftParser
SwiftSyntax::SwiftParserDiagnostics
libcmark-gfm
libcmark-gfm-extensions)

_install_target(SwiftFormat)
13 changes: 13 additions & 0 deletions Sources/_SwiftFormatInstructionCounter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

add_library(_SwiftFormatInstructionCounter STATIC
src/InstructionsExecuted.c)
target_include_directories(_SwiftFormatInstructionCounter PUBLIC
include)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module _SwiftFormatInstructionCounter {
header "InstructionsExecuted.h"
}
37 changes: 37 additions & 0 deletions Sources/swift-format/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

add_executable(swift-format
PrintVersion.swift
SwiftFormatCommand.swift
VersionOptions.swift
Frontend/ConfigurationLoader.swift
Frontend/FormatFrontend.swift
Frontend/Frontend.swift
Frontend/LintFrontend.swift
Subcommands/DumpConfiguration.swift
Subcommands/Format.swift
Subcommands/Lint.swift
Subcommands/LintFormatOptions.swift
Subcommands/PerformanceMeasurement.swift
Utilities/Diagnostic.swift
Utilities/DiagnosticsEngine.swift
Utilities/FileHandleTextOutputStream.swift
Utilities/FileIterator.swift
Utilities/FormatError.swift
Utilities/StderrDiagnosticPrinter.swift
Utilities/TTY.swift)
target_link_libraries(swift-format PRIVATE
_SwiftFormatInstructionCounter
ArgumentParser
SwiftFormat
SwiftParser
SwiftSyntax)

_install_target(swift-format)
20 changes: 20 additions & 0 deletions cmake/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

set(SWIFT_FORMAT_EXPORTS_FILE
${CMAKE_CURRENT_BINARY_DIR}/SwiftFormatExports.cmake)

configure_file(SwiftFormatConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/SwiftFormatConfig.cmake)

get_property(SWIFT_FORMAT_EXPORTS GLOBAL PROPERTY SWIFT_FORMAT_EXPORTS)
export(TARGETS ${SWIFT_FORMAT_EXPORTS}
NAMESPACE SwiftFormat::
FILE ${SWIFT_FORMAT_EXPORTS_FILE}
EXPORT_LINK_INTERFACE_LIBRARIES)
12 changes: 12 additions & 0 deletions cmake/modules/SwiftFormatConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[[
This source file is part of the swift-format open source project

Copyright (c) 2024 Apple Inc. and the swift-format project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
#]]

if(NOT TARGET SwiftFormat)
include("@SWIFT_FORMAT_EXPORTS_FILE@")
endif()
Loading

0 comments on commit e5b594c

Please sign in to comment.