Skip to content

[Runtimes][CMake] Build Differentiation in new style. #81239

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 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions Runtimes/Resync.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ copy_files(public/Platform Overlay/Windows/CRT
TiocConstants.swift
tgmath.swift.gyb)

# TODO: Add source directories for the platform overlays, supplemental
# libraries, and test support libraries.

# Supplemental Libraries

# Copy Differentiation sources
copy_library_sources("linker-support" "" "Supplemental/Differentiation")
copy_library_sources("Differentiation" "public" "Supplemental")

# Copy StringProcessing, RegexParser, RegexBuilder
if(NOT DEFINED StringProcessing_ROOT_DIR)
find_path(StringProcessing_ROOT_DIR
Expand Down
12 changes: 12 additions & 0 deletions Runtimes/Supplemental/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ if(SwiftRuntime_ENABLE_stringprocessing)
CMAKE_ARGS
${COMMON_OPTIONS})
endif()


# Differentiation
if(SwiftRuntime_ENABLE_differentiation)
ExternalProject_Add(Differentiation
PREFIX "Differentiation"
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Differentiation"
INSTALL_DIR "${CMAKE_INSTALL_PREFIX}"
INSTALL_COMMAND ""
CMAKE_ARGS
${COMMON_OPTIONS})
endif()
96 changes: 96 additions & 0 deletions Runtimes/Supplemental/Differentiation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
cmake_minimum_required(VERSION 3.29)

if($ENV{BUILD_NUMBER})
math(EXPR BUILD_NUMBER "$ENV{BUILD_NUMBER} % 65535")
set(BUILD_NUMBER ".${BUILD_NUMBER}")
endif()
project(SwiftDifferentiation
LANGUAGES Swift C
VERSION 6.1.0${BUILD_NUMBER})

set(CMAKE_POSITION_INDEPENDENT_CODE YES)
set(CMAKE_Swift_LANGUAGE_VERSION 5)
Copy link
Member

Choose a reason for hiding this comment

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

Do we set the language version for the other libraries?


list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../cmake/modules")

if(NOT PROJECT_IS_TOP_LEVEL)
message(SEND_ERROR "Swift Differentiation must build as a standalone project")
endif()

set(${PROJECT_NAME}_SWIFTC_SOURCE_DIR
"${PROJECT_SOURCE_DIR}/../../../"
CACHE FILEPATH "Path to the root source directory of the Swift compiler")

find_package(SwiftCore)
find_package(SwiftMath)

include(gyb)
include(AvailabilityMacros)
include(CatalystSupport)
include(EmitSwiftInterface)
include(ResourceEmbedding)
Copy link
Contributor

Choose a reason for hiding this comment

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

We also need InstallSwiftInterface to be able to call install_swift_interface

include(InstallSwiftInterface)


option(${PROJECT_NAME}_ENABLE_VECTOR_TYPES "Enable vector support"
${SwiftCore_ENABLE_VECTOR_TYPES})

option(${PROJECT_NAME}_ENABLE_LIBRARY_EVOLUTION "Enable library evolution"
${SwiftCore_ENABLE_LIBRARY_EVOLUTION})

add_compile_options(
$<$<COMPILE_LANGUAGE:Swift>:-explicit-module-build>
$<$<COMPILE_LANGUAGE:Swift>:-nostdlibimport>
$<$<COMPILE_LANGUAGE:Swift>:-parse-stdlib>
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-library-level api>"
$<$<COMPILE_LANGUAGE:Swift>:-enforce-exclusivity=unchecked>
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-target-min-inlining-version min>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature NoncopyableGenerics2>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature SuppressedAssociatedTypes>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature SE427NoInferenceOnExtension>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature NonescapableTypes>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature LifetimeDependence>"
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-enable-experimental-feature MemberImportVisibility>"
$<$<AND:$<BOOL:${SwiftDifferentiation_ENABLE_LIBRARY_EVOLUTION}>,$<COMPILE_LANGUAGE:Swift>>:-enable-library-evolution>)

if(SwiftDifferentiation_ENABLE_VECTOR_TYPES)
Copy link
Member

Choose a reason for hiding this comment

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

We should probably put the sources under a Sources directory. While that is not required for CMake to work properly, I think that it would be nice to have the layout be similar to other projects.

Copy link
Contributor

@edymtt edymtt May 5, 2025

Choose a reason for hiding this comment

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

I'm not sure if I understand correctly your point -- are you suggesting to have a directory structure in which the sources are not mingled with the CMake code? For example

  • Supplemental
    • Differentiation
      • CMakeLists.txt
      • Sources
        • AnyDifferentiable.swift
        • ...
    • Synchronization
      • CMakeLists.txt
      • Sources
    • ...
    • cmake/modules

Copy link
Member

Choose a reason for hiding this comment

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

You would still have the intermingled CMakeLists.txt, but the idea is that we could have the same structure across all the libraries. I think that @etcwilde suggested that we do that subsequently, which I am fine with.

gyb_expand(SIMDDifferentiation.swift.gyb SIMDDifferentiation.swift)
endif()
gyb_expand(TgmathDerivatives.swift.gyb TgmathDerivatives.swift)
gyb_expand(FloatingPointDifferentiation.swift.gyb
FloatingPointDifferentiation.swift)

add_library(swift_Differentiation
AnyDifferentiable.swift
ArrayDifferentiation.swift
Differentiable.swift
DifferentialOperators.swift
DifferentiationUtilities.swift
OptionalDifferentiation.swift
$<$<BOOL:${SwiftDifferentiation_ENABLE_VECTOR_TYPES}>:SIMDDifferentiation.swift>
TgmathDerivatives.swift
FloatingPointDifferentiation.swift
linker-support/magic-symbols-for-install-name.c)

set_target_properties(swift_Differentiation PROPERTIES
Swift_MODULE_NAME _Differentiation)

if(APPLE AND BUILD_SHARED_LIBS)
target_link_options(swift_Differentiation PRIVATE "SHELL:-Xlinker -headerpad_max_install_names")
endif()

target_link_libraries(swift_Differentiation PRIVATE swiftCore SwiftMath)

set(${PROJECT_NAME}_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>$<$<BOOL:${Supplemental_INSTALL_NESTED_SUBDIR}>:/${Supplemental_PLATFORM_SUBDIR}/${Supplemental_ARCH_SUBDIR}>" CACHE STRING "")
set(${PROJECT_NAME}_INSTALL_SWIFTMODULEDIR "${CMAKE_INSTALL_LIBDIR}/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>$<$<BOOL:${Supplemental_INSTALL_NESTED_SUBDIR}>:/${Supplemental_PLATFORM_SUBDIR}>" CACHE STRING "")

install(TARGETS swift_Differentiation
EXPORT SwiftSupplementalTargets
COMPONENT SwiftCore_runtime
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest to match the same name we are using in #81310

Suggested change
COMPONENT SwiftCore_runtime
COMPONENT ${PROJECT_NAME}_runtime

ARCHIVE DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${${PROJECT_NAME}_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
emit_swift_interface(swift_Differentiation)
install_swift_interface(swift_Differentiation)

# Configure plist creation for Darwin platforms.
generate_plist("${CMAKE_PROJECT_NAME}" "${CMAKE_PROJECT_VERSION}" swift_Differentiation)
embed_manifest(swift_Differentiation)
85 changes: 85 additions & 0 deletions Runtimes/Supplemental/cmake/modules/FindSwiftMath.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#[=======================================================================[.rst:
FindSwiftMath
------------

Find the Swift `_math` module interface, deferring to mathConfig.cmake when
requested.

This module locates the Swift `_math` module, which provides standard math
functions to Swift code. The module looks for the `_math.swiftinterface` file
in the SDK and sets up an imported target for use in CMake.
Imported Targets
^^^^^^^^^^^^^^^^

The following :prop_tgt:`IMPORTED` TARGETS may be defined:

``math``

Hint Variables
^^^^^^^^^^^^^^

``swift_SDKROOT``
Set the path to the Swift SDK installation.
This only affects Linux and Windows builds.
Apple builds always use the library provided by the SDK.

Result Variables
^^^^^^^^^^^^^^^^

The module may set the following variables if `SwiftMath_DIR` is not set.

``SwiftMath_FOUND``
true if the `_math` module interface (and required library) were found.

``SwiftMath_INCLUDE_DIR``
The directory containing the `_math.swiftinterface` file

``SwiftMath_LIBRARIES`` OR ``SwiftMath_IMPLIB``
the libraries to be linked

#]=======================================================================]

# If the math_DIR is specified, look there instead. The cmake-generated
# config file is more accurate, but requires that the SDK has one available.
if(SwiftMath_DIR)
if(SwiftMath_FIND_REQUIRED)
list(APPEND args REQUIRED)
endif()
if(SwiftMath_FIND_QUIETLY)
list(APPEND args QUIET)
endif()
find_package(SwiftMath NO_MODULE ${args})
return()
endif()

include(FindPackageHandleStandardArgs)
include(PlatformInfo)

if(APPLE)
find_path(SwiftMath_INCLUDE_DIR
NAMES _math.swiftmodule
PATHS ${CMAKE_OSX_SYSROOT}/usr/lib/swift)
find_path(SwiftMath_IMPLIB
NAMES libm.tbd
PATHS usr/lib)
add_library(SwiftMath SHARED IMPORTED GLOBAL)
set_target_properties(SwiftMath PROPERTIES
IMPORTED_IMPLIB ${SwiftMath_IMPLIB}
INTERFACE_INCLUDE_DIRECTORIES ${SwiftMath_INCLUDE_DIR})
find_package_handle_standard_args(SwiftMath DEFAULT_MSG
SwiftMath_IMPLIB SwiftMath_INCLUDE_DIR)
elseif(LINUX)
find_path(SwiftMath_INCLUDE_DIR
glibc.modulemap
PATHS ${Swift_SDKROOT}/usr/lib/swift/linux/${SwiftSupplemental_ARCH_SUBDIR})
find_path(SwiftMath_GLIBC_DIR
Glibc.swiftmodule
PATHS ${Swift_SDKROOT}/usr/lib/swift/linux)
add_library(SwiftMath INTERFACE IMPORTED GLOBAL)
set_target_properties(SwiftMath PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${SwiftMath_INCLUDE_DIR} ${SwiftMath_GLIBC_DIR})
find_package_handle_standard_args(SwiftMath DEFAULT_MSG SwiftMath_INCLUDE_DIR)
else()
# TODO: Windows
message(FATAL_ERROR "FindSwiftMath.cmake module search not implemented for targeted platform\n")
endif()