|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +# force the usage of /bin/bash instead of /bin/sh |
| 16 | +SHELL := /bin/bash |
| 17 | + |
15 | 18 | .PHONY: build-% build container-% container push-% push clean test |
16 | 19 |
|
17 | 20 | # A space-separated list of all commands in the repository, must be |
@@ -63,26 +66,35 @@ endif |
63 | 66 | # Specific packages can be excluded from each of the tests below by setting the *_FILTER_CMD variables |
64 | 67 | # to something like "| grep -v 'github.com/kubernetes-csi/project/pkg/foobar'". See usage below. |
65 | 68 |
|
66 | | -# BUILD_PLATFORMS contains a set of <os> <arch> <suffix> triplets, |
| 69 | +# BUILD_PLATFORMS contains a set of tuples [os arch suffix base_image addon_image] |
67 | 70 | # separated by semicolon. An empty variable or empty entry (= just a |
68 | 71 | # semicolon) builds for the default platform of the current Go |
69 | 72 | # toolchain. |
70 | 73 | BUILD_PLATFORMS = |
71 | 74 |
|
72 | 75 | # Add go ldflags using LDFLAGS at the time of compilation. |
73 | | -IMPORTPATH_LDFLAGS = -X main.version=$(REV) |
| 76 | +IMPORTPATH_LDFLAGS = -X main.version=$(REV) |
74 | 77 | EXT_LDFLAGS = -extldflags "-static" |
75 | | -LDFLAGS = |
| 78 | +LDFLAGS = |
76 | 79 | FULL_LDFLAGS = $(LDFLAGS) $(IMPORTPATH_LDFLAGS) $(EXT_LDFLAGS) |
77 | 80 | # This builds each command (= the sub-directories of ./cmd) for the target platform(s) |
78 | 81 | # defined by BUILD_PLATFORMS. |
79 | 82 | $(CMDS:%=build-%): build-%: check-go-version-go |
80 | 83 | mkdir -p bin |
81 | | - echo '$(BUILD_PLATFORMS)' | tr ';' '\n' | while read -r os arch suffix; do \ |
| 84 | + # os_arch_seen captures all of the $$os-$$arch seen for the current binary |
| 85 | + # that we want to build, if we've seen an $$os-$$arch before it means that |
| 86 | + # we don't need to build it again, this is done to avoid building |
| 87 | + # the windows binary multiple times (see the default value of $$BUILD_PLATFORMS) |
| 88 | + export os_arch_seen="" && echo '$(BUILD_PLATFORMS)' | tr ';' '\n' | while read -r os arch suffix base_image addon_image; do \ |
| 89 | + os_arch_seen_pre=$${os_arch_seen%%$$os-$$arch*}; \ |
| 90 | + if ! [ $${#os_arch_seen_pre} = $${#os_arch_seen} ]; then \ |
| 91 | + continue; \ |
| 92 | + fi; \ |
82 | 93 | if ! (set -x; CGO_ENABLED=0 GOOS="$$os" GOARCH="$$arch" go build $(GOFLAGS_VENDOR) -a -ldflags '$(FULL_LDFLAGS)' -o "./bin/$*$$suffix" ./cmd/$*); then \ |
83 | 94 | echo "Building $* for GOOS=$$os GOARCH=$$arch failed, see error(s) above."; \ |
84 | 95 | exit 1; \ |
85 | 96 | fi; \ |
| 97 | + os_arch_seen+=";$$os-$$arch"; \ |
86 | 98 | done |
87 | 99 |
|
88 | 100 | $(CMDS:%=container-%): container-%: build-% |
@@ -131,30 +143,46 @@ DOCKER_BUILDX_CREATE_ARGS ?= |
131 | 143 | # the tag for the resulting multiarch image. |
132 | 144 | $(CMDS:%=push-multiarch-%): push-multiarch-%: check-pull-base-ref build-% |
133 | 145 | set -ex; \ |
134 | | - DOCKER_CLI_EXPERIMENTAL=enabled; \ |
135 | | - export DOCKER_CLI_EXPERIMENTAL; \ |
| 146 | + export DOCKER_CLI_EXPERIMENTAL=enabled; \ |
136 | 147 | docker buildx create $(DOCKER_BUILDX_CREATE_ARGS) --use --name multiarchimage-buildertest; \ |
137 | 148 | trap "docker buildx rm multiarchimage-buildertest" EXIT; \ |
138 | 149 | dockerfile_linux=$$(if [ -e ./cmd/$*/Dockerfile ]; then echo ./cmd/$*/Dockerfile; else echo Dockerfile; fi); \ |
139 | 150 | dockerfile_windows=$$(if [ -e ./cmd/$*/Dockerfile.Windows ]; then echo ./cmd/$*/Dockerfile.Windows; else echo Dockerfile.Windows; fi); \ |
140 | 151 | if [ '$(BUILD_PLATFORMS)' ]; then build_platforms='$(BUILD_PLATFORMS)'; else build_platforms="linux amd64"; fi; \ |
141 | 152 | if ! [ -f "$$dockerfile_windows" ]; then \ |
142 | | - build_platforms="$$(echo "$$build_platforms" | sed -e 's/windows *[^ ]* *.exe//g' -e 's/; *;/;/g')"; \ |
| 153 | + build_platforms="$$(echo "$$build_platforms" | sed -e 's/windows *[^ ]* *.exe *[^ ]* *[^ ]*//g' -e 's/; *;/;/g' -e 's/;[ ]*$$//')"; \ |
143 | 154 | fi; \ |
144 | 155 | pushMultiArch () { \ |
145 | 156 | tag=$$1; \ |
146 | | - echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do \ |
| 157 | + echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix base_image addon_image; do \ |
| 158 | + escaped_base_image=$${base_image/:/-}; \ |
| 159 | + if ! [ -z $$escaped_base_image ]; then escaped_base_image+="-"; fi; \ |
147 | 160 | docker buildx build --push \ |
148 | | - --tag $(IMAGE_NAME):$$arch-$$os-$$tag \ |
| 161 | + --tag $(IMAGE_NAME):$$arch-$$os-$$escaped_base_image$$tag \ |
149 | 162 | --platform=$$os/$$arch \ |
150 | 163 | --file $$(eval echo \$${dockerfile_$$os}) \ |
151 | 164 | --build-arg binary=./bin/$*$$suffix \ |
152 | 165 | --build-arg ARCH=$$arch \ |
| 166 | + --build-arg BASE_IMAGE=$$base_image \ |
| 167 | + --build-arg ADDON_IMAGE=$$addon_image \ |
153 | 168 | --label revision=$(REV) \ |
154 | 169 | .; \ |
155 | 170 | done; \ |
156 | | - images=$$(echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix; do echo $(IMAGE_NAME):$$arch-$$os-$$tag; done); \ |
| 171 | + images=$$(echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix base_image addon_image; do \ |
| 172 | + escaped_base_image=$${base_image/:/-}; \ |
| 173 | + if ! [ -z $$escaped_base_image ]; then escaped_base_image+="-"; fi; \ |
| 174 | + echo $(IMAGE_NAME):$$arch-$$os-$$escaped_base_image$$tag; \ |
| 175 | + done); \ |
157 | 176 | docker manifest create --amend $(IMAGE_NAME):$$tag $$images; \ |
| 177 | + echo "$$build_platforms" | tr ';' '\n' | while read -r os arch suffix base_image addon_image; do \ |
| 178 | + if [ $$os = "windows" ]; then \ |
| 179 | + escaped_base_image=$${base_image/:/-}; \ |
| 180 | + if ! [ -z $$escaped_base_image ]; then escaped_base_image+="-"; fi; \ |
| 181 | + image=$(IMAGE_NAME):$$arch-$$os-$$escaped_base_image$$tag; \ |
| 182 | + os_version=$$(docker manifest inspect mcr.microsoft.com/windows/$${base_image} | grep "os.version" | head -n 1 | awk '{print $$2}' | sed -e 's/"//g') || true; \ |
| 183 | + docker manifest annotate --os-version $$os_version $(IMAGE_NAME):$$tag $$image; \ |
| 184 | + fi; \ |
| 185 | + done; \ |
158 | 186 | docker manifest push -p $(IMAGE_NAME):$$tag; \ |
159 | 187 | }; \ |
160 | 188 | if [ $(PULL_BASE_REF) = "master" ]; then \ |
@@ -288,4 +316,3 @@ test-spelling: |
288 | 316 | test-boilerplate: |
289 | 317 | @ echo; echo "### $@:" |
290 | 318 | @ ./release-tools/verify-boilerplate.sh "$(pwd)" |
291 | | - |
|
0 commit comments