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

Construct package dependencies using Go #3239

Merged
merged 2 commits into from
Jan 7, 2025
Merged

Conversation

skitt
Copy link
Member

@skitt skitt commented Dec 4, 2024

For best results, Make should be told about all the files a given target depends on. For Go programs, this is the program's main Go file, and all non-test Go files in directories corresponding to packages the main Go file depends upon (including transitive dependencies).

This adds a Make macro using go list to retrieve a package's dependencies, and constructs the corresponding list of files to use as target prerequisites.

@submariner-bot
Copy link
Contributor

🤖 Created branch: z_pr3239/skitt/pkgdeps
🚀 Full E2E won't run until the "ready-to-test" label is applied. I will add it automatically once the PR has 2 approvals, or you can add it manually.

Makefile Show resolved Hide resolved
Copy link
Member

@dfarrell07 dfarrell07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Minor question/docs feedback inline.

@submariner-bot submariner-bot added the ready-to-test When a PR is ready for full E2E testing label Dec 11, 2024
pkgdeps = $(shell find $$(go list -json $(1) | jq -r '.Imports[] | select(startswith("$(basepkg)")) | sub("$(basepkg)"; ".")') -name '*.go' -not -name '*_test.go')

# natdiscovery.pb.go must be listed explicitly because it might not exist when Make evaluates pkgdeps
bin/%/submariner-gateway: main.go $(call pkgdeps,.) pkg/natdiscovery/proto/natdiscovery.pb.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be?

Suggested change
bin/%/submariner-gateway: main.go $(call pkgdeps,.) pkg/natdiscovery/proto/natdiscovery.pb.go
bin/%/submariner-gateway: main.go $(call pkgdeps,./pkg/gateway) pkg/natdiscovery/proto/natdiscovery.pb.go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root of the dependency tree is the package containing the main file, so . in this case. However while investigating this comment I realised that .Imports[] only contains first-level imports, and the pkgdeps macro really needs to look at .Deps[] instead…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note too how the argument to pkgdeps matches the argument to compile.sh 😉

For best results, Make should be told about all the files a given
target depends on. For Go programs, this is the program's main Go
file, and all non-test Go files in directories corresponding to
packages the main Go file depends upon (including transitive
dependencies).

This adds a Make macro using go list to retrieve a package's
dependencies, and constructs the corresponding list of files to use as
target prerequisites.

Signed-off-by: Stephen Kitt <[email protected]>
@tpantelis tpantelis enabled auto-merge (rebase) January 7, 2025 13:23
@tpantelis tpantelis merged commit 0dc3a34 into submariner-io:devel Jan 7, 2025
39 checks passed
@submariner-bot
Copy link
Contributor

🤖 Closed branches: [z_pr3239/skitt/pkgdeps]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-test When a PR is ready for full E2E testing
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

5 participants