|
19 | 19 | # https://gist.github.com/subfuzion/0bd969d08fe0d8b5cc4b23c795854a13
|
20 | 20 | # https://stackoverflow.com/questions/10858261/abort-makefile-if-variable-not-set
|
21 | 21 | # https://stackoverflow.com/questions/38801796/makefile-set-if-variable-is-empty
|
| 22 | +# https://www.gnu.org/software/make/manual/make.html#Flavors |
22 | 23 |
|
23 |
| -SHELL = /bin/bash |
| 24 | +SHELL := /bin/bash |
24 | 25 |
|
25 | 26 | # Space-separated list of cmd/BINARY_NAME directories to build
|
26 |
| -WHAT = mysql2sqlite check_mysql2sqlite |
| 27 | +WHAT := mysql2sqlite check_mysql2sqlite |
27 | 28 |
|
28 | 29 | # What package holds the "version" variable used in branding/version output?
|
29 |
| -# VERSION_VAR_PKG = $(shell go list .) |
30 |
| -# VERSION_VAR_PKG = main |
31 |
| -VERSION_VAR_PKG = $(shell go list .)/internal/config |
| 30 | +# VERSION_VAR_PKG := $(shell go list .) |
| 31 | +# VERSION_VAR_PKG := main |
| 32 | +VERSION_VAR_PKG := $(shell go list .)/internal/config |
32 | 33 |
|
33 |
| -OUTPUTDIR = release_assets |
| 34 | +OUTPUTDIR := release_assets |
34 | 35 |
|
35 | 36 | # https://gist.github.com/TheHippo/7e4d9ec4b7ed4c0d7a39839e6800cc16
|
36 |
| -VERSION = $(shell git describe --always --long --dirty) |
| 37 | +VERSION := $(shell git describe --always --long --dirty) |
37 | 38 |
|
38 | 39 | # The default `go build` process embeds debugging information. Building
|
39 | 40 | # without that debugging information reduces the binary size by around 28%.
|
40 |
| -BUILDCMD = go build -mod=vendor -a -ldflags="-s -w -X $(VERSION_VAR_PKG).Version=$(VERSION)" |
41 |
| -GOCLEANCMD = go clean -mod=vendor ./... |
42 |
| -GITCLEANCMD = git clean -xfd |
43 |
| -CHECKSUMCMD = sha256sum -b |
| 41 | +# |
| 42 | +# We also include additional flags in an effort to generate static binaries |
| 43 | +# that do not have external dependencies. As of Go 1.15 this still appears to |
| 44 | +# be a mixed bag, so YMMV. |
| 45 | +# |
| 46 | +# See https://github.com/golang/go/issues/26492 for more information. |
| 47 | +# |
| 48 | +# -s |
| 49 | +# Omit the symbol table and debug information. |
| 50 | +# |
| 51 | +# -w |
| 52 | +# Omit the DWARF symbol table. |
| 53 | +# |
| 54 | +# -tags 'osusergo,netgo' |
| 55 | +# Use pure Go implementation of user and group id/name resolution. |
| 56 | +# Use pure Go implementation of DNS resolver. |
| 57 | +# |
| 58 | +# -extldflags '-static' |
| 59 | +# Pass 'static' flag to external linker. |
| 60 | +# |
| 61 | +# -linkmode=external |
| 62 | +# https://golang.org/src/cmd/cgo/doc.go |
| 63 | +# |
| 64 | +# NOTE: Using external linker requires installation of `gcc-multilib` |
| 65 | +# package when building 32-bit binaries on a Debian/Ubuntu system. It also |
| 66 | +# seems to result in an unstable build that crashes on startup. This *might* |
| 67 | +# be specific to the WSL environment used for builds. Further testing is |
| 68 | +# needed to confirm. |
| 69 | +# |
| 70 | +# CGO_ENABLED=1 |
| 71 | +# CGO is disabled by default for cross-compilation. You need to enable it |
| 72 | +# explicitly to use CGO for multiple architectures. |
| 73 | +BUILD_LDFLAGS_COMMON := -s -w -X $(VERSION_VAR_PKG).version=$(VERSION) |
| 74 | +BUILD_LDFLAGS_STATIC := -linkmode=external -extldflags '-static' |
| 75 | +BUILDCMD_COMMON := CGO_ENABLED=1 go build -mod=vendor -a |
| 76 | +BUILDCMD_STATIC := $(BUILDCMD_COMMON) -tags 'osusergo,netgo,sqlite_omit_load_extension' -ldflags "$(BUILD_LDFLAGS_STATIC) $(BUILD_LDFLAGS_COMMON)" |
| 77 | +BUILDCMD_DYNAMIC := $(BUILDCMD_COMMON) -ldflags "$(BUILD_LDFLAGS_COMMON)" |
| 78 | + |
| 79 | +BUILD_TYPE_STATIC := static |
| 80 | +BUILD_TYPE_DYNAMIC := dynamic |
| 81 | + |
| 82 | +# Default build command and type if not overridden |
| 83 | +BUILDCMD := $(BUILDCMD_DYNAMIC) |
| 84 | +BUILDTYPE := $(BUILD_TYPE_DYNAMIC) |
44 | 85 |
|
45 |
| -.DEFAULT_GOAL := help |
| 86 | +# Use mingw as C compiler to build Windows cgo-enabled binaries. |
| 87 | +WINCOMPILERX86 := CC=i686-w64-mingw32-gcc |
| 88 | +WINCOMPILERX64 := CC=x86_64-w64-mingw32-gcc |
| 89 | + |
| 90 | +DOCKER_BUILD_IMG_X86 := atc0005/go-ci:go-ci-stable-alpine-buildx86 |
| 91 | +DOCKER_BUILD_IMG_X64 := atc0005/go-ci:go-ci-stable-alpine-buildx64 |
| 92 | + |
| 93 | +GOCLEANCMD := go clean -mod=vendor ./... |
| 94 | +GITCLEANCMD := git clean -xfd |
| 95 | +CHECKSUMCMD := sha256sum -b |
| 96 | + |
| 97 | +.DEFAULT_GOAL := help |
46 | 98 |
|
47 | 99 | ##########################################################################
|
48 | 100 | # Targets will not work properly if a file with the same name is ever
|
@@ -122,46 +174,139 @@ pristine: goclean gitclean
|
122 | 174 |
|
123 | 175 | .PHONY: all
|
124 | 176 | # https://stackoverflow.com/questions/3267145/makefile-execute-another-target
|
125 |
| -## all: generates assets for Linux distros and Windows |
| 177 | +## all: generates dynamically linked assets for Linux and Windows systems |
126 | 178 | all: clean windows linux
|
127 | 179 | @echo "Completed all cross-platform builds ..."
|
128 | 180 |
|
| 181 | +.PHONE: all-static |
| 182 | +## all-static: generates statically linked x86 and x64 assets for Linux and Windows systems |
| 183 | +all-static: clean windows-static linux-static |
| 184 | + @echo "Completed all cross-platform builds ..." |
| 185 | + |
129 | 186 | .PHONY: windows
|
130 |
| -## windows: generates assets for Windows systems |
131 |
| -windows: |
132 |
| - @echo "Building release assets for windows ..." |
| 187 | +## windows: generates dynamically linked x86 and x64 Windows assets |
| 188 | +windows: windows-x86 windows-x64 |
| 189 | + @echo "Completed build tasks for windows" |
| 190 | + |
| 191 | +.PHONY: windows-static |
| 192 | +## windows-static: generates dynamically linked x86 and x64 Windows assets |
| 193 | +windows-static: windows-x86-static windows-x64-static |
| 194 | + @echo "Completed build tasks for windows" |
133 | 195 |
|
| 196 | +.PHONY: windows-x86 |
| 197 | +## windows-x86: generates dynamically linked Windows x86 assets |
| 198 | +windows-x86: |
| 199 | + @echo "Building ($(BUILDTYPE)) release assets for windows x86 ..." |
134 | 200 | @for target in $(WHAT); do \
|
135 | 201 | mkdir -p $(OUTPUTDIR)/$$target && \
|
136 | 202 | echo "Building $$target 386 binaries" && \
|
137 |
| - env GOOS=windows GOARCH=386 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-windows-386.exe ./cmd/$$target && \ |
| 203 | + env GOOS=windows GOARCH=386 $(WINCOMPILERX86) $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-windows-386.exe ./cmd/$$target && \ |
| 204 | + echo "Generating $$target x86 checksum files" && \ |
| 205 | + cd $(OUTPUTDIR)/$$target && \ |
| 206 | + $(CHECKSUMCMD) $$target-$(VERSION)-windows-386.exe > $$target-$(VERSION)-windows-386.exe.sha256 && \ |
| 207 | + cd $$OLDPWD; \ |
| 208 | + done |
| 209 | + @echo "Completed ($(BUILDTYPE)) release assets build tasks for windows x86" |
| 210 | + |
| 211 | +.PHONY: windows-x86-static |
| 212 | +## windows-x86-static: generates assets statically, specifically for Windows x86 systems |
| 213 | +windows-x86-static: BUILDCMD = $(BUILDCMD_STATIC) |
| 214 | +windows-x86-static: BUILDTYPE = $(BUILD_TYPE_STATIC) |
| 215 | +windows-x86-static: windows-x86 |
| 216 | + |
| 217 | +.PHONY: windows-x64 |
| 218 | +## windows-x64: generates assets specifically for x64 Windows systems |
| 219 | +windows-x64: |
| 220 | + @echo "Building ($(BUILDTYPE)) release assets for windows x64 ..." |
| 221 | + @for target in $(WHAT); do \ |
| 222 | + mkdir -p $(OUTPUTDIR)/$$target && \ |
138 | 223 | echo "Building $$target amd64 binaries" && \
|
139 |
| - env GOOS=windows GOARCH=amd64 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-windows-amd64.exe ./cmd/$$target && \ |
| 224 | + env GOOS=windows GOARCH=amd64 $(WINCOMPILERX64) $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-windows-amd64.exe ./cmd/$$target && \ |
140 | 225 | echo "Generating $$target checksum files" && \
|
141 | 226 | cd $(OUTPUTDIR)/$$target && \
|
142 |
| - $(CHECKSUMCMD) $$target-$(VERSION)-windows-386.exe > $$target-$(VERSION)-windows-386.exe.sha256 && \ |
143 | 227 | $(CHECKSUMCMD) $$target-$(VERSION)-windows-amd64.exe > $$target-$(VERSION)-windows-amd64.exe.sha256 && \
|
144 | 228 | cd $$OLDPWD; \
|
145 | 229 | done
|
| 230 | + @echo "Completed ($(BUILDTYPE)) release assets build tasks for windows x64" |
146 | 231 |
|
147 |
| - @echo "Completed build tasks for windows" |
| 232 | +.PHONY: windows-x64-static |
| 233 | +## windows-x64-static: generates assets statically, specifically for Windows x64 systems |
| 234 | +windows-x64-static: BUILDCMD = $(BUILDCMD_STATIC) |
| 235 | +windows-x64-static: BUILDTYPE = $(BUILD_TYPE_STATIC) |
| 236 | +windows-x64-static: windows-x64 |
148 | 237 |
|
149 | 238 | .PHONY: linux
|
150 |
| -## linux: generates assets for Linux distros |
151 |
| -linux: |
152 |
| - @echo "Building release assets for linux ..." |
| 239 | +## linux: generates dynamically linked x86 and x64 assets for Linux distros |
| 240 | +linux: linux-x86 linux-x64 |
| 241 | + @echo "Completed ($(BUILDTYPE)) release assets build tasks for linux" |
| 242 | + |
| 243 | +.PHONE: linux-static |
| 244 | +## linux-static: generates statically linked x86 and x64 assets for Linux distros |
| 245 | +linux-static: linux-x86-static linux-x64-static |
| 246 | + @echo "Completed ($(BUILDTYPE)) release assets build tasks for linux" |
| 247 | + |
| 248 | +.PHONY: linux-x86 |
| 249 | +## linux-x86: generates assets specifically for Linux x86 systems |
| 250 | +linux-x86: |
| 251 | + @echo "Building ($(BUILDTYPE)) release assets for linux x86 ..." |
153 | 252 |
|
154 | 253 | @for target in $(WHAT); do \
|
155 | 254 | mkdir -p $(OUTPUTDIR)/$$target && \
|
156 | 255 | echo "Building $$target 386 binaries" && \
|
157 | 256 | env GOOS=linux GOARCH=386 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-linux-386 ./cmd/$$target && \
|
| 257 | + echo "Generating $$target checksum files" && \ |
| 258 | + cd $(OUTPUTDIR)/$$target && \ |
| 259 | + $(CHECKSUMCMD) $$target-$(VERSION)-linux-386 > $$target-$(VERSION)-linux-386.sha256 && \ |
| 260 | + cd $$OLDPWD; \ |
| 261 | + done |
| 262 | + |
| 263 | + @echo "Completed ($(BUILDTYPE)) release assets build tasks for linux x86" |
| 264 | + |
| 265 | +.PHONY: linux-x86-static |
| 266 | +## linux-x86-static: generates assets statically, specifically for Linux x86 systems |
| 267 | +linux-x86-static: BUILDCMD = $(BUILDCMD_STATIC) |
| 268 | +linux-x86-static: BUILDTYPE = $(BUILD_TYPE_STATIC) |
| 269 | +linux-x86-static: linux-x86 |
| 270 | + |
| 271 | +.PHONY: linux-x64 |
| 272 | +## linux-x64: generates assets specifically for Linux x64 systems |
| 273 | +linux-x64: |
| 274 | + @echo "Building ($(BUILDTYPE)) release assets for linux x64 ..." |
| 275 | + |
| 276 | + @for target in $(WHAT); do \ |
| 277 | + mkdir -p $(OUTPUTDIR)/$$target && \ |
158 | 278 | echo "Building $$target amd64 binaries" && \
|
159 | 279 | env GOOS=linux GOARCH=amd64 $(BUILDCMD) -o $(OUTPUTDIR)/$$target/$$target-$(VERSION)-linux-amd64 ./cmd/$$target && \
|
160 | 280 | echo "Generating $$target checksum files" && \
|
161 | 281 | cd $(OUTPUTDIR)/$$target && \
|
162 |
| - $(CHECKSUMCMD) $$target-$(VERSION)-linux-386 > $$target-$(VERSION)-linux-386.sha256 && \ |
163 | 282 | $(CHECKSUMCMD) $$target-$(VERSION)-linux-amd64 > $$target-$(VERSION)-linux-amd64.sha256 && \
|
164 | 283 | cd $$OLDPWD; \
|
165 | 284 | done
|
166 | 285 |
|
167 |
| - @echo "Completed build tasks for linux" |
| 286 | + @echo "Completed ($(BUILDTYPE)) release assets build tasks for linux x64" |
| 287 | + |
| 288 | + |
| 289 | +.PHONY: linux-x64-static |
| 290 | +## linux-x64-static: generates assets statically, specifically for Linux x64 systems |
| 291 | +linux-x64-static: BUILDCMD = $(BUILDCMD_STATIC) |
| 292 | +linux-x64-static: BUILDTYPE = $(BUILD_TYPE_STATIC) |
| 293 | +linux-x64-static: linux-x64 |
| 294 | + |
| 295 | +.PHONY: docker |
| 296 | +## docker: generates assets for Linux distros and Windows using Docker |
| 297 | +docker: clean |
| 298 | + @docker run \ |
| 299 | + --rm \ |
| 300 | + -i \ |
| 301 | + -v $$PWD:$$PWD \ |
| 302 | + -w $$PWD \ |
| 303 | + $(DOCKER_BUILD_IMG_X86) \ |
| 304 | + make windows-x86-static linux-x86-static |
| 305 | + @docker run \ |
| 306 | + --rm \ |
| 307 | + -i \ |
| 308 | + -v $$PWD:$$PWD \ |
| 309 | + -w $$PWD \ |
| 310 | + $(DOCKER_BUILD_IMG_X64) \ |
| 311 | + make windows-x64-static linux-x64-static |
| 312 | + @echo "Completed all cross-platform builds via Docker containers ..." |
0 commit comments