golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124k stars 17.67k forks source link

x/mobile: gomobile bind can't use target specific LDFLAGS / CPPFLAGS #48203

Open dpanic opened 3 years ago

dpanic commented 3 years ago

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/dpanic/go/bin"
GOCACHE="/home/dpanic/.cache/go-build"
GOENV="/home/dpanic/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dpanic/go/pkg/mod"
GONOPROXY="gore.salestrekker.com,github.com/dpanic"
GONOSUMDB="gore.salestrekker.com,github.com/dpanic"
GOOS="linux"
GOPATH="/home/dpanic/go"
GOPRIVATE="gore.salestrekker.com,github.com/dpanic"
GOPROXY="https://goproxy.io,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2246104607=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am trying to build .aar archive with gomobile bind but with CGO_LDFLAGS and CGO_CPPFLAGS which are TARGET dependant.

I can build for loop, to create 4 .aar files and to manually combine them at the end, but I think there should be smarter way.

export CGO_LDFLAGS="
    -L$ROOT/../dependencies/libs/armeabi-v7a  
    -L$ROOT/../dependencies/libs/arm64-v8a
    -L$ROOT/../dependencies/libs/x86  
    -L$ROOT/../dependencies/libs/x86_64  
    -lopencv_core
    ... [ REDACTED ] ...
"
CGO_ENABLED=1 \
    gomobile bind \
    -target=android/arm,android/arm64,android/386,android/amd64
    -o export.aar \
    -v

What did you expect to see?

AAR file with compiled .so libraries without issues.

What did you see instead?

# runtime/cgo
ld: error: /home/user/libs/armeabi-v7a/libopencv_calib3d.so is incompatible with elf_i386
dpanic commented 3 years ago

I tried with this:


// #cgo android,amd64 LDFLAGS: -L${SRCDIR}/../dependencies/libs/x86_64 -lopencv_calib3d -lopencv_core -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_gapi -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_videoio

But it doesn't work, as this is project which is external dependency, I can't change their files. Changing in my files which imports packages from that project doesn't work.

dpanic commented 3 years ago

I would be happy to do a PR. My idea is the following:

Implement CGOLDFLAGS%GOOS_%ARCH with fallback to CGO_LDFLAGS.

Examples:

cagedmantis commented 3 years ago

/cc @hyangah

kedric commented 3 years ago

you can use LDFLAGS

#cgo CFLAGS: -I${SRCDIR}/vendor/headers
#cgo CPPFLAGS: -I${SRCDIR}/vendor/headers
#cgo !windows,!ios,!android pkg-config: opencv4
#cgo CXXFLAGS: --std=c++11
#cgo darwin,amd64,ios LDFLAGS: -F${SRCDIR}/vendor/libs/ios -framework Foundation -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework CoreGraphics -framework opencv2 -miphoneos-version-min=9.0
#cgo darwin,arm64,ios CFLAGS:  -I${SRCDIR}/vendor/headers
#cgo darwin,arm64,ios LDFLAGS: -F${SRCDIR}/vendor/libs/ios -framework Foundation -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework CoreGraphics -framework opencv2 -miphoneos-version-min=9.0
#cgo android LDFLAGS: -lopencv_core -lopencv_imgproc -lz -llog
#cgo android,arm64 LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/arm64-v8a
#cgo android,arm LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/armeabi-v7a
#cgo android,amd64 LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/x86_64
#cgo android,386 LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/x86

and if you use go mod add this line in go.mod

replace github.com/hybridgroup/go-cv => ../../hybridgroup/go-cv
dpanic commented 3 years ago

you can use LDFLAGS

#cgo CFLAGS: -I${SRCDIR}/vendor/headers
#cgo CPPFLAGS: -I${SRCDIR}/vendor/headers
#cgo !windows,!ios,!android pkg-config: opencv4
#cgo CXXFLAGS: --std=c++11
#cgo darwin,amd64,ios LDFLAGS: -F${SRCDIR}/vendor/libs/ios -framework Foundation -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework CoreGraphics -framework opencv2 -miphoneos-version-min=9.0
#cgo darwin,arm64,ios CFLAGS:  -I${SRCDIR}/vendor/headers
#cgo darwin,arm64,ios LDFLAGS: -F${SRCDIR}/vendor/libs/ios -framework Foundation -framework CoreVideo -framework CoreMedia -framework AVFoundation -framework CoreGraphics -framework opencv2 -miphoneos-version-min=9.0
#cgo android LDFLAGS: -lopencv_core -lopencv_imgproc -lz -llog
#cgo android,arm64 LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/arm64-v8a
#cgo android,arm LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/armeabi-v7a
#cgo android,amd64 LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/x86_64
#cgo android,386 LDFLAGS: -L${SRCDIR}/vendor/libs/android/sdk/native/staticlibs/x86

and if you use go mod add this line in go.mod

replace github.com/hybridgroup/go-cv => ../../hybridgroup/go-cv

Hey, thank you for the efforts. That would work if I make that imported lib in vendor folder... But that is exactly what I don't want to do, to change other peoples code.

I am perfectly aware of compiler directives such as #cgo ...