shareup / json-apple

MIT License
3 stars 1 forks source link

Linux Support #6

Open abegehr opened 9 months ago

abegehr commented 9 months ago

The package looks great and is easy to use! However running on Linux as part of the vanilla Dockerfile for Vapor fails:

 > [app build  8/14] RUN swift build -c release                 --static-swift-stdlib                 -Xlinker -ljemalloc:
3.967 Building for production...
4.351 [0/874] Compiling _NumericsShims _NumericsShims.c
4.417 [1/874] Compiling _AtomicsShims.c
4.424 [2/874] Compiling errno.c
4.430 [2/874] Compiling a_type.c
4.430 [3/874] Compiling asn1_lib.c
4.430 [3/874] Compiling a_d2i_fp.c
4.431 [6/874] Compiling a_utctm.c
4.431 [6/874] Compiling a_time.c
6.993 [9/882] Compiling _NIOBase64 Base64.swift
7.041 [10/883] Compiling RealModule AlgebraicField.swift
7.335 [13/884] Compiling JSON Dictionary+JSON.swift
7.335 /build/.build/checkouts/json-apple/Sources/JSON/Dictionary+JSON.swift:73:9: error: cannot find 'CFBooleanGetTypeID' in scope
7.335         CFBooleanGetTypeID() == CFGetTypeID(self)
7.335         ^~~~~~~~~~~~~~~~~~
7.335 /build/.build/checkouts/json-apple/Sources/JSON/Dictionary+JSON.swift:73:33: error: cannot find 'CFGetTypeID' in scope
7.335         CFBooleanGetTypeID() == CFGetTypeID(self)
7.335                                 ^~~~~~~~~~~
7.335 /build/.build/checkouts/json-apple/Sources/JSON/JSON.swift:644:9: error: cannot find 'CFBooleanGetTypeID' in scope
7.335         CFBooleanGetTypeID() == CFGetTypeID(self)
7.335         ^~~~~~~~~~~~~~~~~~
7.335 /build/.build/checkouts/json-apple/Sources/JSON/JSON.swift:644:33: error: cannot find 'CFGetTypeID' in scope
7.335         CFBooleanGetTypeID() == CFGetTypeID(self)
7.335                                 ^~~~~~~~~~~
7.356 error: fatalError

The referenced symbol is in swift-corelibs-foundation: https://github.com/apple/swift-corelibs-foundation/blob/dbca8c7ddcfd19f7f6f6e1b60fd3ee3f748e263c/CoreFoundation/Base.subproj/CFRuntime.c#L732

abegehr commented 9 months ago

Vapor docker file for reference: https://github.com/vapor/template/blob/main/Dockerfile

kamleshboi69 commented 9 months ago

hi i checked this issue mayube the reason could be related to the inability to find 'CFBooleanGetTypeID' and 'CFGetTypeID' in scope. These are Core Foundation functions in Swift, and the error indicates that the compiler cannot find them during the build process. check these SDK Version and Target Platform: Ensure that you are targeting an SDK version that includes the necessary Core Foundation frameworks. Sometimes, issues like this can arise if the SDK version is not compatible with the functions being used.

Importing CoreFoundation: Make sure you have imported the CoreFoundation framework in your Swift files where these functions are used. You can add the following import statement at the top of the file: try this import CoreFoundation Check Dependencies: Verify that your project dependencies are correctly set up and that the versions of the libraries you are using are compatible with each other. It's possible that there is a version mismatch causing these symbols to be unavailable.

Compiler Flags: Ensure that your Swift compiler flags are set up correctly. The build command in your log shows some additional flags like '--static-swift-stdlib' and '-Xlinker -ljemalloc'. Make sure these flags are appropriate for your project.

Swift Version: Ensure that you are using a Swift version that is compatible with the libraries and dependencies you are using. Sometimes, certain functions may have been deprecated or changed in newer Swift versions.

Library Updates: If you are using third-party libraries, check if there are any updates available. The issue might be resolved in a newer version of the library.

if yu have any problem ask

abegehr commented 8 months ago

Thanks for your answer, @kamleshboi69! I've tried simply importing CoreFoundation but that doesn't solve it yet. I've found this thread on the Swift forums that I'm researching now: https://forums.swift.org/t/static-linking-on-linux-in-swift-5-3-1/41989

abegehr commented 8 months ago

I've tried running without the compiler flags, however no luck. It fails with the same fatal error.

I'm on the latest swift-version 5.9, which is also indicated on json-apple's Package.swift: // swift-tools-version: 5.9

Here is a cut out of my Package.swift, showing the dependencies:

    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "4.92.1"),

        .package(url: "https://github.com/vapor/queues-redis-driver.git", from: "1.1.1"),
        .package(url: "https://github.com/orlandos-nl/IkigaJSON.git", from: "2.2.1"),
        .package(url: "https://github.com/shareup/json-apple.git", from: "1.4.0"),

    ],

=> all are updated to the latest version.

By the way, is this the proper way of adding json-apple to an executable target?

        .executableTarget(
            name: "App",
            dependencies: [
                .product(name: "Vapor", package: "vapor"),
                .product(name: "QueuesRedisDriver", package: "queues-redis-driver"),
                .product(name: "IkigaJSON", package: "IkigaJSON"),
                .product(name: "JSON", package: "json-apple"),
            ]
        ),
abegehr commented 8 months ago

I'm finding the definition of CFBooleanGetTypeId and CFGetTypeId in swift-corelibs-foundation: https://github.com/apple/swift-corelibs-foundation/blob/main/Sources/Foundation/NSNumber.swift However it fails when run on Linux. @kamleshboi69, do you have a sample of json-apple successfully compiling for Linux?

abegehr commented 8 months ago

Through this thread: https://forums.swift.org/t/foundation-on-linux-cfbooleangettypeid-cfgettypeid/2736/14 I found this issue: https://github.com/apple/swift-corelibs-foundation/issues/4573 Which states: "2. The existing ways on Darwin that you might perform this coercion do not work on Glibc/Linux because they rely on CoreFoundation types/functions which do not exist (e.g., kCFBooleanTrue, kCFBooleanFalse, CFBooleanGetTypeID, CFGetTypeID, etc...)" EDIT: However, the issue has been fixed and as I linked to above the symbols exist in swift-corelibs-foundation.

abegehr commented 8 months ago

I've setup a simple repro repo here: https://github.com/abegehr/json-apple-vapor

  1. vapor new json-apple-vapor -n
  2. add json-apple dependency in Package.swift. See: https://github.com/abegehr/json-apple-vapor/commit/a7ed4bc82d63bec955eacc9e13a6da3b85d7f45c#diff-f913940c58e8744a2af1c68b909bb6383e49007e6c5a12fb03104a9006ae677e
  3. Start docker and run docker build . in the repo root directory.

Fails with the same error:

1.922 [16/890] Compiling JSON Dictionary+JSON.swift
1.922 /build/.build/checkouts/json-apple/Sources/JSON/Dictionary+JSON.swift:73:9: error: cannot find 'CFBooleanGetTypeID' in scope
1.922         CFBooleanGetTypeID() == CFGetTypeID(self)
1.922         ^~~~~~~~~~~~~~~~~~
1.922 /build/.build/checkouts/json-apple/Sources/JSON/Dictionary+JSON.swift:73:33: error: cannot find 'CFGetTypeID' in scope
1.922         CFBooleanGetTypeID() == CFGetTypeID(self)
1.922                                 ^~~~~~~~~~~
1.922 /build/.build/checkouts/json-apple/Sources/JSON/JSON.swift:644:9: error: cannot find 'CFBooleanGetTypeID' in scope
1.922         CFBooleanGetTypeID() == CFGetTypeID(self)
1.922         ^~~~~~~~~~~~~~~~~~
1.922 /build/.build/checkouts/json-apple/Sources/JSON/JSON.swift:644:33: error: cannot find 'CFGetTypeID' in scope
1.922         CFBooleanGetTypeID() == CFGetTypeID(self)
1.922                                 ^~~~~~~~~~~
1.925 error: fatalError
abegehr commented 8 months ago

I got the simple repro to work:

  1. remove json-apple as dependency
  2. copy over the two files JSON.swift and Dictionary.swift from json-apple's source
  3. import CoreFoundation in both files (one is not enough). => it builds fine with docker build ./: https://github.com/abegehr/json-apple-vapor/commit/7716bee7b6ec167a9dc09469cc11c6fb038586be However, I'm not sure why, since Foundation.swiftinterface imports CoreFoundation.swiftinterface.