Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't cross-build linux/amd64 image on Mac M1 #762

Closed
trekawek opened this issue Jun 6, 2022 · 2 comments · Fixed by #842
Closed

Can't cross-build linux/amd64 image on Mac M1 #762

trekawek opened this issue Jun 6, 2022 · 2 comments · Fixed by #842
Labels
area/ci CI related issues and pull requests bug Something isn't working

Comments

@trekawek
Copy link

trekawek commented Jun 6, 2022

An attempt to build the recent source-controller (9e33041) on Mac M1, targeting the linux/amd64 platform, fails with the error cannot find -lcrypto.

Other architectures: linux/arm64 and linux/arm/v7 work correctly.

Reproduction steps:

$ make docker-build BUILD_PLATFORMS=linux/amd64

Output:

------
 > [linux/arm64->amd64 build  9/10] RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) &&     export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" &&     export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" &&     export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static" &&     GOARCH=amd64 go build          -ldflags "-s -w"         -tags 'netgo,osusergo,static_build'         -o /source-controller -trimpath main.go;:
#0 42.37 # runtime/cgo
#0 42.37 /workspace/build/musl/x86_64-linux-musl-cross/bin/../lib/gcc/x86_64-linux-musl/11.2.1/../../../../x86_64-linux-musl/bin/ld: cannot find -lcrypto
#0 42.37 /workspace/build/musl/x86_64-linux-musl-cross/bin/../lib/gcc/x86_64-linux-musl/11.2.1/../../../../x86_64-linux-musl/bin/ld: cannot find -lssl
#0 42.37 /workspace/build/musl/x86_64-linux-musl-cross/bin/../lib/gcc/x86_64-linux-musl/11.2.1/../../../../x86_64-linux-musl/bin/ld: cannot find -lcrypto
#0 42.37 /workspace/build/musl/x86_64-linux-musl-cross/bin/../lib/gcc/x86_64-linux-musl/11.2.1/../../../../x86_64-linux-musl/bin/ld: cannot find -lssl
#0 42.37 /workspace/build/musl/x86_64-linux-musl-cross/bin/../lib/gcc/x86_64-linux-musl/11.2.1/../../../../x86_64-linux-musl/bin/ld: cannot find -lcrypto
#0 42.37 collect2: error: ld returned 1 exit status
------
Dockerfile:84
--------------------
  83 |     # Instead of using xx-go, (cross) compile with vanilla go leveraging musl tool chain.
  84 | >>> RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) && \
  85 | >>>     export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \
  86 | >>>     export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
  87 | >>>     export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static" && \
  88 | >>>     GOARCH=$TARGETARCH go build  \
  89 | >>>         -ldflags "-s -w" \
  90 | >>>         -tags 'netgo,osusergo,static_build' \
  91 | >>>         -o /source-controller -trimpath main.go;
  92 |
--------------------
error: failed to solve: process "/bin/sh -c export $(cat build/musl/$(xx-info alpine-arch).env | xargs) &&     export LIBRARY_PATH=\"/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64\" &&     export PKG_CONFIG_PATH=\"/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig\" &&     export CGO_LDFLAGS=\"$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static\" &&     GOARCH=$TARGETARCH go build          -ldflags \"-s -w\"         -tags 'netgo,osusergo,static_build'         -o /source-controller -trimpath main.go;" did not complete successfully: exit code: 2
make: *** [docker-build] Error 1

Docker version:

Client:
 Cloud integration: v1.0.25
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:20:34 2022
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.9.0 (80466)
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:14:19 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
@stefanprodan stefanprodan added bug Something isn't working area/ci CI related issues and pull requests labels Jun 6, 2022
@MateuSoares1
Copy link

I have the same problem. In my case I'm trying to generate an image for my azure container registry. I would like to generate one with multi architecture or at least with linux/amd64. It just isn't coming out at all.

Comand: docker build --platform linux/amd64 or % docker buildx build --platform linux/amd64

I'm using Mac M1

Output:

=> ERROR [angular_v1.0 6/6] RUN npm run build 96.1s ------

[angular_v1.0 6/6] RUN npm run build:
#14 1.428
#14 1.428 > layout-first@0.0.0 build /app
#14 1.428 > ng build --prod
#14 1.428
#14 10.98 - Generating browser application bundles (phase: setup)...
#14 95.75 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
#14 95.78 events.js:377
#14 95.78 throw er; // Unhandled 'error' event
#14 95.78 ^
#14 95.78
#14 95.78 Error: write EPIPE
#14 95.78 at afterWriteDispatched (internal/stream_base_commons.js:156:25)
#14 95.78 at writeGeneric (internal/stream_base_commons.js:147:3)
#14 95.78 at Socket._writeGeneric (net.js:798:11)
#14 95.78 at Socket._write (net.js:810:8)
#14 95.78 at writeOrBuffer (internal/streams/writable.js:358:12)
#14 95.78 at Socket.Writable.write (internal/streams/writable.js:303:10)
#14 95.78 at Object.writeToStdin (/app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:1864:19)
#14 95.78 at sendRequest (/app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:637:14)
#14 95.78 at start (/app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:1274:9)
#14 95.78 at Object.transform2 [as transform] (/app/node_modules/@angular-devkit/build-angular/node_modules/esbuild/lib/main.js:1324:5)
#14 95.78 Emitted 'error' event on Socket instance at:
#14 95.78 at emitErrorNT (internal/streams/destroy.js:106:8)
#14 95.78 at emitErrorCloseNT (internal/streams/destroy.js:74:3)
#14 95.78 at processTicksAndRejections (internal/process/task_queues.js:82:21)
#14 95.78 at runNextTicks (internal/process/task_queues.js:64:3)
#14 95.78 at processImmediate (internal/timers.js:437:9) {
#14 95.78 errno: -32,
#14 95.78 code: 'EPIPE',
#14 95.78 syscall: 'write'
#14 95.78 }
#14 96.02 npm ERR! code ELIFECYCLE
#14 96.02 npm ERR! errno 1
#14 96.04 npm ERR! layout-first@0.0.0 build: ng build --prod
#14 96.05 npm ERR! Exit status 1
#14 96.05 npm ERR!
#14 96.05 npm ERR! Failed at the layout-first@0.0.0 build script.
#14 96.05 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#14 96.08
#14 96.08 npm ERR! A complete log of this run can be found in:
#14 96.08 npm ERR! /root/.npm/_logs/2022-07-14T23_16_55_271Z-debug.log`

@pjbgf
Copy link
Member

pjbgf commented Jul 15, 2022

We have a fix for this, which will be proposed once #819 is merged.
In the mean time, you can fix cross build in your local environment by patching your Dockerfile and Makefile:

diff --git a/Dockerfile b/Dockerfile
index e136ab4..46d2ce6 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,7 +2,7 @@ ARG BASE_VARIANT=alpine
 ARG GO_VERSION=1.17
 ARG XX_VERSION=1.1.0

-ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2-all
+ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2-only
 ARG LIBGIT2_TAG=v0.1.2

 FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS libgit2-libs
@@ -84,7 +84,7 @@ ENV CGO_ENABLED=1
 RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) && \
     export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \
     export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
-    export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static" && \
+    export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libgit2) -static" && \
     GOARCH=$TARGETARCH go build  \
         -ldflags "-s -w" \
         -tags 'netgo,osusergo,static_build' \


diff --git a/Makefile b/Makefile
index e78406f..b15bde9 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ IMG ?= fluxcd/source-controller
 TAG ?= latest

 # Base image used to build the Go binary
-LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-all
+LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-only
 LIBGIT2_TAG ?= v0.1.2

 # Allows for defining additional Go test args, e.g. '-tags integration'.
@@ -39,17 +39,17 @@ MUSL-CC =
 export CGO_ENABLED=1
 export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig
 export LIBRARY_PATH=$(LIBGIT2_LIB_PATH)
-export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include -I$(LIBGIT2_PATH)/include/openssl
+export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include


 # The pkg-config command will yield warning messages until libgit2 is downloaded.
 ifeq ($(shell uname -s),Darwin)
-export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2 2>/dev/null)
+export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
 GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build$(addprefix ,,$(GO_TAGS))'
 else
 export PKG_CONFIG_PATH:=$(PKG_CONFIG_PATH):$(LIBGIT2_LIB64_PATH)/pkgconfig
 export LIBRARY_PATH:=$(LIBRARY_PATH):$(LIBGIT2_LIB64_PATH)
-export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2 2>/dev/null)
+export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
 endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ci CI related issues and pull requests bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants