I have a C library backing the Swift framework. When I attempt to port it to Linux and Windows, the compiler raises an error for the absence of the CoreFoundation header.
#ifndef DemoFlags_h
#define DemoFlags_h
#include <CoreFoundation/CoreFoundation.h>
typedef CF_OPTIONS(uint32_t, DemoFlags) {
DemoFlags_0 = 0,
DemoFlags_1 = 1,
};
CF_EXPORT
void greeting(void);
#endif /* DemoFlags_h */
Question 1:
What is the recommended approach for writing a portable C library in SwiftPM?
It will involve extensive utilization of the __attribute(swift)
macro.
Moreover, I would prefer not to write a separate header wrapper for this. The extensive use of CF macros aligns perfectly with my requirements.
eg. CF_EXPORT
CF_OPTION
CF_ASSUME_NONNULL_BEGIN
CF_ASSUME_NONNULL_END
I don't think it's necessary to repeat the invention of the wheel on Linux and Windows.
Question 2:
If the first question is by using #include <CoreFoundation/CoreFoundation.h>
, then how should we include them in our header code?
Initially, I attempted to create a SwiftPM package - CoreFoundation, copying all the header files and making necessary modifications to facilitate compilation.
However, this resulted in a conflict due to the redefinition of the module.
xx/toolchain/swift-5.9/usr/lib/swift/CoreFoundation/module.map:1:8: error: redefinition of module 'CoreFoundation'
Subsequently, I attempted to directly include it by adding an unsafeFlags
entry to the target configuration .unsafeFlags(["-I", "xx/toolchain/swift-5.9/usr/lib/swift/"],.when(platforms: [.linux])),
This, however, still led to a multitude of errors.
toolchain/swift-5.9/usr/lib/swift/CoreFoundation/ForSwiftFoundationOnly.h:93:20: error: declaration of '__CFAllocatorRespectsHintZeroWhenAllocating' has a different language linkage