Skip to content

Commit

Permalink
make: always set CGO_ENABLED=0 for platform builds (#1200)
Browse files Browse the repository at this point in the history
CGO_ENABLED defaults to "1" if a Go build targets the host system, so
that it can use the system's libc and any other libraries.

For cross-builds (when either the target GOOS or GOARCH don't match the
host's), the Go tool can't assume that a C cross-compiler and the
necessary headers are present, so the default is CGO_ENABLED=0.

This can result in binaries published to NPM being inconsistently
static, since the one matching the host's will be the only one to
dynamically link C libraries. For example, on linux/amd64:

	$ make platform-linux platform-windows platform-linux-arm64 platform-darwin platform-freebsd
	[...]
	$ ldd npm/esbuild-*/bin/*
	npm/esbuild-darwin-64/bin/esbuild:
		not a dynamic executable
	npm/esbuild-freebsd-64/bin/esbuild:
		not a dynamic executable
	npm/esbuild-linux-64/bin/esbuild:
		linux-vdso.so.1 (0x00007ffd24570000)
		libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f35a6718000)
		libc.so.6 => /usr/lib/libc.so.6 (0x00007f35a654b000)
		/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f35a6765000)
	npm/esbuild-linux-arm64/bin/esbuild:
		not a dynamic executable
	npm/esbuild-wasm/bin/esbuild:
		not a dynamic executable
	npm/esbuild-windows-32/bin/esbuild:
		not a dynamic executable
	npm/esbuild-windows-64/bin/esbuild:
		not a dynamic executable

With this change, we always force CGO_ENABLED=0, and the resulting
binaries are consistently non-dynamic no matter the host platform.

This change should be safe, as esbuild seems to have zero cgo code, and
the majority of platform builds for NPM were already defaulting to
CGO_ENABLED=0.
  • Loading branch information
mvdan authored Apr 26, 2021
1 parent ed551f9 commit 20ef31e
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ platform-all: cmd/esbuild/version.go test-all

platform-windows:
cd npm/esbuild-windows-64 && npm version "$(ESBUILD_VERSION)" --allow-same-version
GOOS=windows GOARCH=amd64 go build "-ldflags=-s -w" -o npm/esbuild-windows-64/esbuild.exe ./cmd/esbuild
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build "-ldflags=-s -w" -o npm/esbuild-windows-64/esbuild.exe ./cmd/esbuild

platform-windows-32:
cd npm/esbuild-windows-32 && npm version "$(ESBUILD_VERSION)" --allow-same-version
GOOS=windows GOARCH=386 go build "-ldflags=-s -w" -o npm/esbuild-windows-32/esbuild.exe ./cmd/esbuild
CGO_ENABLED=0 GOOS=windows GOARCH=386 go build "-ldflags=-s -w" -o npm/esbuild-windows-32/esbuild.exe ./cmd/esbuild

platform-unixlike:
test -n "$(GOOS)" && test -n "$(GOARCH)" && test -n "$(NPMDIR)"
mkdir -p "$(NPMDIR)/bin"
cd "$(NPMDIR)" && npm version "$(ESBUILD_VERSION)" --allow-same-version
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build "-ldflags=-s -w" -o "$(NPMDIR)/bin/esbuild" ./cmd/esbuild
CGO_ENABLED=0 GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build "-ldflags=-s -w" -o "$(NPMDIR)/bin/esbuild" ./cmd/esbuild

platform-android-arm64:
make GOOS=android GOARCH=arm64 NPMDIR=npm/esbuild-android-arm64 platform-unixlike
Expand Down

0 comments on commit 20ef31e

Please sign in to comment.