Skip to content

Commit 907b764

Browse files
authored
Add Platform target with cross-platform logging support (#201)
1 parent c57b22e commit 907b764

13 files changed

Lines changed: 277 additions & 51 deletions

File tree

‎Package.resolved‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Package.swift‎

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ var sharedCSettings: [CSetting] = [
165165
.define("NDEBUG", .when(configuration: .release)),
166166
]
167167

168-
var sharedCXXSettings: [CXXSetting] = [
168+
var sharedCxxSettings: [CXXSetting] = [
169169
.unsafeFlags(["-I", libSwiftPath], .when(platforms: .nonDarwinPlatforms)),
170170
.define("NDEBUG", .when(configuration: .release)),
171171
]
@@ -185,7 +185,7 @@ sharedCSettings.append(
185185
"-isystem", "\(swiftCheckoutPath)/stdlib/public/SwiftShims",
186186
])
187187
)
188-
sharedCXXSettings.append(
188+
sharedCxxSettings.append(
189189
.unsafeFlags([
190190
"-isystem", "\(swiftCheckoutPath)/include",
191191
"-isystem", "\(swiftCheckoutPath)/stdlib/include",
@@ -208,7 +208,7 @@ if libraryEvolutionCondition {
208208
}
209209
if !compatibilityTestCondition {
210210
sharedCSettings.append(.define("OPENATTRIBUTEGRAPH"))
211-
sharedCXXSettings.append(.define("OPENATTRIBUTEGRAPH"))
211+
sharedCxxSettings.append(.define("OPENATTRIBUTEGRAPH"))
212212
sharedSwiftSettings.append(.define("OPENATTRIBUTEGRAPH"))
213213
}
214214

@@ -231,38 +231,49 @@ extension [Platform] {
231231
}
232232
}
233233

234-
// MARK: - Targets
234+
// MARK: - Plugin
235235

236236
let swiftClonePlugin = Target.plugin(
237237
name: "CloneSwiftPlugin",
238238
capability: .buildTool()
239239
)
240240

241-
let openAttributeGraphTarget = Target.target(
242-
name: "OpenAttributeGraph",
243-
dependencies: ["OpenAttributeGraphCxx"],
244-
cSettings: sharedCSettings,
245-
cxxSettings: sharedCXXSettings,
246-
swiftSettings: sharedSwiftSettings
241+
// MARK: - Targets
242+
243+
let platformTarget = Target.target(
244+
name: "Platform",
245+
cSettings: [
246+
.define("_GNU_SOURCE", .when(platforms: [.linux])),
247+
]
247248
)
248249
// FIXME: Merge into one target
249250
// OpenAttributeGraph is a C++ & Swift mix target.
250251
// The SwiftPM support for such usage is still in progress.
251-
let openAttributeGraphSPITarget = Target.target(
252+
let openAttributeGraphCxxTarget = Target.target(
252253
name: "OpenAttributeGraphCxx",
254+
dependencies: [.target(name: platformTarget.name)],
253255
cSettings: sharedCSettings + [
254256
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
255257
],
256-
cxxSettings: sharedCXXSettings,
258+
cxxSettings: sharedCxxSettings,
257259
linkerSettings: [
258260
.linkedLibrary("z"),
259261
],
260262
plugins: [.plugin(name: swiftClonePlugin.name)]
261263
)
264+
let openAttributeGraphTarget = Target.target(
265+
name: "OpenAttributeGraph",
266+
dependencies: [
267+
.target(name: openAttributeGraphCxxTarget.name),
268+
],
269+
cSettings: sharedCSettings,
270+
cxxSettings: sharedCxxSettings,
271+
swiftSettings: sharedSwiftSettings
272+
)
262273
let openAttributeGraphShimsTarget = Target.target(
263274
name: "OpenAttributeGraphShims",
264275
cSettings: sharedCSettings,
265-
cxxSettings: sharedCXXSettings,
276+
cxxSettings: sharedCxxSettings,
266277
swiftSettings: sharedSwiftSettings
267278
)
268279

@@ -271,41 +282,41 @@ let openAttributeGraphShimsTarget = Target.target(
271282
let openAttributeGraphTestsTarget = Target.testTarget(
272283
name: "OpenAttributeGraphTests",
273284
dependencies: [
274-
"OpenAttributeGraph",
285+
.target(name: openAttributeGraphTarget.name),
275286
],
276287
exclude: ["README.md"],
277288
cSettings: sharedCSettings,
278-
cxxSettings: sharedCXXSettings,
289+
cxxSettings: sharedCxxSettings,
279290
swiftSettings: sharedSwiftSettings
280291
)
281292
let openAttributeGraphCxxTestsTarget = Target.testTarget(
282293
name: "OpenAttributeGraphCxxTests",
283294
dependencies: [
284-
"OpenAttributeGraphCxx",
295+
.target(name: openAttributeGraphCxxTarget.name),
285296
],
286297
exclude: ["README.md"],
287298
cSettings: sharedCSettings + [.define("SWIFT_TESTING")],
288-
cxxSettings: sharedCXXSettings,
299+
cxxSettings: sharedCxxSettings,
289300
swiftSettings: sharedSwiftSettings + [.interoperabilityMode(.Cxx)]
290301
)
291302
let openAttributeGraphShimsTestsTarget = Target.testTarget(
292303
name: "OpenAttributeGraphShimsTests",
293304
dependencies: [
294-
"OpenAttributeGraphShims",
305+
.target(name: openAttributeGraphShimsTarget.name),
295306
],
296307
exclude: ["README.md"],
297308
cSettings: sharedCSettings,
298-
cxxSettings: sharedCXXSettings,
309+
cxxSettings: sharedCxxSettings,
299310
swiftSettings: sharedSwiftSettings
300311
)
301312
let openAttributeGraphCompatibilityTestsTarget = Target.testTarget(
302313
name: "OpenAttributeGraphCompatibilityTests",
303314
dependencies: [
304315
.product(name: "Numerics", package: "swift-numerics"),
305-
] + (compatibilityTestCondition ? [] : ["OpenAttributeGraph"]),
316+
] + (compatibilityTestCondition ? [] : [.target(name: openAttributeGraphTarget.name)]),
306317
exclude: ["README.md"],
307318
cSettings: sharedCSettings,
308-
cxxSettings: sharedCXXSettings,
319+
cxxSettings: sharedCxxSettings,
309320
swiftSettings: sharedSwiftSettings
310321
)
311322

@@ -314,16 +325,17 @@ let openAttributeGraphCompatibilityTestsTarget = Target.testTarget(
314325
let package = Package(
315326
name: "OpenAttributeGraph",
316327
products: [
317-
.library(name: "OpenAttributeGraph", type: .dynamic, targets: ["OpenAttributeGraph", "OpenAttributeGraphCxx"]),
318-
.library(name: "OpenAttributeGraphShims", type: .dynamic, targets: ["OpenAttributeGraph", "OpenAttributeGraphCxx", "OpenAttributeGraphShims"]),
328+
.library(name: "OpenAttributeGraph", type: .dynamic, targets: [openAttributeGraphTarget.name, openAttributeGraphCxxTarget.name]),
329+
.library(name: "OpenAttributeGraphShims", type: .dynamic, targets: [openAttributeGraphTarget.name, openAttributeGraphCxxTarget.name, openAttributeGraphShimsTarget.name]),
319330
],
320331
dependencies: [
321332
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
322333
],
323334
targets: [
324335
swiftClonePlugin,
336+
platformTarget,
325337
openAttributeGraphTarget,
326-
openAttributeGraphSPITarget,
338+
openAttributeGraphCxxTarget,
327339
openAttributeGraphShimsTarget,
328340
],
329341
cxxLanguageStandard: .cxx20
@@ -362,6 +374,6 @@ if attributeGraphCondition {
362374
default: nil
363375
}
364376
} else {
365-
openAttributeGraphShimsTarget.dependencies.append("OpenAttributeGraph")
377+
openAttributeGraphShimsTarget.dependencies.append(.target(name: openAttributeGraphTarget.name))
366378
package.platforms = [.iOS(.v13), .macOS(.v10_15), .macCatalyst(.v13), .tvOS(.v13), .watchOS(.v5)]
367379
}

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ For a simpler setup, you can use the prebuilt XCFramework available on the [rele
5050

5151
The current suggested toolchain to build the project is Swift 6.1.2 / Xcode 16.4.
5252

53+
### Clone Swift headers
54+
55+
The project requires Swift toolchain headers for compilation. You can either clone them manually or let the build plugin handle it:
56+
57+
```shell
58+
# Option 1: Clone headers manually
59+
./Scripts/clone-swift.sh
60+
61+
# Option 2: Let the build plugin clone headers (requires --disable-sandbox)
62+
swift build --disable-sandbox
63+
```
64+
5365
### Set up LIB_SWIFT_PATH on non-Darwin platform
5466

5567
If your swift binary path is located in your `<toolchain>/usr/bin/swift` (eg. installed by [swiftbox](https://github.com/stevapple/swiftbox)), no setup is required.

‎Scripts/build.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ OPENATTRIBUTEGRAPH_ROOT="$(dirname $(dirname $(filepath $0)))"
99

1010
cd $OPENATTRIBUTEGRAPH_ROOT
1111

12-
swift build
12+
swift build --disable-sandbox

‎Sources/OpenAttributeGraphCxx/Misc/assert.cpp‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ void precondition_failure(const char *format, ...) {
1818
vasprintf(&s, format, va);
1919
va_end(va);
2020
if (s != nullptr) {
21-
#if OAG_TARGET_OS_DARWIN
22-
os_log_error(error_log(), "precondition failure: %s", s);
23-
#endif /* OAG_TARGET_OS_DARWIN */
21+
platform_log_error(error_log(), "precondition failure: %s", s);
2422
#if OAG_TARGET_RELEASE >= OAG_RELEASE_2023
2523
// OAG::Graph::trace_assertion_failure(true, "precondition failure: %s", s)
2624
#endif
@@ -39,9 +37,7 @@ void non_fatal_precondition_failure(const char *format, ...) {
3937
vasprintf(&s, format, va);
4038
va_end(va);
4139
if (s != nullptr) {
42-
#if OAG_TARGET_OS_DARWIN
43-
os_log_fault(error_log(), "precondition failure: %s", s);
44-
#endif /* OAG_TARGET_OS_DARWIN */
40+
platform_log_fault(error_log(), "precondition failure: %s", s);
4541
free(s);
4642
}
4743
return;

‎Sources/OpenAttributeGraphCxx/Misc/log.cpp‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22
// OAGLog.cpp
33
// OpenAttributeGraphCxx
44
//
5-
// Audited for 2021 Release
5+
// Status: Complete
6+
// Audited for 6.5.1
67

78
#include <OpenAttributeGraphCxx/Misc/log.hpp>
89

9-
#if OAG_TARGET_OS_DARWIN
10-
1110
namespace OAG {
12-
os_log_t misc_log() {
13-
static os_log_t log = os_log_create("org.OpenSwiftUIProject.OpenAttributeGraph", "misc");
11+
platform_log_t misc_log() {
12+
static platform_log_t log = platform_log_create("org.OpenSwiftUIProject.OpenAttributeGraph", "misc");
1413
return log;
1514
}
16-
os_log_t error_log() {
17-
static os_log_t log = os_log_create("org.OpenSwiftUIProject.OpenAttributeGraph", "error");
15+
platform_log_t error_log() {
16+
static platform_log_t log = platform_log_create("org.OpenSwiftUIProject.OpenAttributeGraph", "error");
1817
return log;
1918
}
2019
} /* OAG */
21-
22-
#endif /* OAG_TARGET_OS_DARWIN */

‎Sources/OpenAttributeGraphCxx/include/OpenAttributeGraphCxx/Misc/log.hpp‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
// log.hpp
33
// OpenAttributeGraphCxx
44
//
5-
// Audited for 2021 Release
5+
// Status: Complete
6+
// Audited for 6.5.1
67

78
#ifndef log_hpp
89
#define log_hpp
910

1011
#include <OpenAttributeGraph/OAGBase.h>
11-
12-
#if OAG_TARGET_OS_DARWIN
13-
14-
#include <os/log.h>
12+
#include <platform/log.h>
1513

1614
namespace OAG {
17-
os_log_t misc_log();
18-
os_log_t error_log();
15+
platform_log_t misc_log();
16+
platform_log_t error_log();
1917
} /* OAG */
2018

21-
#endif /* OAG_TARGET_OS_DARWIN */
22-
2319
#endif /* log_hpp */

‎Sources/Platform/LICENSE‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This module is derived from the Platform target in the Compute project:
2+
https://github.com/jcmosc/Compute
3+
4+
MIT License
5+
6+
Copyright (c) 2025 James Moschou
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Platform {
2+
header "platform/platform.h"
3+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
#include <stddef.h>
4+
#include <stdint.h>
5+
#include <stdbool.h>
6+
7+
#ifndef __has_feature
8+
#define __has_feature(x) 0
9+
#endif
10+
#ifndef __has_extension
11+
#define __has_extension(x) 0
12+
#endif
13+
14+
#if !defined(PLATFORM_EXTERN_C_BEGIN)
15+
#if defined(__cplusplus)
16+
#define PLATFORM_EXTERN_C_BEGIN extern "C" {
17+
#define PLATFORM_EXTERN_C_END }
18+
#else
19+
#define PLATFORM_EXTERN_C_BEGIN
20+
#define PLATFORM_EXTERN_C_END
21+
#endif
22+
#endif
23+
24+
#if __GNUC__
25+
#define PLATFORM_EXPORT extern __attribute__((__visibility__("default")))
26+
#else
27+
#define PLATFORM_EXPORT extern
28+
#endif
29+
30+
#if __GNUC__
31+
#define PLATFORM_INLINE static __inline__
32+
#else
33+
#define PLATFORM_INLINE static inline
34+
#endif
35+
36+
#if __has_feature(assume_nonnull)
37+
#define PLATFORM_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
38+
#define PLATFORM_ASSUME_NONNULL_END _Pragma("clang assume_nonnull end")
39+
#else
40+
#define PLATFORM_ASSUME_NONNULL_BEGIN
41+
#define PLATFORM_ASSUME_NONNULL_END
42+
#endif
43+
44+
#if !__has_feature(nullability)
45+
#ifndef _Nullable
46+
#define _Nullable
47+
#endif
48+
#ifndef _Nonnull
49+
#define _Nonnull
50+
#endif
51+
#ifndef _Null_unspecified
52+
#define _Null_unspecified
53+
#endif
54+
#endif
55+
56+
#if __has_feature(objc_fixed_enum) || __has_extension(cxx_fixed_enum) || \
57+
__has_extension(cxx_strong_enums)
58+
#define PLATFORM_ENUM(_name, _type, ...) \
59+
typedef enum : _type { __VA_ARGS__ } _name##_t
60+
#else
61+
#define PLATFORM_ENUM(_name, _type, ...) \
62+
typedef _type _name##_t; enum { __VA_ARGS__ }
63+
#endif

0 commit comments

Comments
 (0)