diff --git a/.gitignore b/.gitignore index a2fc00ad08b6..75d65be26efc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.so *.dylib /trivy +/trivy-debug ## chart release .cr-release-packages diff --git a/docs/community/contribute/pr.md b/docs/community/contribute/pr.md index 97e64a2dbf4f..c359524b04e2 100644 --- a/docs/community/contribute/pr.md +++ b/docs/community/contribute/pr.md @@ -18,10 +18,17 @@ Install the necessary tools for development by following their respective instal After making changes to the Go source code, build the project with the following command: ```shell -$ mage build +$ mage build:prod $ ./trivy -h ``` +Or if you want to use [delve](https://github.com/go-delve/delve) for debugging, build with the following command: + +```shell +$ mage build:dev +$ dlv exec ./trivy-debug -h +``` + ### Lint You must pass the linter checks: diff --git a/docs/community/contribute/vulnerability-database/add-vulnerability-source.md b/docs/community/contribute/vulnerability-database/add-vulnerability-source.md index c560ed3fc543..200233447017 100644 --- a/docs/community/contribute/vulnerability-database/add-vulnerability-source.md +++ b/docs/community/contribute/vulnerability-database/add-vulnerability-source.md @@ -123,7 +123,7 @@ This allows you to verify that your vulnerabilities are correctly stored in the ```bash # Build Trivy with your changes -mage build +mage build:prod # Use your local database ./trivy image --skip-db-update --cache-dir /path/to/cache your-test-image diff --git a/magefiles/magefile.go b/magefiles/magefile.go index 703e1f8189f9..1f45be2e532c 100644 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -47,12 +47,20 @@ func version() (string, error) { return strings.TrimPrefix(ver, "v"), nil } -func buildLdflags() (string, error) { +func buildLdflags(debug bool) (string, error) { ver, err := version() if err != nil { return "", err } - return fmt.Sprintf("-s -w -X=github.com/aquasecurity/trivy/pkg/version/app.ver=%s", ver), nil + var build string + + if debug { + build = "-X=github.com/aquasecurity/trivy/pkg/version/app.ver=%s" + } else { + build = "-s -w -X=github.com/aquasecurity/trivy/pkg/version/app.ver=%s" + } + + return fmt.Sprintf(build, ver), nil } type Tool mg.Namespace @@ -374,15 +382,35 @@ func Tidy() error { return sh.RunV("go", "mod", "tidy") } -// Build builds Trivy -func Build() error { +type Build mg.Namespace + +// Prod build Trivy without debug symbols +func (b Build) Prod() error { + fmt.Println("Building Trivy production binary") + return build(false) +} + +// Dev build Trivy with debug symbols +func (b Build) Dev() error { + fmt.Println("Building Trivy debug binary") + return build(true) +} + +func build(debug bool) error { if updated, err := target.Dir("trivy", "pkg", "cmd"); err != nil { return err } else if !updated { return nil } - ldflags, err := buildLdflags() + execName := "trivy" + gcflags := "" + if debug { + execName = "trivy-debug" + gcflags = "-gcflags=all=-N -l" + } + + ldflags, err := buildLdflags(debug) if err != nil { return err } @@ -390,12 +418,13 @@ func Build() error { if err != nil { return err } - return sh.RunWith(ENV, "go", "build", "-ldflags", ldflags, filepath.Join(wd, "cmd", "trivy")) + return sh.RunWith(ENV, "go", "build", "-o", execName, "-ldflags", ldflags, + gcflags, filepath.Join(wd, "cmd", "trivy")) } // Install installs Trivy func Install() error { - ldflags, err := buildLdflags() + ldflags, err := buildLdflags(false) if err != nil { return err }