From ad6a1699e2b287165c8c6fbf1e3fd4a8f0b2bc93 Mon Sep 17 00:00:00 2001 From: toyeshhm Date: Sun, 22 Nov 2026 16:44:08 -0600 Subject: [PATCH] Default requiresObjCLinking to true for static frameworks --- Docs/ProjectSpec.md | 2 +- Sources/XcodeGenKit/PBXProjGenerator.swift | 4 +- .../ProjectGeneratorTests.swift | 88 ++++++++++++++++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/Docs/ProjectSpec.md b/Docs/ProjectSpec.md index 32380ca3..2de1dd24 100644 --- a/Docs/ProjectSpec.md +++ b/Docs/ProjectSpec.md @@ -398,7 +398,7 @@ Settings are merged in the following order: `groups`, `base`, `configs` (simple - [ ] **templateAttributes**: **[String: String]** - A list of attributes where each instance of `${attributeName}` within the templates listed in `templates` will be replaced with the value specified. - [ ] **transitivelyLinkDependencies**: **Bool** - If this is not specified the value from the project set in [Options](#options)`.transitivelyLinkDependencies` will be used. - [ ] **directlyEmbedCarthageDependencies**: **Bool** - If this is `true` Carthage framework dependencies will be embedded using an `Embed Frameworks` build phase instead of the `copy-frameworks` script. Defaults to `true` for all targets except iOS/tvOS/watchOS Applications. -- [ ] **requiresObjCLinking**: **Bool** - If this is `true` any targets that link to this target will have `-ObjC` added to their `OTHER_LDFLAGS`. This is required if a static library has any categories or extensions on Objective-C code. See [this guide](https://pewpewthespells.com/blog/objc_linker_flags.html#objc) for more details. Defaults to `true` if `type` is `library.static`. If you are 100% sure you don't have categories or extensions on Objective-C code (pure Swift with no use of Foundation/UIKit) you can set this to `false`, otherwise it's best to leave it alone. +- [ ] **requiresObjCLinking**: **Bool** - If this is `true` any targets that link to this target will have `-ObjC` added to their `OTHER_LDFLAGS`. This is required if a static library or framework has any categories or extensions on Objective-C code. See [this guide](https://pewpewthespells.com/blog/objc_linker_flags.html#objc) for more details. Defaults to `true` if `type` is `library.static` or `framework.static`, or if `type` is `framework` with `MACH_O_TYPE` set to `staticlib`. If you are 100% sure you don't have categories or extensions on Objective-C code (pure Swift with no use of Foundation/UIKit) you can set this to `false`, otherwise it's best to leave it alone. - [ ] **onlyCopyFilesOnInstall**: **Bool** – If this is `true`, the `Embed Frameworks` and `Embed App Extensions` (if available) build phases will have the "Copy only when installing" chekbox checked. Defaults to `false`. - [ ] **buildToolPlugins**: **[[Build Tool Plug-ins](#build-tool-plug-ins)]** - Commands for the build system that run automatically *during* the build. - [ ] **preBuildScripts**: **[[Build Script](#build-script)]** - Build scripts that run *before* any other build phases diff --git a/Sources/XcodeGenKit/PBXProjGenerator.swift b/Sources/XcodeGenKit/PBXProjGenerator.swift index 4f98743d..c5d052c6 100644 --- a/Sources/XcodeGenKit/PBXProjGenerator.swift +++ b/Sources/XcodeGenKit/PBXProjGenerator.swift @@ -479,7 +479,7 @@ public class PBXProjGenerator { let settings = Settings(buildSettings: buildSettings, configSettings: [:], groups: []) let deploymentTargetString = buildSettings[platform.deploymentTargetSetting]?.stringValue let deploymentTarget = deploymentTargetString == nil ? nil : try Version.parse(deploymentTargetString!) - let requiresObjCLinking = buildSettings["OTHER_LDFLAGS"]?.stringValue?.contains("-ObjC") ?? (productType == .staticLibrary) + let requiresObjCLinking = buildSettings["OTHER_LDFLAGS"]?.stringValue?.contains("-ObjC") let dependencyTarget = Target( name: targetObject.name, type: productType, @@ -765,7 +765,7 @@ public class PBXProjGenerator { targetFrameworkBuildFiles.append(buildFile) if !anyDependencyRequiresObjCLinking - && dependencyTarget.requiresObjCLinking ?? (dependencyTarget.type == .staticLibrary) { + && dependencyTarget.requiresObjCLinking ?? (dependencyLinkage == .static) { anyDependencyRequiresObjCLinking = true } } diff --git a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift index fd8a5571..9303c78c 100644 --- a/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift +++ b/Tests/XcodeGenKitTests/ProjectGeneratorTests.swift @@ -1272,7 +1272,57 @@ class ProjectGeneratorTests: XCTestCase { dependencies: [Dependency(type: .target, reference: implicitlyRequiresObjCLinking.name)] ) - let targets = [requiresObjCLinking, doesntRequireObjCLinking, implicitlyRequiresObjCLinking, framework, app1, app2, app3] + let staticFramework = Target( + name: "staticFramework", + type: .staticFramework, + platform: .iOS + ) + let machOStaticFramework = Target( + name: "machOStaticFramework", + type: .framework, + platform: .iOS, + settings: Settings(buildSettings: ["MACH_O_TYPE": "staticlib"]) + ) + let staticFrameworkOptOut = Target( + name: "staticFrameworkOptOut", + type: .staticFramework, + platform: .iOS, + requiresObjCLinking: false + ) + let dynamicFramework = Target( + name: "dynamicFramework", + type: .framework, + platform: .iOS + ) + let app4 = Target( + name: "app4", + type: .application, + platform: .iOS, + dependencies: [Dependency(type: .target, reference: staticFramework.name)] + ) + let app5 = Target( + name: "app5", + type: .application, + platform: .iOS, + dependencies: [Dependency(type: .target, reference: machOStaticFramework.name)] + ) + let app6 = Target( + name: "app6", + type: .application, + platform: .iOS, + dependencies: [Dependency(type: .target, reference: staticFrameworkOptOut.name)] + ) + let app7 = Target( + name: "app7", + type: .application, + platform: .iOS, + dependencies: [Dependency(type: .target, reference: dynamicFramework.name)] + ) + + let targets = [ + requiresObjCLinking, doesntRequireObjCLinking, implicitlyRequiresObjCLinking, framework, app1, app2, app3, + staticFramework, machOStaticFramework, staticFrameworkOptOut, dynamicFramework, app4, app5, app6, app7, + ] let project = Project( basePath: fixturePath + "TestProject", @@ -1302,6 +1352,42 @@ class ProjectGeneratorTests: XCTestCase { try expect(app1OtherLinkerSettings.contains("-ObjC")) == true try expect(app2OtherLinkerSettings.contains("-ObjC")) == false try expect(app3OtherLinkerSettings.contains("-ObjC")) == true + let app4OtherLinkerSettings = try buildSettings(for: app4)["OTHER_LDFLAGS"]?.arrayValue ?? [] + let app5OtherLinkerSettings = try buildSettings(for: app5)["OTHER_LDFLAGS"]?.arrayValue ?? [] + let app6OtherLinkerSettings = try buildSettings(for: app6)["OTHER_LDFLAGS"]?.arrayValue ?? [] + let app7OtherLinkerSettings = try buildSettings(for: app7)["OTHER_LDFLAGS"]?.arrayValue ?? [] + + try expect(app4OtherLinkerSettings.contains("-ObjC")) == true + try expect(app5OtherLinkerSettings.contains("-ObjC")) == true + try expect(app6OtherLinkerSettings.contains("-ObjC")) == false + try expect(app7OtherLinkerSettings.contains("-ObjC")) == false + } + + $0.it("sets -ObjC for targets that depend on a static framework in an external project") { + let externalPath = try Path.uniqueTemporary() + defer { try? externalPath.delete() } + + let externalProject = Project( + basePath: externalPath, + name: "ExternalProject", + targets: [Target(name: "ExternalStaticFramework", type: .staticFramework, platform: .iOS)] + ) + let xcodeProject = try ProjectGenerator(project: externalProject).generateXcodeProject(userName: "someUser") + try FileWriter(project: externalProject).writeXcodeProject(xcodeProject) + + var target = app + target.dependencies = [Dependency(type: .target, reference: "ExternalProject/ExternalStaticFramework")] + let project = Project( + name: "test", + targets: [target], + projectReferences: [ + ProjectReference(name: "ExternalProject", path: (externalPath + "ExternalProject.xcodeproj").string), + ] + ) + + let pbxProj = try project.generatePbxProj() + let buildConfig = try unwrap(pbxProj.targets(named: target.name).first?.buildConfigurationList?.buildConfigurations.first) + try expect(buildConfig.buildSettings["OTHER_LDFLAGS"]?.arrayValue?.contains("-ObjC") ?? false) == true } $0.it("filter sources with inferDestinationFiltersByPath") {