Swift Package Manager for ObjC targets

Hi!

I would like to setup Package for ObjC targets: Dynamic Library and Static Library.
I have following folder structure:

.
|── .git
|── .gitignore
|── Package.swift

|── Core
	|── FolderOne
		|── Worker.h
		|── Worker.m
	|── FolderTwo
		|── Worker2.h
		|── Worker2.m
	|── FolderThree
		|── FolderFour
			|── Worker3.h
			|── Worker3.m
	|── FrameworkSupplement
		|── UmbrellaHeader.h
		|── map.modulemap

|-- Dependencies
	|-- OneDependency
		|-- OneDependency.h
		|-- OneDependency.m

|── Framework
	|── Name
		|── Info.plist

|── Tests
	|── iOS_Tests
		|── Info.plist
	|── macOS_Tests 
		|── Info.plist
	|── SharedTests
		|── SharedTest2.m
		|── SharedTest3.m

and Package.swift:

import PackageDescription

let package = Package(
    name: "SuperLibrary",
    products: [
        .library(
            name: "SuperLibrary",
            type: .dynamic,
            targets: ["SuperLibrary"]),
    ],
    targets: [
        .target(name: "SuperLibrary",
                path: ".",
                exclude: [],
                sources: ["Core", "Dependencies/OneDependency/OneDependency.m"],
                publicHeadersPath: "Core"), // OneDependency.h is not public, it is private and had been added by hands as plain sources.
    ]
)

There is no warnings in REPL, but, it doesn't compile. Build action fails because SwiftPM can't find any of my header files ( umbrella or non-umbrella ). Also it can't handle neither framework #import <SuperLibrary/File.h> nor plain #import "File.h" ).

1 Like

I tried AFNetworking, and it works for me. Maybe publicHeadersPath: nil, and then put all your public headers in include directory.

Is this folder virtual or physical ( file on disk )? I am not willing to change it for only swift package manager.

39

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

import PackageDescription

let package = Package(
    name: "SuperLibrary",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "SuperLibrary",
            type: .dynamic,
            targets: ["SuperLibrary"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "SuperLibrary",
            path: "Sources",
            exclude: [],
            sources: ["Core", "Dependencies/OneDependency/OneDependency.m"],
            publicHeadersPath: "Core"),
        .testTarget(
            name: "SuperLibraryTests",
            dependencies: ["SuperLibrary"]),
    ]
)

Maybe this is what you need. No need to create include directory.

Can you share example project?
By the way, neither of these headers are included into each other.
So, they are pure public headers without headers imports?

1 Like

Import statements in this example has relative paths.
So, I have reached "alternative" solution by putting all headers in "Core/Include" directory.
Ugly, though.

I still have issues with test targets. Headers can not be marked as public or private.
By the way, .testTarget even doesn't have headers, thus, it is impossible to mix helpers/scaffolding/fixtures and test targets.

Another interesting problem.

Since I can't use framework import #import <Library/Umbrella.h> ( because umbrella contains framework imports, hah ), I change imports to modern style @import Library;

Everything works fine in Xcode, but I can't build package from command line: swift build throws error: "@import Library; - module not found".

As I mentioned above, I can build package in Xcode and even run tests, but command line swift build/test doesn't work at all.

Terms of Service

Privacy Policy

Cookie Policy