Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping an existing C library as a Framework

Tags:

c

xcode

ios

I am trying to find a way of wrapping an existing C library with an Apple .framework structure. The key sticking point is avoiding the need to specify the Search Headers field in Settings.

Typically in a framework you specify something like:

#import <Foundation/Foundation.h>

where Foundation is the framework name and the .h file is an umbrella header.

When testing with existing code, for the sake of argument OpenSSL, the project is using #include <openssl/file.h> internally to refer to its files. Once you want to place this inside a framework for convenience every include naturally needs to be changed to <NameOfFramwork/openssl/file.h> or you must add the $(SRCROOT)/Path/To/Frameworks/NameOfFramework.framework/Headers to the search path. This is terribly inconvenient and kills a lot of the value of the framework format. It only becomes worse when you want to wrap multiple SDK versions of the library in an XCFramework.

I'm wondering specifically if the ModuleMap can help avoid the need to change the #includes? I've added a modulemap I'm creating as a test.

Experimental module.modulemap

framework module LibreSSL [extern_c] {
    umbrella header "LibreSSL.h"
    export *
    module * { export * }
    
    explicit module LibreSSL_Private [extern_c] {
        umbrella "Headers/include"
        link "LibreSSL"
        export *
    }
}
like image 421
Cameron Lowell Palmer Avatar asked Nov 06 '22 05:11

Cameron Lowell Palmer


1 Answers

One unsatisfying solution is to switch to an XCFramework comprised of Libraries instead of Frameworks.

xcodebuild \
    -create-xcframework \
    -library <path/to/library> \
    -header <path/to/headers> \
    -output MyCool.xcframework

Then header search paths work correctly, but you lose the .modulemap and the nice framework structure that keeps everything together neatly per SDK build.

like image 104
Cameron Lowell Palmer Avatar answered Nov 14 '22 22:11

Cameron Lowell Palmer