Skip to content

Include xcspec resources in the CMake build #488

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

Merged
merged 2 commits into from
May 8, 2025
Merged
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
42 changes: 42 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@ See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
]]

include(CMakeParseArguments)
function(SwiftBuild_Bundle)
set(Options)
set(OneValueArguments MODULE)
set(MultiValueArguments FILES)
cmake_parse_arguments(PARSE_ARGV 0 BundleXCSpecs
"${Options}" "${OneValueArguments}" "${MultiValueArguments}")

list(TRANSFORM BundleXCSpecs_FILES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
add_custom_command(TARGET ${BundleXCSpecs_MODULE} POST_BUILD
COMMAND
${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/share/pm/SwiftBuild_${BundleXCSpecs_MODULE}.resources"
COMMAND
${CMAKE_COMMAND} -E copy_if_different ${BundleXCSpecs_FILES} "${CMAKE_BINARY_DIR}/share/pm/SwiftBuild_${BundleXCSpecs_MODULE}.resources/")

file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/share/pm/SwiftBuild_${BundleXCSpecs_MODULE}.resources" _SWIFT_BUILD_RESOURCE_BUNDLE_BUILD_PATH)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we can use $<TARGET_PROEPRTY:${BundleXCSpecs_MODULE},BINARY_DIR> to get the module's binary dir rather than placing this at the top-level.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

gave this a try but I was unable to use a generator expression in the output path when creating the resource accessor

file(CONFIGURE
OUTPUT "${CMAKE_BINARY_DIR}/resource_accessors/SwiftBuild_${BundleXCSpecs_MODULE}_resource_bundle_accessor.swift"
CONTENT [[
import Foundation
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("SwiftBuild_@BundleXCSpecs_MODULE@.resources").path
let buildPath = #"@_SWIFT_BUILD_RESOURCE_BUNDLE_BUILD_PATH@"#
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
Swift.fatalError("could not load resource bundle: from \(mainPath) or \(buildPath)")
}
return bundle
}()
}
]]
ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF)

target_sources("${BundleXCSpecs_MODULE}" PRIVATE
"${CMAKE_BINARY_DIR}/resource_accessors/SwiftBuild_${BundleXCSpecs_MODULE}_resource_bundle_accessor.swift")

install(DIRECTORY
"${CMAKE_BINARY_DIR}/share/pm/SwiftBuild_${BundleXCSpecs_MODULE}.resources/"
DESTINATION share/pm/SwiftBuild_${BundleXCSpecs_MODULE}.resources/)
endfunction()

add_subdirectory(SWBCSupport)
add_subdirectory(SWBCLibc)
add_subdirectory(SWBLibc)
Expand Down
24 changes: 2 additions & 22 deletions Sources/SWBAndroidPlatform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,15 @@ See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
]]

file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}"
_SWBAndroidPlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR)
file(CONFIGURE
OUTPUT resource_bundle_accessor.swift
CONTENT [[
import Foundation
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("SwiftBuild_SWBAndroidPlatform.resources").path
let buildPath = #"@_SWBAndroidPlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR@\SwiftBuild_SWBAndroidPlatform.resources"#
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
Swift.fatalError("could not load resource bundle: from \(mainPath) or \(buildPath)")
}
return bundle
}()
}
]]
ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF)

add_library(SWBAndroidPlatform
AndroidSDK.swift
Plugin.swift)
SwiftBuild_Bundle(MODULE SWBAndroidPlatform FILES
Specs/Android.xcspec)
target_link_libraries(SWBAndroidPlatform PUBLIC
SWBCore
SWBMacro
SWBUtil)
target_sources(SWBAndroidPlatform PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/resource_bundle_accessor.swift")

set_target_properties(SWBAndroidPlatform PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
93 changes: 71 additions & 22 deletions Sources/SWBApplePlatform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
]]

file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}"
_SWBApplePlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR)
file(CONFIGURE
OUTPUT resource_bundle_accessor.swift
CONTENT [[
import Foundation
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("SwiftBuild_SWBApplePlatform.resources").path
let buildPath = #"@_SWBApplePlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR@\SwiftBuild_SWBApplePlatform.resources"#
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
Swift.fatalError("could not load resource bundle: from \(mainPath) or \(buildPath)")
}
return bundle
}()
}
]]
ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF)

add_library(SWBApplePlatform
AppIntentsMetadataCompiler.swift
AppIntentsMetadataTaskProducer.swift
Expand Down Expand Up @@ -65,6 +45,77 @@ add_library(SWBApplePlatform
StringCatalogCompilerOutputParser.swift
StubBinaryTaskProducer.swift
XCStringsInputFileGroupingStrategy.swift)
SwiftBuild_Bundle(MODULE SWBApplePlatform FILES
Specs/AppIntentsMetadata.xcspec
Specs/AppIntentsNLTraining.xcspec
Specs/AppShortcutStringsMetadata.xcspec
Specs/AssetCatalogCompiler.xcspec
Specs/CompileSkybox.xcspec
Specs/CopyPNGFile.xcspec
Specs/CopyTiffFile.xcspec
"Specs/Core Data.xcspec"
Specs/CoreML.xcspec
"Specs/Darwin Package Types.xcspec"
"Specs/Darwin Product Types.xcspec"
Specs/DriverKit.xcspec
Specs/DTrace.xcspec
Specs/Embedded-Device.xcspec
Specs/Embedded-Shared.xcspec
Specs/Embedded-Simulator.xcspec
Specs/EmbeddedBinaryValidationUtility.xcspec
Specs/GenerateAppPlaygroundAssetCatalog.xcspec
Specs/GenerateTextureAtlas.xcspec
Specs/IBCompiler.xcspec
Specs/IBPostprocessor.xcspec
Specs/IBStoryboardCompiler.xcspec
Specs/IBStoryboardLinker.xcspec
Specs/IBStoryboardPostprocessor.xcspec
Specs/Iconutil.xcspec
Specs/Iig.xcspec
Specs/InfoPlistUtility.xcspec
Specs/InstrumentsPackage.xcspec
Specs/Intents.xcspec
"Specs/Interface Builder File Types.xcspec"
"Specs/iOS Device.xcspec"
"Specs/iOS Shared.xcspec"
"Specs/iOS Simulator.xcspec"
Specs/KernelExtension.xcspec
Specs/Lipo.xcspec
Specs/LSRegisterURL.xcspec
"Specs/MacOSX Architectures.xcspec"
"Specs/MacOSX Core Build System.xcspec"
"Specs/MacOSX Native Build System.xcspec"
"Specs/MacOSX Package Types.xcspec"
"Specs/MacOSX Product Types.xcspec"
Specs/MetalCompiler.xcspec
Specs/MetalFileTypes.xcspec
Specs/MetalLinker.xcspec
Specs/MetalPackageTypes.xcspec
Specs/MetalProductTypes.xcspec
Specs/MiG.xcspec
Specs/OpenCL.xcspec
Specs/OSACompile.xcspec
Specs/RCFileTypes.xcspec
Specs/RealityAssets.xcspec
Specs/ReferenceObject.xcspec
Specs/ResMerger.xcspec
Specs/Rez.xcspec
"Specs/SceneKit FileTypes.xcspec"
"Specs/SceneKit Tools.xcspec"
Specs/SpriteKitFileTypes.xcspec
Specs/TiffUtil.xcspec
"Specs/tvOS Device.xcspec"
"Specs/tvOS Shared.xcspec"
"Specs/tvOS Simulator.xcspec"
Specs/WatchKit1ProductTypes.xcspec
"Specs/watchOS Device.xcspec"
"Specs/watchOS Shared.xcspec"
"Specs/watchOS Simulator.xcspec"
Specs/XCAppExtensionPoints.xcspec
Specs/XCStrings.xcspec
"Specs/xrOS Device.xcspec"
"Specs/xrOS Shared.xcspec"
"Specs/xrOS Simulator.xcspec")
set_target_properties(SWBApplePlatform PROPERTIES
Swift_LANGUAGE_VERSION 6)
target_link_libraries(SWBApplePlatform PUBLIC
Expand All @@ -73,8 +124,6 @@ target_link_libraries(SWBApplePlatform PUBLIC
SWBUtil
SWBProtocol
SWBTaskConstruction)
target_sources(SWBApplePlatform PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/resource_bundle_accessor.swift")

set_target_properties(SWBApplePlatform PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
26 changes: 4 additions & 22 deletions Sources/SWBCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
]]

file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}"
_SWBCore_NATIVE_CMAKE_CURRENT_BINARY_DIR)
file(CONFIGURE
OUTPUT resource_bundle_accessor.swift
CONTENT [[
import Foundation
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("SwiftBuild_SWBCore.resources").path
let buildPath = #"@_SWBCore_NATIVE_CMAKE_CURRENT_BINARY_DIR@\SwiftBuild_SWBCore.resources"#
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
Swift.fatalError("could not load resource bundle: from \(mainPath) or \(buildPath)")
}
return bundle
}()
}
]]
ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF)

add_library(SWBCore
ActivityReporting.swift
Apple/DeviceFamily.swift
Expand Down Expand Up @@ -195,6 +175,10 @@ add_library(SWBCore
WorkspaceContext.swift
WorkspaceSettingsCache.swift
XCFramework.swift)
SwiftBuild_Bundle(MODULE SWBCore FILES
Specs/CoreBuildSystem.xcspec
Specs/ExternalBuildSystem.xcspec
Specs/NativeBuildSystem.xcspec)
set_target_properties(SWBCore PROPERTIES
Swift_LANGUAGE_VERSION 5)
target_link_libraries(SWBCore PUBLIC
Expand All @@ -205,8 +189,6 @@ target_link_libraries(SWBCore PUBLIC
SWBCAS
SWBLLBuild
SwiftDriver)
target_sources(SWBCore PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/resource_bundle_accessor.swift")

set_target_properties(SWBCore PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
27 changes: 5 additions & 22 deletions Sources/SWBGenericUnixPlatform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,16 @@ See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
]]

file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}"
_SWBGenericUnixPlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR)
file(CONFIGURE
OUTPUT resource_bundle_accessor.swift
CONTENT [[
import Foundation
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("SwiftBuild_SWBGenericUnixPlatform.resources").path
let buildPath = #"@_SWBGenericUnixPlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR@\SwiftBuild_SWBGenericUnixPlatform.resources"#
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
Swift.fatalError("could not load resource bundle: from \(mainPath) or \(buildPath)")
}
return bundle
}()
}
]]
ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF)

add_library(SWBGenericUnixPlatform
Plugin.swift)
SwiftBuild_Bundle(MODULE SWBGenericUnixPlatform FILES
Specs/Unix.xcspec
Specs/UnixCompile.xcspec
Specs/UnixLd.xcspec
Specs/UnixLibtool.xcspec)
target_link_libraries(SWBGenericUnixPlatform PUBLIC
SWBCore
SWBUtil)
target_sources(SWBGenericUnixPlatform PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/resource_bundle_accessor.swift")

set_target_properties(SWBGenericUnixPlatform PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
27 changes: 5 additions & 22 deletions Sources/SWBQNXPlatform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,18 @@ See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
]]

file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}"
_SWBQNXPlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR)
file(CONFIGURE
OUTPUT resource_bundle_accessor.swift
CONTENT [[
import Foundation
extension Foundation.Bundle {
static let module: Bundle = {
let mainPath = Bundle.main.bundleURL.appendingPathComponent("SwiftBuild_SWBQNXPlatform.resources").path
let buildPath = #"@_SWBQNXPlatform_NATIVE_CMAKE_CURRENT_BINARY_DIR@\SwiftBuild_SWBQNXPlatform.resources"#
let preferredBundle = Bundle(path: mainPath)
guard let bundle = preferredBundle ?? Bundle(path: buildPath) else {
Swift.fatalError("could not load resource bundle: from \(mainPath) or \(buildPath)")
}
return bundle
}()
}
]]
ESCAPE_QUOTES @ONLY NEWLINE_STYLE LF)

add_library(SWBQNXPlatform
Plugin.swift
QNXSDP.swift)
SwiftBuild_Bundle(MODULE SWBQNXPlatform FILES
Specs/QNX.xcspec
Specs/QNXCompile.xcspec
Specs/QNXLd.xcspec
Specs/QNXLibtool.xcspec)
target_link_libraries(SWBQNXPlatform PUBLIC
SWBCore
SWBMacro
SWBUtil)
target_sources(SWBQNXPlatform PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/resource_bundle_accessor.swift")

set_target_properties(SWBQNXPlatform PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
Loading