Search code examples
c++swiftautomatic-ref-countingobjective-c++swift-package-manager

Swift Package Manager: add compile flag to a single file -fno-objc-arc


Overview

I'm porting an C++ / ObjC++ library to Swift Package Manager. The library targets common Apple platforms (iOS, macOS). Currently, the library can be built using xcodebuild and a static library target.

Question

Since the library contains C++ <> ObjC bridging code, it has one file compiled with the flag -fno-objc-arc. All of the other files are compiled with ARC. Is it possible to pass a flag telling SPM to also compile that file without ARC?

Screenshot

screenshot

Package.swift

// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "library_name",
    platforms: [
        .iOS(.v13)
    ],
    products: [
        .library(
            name: "library_name",
            targets: ["library_name"]),
    ],
    dependencies: [
    ],
    targets: [
        .target(
            name: "library_name",
            dependencies: [],
            path: "./sources/",
            exclude: [

            ],
            sources:
                [
                    "sourcefile_noARC.mm",
                    "sourcefile2.mm",
                    "sourcefile3.cpp",
                ],
            publicHeadersPath: "SwiftPackage/headers/",
            cxxSettings: [
                .headerSearchPath("./")
            ]
        ),
    ],
    cxxLanguageStandard: .cxx14
)

"Unsafe Flags"

I've tried using unsafe flags feature to pass the compile flag to the whole library:

.target(
            name: "fs_helpers",
            dependencies: [],
            path: "./sources/",
            exclude: [

            ],
            sources:
                [
                    "sourcefile_noARC.mm",
                    "sourcefile2.mm",
                    "sourcefile3.cpp",
                ],
            publicHeadersPath: "SwiftPackage/headers/",
            cxxSettings: [
                .headerSearchPath("./"),
                .unsafeFlags(["-fno-objc-arc"]) // ADDING THE FLAG
            ]
        ),

However, this adds the flag to the whole library and not just to a single file as I want.


Solution

  • I believe it is not possible at the moment. Yet we have a hope that this proposal will be accepted and your (and mine) problem will be solved. There are few alternatives that may help:

    1. Use Cocoa pods
    2. Use submodules
    3. Use precompiled binary