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

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

Open
dpanic opened this issue Sep 5, 2021 · 5 comments
Open

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

dpanic opened this issue Sep 5, 2021 · 5 comments
Labels
mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dpanic
Copy link

dpanic commented Sep 5, 2021

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
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label Sep 5, 2021
@gopherbot gopherbot added this to the Unreleased milestone Sep 5, 2021
@dpanic
Copy link
Author

dpanic commented Sep 6, 2021

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
Copy link
Author

dpanic commented Sep 8, 2021

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

Implement CGO_LDFLAGS_%GOOS_%ARCH with fallback to CGO_LDFLAGS.

Examples:

  • CGO_LDFLAGS_ANDROID_ARM
  • CGO_LDFLAGS_LINUX_X86
  • CGO_LDFLAGS_ANDROID_X86_64

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 14, 2021
@cagedmantis
Copy link
Contributor

/cc @hyangah

@kedric
Copy link

kedric commented Oct 5, 2021

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
Copy link
Author

dpanic commented Oct 5, 2021

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 ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mobile Android, iOS, and x/mobile NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants