Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forward declarations to cgo exported funcs #5

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

iamcalledrob
Copy link
Contributor

Parallel PR to crawshaw/sqlite#146 -- I feel like this one has a better chance of being merged than the crawshaw repo :)

Full details of the change in PR linked above.

tl;dr, fixes building on windows with clang, as well as a typo in readme.md

@iamcalledrob
Copy link
Contributor Author

To hopefully save someone else some time

My personal use-case here is to easily cross-compile a project that uses sqlite.

https://github.com/zombiezen/go-sqlite would have been my first choice of sqlite driver, but the modernc/sqlite foundation it's built on doesn't support many GOOS/GOARCH combos. For example, Android (builds but makes prohibited syscalls), iOS, various BSDs.

Zig is great, and makes cross compilation a dream, e.g.
GOARGS GOOS=linux GOARCH=arm64 "CC=zig cc -target aarch64-linux-gnu" go build ... to build for arm64 linux.

Unfortunately, Zig uses clang internally, leading to the build errors on windows detailed in the main PR. This PR makes cross-compiling as "easy" as:
GOARGS GOOS=windows GOARCH=amd64 "CC=zig cc -target x86_64-windows-gnu" go build ...

Caveat: The zig toolchain is currently missing winpthreads for windows, so you'll need to build and link against winpthreads manually, e.g. by cross-compiling using https://github.com/kassane/winpthreads-zigbuild and passing zig-out/include and zig-out/winpthreads.lib into CGO_CFLAGS / CGO_LDFLAGS as appropriate/

@anacrolix
Copy link
Contributor

@theraphim do you have any thoughts?

What version of Zig are you cross compiling with?

I have a project that I cross compile to Windows and I don't recall any errors but it's been a while. I'll try it with this PR.

Thanks for the PR!

@anacrolix
Copy link
Contributor

I should mention that I've used zombiezen, and Zig for cross compilation too, so I understand the difficulty.

In my case I had to stop using zombiezen because I needed to use extensions, and I couldn't figure them out in Go with zombiezen, and couldn't call through to C extensions from zombiezen, and I started writing my extensions in Rust which was all too much to deal with.

@iamcalledrob
Copy link
Contributor Author

@anacrolix Cross compiling with both Zig stable and nightly -- behaviour is the same!

I believe cross-compiling with a gcc based toolchain does work, but not with a clang-based one (i.e. zig, but also... clang).

I'm recklessly assuming you're not a Windows user, but you can verify the problem and fix on linux/macos because zig bundles a mingw64+clang cross compilation toolchain:

  1. Install zig, e.g. via brew on macos brew install zig
  2. Download then cross-compile winpthreads: cd /path/to/winpthreads; zig build -Dtarget=x86_64-windows-gnu -Dcpu=baseline. That will build in source into a "zig-out" dir.
  3. Cross-compile a go project that depends on the sqlite driver, e.g.: GOARGS GOOS=windows GOARCH=amd64 CGO_CFLAGS="-I/path/to/winpthreads/zig-out/include" CGO_LDFLAGS="-L/path/to/winpthreads/zig-out/lib -lwinpthreads" CC=zig cc -target x86_64-windows-gnu" go build someproject
  4. Witness the errors and warnings listed in Remove forward declarations to cgo exported funcs crawshaw/sqlite#146

@anacrolix
Copy link
Contributor

Fantastic. Thanks for the thorough reproduction steps.

@anacrolix anacrolix merged commit f30eb77 into go-llsqlite:master Apr 25, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants