gographics / imagick

Go binding to ImageMagick's MagickWand C API
https://godoc.org/github.com/gographics/imagick/imagick
Other
1.77k stars 184 forks source link

mac m1 build for alpine linux failed #292

Closed charliecen closed 1 year ago

charliecen commented 1 year ago

I use command build go project error

$ CGO_ENABLED=1 GOOS=linux  GOARCH=amd64  CC=x86_64-linux-musl-gcc  CXX=x86_64-linux-musl-g++ go build -o bin/convert/rpc/convert service/convert/rpc/convert.go
# gopkg.in/gographics/imagick.v3/imagick
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: $WORK/b834/_x002.o: in function `_cgo_40cab26f1df6_Cfunc_GetAffineMatrix':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `GetAffineMatrix'
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: $WORK/b834/_x012.o: in function `_cgo_40cab26f1df6_Cfunc_ConvertHSLToRGB':
...

/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: $WORK/b834/_x074.o: in function `_cgo_40cab26f1df6_Cfunc_GetImageEntropy':
/tmp/go-build/cgo-gcc-prolog:55: undefined reference to `GetImageEntropy'
collect2: error: ld returned 1 exit status

$ export CGO_CFLAGS="-I`pkg-config --cflags MagickWand`"                                   
$ export CGO_LDFLAGS="-I`pkg-config --libs MagickWand`"
$ export CGO_CFLAGS_ALLOW='-Xpreprocessor'

$ CGO_ENABLED=1 GOOS=linux  GOARCH=amd64  CC=x86_64-linux-musl-gcc  CXX=x86_64-linux-musl-g++ go build -o bin/convert/rpc/convert service/convert/rpc/convert.go
# runtime/cgo
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: cannot find -lMagickWand-7.Q16HDRI
/opt/homebrew/Cellar/musl-cross/0.9.9_1/libexec/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: cannot find -lMagickCore-7.Q16HDRI
collect2: error: ld returned 1 exit status

$ pkg-config --libs --static MagickWand MagickCore                                         
-L/opt/homebrew/Cellar/imagemagick/7.1.1-5/lib -L/opt/homebrew/Cellar/little-cms2/2.14/lib -L/opt/homebrew/opt/freetype/lib -L/opt/homebrew/Cellar/glib/2.76.1/lib -L/opt/homebrew/opt/gettext/lib -L/opt/homebrew/opt/freetype/lib -L/opt/homebrew/Cellar/fontconfig/2.14.2/lib -L/opt/homebrew/Cellar/imagemagick/7.1.1-5/lib -L/opt/homebrew/Cellar/little-cms2/2.14/lib -L/opt/homebrew/opt/freetype/lib -L/opt/homebrew/Cellar/glib/2.76.1/lib -L/opt/homebrew/opt/gettext/lib -L/opt/homebrew/opt/freetype/lib -L/opt/homebrew/Cellar/fontconfig/2.14.2/lib -L/opt/homebrew/Cellar/imagemagick/7.1.1-5/lib -L/opt/homebrew/Cellar/little-cms2/2.14/lib -L/opt/homebrew/opt/freetype/lib -L/opt/homebrew/Cellar/glib/2.76.1/lib -L/opt/homebrew/opt/gettext/lib -L/opt/homebrew/opt/freetype/lib -L/opt/homebrew/Cellar/fontconfig/2.14.2/lib -lMagickWand-7.Q16HDRI -llcms2 -lfreetype -llqr-1 -lglib-2.0 -lintl -lxml2 -lfontconfig -lfreetype -lbz2 -lz -lltdl -lm -lpthread -lm -lMagickCore-7.Q16HDRI -llcms2 -lfreetype -llqr-1 -lglib-2.0 -lintl -lxml2 -lfontconfig -lfreetype -lbz2 -lz -lltdl -lm -lpthread -lm -lMagickCore-7.Q16HDRI -llcms2 -lfreetype -llqr-1 -lglib-2.0 -lintl -lxml2 -lfontconfig -lfreetype -lbz2 -lz -lltdl -lm -lpthread -lm

my os

$ uname -a                                                                                    
Darwin 172-15-82-96.lightspeed.frokca.sbcglobal.net 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64 arm Darwin

go version

$ go version                                                                                  
go version go1.18.10 darwin/arm64

magic version

$  magick -version                                                                              
Version: ImageMagick 7.1.1-5 Q16-HDRI aarch64 20993 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0)
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib
Compiler: gcc (4.2)
justinfx commented 1 year ago

Am I reading this correctly? You are trying to cross compile from your macos to a Linux target using the musl compiler built and installed on macos and get it to link against an ImageMagick that was built for macos and not Linux?

charliecen commented 1 year ago

Am I reading this correctly? You are trying to cross compile from your macos to a Linux target using the musl compiler built and installed on macos and get it to link against an ImageMagick that was built for macos and not Linux?

yes

justinfx commented 1 year ago

Wouldn't you need to have an ImageMagick7 that you have also compiled with musl, for the same linux target? And then you might be able to point your Go build to link against the Linux ImageMagick. But it isn't likely going to work to try to link against your macos ImageMagick. Also, if you go with musl, everything needs to be compiled with musl and it needs to run under a musl distro.

charliecen commented 1 year ago

It is possible to compile golang project on alpine linux.

justinfx commented 1 year ago

Yes, I know it is possible to compile a Go project on alpine linux. What I am pointing out though is not only do you want to compile your Golang project on Alpine Linux, but you also want to compile a cgo project that needs to link against a C library called ImageMagick. If you want to do that, you will also need to have ImageMagick compiled for linux, using the musl compiler.

You might find it easier to just use Docker. You can see an older example that we use to build a specific version of ImageMagick and the compile, link, and run the tests: https://github.com/gographics/imagick/blob/im-7/Dockerfile

Now if you really want to use Alpine, you could switch to using an alpine image and install ImageMagick and the compiler tools:

FROM golang:1.20-alpine

RUN apk add g++ gcc imagemagick-dev

WORKDIR /src
CMD go build .
justinfx commented 1 year ago

Here is a more complete example, using alpine:

Dockerfile

FROM golang:1.20-alpine

RUN apk add g++ gcc imagemagick-dev

WORKDIR /src
CMD go build .

Now make an image

docker build -t imagick_builder:latest .

Jump into your project, and build it:

cd my_project
docker run --rm -v $PWD:/src imagick_builder:latest

It should mount your current source dir into the container, build it with musl and ImageMagick under linux and write your binary to the same dir

charliecen commented 1 year ago

ok, thinks