Open testinfected opened 8 months ago
Should I understand nobody is using Buck2 to build go?
We are using Buck2 internally to build Go. But we don't have any open source samples. That's kind of the weird middle ground - you can probably get it working, but it will require a bit of work. I'll see if anyone internally can be persuaded to shed some light (I know the build system, but not much about how Go works with it).
Hey @testinfected,
I can share some context on buck2&Go integration, we are using it internally for limited purposes.
This is in active development at the moment. We will make some breaking changes time to time to make it better. So, at this moment we can't give any promise of API compatibility.
However I can answer some of your questions.
compiler_flags_shared, compiler_flags_static, linker_flags_shared, linker_flags_static
Just leave them empty for the beginning.
don't know what to provide for prebuilt_stdlib
You need to pre-build it manually using following command
export GODEBUG="installgoroot=all"
shared_flags="-gcflags -shared -asmflags -shared"
go install -pkgdir "$out_dir/prebuilt_std/static" std
go install -pkgdir "$out_dir/prebuilt_std/shared" $shared_flags std
Then wrap into into filegroups
filegroup(
name = "stdlib_static",
srcs = native.glob(["prebuilt_std/static/**/*"]),
)
filegroup(
name = "stdlib_shared",
srcs = native.glob(["prebuilt_std/shared/**/*"]),
)
And then supply it into GoToolChainInfo
. I suppose you are using go_toolchain for it 😀
That’s close to what we do in Bazel’s rules_go https://github.com/bazelbuild/rules_go/blob/2f29bd3956a37156f0dee39b761109a490f0a47b/go/tools/builders/stdlib.go#L115
Note that would would probably want to filter out go install
generated buildid
as it could make your build not reproducible.
Thanks, will try that. I'm very new to Buck, so fingers crossed
@podtserkovskiy hey what should be the param for gen_stdlib_importcfg should be. I am trying to make https://github.com/sluongng/buck2-go work with the current version of buck2. and currently getting an error like this
https://github.com/dvtkrlbs/buck2-prelude here is my fork of the prelude
nvm i figured it out. apparently some of the tools like go_wrapper and gen_stdlib_import_cfg were not included in the attrs declaration. defining them made the issue go away. Will open a PR about this. another question I have is if I open a PR with remote go toolchain downloading implemented would this be approved if not I will just contribute the simple fixes I did
I'm sorry for the late reply. Thanks for dong this work.
I removed need to pre-compile stdlibs, so now it should be easier.
I've just fixed some inconsistencies we had in GoToolchainInfo
attributes and now #577 is ready for merge and will be merged shortly.
if I open a PR with remote go toolchain downloading implemented would this be approved
Let's double check with @ndmitchell and @JakobDegen, but personally I'm happy to have it supported in buck
I don't think we'd just approve it, I honestly think that more generally useful toolchain rules across languages is one of the highest value contributions buck2 could see right now. So yes, please do!
BTW, I'm working on making system_go_toolchain
work with go binary from PATH.
Also, I'm adding some examples which will be compiled on CI and prevent breaking the system toolchain in the future.
I've just noticed we have some examples of downloading Go toolchain in no_prelude
directory. You can use it together with system_go_toolchain
for the inspiration 😀
@dvtkrlbs @testinfected I've merged #588. So, system_go_toolchain
works on Linux and MacOS, has an example of usage in examples/with_prelude/go/hello
and those examples being validated on CI.
Great news @podtserkovskiy ! Kudos
This issue is a bit old and the system_go_toolchain
was published, but I'm curious how this can be used to do cross compilation of the Go binary.
I was trying to create toolchain similar to system_go_toolchain
where I hardcoded env_go_os
and env_go_arch
, but I still need to provide stdlib which is compiled for that platform (it's using the default one which appears to be compiled always for the current platform, even though I thought it would pick it from toolchain definition) and I'm not sure how.
This issue is a bit old and the
system_go_toolchain
was published, but I'm curious how this can be used to do cross compilation of the Go binary.I was trying to create toolchain similar to
system_go_toolchain
where I hardcodedenv_go_os
andenv_go_arch
, but I still need to provide stdlib which is compiled for that platform (it's using the default one which appears to be compiled always for the current platform, even though I thought it would pick it from toolchain definition) and I'm not sure how.
@dankox setting env_go_os and env_go_arch should be enough for cross-compilation, there's no need to supply stdlib anymore, we build it on-demand. If you still see it in GoToolchainInfo please upgrade buck2/buck2-prelude.
If something doesn't work I'm here and happy to help 😀
@dankox setting env_go_os and env_go_arch should be enough for cross-compilation, there's no need to supply stdlib anymore, we build it on-demand. If you still see it in GoToolchainInfo please upgrade buck2/buck2-prelude.
If something doesn't work I'm here and happy to help 😀
@podtserkovskiy Hey, thanks for coming back to me for this issue:) I'm still having a problem and I have the latest prelude. I could configure the env_go_os
and env_go_arch
but the stdlib is causing troubles anyway.
I have pushed my setup here (it's really simple one file project where I was trying it first): https://github.com/dankox/colorx/tree/buck2-test
I'm not sure if I'm using it correctly, but it seems that the build of the main.go
works fine, until it tries to import fmt
package which was build for another platform. I thought it might be caused by me building first the binary for my platform and later trying to cross-compile, but even if I remove everything and go for other platform, I will get this error:
Connected to new buck2 daemon.
Action failed: root//:colorx_linux (go_compile main)
Local command returned non-zero exit code 1
Reproduce locally: `env -- 'BUCK_SCRATCH_PATH=buck-out/v2/tmp/root/ca7581441d28fa24/go_compile/main' 'GOARCH=amd64' 'GOE ...<omitted>... g -o buck-out/v2/gen/root/3a2dd161323a3ea9/__colorx_linux__/__action__4__/go_compile_out.a ./main.go (run `buck2 log what-failed` to get the full command)`
stdout:
./main.go:4:2: could not import fmt (object is [go object darwin amd64 go1.22.2 GOAMD64=v1 X:regabiwrappers,regabiargs,allocheaders,exectracer2
] expected [go object linux amd64 go1.22.2 GOAMD64=v1 X:regabiwrappers,regabiargs,allocheaders,exectracer2
])
stderr:
Build ID: 899d8947-949a-4091-a477-bb48bb370f2f
Jobs completed: 75. Time elapsed: 20.8s.
Cache hits: 0%. Commands: 6 (cached: 0, remote: 0, local: 6)
BUILD FAILED
Hi, I'm trying to use the example go toolchain, but it seems out of date.
Apparently it does not include some require values for
GoToolChainInfo
, namelycompiler_flags_shared
,compiler_flags_static
,linker_flags_shared
,linker_flags_static
as well asprebuilt_stdlib
andprebuilt_stdlib_shared
.I tried filling out those values, but don't know what to provide for
prebuilt_stdlib
. With an empty map I get the following error:Error running analysis for
root//... (prelude//platforms:default#524f8da68ea2a374)`Caused by: Traceback (most recent call last): File, in
int
, actualprovider_callable
--> prelude/go/packages.bzl:56:19`
Is there an example of a working Go Toolchain somewhere?
Any help would be appreciated.
Thanks