Skip to content

Commit

Permalink
Add prepare-static-check target back
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Jan 8, 2024
1 parent f9d79a0 commit ff7243e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ comma := ,
check: FORCE static-check build/cover.html build-all
@printf "\e[1;32m>> All checks successful.\e[0m\n"

run-golangci-lint: FORCE
@printf "\e[1;36m>> golangci-lint\e[0m\n"
prepare-static-check: FORCE
@if ! hash golangci-lint 2>/dev/null; then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n"; go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi
@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi
@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi

run-golangci-lint: FORCE prepare-static-check
@printf "\e[1;36m>> golangci-lint\e[0m\n"
@golangci-lint run

build/cover.out: FORCE | build
Expand All @@ -62,7 +66,7 @@ build/cover.html: build/cover.out
@printf "\e[1;36m>> go tool cover > build/cover.html\e[0m\n"
@go tool cover -html $< -o $@

static-check: FORCE run-golangci-lint check-license-headers
static-check: FORCE run-golangci-lint check-dependency-licenses check-license-headers

build:
@mkdir $@
Expand All @@ -77,19 +81,15 @@ vendor-compat: FORCE
go mod vendor
go mod verify

prepare-addlicense: FORCE
@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi

license-headers: FORCE prepare-addlicense
license-headers: FORCE prepare-static-check
@printf "\e[1;36m>> addlicense\e[0m\n"
@addlicense -c "SAP SE" -ignore "vendor/**" -- **/*.go

check-license-headers: FORCE prepare-addlicense
check-license-headers: FORCE prepare-static-check
@printf "\e[1;36m>> addlicense\e[0m\n"
@bash -c 'shopt -s globstar; addlicense --check -ignore "vendor/**" -- **/*.go'

check-dependency-licenses: FORCE
@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi
check-dependency-licenses: FORCE prepare-static-check
@printf "\e[1;36m>> go-licence-detector\e[0m\n"
@go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules .license-scan-rules.json -overrides .license-scan-overrides.jsonl

Expand Down Expand Up @@ -120,6 +120,7 @@ help: FORCE
@printf "\n"
@printf "\e[1mTest\e[0m\n"
@printf " \e[36mcheck\e[0m Run the test suite (unit tests and golangci-lint).\n"
@printf " \e[36mprepare-static-check\e[0m Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager\n"
@printf " \e[36mrun-golangci-lint\e[0m Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.\n"
@printf " \e[36mbuild/cover.out\e[0m Run tests and generate coverage report.\n"
@printf " \e[36mbuild/cover.html\e[0m Generate an HTML file with source code annotations from the coverage report.\n"
Expand All @@ -128,7 +129,6 @@ help: FORCE
@printf "\e[1mDevelopment\e[0m\n"
@printf " \e[36mvendor\e[0m Run go mod tidy, go mod verify, and go mod vendor.\n"
@printf " \e[36mvendor-compat\e[0m Same as 'make vendor' but go mod tidy will use '-compat' flag with the Go version from go.mod file as value.\n"
@printf " \e[36mprepare-addlicense\e[0m Install addlicense\n"
@printf " \e[36mlicense-headers\e[0m Add license headers to all .go files excluding the vendor directory.\n"
@printf " \e[36mcheck-license-headers\e[0m Check license headers in all .go files excluding the vendor directory.\n"
@printf " \e[36mcheck-dependency-licenses\e[0m Check all dependency licenses using go-licence-detector.\n"
Expand Down
62 changes: 37 additions & 25 deletions internal/makefile/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ endif
test.addDefinition(`space := $(null) $(null)`)
test.addDefinition(`comma := ,`)

isSAPCC := strings.HasPrefix(sr.ModulePath, "github.com/sapcc") || strings.HasPrefix(sr.ModulePath, "github.wdf.sap.corp") || strings.HasPrefix(sr.ModulePath, "github.tools.sap")

//add main testing target
checkPrerequisites := []string{"static-check", "build/cover.html"}
if hasBinaries {
Expand All @@ -139,15 +141,36 @@ endif
})

//add target for installing dependencies for `make check`
prepareStaticRecipe := []string{
`@if ! hash golangci-lint 2>/dev/null; then` +
` printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n";` +
` go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi`,
}
if isSAPCC {
prepareStaticRecipe = append(prepareStaticRecipe, []string{
`@if ! hash go-licence-detector 2>/dev/null; then` +
` printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n";` +
` go install go.elastic.co/go-licence-detector@latest; fi`,
`@if ! hash addlicense 2>/dev/null; then ` +
` printf "\e[1;36m>> Installing addlicense...\e[0m\n"; ` +
` go install github.com/google/addlicense@latest; fi`,
}...)
}
test.addRule(rule{
description: "Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.",
description: "Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager",
phony: true,
target: "run-golangci-lint",
target: "prepare-static-check",
recipe: prepareStaticRecipe,
})

// add target to run golangci-lint
test.addRule(rule{
description: "Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.",
phony: true,
target: "run-golangci-lint",
prerequisites: []string{"prepare-static-check"},
recipe: []string{
`@printf "\e[1;36m>> golangci-lint\e[0m\n"`,
`@if ! hash golangci-lint 2>/dev/null;` +
` then printf "\e[1;36m>> Installing golangci-lint (this may take a while)...\e[0m\n";` +
` go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest; fi`,
`@golangci-lint run`,
},
})
Expand Down Expand Up @@ -222,8 +245,6 @@ endif
})
}

isSAPCC := strings.HasPrefix(sr.ModulePath, "github.com/sapcc") || strings.HasPrefix(sr.ModulePath, "github.wdf.sap.corp") || strings.HasPrefix(sr.ModulePath, "github.tools.sap")

if isSAPCC {
// Default behavior is to check all Go files excluding the vendor directory.
patterns := []string{"**/*.go"}
Expand All @@ -240,20 +261,11 @@ endif
ignorePatterns[i] = fmt.Sprintf("-ignore %q", v)
}

dev.addRule(rule{
description: "Install addlicense",
target: "prepare-addlicense",
phony: true,
recipe: []string{
`@if ! hash addlicense 2>/dev/null; then printf "\e[1;36m>> Installing addlicense...\e[0m\n"; go install github.com/google/addlicense@latest; fi`,
},
})

dev.addRule(rule{
description: "Add license headers to all .go files excluding the vendor directory.",
target: "license-headers",
phony: true,
prerequisites: []string{"prepare-addlicense"},
prerequisites: []string{"prepare-static-check"},
recipe: []string{
`@printf "\e[1;36m>> addlicense\e[0m\n"`,
fmt.Sprintf(`@addlicense -c "SAP SE" %s -- %s`,
Expand All @@ -266,7 +278,7 @@ endif
description: "Check license headers in all .go files excluding the vendor directory.",
target: "check-license-headers",
phony: true,
prerequisites: []string{"prepare-addlicense"},
prerequisites: []string{"prepare-static-check"},
recipe: []string{
`@printf "\e[1;36m>> addlicense\e[0m\n"`,
fmt.Sprintf(`@bash -c 'shopt -s globstar; addlicense --check %s -- %s'`,
Expand All @@ -282,11 +294,12 @@ endif
must.Succeed(os.WriteFile(scanOverridesFile, scanOverrides, 0666))

dev.addRule(rule{
description: "Check all dependency licenses using go-licence-detector.",
target: "check-dependency-licenses",
phony: true,
description: "Check all dependency licenses using go-licence-detector.",
target: "check-dependency-licenses",
phony: true,
prerequisites: []string{"prepare-static-check"},
recipe: []string{
`@if ! hash go-licence-detector 2>/dev/null; then printf "\e[1;36m>> Installing go-licence-detector...\e[0m\n"; go install go.elastic.co/go-licence-detector@latest; fi`,

`@printf "\e[1;36m>> go-licence-detector\e[0m\n"`,
fmt.Sprintf(`@go list -m -mod=readonly -json all | go-licence-detector -includeIndirect -rules %s -overrides %s`,
licenseRulesFile, scanOverridesFile),
Expand All @@ -297,7 +310,7 @@ endif
//add target for static code checks
staticCheckPrerequisites := []string{"run-golangci-lint"}
if isSAPCC {
staticCheckPrerequisites = append(staticCheckPrerequisites, "check-license-headers")
staticCheckPrerequisites = append(staticCheckPrerequisites, "check-dependency-licenses", "check-license-headers")
}
test.addRule(rule{
description: "Run static code checks",
Expand Down Expand Up @@ -366,8 +379,7 @@ func makeDefaultLinkerFlags(binaryName string, sr core.ScanResult) string {
return flags
}

// installTarget also returns a bool that tells whether the install target was requested
// in the config.
// installTarget also returns a bool that tells whether the install target was requested in the config.
func installTarget(binaries []core.BinaryConfiguration) (rule, bool) {
r := rule{
description: "Install all binaries. " +
Expand Down

0 comments on commit ff7243e

Please sign in to comment.