Skip to content

Commit 43b106e

Browse files
committed
Added support for supplying an --xcconfig option that can be used to set project-wide build settings
1 parent 6d4c39c commit 43b106e

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed

Diff for: Sources/CreateXCFramework/Command+Options.swift

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ extension Command {
3030

3131
@Flag(help: "Prints the available products and targets")
3232
var listProducts: Bool
33+
34+
@Option(help: "The path to a .xcconfig file that can be used to override Xcode build settings. Relative to the package path.")
35+
var xcconfig: String?
3336

3437

3538
// MARK: - Output Options

Diff for: Sources/CreateXCFramework/Command.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct Command: ParsableCommand {
5050
let platforms = try package.supportedPlatforms()
5151

5252
// get what we're building
53-
try generator.writeXcconfig()
53+
try generator.writeDistributionXcconfig()
5454
let project = try generator.generate()
5555

5656
// printing packages?
@@ -65,9 +65,7 @@ struct Command: ParsableCommand {
6565

6666
// we've applied the xcconfig to everything, but some dependencies (*cough* swift-nio)
6767
// have build errors, so we remove it from targets we're not building
68-
for target in project.targets where productNames.contains(target.name) == false {
69-
target.buildSettings.xcconfigFileRef = nil
70-
}
68+
try project.enableDistribution(targets: productNames, xcconfig: AbsolutePath(package.distributionBuildXcconfig.path).relative(to: AbsolutePath(package.rootDirectory.path)))
7169

7270
// save the project
7371
try project.save(to: generator.projectPath)

Diff for: Sources/CreateXCFramework/PackageInfo.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@ struct PackageInfo {
3232
.appendingPathComponent("Distribution.xcconfig")
3333
.absoluteURL
3434
}
35-
35+
36+
var overridesXcconfig: Foundation.URL? {
37+
guard let path = self.options.xcconfig else { return nil }
38+
39+
// absolute path
40+
if path.hasPrefix("/") {
41+
return Foundation.URL(fileURLWithPath: path)
42+
43+
// strip current directory if thats where we are
44+
} else if path.hasPrefix("./") {
45+
return self.rootDirectory.appendingPathComponent(String(path[path.index(path.startIndex, offsetBy: 2)...]))
46+
}
47+
48+
return self.rootDirectory.appendingPathComponent(path)
49+
}
50+
3651
// TODO: Map diagnostics to swift-log
3752
let diagnostics = DiagnosticsEngine()
3853

Diff for: Sources/CreateXCFramework/ProjectGenerator.swift

+45-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ struct ProjectGenerator {
1515
private enum Constants {
1616
static let `extension` = "xcodeproj"
1717
}
18-
18+
19+
1920
// MARK: - Properties
2021

2122
let package: PackageInfo
@@ -24,19 +25,32 @@ struct ProjectGenerator {
2425
let dir = AbsolutePath(self.package.projectBuildDirectory.path)
2526
return buildXcodeprojPath(outputDir: dir, projectName: self.package.package.name)
2627
}
27-
28+
29+
2830
// MARK: - Initialisation
2931

3032
init (package: PackageInfo) {
3133
self.package = package
3234
}
33-
35+
36+
3437
// MARK: - Generation
3538

3639
/// Writes out the Xcconfig file
37-
func writeXcconfig () throws {
40+
func writeDistributionXcconfig () throws {
3841
try makeDirectories(self.projectPath)
39-
try open(AbsolutePath(self.package.distributionBuildXcconfig.path)) { stream in
42+
43+
let path = AbsolutePath(self.package.distributionBuildXcconfig.path)
44+
try open(path) { stream in
45+
if let absolutePath = self.package.overridesXcconfig?.path {
46+
stream (
47+
"""
48+
#include "\(AbsolutePath(absolutePath).relative(to: AbsolutePath(path.dirname)).pathString)"
49+
50+
"""
51+
)
52+
}
53+
4054
stream (
4155
"""
4256
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
@@ -59,19 +73,44 @@ struct ProjectGenerator {
5973
graph: self.package.graph,
6074
extraDirs: [],
6175
extraFiles: [],
62-
options: XcodeprojOptions(xcconfigOverrides: AbsolutePath(self.package.distributionBuildXcconfig.path)),
76+
options: XcodeprojOptions(xcconfigOverrides: (self.package.overridesXcconfig?.path).flatMap { AbsolutePath($0) }),
6377
diagnostics: self.package.diagnostics
6478
)
6579

6680
return project
6781
}
82+
6883
}
6984

7085

7186
// MARK: - Saving Xcode Projects
7287

7388
extension Xcode.Project {
7489

90+
/// This is the group that is normally created in Xcodeproj.xcodeProject() when you specify an xcconfigOverride
91+
var configGroup: Xcode.Group {
92+
let name = "Configs"
93+
94+
if let group = self.mainGroup.subitems.lazy.compactMap({ $0 as? Xcode.Group }).first(where: { $0.name == name }) {
95+
return group
96+
}
97+
98+
// doesn't exist - lets creat it
99+
return self.mainGroup.addGroup(path: "", name: name)
100+
}
101+
102+
func enableDistribution (targets: [String], xcconfig: RelativePath) throws {
103+
let group = self.configGroup
104+
let ref = group.addFileReference (
105+
path: xcconfig.pathString,
106+
name: xcconfig.basename
107+
)
108+
109+
for target in self.targets where targets.contains(target.name) {
110+
target.buildSettings.xcconfigFileRef = ref
111+
}
112+
}
113+
75114
func save (to path: AbsolutePath) throws {
76115
try open(path.appending(component: "project.pbxproj")) { stream in
77116
// Serialize the project model we created to a plist, and return

Diff for: action.js

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function run () {
1515
let targets = core.getInput('target', { required: false })
1616
let configuration = core.getInput('configuration', { required: false })
1717
let platforms = core.getInput('platforms', { required: false })
18+
let xcconfig = core.getInput('xcconfig', { required: false })
1819

1920
// install mint if its not installed
2021
await installUsingBrewIfRequired("mint")
@@ -34,6 +35,11 @@ async function run () {
3435
options.push(configuration)
3536
}
3637

38+
if (!!xcconfig) {
39+
options.push('--xcconfig')
40+
options.push(xcconfig)
41+
}
42+
3743
if (!!platforms) {
3844
platforms
3945
.split(',')

Diff for: action.yml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ inputs:
1212
description: "Build with a specific configuration ('debug' or 'release')"
1313
required: false
1414
default: release
15+
xcconfig:
16+
description: "The path to a .xcconfig file that can be used to override Xcode build settings. Relative to the package path."
17+
required: false
1518

1619
runs:
1720
using: node12

0 commit comments

Comments
 (0)