Add C ABI - #229
Merged
Merged
Conversation
colemancda
force-pushed
the
feature/c-abi
branch
from
August 1, 2026 16:24
c0dd12d to
5e538a5
Compare
colemancda
force-pushed
the
feature/c-abi
branch
from
August 3, 2026 11:56
add63e7 to
eb82384
Compare
…rait Adds SWIFTPM_BLUETOOTH_CABI-gated targets for the C ABI (off by default), and moves the BluetoothMetadata dependency behind a new opt-in Metadata package trait so the base package no longer links Foundation and a resource bundle unconditionally.
Rebasing feature/c-abi onto master surfaced a name collision: feature/c-abi's own C-ABI-style sdp_* implementation was declared as a target/product named BluetoothSDP, unaware that master had since gained an unrelated pure-Swift BluetoothSDP target (merged via #230, after the "no C ABI at all" decision for feature/sdp). Both ended up declared in Package.swift and both wrote into Sources/BluetoothSDP/. Folds the C-ABI sdp_* implementation into BluetoothABI instead of giving it a separate module — CMake already links them into a single shared object (bluetooth-util), so this also removes a redundant static-library layer. The 8 source files move to Sources/BluetoothABI/ with an SDP prefix (avoiding a real filename collision on UUID.swift, whose bt_uuid_* and sdp_* implementations are unrelated), and their round-trip tests move into BluetoothABITests. Sources/BluetoothSDP/ now holds only master's pure-Swift implementation, untouched. Updates CMakeLists.txt (drops the separate BluetoothSDP static library), Conformance/compare.sh (sdp_* symbols now live in libBluetoothABI.so, so the differential driver no longer needs a second binary path), and CBluetooth's README.
colemancda
force-pushed
the
feature/c-abi
branch
from
August 3, 2026 14:07
eb82384 to
fa72073
Compare
CMakeLists.txt builds libbluetooth-util.so independently of SwiftPM's own build graph (soname, symbol versioning, and install name aren't expressible in Package.swift), so nothing else in CI currently exercises it. Configures, builds, and checks the exported symbol table against cmake/symbols.txt on both Swift versions the main Linux job tests. Verified locally against the actual swift:X-jammy containers before adding.
CMakeLists.txt requires CMake 3.26+, but jammy's apt only ships 3.22.1, so the job failed at the version check before configuring. noble ships 3.28.3.
The C ABI layer is bound with `@c`, which Swift 6.2 rejects as an unknown attribute, so that matrix cell could never succeed. The rest of the package still builds on 6.2 — the C ABI targets are only added when SWIFTPM_BLUETOOTH_CABI=1, which no other workflow sets. Also stops one cell's failure from cancelling the others.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a Swift implementation of the non-socket half of BlueZ's libbluetooth C API: the 17
bluetooth.centry points, the 10bt_uuid_*functions, and the 75 puresdp_*symbols — the SDP data model, PDU wire-format codec, andsdp_record_tattribute accessors. The socket/session half of SDP (sdp_connect,sdp_process, the register/update/unregister and async request families) belongs to BluetoothLinux, along with the rest oflibbluetooth.Sources/CBluetoothvendorsbluetooth.h,uuid.h,sdp.h,sdp_lib.h(andhci.h, only becausesdp_lib.hincludes it) verbatim from BlueZ, underinclude/bluetooth/— GPL-2.0-or-later, isolated in its own directory with its own LICENSE and README so the rest of the package's MIT licensing stays unambiguous — plus C implementations of the four functions that can't be exported from Swift (baprintf/bafprintf/basprintf/basnprintfare C variadics) and smallsscanftrampolines for the UUID parser.Sources/BluetoothABIandSources/BluetoothSDPimplement the rest in Swift, exported via@cso each signature is checked against the vendored header declaration rather than trusted as a string.sdp_data_t,sdp_list_tandsdp_record_tare transparent structs (every field is read directly by real consumers), so their layout is itself the ABI contract — these functions operate on the imported C types directly, the same pattern already used forbdaddr_tandbt_uuid_t.SWIFTPM_BLUETOOTH_CABI=1, off by default — the ordinary build is unaffected.CMakeLists.txtbuilds the installable artifact (libbluetooth-util.so.0), since soname/export-list/install-name aren't expressible inPackage.swift.Scripts/check-exports.shasserts the built library's exports (102 symbols) againstcmake/symbols.txt. Closes Add CMake support #163.Conformance/differentially compares output against the reallibbluetooth.so.3, byte for byte, across three drivers (address functions,bt_uuid_*, and the SDP family — the last two build a moderately complex service record, encode it, and decode it back). All three are identical to the reference except three documentedbt_compidtostrmetadata-freshness deltas and one documentedsdp_uuid16_cmpmemcmp-magnitude delta (the sign — the only part ofmemcmp's contract that's actually specified — matches).bt_compidtostr's name table is generated ahead of time from the existing assigned-numbers metadata (Scripts/generate-company-names.py) into a static blob, rather than read at runtime — this avoids a Foundation/Bundle.moduledependency in the ABI layer.This work also caught and fixed two real bugs along the way, both confirmed via the differential conformance harness rather than by inspection:
bt_uuid_cmp/sdp_uuid_cmp-family functions computing an explicit byte difference instead of callingmemcmp, after finding that-Ooptimizes small fixed-sizememcmpcalls to a normalized ±1 result while the reference's own build does not — silently optimization-level-dependent output for a function whose only real contract is its sign..pointee(&d.pointee.val.uint16) is only valid for the duration of the call it's a direct argument to; extracting that address as a value to store in an array for a later, separate call (thedtds/valuesarray-building pattern this ABI needs throughout) produced heap-layout-dependent corruption. Fixed by taking the address of the whole union instead (stable, since every C union member starts at the same offset) via small helpers inPointers.swift.Also in this PR
The
BluetoothMetadatadependency (assigned-numbersnamelookups onCompanyIdentifier,BluetoothUUIDandUnitIdentifier) moves behind a new opt-inMetadatapackage trait, off by default. It was previously always linked, which pulled in Foundation and a resource bundle unconditionally; gating it keeps the base package dependency-free and buildable outside SwiftPM (which the CMake build needs). The generated numeric definitions (CompanyIdentifier.apple, etc.) are unaffected.Test plan
swift build/swift test— default configuration (485 tests)swift build --traits Metadata/swift test --traits Metadata— metadata enabled (485 tests)SWIFTPM_BLUETOOTH_CABI=1 swift build/swift test— C ABI enabled (517 tests)cmake -B build -G Ninja && cmake --build build && cmake --build build --target check-exports— 102/102 symbols matchConformance/compare.shagainst systemlibbluetooth.so.3— identical output modulo the four documented deltas, on both the SwiftPM debug build and the CMake release build, repeated across multiple clean runs to confirm no remaining non-determinism