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

How to add go build -ldflags to bazel compilation #3731

Open
ly0511 opened this issue Oct 18, 2023 · 3 comments
Open

How to add go build -ldflags to bazel compilation #3731

ly0511 opened this issue Oct 18, 2023 · 3 comments

Comments

@ly0511
Copy link

ly0511 commented Oct 18, 2023

What version of rules_go are you using?

v0.41.0

What version of gazelle are you using?

v0.33.0

What version of Bazel are you using?

bazel 6.3.2

Does this issue reproduce with the latest releases of all the above?

What operating system and processor architecture are you using?

Any other potentially useful information about your toolchain?

What did you do?

Migrate my golang project to compile using bazel
My golang compilation script is as follows

COMMIT_HASH=$(shell git rev-parse --verify HEAD | cut -c 1-8)
BUILD_DATE=$(shell date +%Y-%m-%d_%H:%M:%S%z)
GIT_TAG=$(shell git describe --tags)
GIT_AUTHOR=$(shell git show -s --format=%an)
SHELL:=/bin/bash
BIN_NAME="test_server"

build: mod
go build -ldflags "-X main.GitTag=$(GIT_TAG) -X main.BuildTime=$(BUILD_DATE) -X main.GitCommit=$(COMMIT_HASH) -X main.GitAuthor=$(GIT_AUTHOR)" -o ${BIN_NAME} ./cmd/main.go

My bazel compilation command is as follows:
go mod tidy
bazel run //:gazelle-update-repos
bazel run //:gazelle
bazel build ...

How to add ldflags information to bazel compilation?

What did you expect to see?

run ./test_server version,You can see the tag, author and commit information, etc.

What did you see instead?

@fmeum
Copy link
Collaborator

fmeum commented Oct 18, 2023

You can use the x_defs attribute on go_binary and go_library.

@ly0511
Copy link
Author

ly0511 commented Oct 19, 2023

@fmeum x_defs can indeed be implemented, but I use bazel_gazelle to generate BUILD.bazel. I don’t want to maintain BUILD.bazel separately. Is there any way for gazelle to solve this kind of custom parameter setting?

@DolceTriade
Copy link
Contributor

Gazelle should maintain your local edits

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

No branches or pull requests

3 participants