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

go install std doesn't install runtime/cgo.a when cross compiling #43700

Closed
Tatskaari opened this issue Jan 14, 2021 · 2 comments
Closed

go install std doesn't install runtime/cgo.a when cross compiling #43700

Tatskaari opened this issue Jan 14, 2021 · 2 comments

Comments

@Tatskaari
Copy link

Tatskaari commented Jan 14, 2021

For context, I'm working on a build system and I'm trying to cross compile go modules without leaning on go build/install. The generated cgo files imports runtime/go so my tool expects pkg/"$TARGET_GOOS"_"$TARGET_ARCH"/runtime/cgo.a to exist. I guess this assumption is wrong so I was wondering how I'm meant to compile this target? How do I satisfy the runtime/cgo import for the target arch?

What version of Go are you using (go version)?

$ go version
go version go1.15.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
OS=darwin
TEST_ARGS=
XOS=osx
PATH=/Users/work/.please:/usr/local/bin:/usr/bin:/bin
XARCH=x86_64
GOARCH=amd64
LANG=en_GB.UTF-8
SHLVL=1
GOOS=darwin
ARCH=amd64

What did you do?

export GOOS=linux && export GOARCH=arm64 && go install std

What did you expect to see?

The file $GOROOT/pkg/linux_arm64/runtime/cgo.a to exist

What did you see instead?

The file doesn't exist and then my tool breaks down the line when executing:

#!/bin/sh
set -e
mkdir -p pkg/linux_arm64/github.com/mattn
cd third_party/go/src/github.com/mattn/go-sqlite3
/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/bin/go tool cgo sqlite3_other.go error.go backup.go sqlite3_type.go callback.go sqlite3_load_extension.go sqlite3_context.go sqlite3_opt_userauth_omit.go
ln static_mock.go _obj/
ln doc.go _obj/
ln sqlite3_func_crypt.go _obj/
/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/bin/go tool compile -pack -importcfg /importconfig -o out.a _obj/*.go
/usr/bin/gcc -Wno-error -ldl -Wno-unused-parameter -c -I _obj -I . *.c
/usr/bin/gcc -Wno-error -ldl -Wno-unused-parameter -c -I _obj -I . _obj/_cgo_export.c _obj/*.cgo2.c
/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/bin/go tool pack r out.a *.o 
cd  && mv third_party/go/src/github.com/mattn/go-sqlite3/out.a pkg/linux_arm64/github.com/mattn/go-sqlite3.a
echo "packagefile github.com/mattn/go-sqlite3=pkg/linux_arm64/github.com/mattn/go-sqlite3.a" >> importconfig

Which fails with:

_obj/_cgo_gotypes.go, line 7, column 8: can't find import: "runtime/cgo"

import config is a bit hefty but here's a snippet:

$ cat importconfig | grep runtime
packagefile runtime/debug=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/debug.a
packagefile runtime/internal/atomic=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/internal/atomic.a
packagefile runtime/internal/math=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/internal/math.a
packagefile runtime/internal/sys=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/internal/sys.a
packagefile runtime/pprof=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/pprof.a
packagefile runtime/race=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/race.a
packagefile runtime/trace=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime/trace.a
packagefile runtime=/Users/work/please/plz-out/tmp/test/go_modules/please_go_install_cross_compile_test._test/run_1/test/go_modules/test_repo/plz-out/bin/tools/go/toolchain/pkg/linux_arm64/runtime.a

PS: It would be fantastic if there was a go tool install -importcfg ... or at least a go list -importcfg so that we can introspect a module without having to bend to go builds idea of what the file structure should be

@Tatskaari Tatskaari changed the title go install std doesn't install runtime/cgo.a go install std doesn't install runtime/cgo.a when cross compiling Jan 14, 2021
@ianlancetaylor
Copy link
Contributor

When cross-compiling, cgo is disabled by default. To enable it, and to get runtime/cgo.a installed, set CGO_ENABLED=1 in the environment. You'll also have to set CC to a working cross-compiler for your target.

@Tatskaari
Copy link
Author

Tatskaari commented Jan 14, 2021

Ah yes of course. Thanks so much!

Managed to get that working here: thought-machine/please#1482

Don't suppose you have any thoughts on making it a bit easier for build systems that aren't go build to build modules? go list does a good job of providing all the information I need but makes lots of assumptions about where things should be which I can't guarantee. It'd be great if it could take an import config.

@golang golang locked and limited conversation to collaborators Jan 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants