dagronf / QRCode

A quick and beautiful macOS/iOS/tvOS/watchOS QR Code generator/detector library for SwiftUI, Swift and Objective-C.
MIT License
450 stars 58 forks source link

Error: no such module 'CoreGraphics' [Linux] #46

Closed akaibukai closed 3 months ago

akaibukai commented 3 months ago

Hello and thanks for this amazing library!

I hope it's the right place to post this, otherwise I'll be happy to close the issue.

So, I wanted to try it using a binary as I have absolutely no experience with any Swift/macOS/iOS etc. projects. Then I went ahead and simply tried to generate the binary using the provided command (on Linux):

swift build -c release --product qrcodegen

I got the following error, (full ouput at the very bottom):

/app/QRCode/Sources/QRCode/QRCode+Builder.swift:33:8: error: no such module 'CoreGraphics'
import CoreGraphics
       ^
error: fatalError

I tried to search on the internet, it seems CoreGraphics is a thing in Apple computers? Is there any alternative I can use on Linux?

I found for example the following library: https://github.com/PureSwift/Silica It says Pure Swift CoreGraphics (or Quartz2D) implementation (Supports Linux) If I replace any import CoreGraphics with import Silica will it work?

FWIW, I'm running the official swift docker container swift:5.10 Maybe I need to install some dependencies in the container?

PS: Here is the complete output:

root@a783275defa0:/app/QRCode# swift build -c release --product qrcodegen
Fetching https://github.com/dagronf/swift-qrcode-generator
Fetching https://github.com/dagronf/SwiftImageReadWrite
Fetching https://github.com/apple/swift-argument-parser
Fetched https://github.com/dagronf/SwiftImageReadWrite from cache (1.84s)
Fetched https://github.com/dagronf/swift-qrcode-generator from cache (4.67s)
Fetched https://github.com/apple/swift-argument-parser from cache (4.68s)
Computing version for https://github.com/dagronf/SwiftImageReadWrite
Computed https://github.com/dagronf/SwiftImageReadWrite at 1.7.2 (0.20s)
Computing version for https://github.com/dagronf/swift-qrcode-generator
Computed https://github.com/dagronf/swift-qrcode-generator at 2.0.2 (0.19s)
Computed https://github.com/dagronf/SwiftImageReadWrite at 1.7.2 (0.00s)
Computed https://github.com/dagronf/swift-qrcode-generator at 2.0.2 (0.00s)
Computing version for https://github.com/apple/swift-argument-parser
Computed https://github.com/apple/swift-argument-parser at 0.4.4 (0.21s)
Computed https://github.com/apple/swift-argument-parser at 0.4.4 (0.00s)
Creating working copy for https://github.com/dagronf/swift-qrcode-generator
Working copy of https://github.com/dagronf/swift-qrcode-generator resolved at 2.0.2
Creating working copy for https://github.com/dagronf/SwiftImageReadWrite
Working copy of https://github.com/dagronf/SwiftImageReadWrite resolved at 1.7.2
Creating working copy for https://github.com/apple/swift-argument-parser
Working copy of https://github.com/apple/swift-argument-parser resolved at 0.4.4
Building for production...
/app/QRCode/Sources/QRCode/QRCode+Builder.swift:33:8: error: no such module 'CoreGraphics'
import CoreGraphics
       ^
error: fatalError
[12/15] Compiling QRCode QRCode+ASCII.swift
akaibukai commented 3 months ago

By googling a little bit I was able to get how to add a package in a swift package, so I did the following on on Pckage.swift

dependencies: [
        .package(
            name: "silica",
            url: "https://github.com/PureSwift/Silica",
            .upToNextMinor(from: "1.3.2")
        ),

Then I just replaced import CoreGrphicsby import Silica in the first module it was failing, i.e. QRCode+Builder.swift But I got an error about incompatible tools version..

error: Dependencies could not be resolved because 'silica' contains incompatible tools version (3.0.2) and root depends on 'silica' 1.3.2..<1.4.0.

I guess that this package is simply way too old...

It seems there is not really another drop-in replacement that will work on Linux.. Too bad :(

Edit: I found the following library: https://github.com/fwcd/swift-graphics

This one is not old, and they are referencing the Cairo library for Linux.. It seems this one was also used in the package above.

So it seems that indeed Cairo is NOT a drop-in replacement for CoreGraphics and that the library above (Silica) tried to be a wrapper..

Seems I'm stuck and won't be able to even test this library :(

Unless I try on a mac...

PS: will keep it open a little just to read for any comment juts in case... Then will close it.. Dragonf, you can also simply close it as well :)

dagronf commented 3 months ago

Hi @akaibukai, thanks for the lovely comments!

Unfortunately this library doesn't support non-Apple platforms. As you've found, CoreGraphics isn't available on Linux (or Windows) and it would be a BIG job to rewrite the 'arty' parts to support a different rendering library. I've thought a number of times about how I would go about making this truly cross-platform but I haven't had the impetus to move forward on it.

I've never heard of Silica - it looks very promising but I notice that it hasn't been updated since 2018 (and hasn't been updated to Swift 5 either). I'll have a wander through it.

Sorry to be the bearer of bad news.

akaibukai commented 3 months ago

@dagronf Hey :wave: Thank you very much for your reply! You don't need to be sorry :)

I'll ask a friend with a mac to build and give me the binary.. Hoping it'll be compatible to run on Linux once build on Mac...

akaibukai commented 3 months ago

Little update: I have a friend that was able to build the binary on his mac easily using swift build -c release --product qrcodegen and sent the binary to me.. As I was expecting I got the following error when I run it on my Linux:

exec: Failed to execute process: './qrcodegen' the file could not be run by the operating system.

I also tried running it within the Swift docker container and this time I'm getting the following error:

./qrcodegen: cannot execute binary file: Exec format error

I wonder if we need to set some dedicated flags for a cross platform build?

Edit: I wonder if it's not because of being built on ARM mac?

dagronf commented 3 months ago

@akaibukai you will not be able to run this on a non-apple platform, as CoreGraphics only exists for Apple. Compiling on macOS and copying to Linux will not work.

Cross-compiling will only work if we replace all the CoreGraphics code in this library with a cross-platform replacement, which is a huge challenge.