Skip to content

build: enable vendoring dependencies #7199

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 118 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

cmake_minimum_required(VERSION 3.19)

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

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

cmake_minimum_required(VERSION 3.19)

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

project(SwiftPM LANGUAGES C Swift)

option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
option(FIND_PM_DEPS "Search for all external Package Manager dependencies" YES)

set(CMAKE_Swift_LANGUAGE_VERSION 5)
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
Expand All @@ -27,32 +28,131 @@ 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)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
set(CMAKE_POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS})

if(FIND_PM_DEPS)
find_package(SwiftSystem CONFIG REQUIRED)
find_package(TSC CONFIG REQUIRED)
# Toolchain Vended dependencies
find_package(dispatch QUIET)
find_package(Foundation QUIET)

include(FetchContent)

set(_SPM_SAVED_BUILD_TESTING ${BUILD_TESTING})
set(_SPM_SAVED_BUILD_EXAMPLES ${BUILD_EXAMPLES})
set(_SPM_VENDOR_DEPENDENCIES)

set(BUILD_EXAMPLES NO)
set(BUILD_TESTING NO)

find_package(ArgumentParser CONFIG)
Copy link
Member

Choose a reason for hiding this comment

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

Do you need to explicitly call find_package first? If you don't do anything FetchContent will call find_package first before doing anything, and only if it's not found will it do things. To tell it to use the CONFIG search mode, add do

FetchContent_Declare(ArgumentParser
  GIT_REPOSITORY https://github.com/apple/swift-argument-parser
  GIT_TAG 1.2.3
  FIND_PACKAGE_ARGS CONFIG)

https://cmake.org/cmake/help/latest/module/FetchContent.html#command:fetchcontent_declare -- FIND_PACKAGE_ARGS

erm... wait, that's only available in 3.24+. That makes things more challenging.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I'll leave this as is until we get a newer CMake.

if(NOT ArgumentParser_FOUND)
message(STATUS "Vending swift-argument-parser")
FetchContent_Declare(ArgumentParser
GIT_REPOSITORY https://github.com/apple/swift-argument-parser
GIT_TAG 1.2.3)
list(APPEND _SPM_VENDOR_DEPENDENCIES ArgumentParser)
endif()

find_package(TSC CONFIG)
if(NOT TSC_FOUND)
message(STATUS "Vending swift-tools-support-core")
FetchContent_Declare(ToolsSupportCore
GIT_REPOSITORY https://github.com/apple/swift-tools-support-core
GIT_TAG main)
list(APPEND _SPM_VENDOR_DEPENDENCIES ToolsSupportCore)
endif()

find_package(LLBuild CONFIG)
if(NOT LLBuild_FOUND)
find_package(LLBuild CONFIG)
if(NOT LLBuild_FOUND)
if(APPLE)
find_package(LLBuild REQUIRED)
else()
message(STATUS "Vending swift-llbuild")
set(LLBUILD_SUPPORT_BINDINGS Swift)
FetchContent_Declare(LLBuild
GIT_REPOSITORY https://github.com/apple/swift-llbuild
GIT_TAG main)
list(APPEND _SPM_VENDOR_DEPENDENCIES LLBuild)
endif()
endif()

find_package(ArgumentParser CONFIG REQUIRED)
find_package(SwiftDriver CONFIG REQUIRED)
find_package(SwiftCollections CONFIG REQUIRED)
find_package(SwiftASN1 CONFIG REQUIRED)
find_package(SwiftCertificates CONFIG REQUIRED)
find_package(SwiftCrypto CONFIG REQUIRED)
find_package(SwiftASN1 CONFIG)
if(NOT SwiftASN1_FOUND)
message(STATUS "Vending swift-asn1")
FetchContent_Declare(ASN1
GIT_REPOSITORY https://github.com/apple/swift-asn1
GIT_TAG 1.1.0)
list(APPEND _SPM_VENDOR_DEPENDENCIES ASN1)
endif()

find_package(SwiftCertificates CONFIG)
if(NOT SwiftCertificates_FOUND)
message(STATUS "Vending swift-certificates")
FetchContent_Declare(Certificates
GIT_REPOSITORY https://github.com/apple/swift-certificates
GIT_TAG main)
list(APPEND _SPM_VENDOR_DEPENDENCIES Certificates)
endif()

find_package(SwiftCollections CONFIG)
if(NOT SwiftCollections_FOUND)
message(STATUS "Vending swift-collections")
FetchContent_Declare(Collections
GIT_REPOSITORY https://github.com/apple/swift-collections
GIT_TAG release/1.0)
list(APPEND _SPM_VENDOR_DEPENDENCIES Collections)
endif()

find_package(SwiftCrypto CONFIG)
if(NOT SwiftCrypto_FOUND)
message(STATUS "Vending swift-crypto")
FetchContent_Declare(Crypto
GIT_REPOSITORY https://github.com/apple/swift-crypto
GIT_TAG 3.0.0)
list(APPEND _SPM_VENDOR_DEPENDENCIES Crypto)
endif()

find_package(SwiftDriver CONFIG)
if(NOT SwiftDriver_FOUND)
message(STATUS "Vending swift-driver")
FetchContent_Declare(Driver
GIT_REPOSITORY https://github.com/apple/swift-driver
GIT_TAG main)
list(APPEND _SPM_VENDOR_DEPENDENCIES Driver)
endif()

find_package(SwiftSystem CONFIG)
if(NOT SwiftSystem_FOUND)
message(STATUS "Vending swift-system")
FetchContent_Declare(System
GIT_REPOSITORY https://github.com/apple/swift-system
GIT_TAG 1.1.1)
list(APPEND _SPM_VENDOR_DEPENDENCIES System)
endif()

if(_SPM_VENDOR_DEPENDENCIES)
FetchContent_MakeAvailable(${_SPM_VENDOR_DEPENDENCIES})

if(NOT TARGET SwiftCollections::DequeModule)
add_library(SwiftCollections::DequeModule ALIAS DequeModule)
endif()
if(NOT TARGET SwiftCollections::OrderedCollections)
add_library(SwiftCollections::OrderedCollections ALIAS OrderedCollections)
endif()

if(NOT TARGET SwiftSystem::SystemPackage)
add_library(SwiftSystem::SystemPackage ALIAS SystemPackage)
endif()
endif()

find_package(dispatch QUIET)
find_package(Foundation QUIET)
find_package(SQLite3 REQUIRED)

# Enable `package` modifier for the whole package.
add_compile_options("$<$<COMPILE_LANGUAGE:Swift>:-package-name;SwiftPM>")

set(BUILD_TESTING ${_SPM_SAVED_BUILD_TESTING})
set(BUILD_EXAMPLES ${_SPM_SAVED_BUILD_EXAMPLES})

add_subdirectory(Sources)
add_subdirectory(cmake/modules)