1414
1515# ##################### Generate LICENSES files ######################
1616
17+ # Create a go.work file so that go-licenses can discover the LICENSE file of the
18+ # other modules in the repo.
19+ #
20+ # Without this, go-licenses *guesses* the wrong LICENSE for local dependencies and
21+ # links to the wrong versions of LICENSES for transitive dependencies.
22+ licenses_go_work := $(bin_dir ) /scratch/LICENSES.go.work
23+ $(licenses_go_work ) : $(bin_dir ) /scratch
24+ GOWORK=$(abspath $@ ) \
25+ $(MAKE ) go-workspace
26+
1727# # Generate licenses for the golang dependencies
1828# # @category [shared] Generate/Verify
1929generate-go-licenses : #
2030shared_generate_targets += generate-go-licenses
2131
22- define license_generate
23- $1/LICENSES: $1/go.mod | $(NEEDS_GO-LICENSES )
24- cd $$(dir $$@ ) && GOOS=linux GOARCH=amd64 $(GO-LICENSES ) report --ignore "$$(license_ignore ) " ./... > LICENSES
32+ define licenses_target
33+ $1/LICENSES: $1/go.mod $(licenses_go_work ) | $(NEEDS_GO-LICENSES )
34+ cd $$(dir $$@ ) && \
35+ GOWORK=$(abspath $(licenses_go_work ) ) \
36+ GOOS=linux GOARCH=amd64 \
37+ $(GO-LICENSES ) report --ignore "$$(license_ignore ) " ./... > LICENSES
2538
2639generate-go-licenses: $1/LICENSES
40+ # The /LICENSE targets make sure these files exist.
41+ # Otherwise, make will error.
42+ generate-go-licenses: $1/LICENSE
2743endef
2844
2945# Calculate all the go.mod directories, build targets may share go.mod dirs so
3046# we use $(sort) to de-duplicate.
31- go_mod_dirs := $(sort $(foreach build_name,$(build_names ) ,$(go_$(build_name ) _mod_dir ) ) )
32- $(foreach go_mod_dir,$(go_mod_dirs),$(eval $(call license_generate,$(go_mod_dir))))
47+ go_mod_dirs := $(foreach build_name,$(build_names ) ,$(go_$(build_name ) _mod_dir ) )
48+ ifneq ("$(wildcard go.mod) ","")
49+ go_mod_dirs += .
50+ endif
51+ go_mod_dirs := $(sort $(go_mod_dirs ) )
52+ $(foreach go_mod_dir,$(go_mod_dirs),$(eval $(call licenses_target,$(go_mod_dir))))
3353
3454# ##################### Include LICENSES in OCI image ######################
3555
@@ -41,7 +61,7 @@ license_layer_path_$1 := $$(abspath $(bin_dir)/scratch/licenses-$1)
4161oci-license-layer-$1: | $(bin_dir ) /scratch $(NEEDS_GO-LICENSES )
4262 rm -rf $$(license_layer_path_$1 )
4363 mkdir -p $$(license_layer_path_$1 ) /licenses
44- cp LICENSE $$(license_layer_path_$1 ) /licenses/LICENSE
64+ cp $$( go_$1_mod_dir ) / LICENSE $$(license_layer_path_$1 ) /licenses/LICENSE
4565 cp $$(go_$1_mod_dir ) /LICENSES $$(license_layer_path_$1 ) /licenses/LICENSES
4666
4767oci-build-$1: oci-license-layer-$1
0 commit comments