Add build tags to support both x86 and ARM compilation on macOS#1069
Add build tags to support both x86 and ARM compilation on macOS#1069rittneje merged 5 commits intomattn:masterfrom
Conversation
README.md
Outdated
| To compile for macOS X on x86: | ||
|
|
||
| ```bash | ||
| go build --tags "darwin,amd64" |
There was a problem hiding this comment.
These instructions were actually totally wrong. It should be using GOOS and GOARCH for this. The same applies for Windows and Linux. (Also the flag is -tags not --tags.)
There was a problem hiding this comment.
Sorry, I'm not following where's my fault here. I've added architecture flags exactly in a way it was for OS selection: by explicitly define target OS/arch. I'm a roocky in cgo and not sure how to re-write this to make it refer to GOOS and GOARCH env vars: #cgo flag does not have if-then-else notion.
There was a problem hiding this comment.
The original instructions (on master) were incorrect.
If you are doing a native compile, it is just go build.
If you are doing a cross compile, it is GOOS=<target-os> GOARCH=<target-arch> CGO_ENABLED=1 CC=<c-cross-compiler> go build
For example: GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 CC=o64-clang go build
Separately from that, if you are trying to include optional features via build tags, in modern versions of Go you provide a comma-separated list via -tags.
-tags tag,list
a comma-separated list of build tags to consider satisfied during the
build. For more information about build tags, see the description of
build constraints in the documentation for the go/build package.
(Earlier versions of Go used a space-separated list, and that form
is deprecated but still recognized.)
For example: go build -tags sqlite_stat4,secure_delete
Note that all of this is separate from the #cgo directive changes you made. (Those changes are correct.)
There was a problem hiding this comment.
Thanks for an explanation, now it makes a perfect sense. Looks like the problem of both native and cross-compilcation can be resolved only by external tool. For example, shell script which will set CGO_CFLAGS and CGO_LDFLAGS based on target GOOS and GOARCH. But this is beyond the scope of my PR.
|
@mattn Do you know if GitHub has any M1/M2 build agents we could add to the workflow? https://github.com/mattn/go-sqlite3/blob/master/.github/workflows/go.yaml |
|
No I don't. |
| #cgo darwin,amd64 CFLAGS: -I/usr/local/opt/icu4c/include | ||
| #cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/icu4c/lib | ||
| #cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/icu4c/include | ||
| #cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/icu4c/lib |
There was a problem hiding this comment.
I'm wondering why this is different for them.
-I/usr/local/opt/icu4c/include -I/opt/homebrew/opt/icu4c/include
-L/usr/local/opt/icu4c/lib -L/opt/homebrew/opt/icu4c/lib
This is not good?
There was a problem hiding this comment.
@mattn Not sure I understand your question, but Homebrew installs to a different location on arm64 (i.e, M1/M2) than on amd64 (i.e., Intel), as mentioned in the docs. https://docs.brew.sh/Installation
We could also choose to include both directories for macOS regardless of GOARCH, and I suppose that would also work. (Although I think we should put /opt/homebrew first in that case.)
There was a problem hiding this comment.
I just hit this issue on a M1 Mac and these locations are correct. This PR along with the build tags go build -tags "libsqlite3 darwin arm64" allowed me to build against the sqlite version installed via homebrew.
|
Hey folks! I'm wondering if any progress there? |
|
I've resolved the conflict in |
|
Just checking in. It looks like this PR was approved, but never merged. Is there anything blocking the merge at this time? |
…n#1069) * Add build tags to support both x86 and ARM compilation on macOS * Documentation fix: command line for build under macOS * Global replace 'macOS X' -> 'macOS'. Fix typo in -tags cmd line param * `README.md`: fix all `--tags` -> `-tags` --------- Co-authored-by: Denis Dmitriev <dmitriev@itspartner.net>
Fix for #1068
Changes
arm64andamd64build tags