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