Profile Recorder (and projects depending on it) not compiling with Static Linux SDK

The latest release (0.3.17) and main branch seem to be broken for static Linux builds.

Looks like the lack of Glibc is addressed in some of the earlier files but not on the newer additions. The CI check appears failed but I'm not sure it is related as it's probably not verifying static linux builds.

What follows is a transcript of the bug filed under Error when compiling with Static Linux SDK · Issue #64 · apple/swift-profile-recorder · GitHub. Thanks

Sources/ProfileRecorderSampleConversion/HelperFunctions.swift has

#if canImport(Glibc)
@preconcurrency import Glibc // Sendability of stdout/stderr/..., needs to be at the top of the file
#endif
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

func swift_reportWarning(_ dunno: Int, _ message: String) {
    fputs("WARNING: \(message)\n", stderr)
}

Which causes this double compiler error:

/opt/src/src/bitcoin-crypto/UInt256.swift:11:5: error: cannot find 'fputs' in scope
  9 | 
 10 | func swift_reportWarning(_ dunno: Int, _ message: String) {
 11 |     fputs("WARNING: \(message)\n", stderr)
    |     `- error: cannot find 'fputs' in scope
 12 | }
 13 | 

/opt/src/src/bitcoin-crypto/UInt256.swift:11:36: error: cannot find 'stderr' in scope
  9 | 
 10 | func swift_reportWarning(_ dunno: Int, _ message: String) {
 11 |     fputs("WARNING: \(message)\n", stderr)
    |                                    `- error: cannot find 'stderr' in scope
 12 | }
 13 | 

The issue can be fixed by importing Musl when available (Glibc will not be available). This is already done in some parts of the codebase like Sources/ProfileRecorderSampleConversion/NativeELFSymboliser/Elf.swift but it's missing in HelperFunctions, Sources/ProfileRecorder/ProfileRecorderSampler.swift, Sources/swipr-sample-conv/LLVMJSONOutputParserHandler.swift, Sources/swipr-sample-conv/LLVMOutputParserHandler.swift, Sources/swipr-sample-conv/LLVMSymbolizer.swift and Sources/swipr-sample-conv/MainFile.swift.

#if canImport(Glibc)
@preconcurrency import Glibc // Sendability of stdout/stderr/..., needs to be at the top of the file
#endif
#if canImport(Musl)
@preconcurrency import Musl // Sendability of stdout/stderr/..., needs to be at the top of the file
#endif
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import Foundation
#endif

func swift_reportWarning(_ dunno: Int, _ message: String) {
    fputs("WARNING: \(message)\n", stderr)
}

The more robust way adopted in other parts of the project is:

#if canImport(Darwin)
import Darwin
#elseif os(Windows)
import WinSDK
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif

I saw your pull request to import Musl, thanks for that!

2 Likes