Browse code

Support cross-compile for arm

Pretty much cross-compile doesn't work because of this:

> profiles/seccomp/seccomp.go:13:2: build constraints exclude all Go files in /go/src/github.com/docker/docker/vendor/github.com/seccomp/libseccomp-golang

This changes adds a new Dockerfile target for cross compilation with the
neccesary arch specific libseccomp packages and CC toolchains.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Brian Goff authored on 2019/04/06 09:20:06
Showing 3 changed files
... ...
@@ -24,6 +24,8 @@
24 24
 # the case. Therefore, you don't have to disable it anymore.
25 25
 #
26 26
 
27
+ARG CROSS="false"
28
+
27 29
 FROM golang:1.12.3 AS base
28 30
 # allow replacing httpredir or deb mirror
29 31
 ARG APT_MIRROR=deb.debian.org
... ...
@@ -93,11 +95,31 @@ RUN /download-frozen-image-v2.sh /build \
93 93
 # See also ensureFrozenImagesLinux() in "integration-cli/fixtures_linux_daemon_test.go" (which needs to be updated when adding images to this list)
94 94
 
95 95
 # Just a little hack so we don't have to install these deps twice, once for runc and once for dockerd
96
-FROM base AS runtime-dev
96
+FROM base AS runtime-dev-cross-false
97 97
 RUN apt-get update && apt-get install -y \
98 98
 	libapparmor-dev \
99 99
 	libseccomp-dev
100 100
 
101
+FROM runtime-dev-cross-false AS runtime-dev-cross-true
102
+RUN dpkg --add-architecture armhf
103
+RUN dpkg --add-architecture arm64
104
+RUN dpkg --add-architecture armel
105
+# These crossbuild packages rely on gcc-<arch>, but this doesn't want to install
106
+# on non-amd64 systems.
107
+# Additionally, the crossbuild-amd64 is currently only on debian:buster, so
108
+# other architectures cannnot crossbuild amd64.
109
+RUN if [ "$(go env GOHOSTARCH)" = "amd64" ]; then \
110
+	apt-get update \
111
+	&& apt-get install -y \
112
+		crossbuild-essential-armhf \
113
+		crossbuild-essential-arm64 \
114
+		crossbuild-essential-armel \
115
+		libseccomp-dev:armhf \
116
+		libseccomp-dev:arm64 \
117
+		libseccomp-dev:armel; \
118
+	fi
119
+
120
+FROM runtime-dev-cross-${CROSS} AS runtime-dev
101 121
 
102 122
 FROM base AS tomlv
103 123
 ENV INSTALL_BINARY_NAME=tomlv
... ...
@@ -117,9 +117,6 @@ INTERACTIVE := $(shell [ -t 0 ] && echo 1 || echo 0)
117 117
 ifeq ($(INTERACTIVE), 1)
118 118
 	DOCKER_FLAGS += -t
119 119
 endif
120
-ifeq ($(BIND_DIR), .)
121
-	DOCKER_BUILD_OPTS += --target=dev
122
-endif
123 120
 
124 121
 DOCKER_RUN_DOCKER := $(DOCKER_FLAGS) "$(DOCKER_IMAGE)"
125 122
 
... ...
@@ -134,6 +131,21 @@ binary: build ## build the linux binaries
134 134
 dynbinary: build ## build the linux dynbinaries
135 135
 	$(DOCKER_RUN_DOCKER) hack/make.sh dynbinary
136 136
 
137
+
138
+
139
+cross: DOCKER_CROSS := true
140
+cross: build ## cross build the binaries for darwin, freebsd and\nwindows
141
+	$(DOCKER_RUN_DOCKER) hack/make.sh dynbinary binary cross
142
+
143
+ifdef DOCKER_CROSSPLATFORMS
144
+build: DOCKER_CROSS := true
145
+else
146
+build: DOCKER_CROSS ?= false
147
+endif
148
+ifeq ($(BIND_DIR), .)
149
+build: DOCKER_BUILD_OPTS += --target=dev
150
+endif
151
+build: DOCKER_BUILD_ARGS += --build-arg=CROSS=$(DOCKER_CROSS)
137 152
 build: DOCKER_BUILDKIT ?= 1
138 153
 build: bundles
139 154
 	$(warning The docker client CLI has moved to github.com/docker/cli. For a dev-test cycle involving the CLI, run:${\n} DOCKER_CLI_PATH=/host/path/to/cli/binary make shell ${\n} then change the cli and compile into a binary at the same location.${\n})
... ...
@@ -149,9 +161,6 @@ clean: clean-cache
149 149
 clean-cache:
150 150
 	docker volume rm -f docker-dev-cache
151 151
 
152
-cross: build ## cross build the binaries for darwin, freebsd and\nwindows
153
-	$(DOCKER_RUN_DOCKER) hack/make.sh dynbinary binary cross
154
-
155 152
 help: ## this help
156 153
 	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {gsub("\\\\n",sprintf("\n%22c",""), $$2);printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
157 154
 
... ...
@@ -47,6 +47,26 @@ if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARC
47 47
 			export CC=x86_64-w64-mingw32-gcc
48 48
 			export CGO_ENABLED=1
49 49
 			;;
50
+		linux/arm)
51
+			case "${GOARM}" in
52
+			5|"")
53
+				export CC=arm-linux-gnueabi-gcc
54
+				export CGO_ENABLED=1
55
+				;;
56
+			7)
57
+				export CC=arm-linux-gnueabihf-gcc
58
+				export CGO_ENABLED=1
59
+				;;
60
+			esac
61
+			;;
62
+		linux/arm64)
63
+			export CC=aarch64-linux-gnu-gcc
64
+			export CGO_ENABLED=1
65
+			;;
66
+		linux/amd64)
67
+			export CC=x86_64-linux-gnu-gcc
68
+			export CGO_ENABLED=1
69
+			;;
50 70
 	esac
51 71
 fi
52 72