go-sdl2 icon indicating copy to clipboard operation
go-sdl2 copied to clipboard

android arm64 support

Open Lundis opened this issue 2 years ago • 15 comments

It seems that android arm64 (AKA aarch64 / arm64-v8a) is the standard now (meaning you cant technically use this library for android). I suppose adding support for it is as "simple" as https://github.com/veandco/go-sdl2/commit/cd80a890a1854d8ec41d8815948b110ac7691709

I would do it myself but I'm guessing you already have SDL cross-compiling set up (which does not exactly seem trivial according to SDL's docs). Seeing how I have no experience compiling C libraries I'm not sure I would figure it out in any reasonable timeframe. Anyhow if you are able to somewhat easily do this, I will do the testing of it for you, and also update the android example (which is really outdated).

If you don't have time for this, maybe you could point me in the right direction?

Lundis avatar Mar 27 '22 15:03 Lundis

I just found https://github.com/veandco/sdl2-static-library-build-scripts

I can probably work with that.

Lundis avatar Mar 28 '22 10:03 Lundis

Ahh sorry, I haven't managed to finalize the scripts. I will push what I have now tomorrow so it will be more up-to-date.

veeableful avatar Mar 28 '22 10:03 veeableful

No worries. I'm thinking that library might be good for me to practise my github actions skills. If you would be interested in an automated solution? I have gained some experience with docker recently which I could put to use to have github produce the static libs.

I noticed that it's set up for the older android NDKs though, which I am not sure if supported arm64 (new ones use LLVM and is much easier to configure). In any case I can take care of updating that at the same time

Lundis avatar Mar 28 '22 10:03 Lundis

That would be extremely helpful! I have met a couple stumbling blocks in building for android and darwin arm64 architecture so I greatly appreciate any help on those matters!

veeableful avatar Mar 28 '22 10:03 veeableful

I started working on it: https://github.com/veandco/sdl2-static-library-build-scripts/pull/1 Now it should be possible to build using that container. No time for that today though. I'll add some instructions also later on.

Lundis avatar Mar 28 '22 18:03 Lundis

Thanks @Lundis! I have also pushed the current state of the repository which may have affected the list of packages installed in the Dockerfile.

veeableful avatar Mar 29 '22 14:03 veeableful

Hey there! Any progress on this? I was trying to build an SDL app for an aarch64 linux embedded system and getting an error:

# error "No ABI matched"

I'm assuming this is because there are no SDL libs for this platform. I'm happy to compile them myself if there are some instructions, but I haven't been able to find any.

n-i-x avatar Sep 01 '22 02:09 n-i-x

Hi @n-i-x, you could try downloading the latest SDL2 library from GitHub at https://github.com/libsdl-org/SDL/releases

After extracting it, you could build it like this:

cd SDL2-2.24.0
mkdir build
cd build
../configure --prefix=$HOME/.local
make -j`nproc`
make install

and then make sure pkg-config can find it by setting the environment variable:

export PKG_CONFIG_PATH="$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH" # perhaps put this in .profile or .bashrc
pkg-config --cflags sdl2 # to test if pkg-config can find it

After that, I believe you should be able to use the binding.

veeableful avatar Sep 01 '22 03:09 veeableful

I don't think that'll let me statically compile though will it? I have all the .a files from the device I'm targeting and tried adding them to the repo... but it couldn't find them. Not sure what I missed.

n-i-x avatar Sep 04 '22 03:09 n-i-x

FYI, I tried compiling SDL 2.0.7 (the version included in the embedded device) using your instructions and then building one of the examples and I get the following error:

$ GOOS=linux GOARCH=arm64 go build main.go
# command-line-arguments
./main.go:6:16: undefined: sdl.Init
./main.go:6:25: undefined: sdl.INIT_EVERYTHING
./main.go:9:12: undefined: sdl.Quit
./main.go:11:21: undefined: sdl.CreateWindow
./main.go:11:46: undefined: sdl.WINDOWPOS_UNDEFINED
./main.go:12:17: undefined: sdl.WINDOW_SHOWN
./main.go:24:14: undefined: sdl.Rect
./main.go:30:20: undefined: sdl.PollEvent

I've tried go mod tidy and go get -v github.com/veandco/go-sdl2/sdl and it sill gives me that error. If I don't try to cross-compile then it works fine.

n-i-x avatar Sep 04 '22 03:09 n-i-x

@n-i-x That is because you need to explicitly enable cgo with CGO_ENABLED=1, Go will by default disable cgo when ever you try to cross-compile. It is a common error when one wants to cross-compile a cgo project just like that.

When you add CGO_ENABLED, the next error will probably be something that tells you that you must use the C compiler/toolchain for the target architecture, i.e. to set CC. Btw. you must also compile SDL with the same toolchain, not with your host CC.

gen2brain avatar Sep 04 '22 07:09 gen2brain

Okay I tried all of that except compiling sdl with the aarch64 gcc I’ll try that next and report back.

n-i-x avatar Sep 04 '22 14:09 n-i-x

$ export PKG_CONFIG_PATH="$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH"
$ pkg-config --cflags sdl2
-D_REENTRANT -I/config/.local/include/SDL2

Then when trying to cross-compile I get the following:

$ CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 go build -tags static -ldflags "-s -w"
<snip>
/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/bin/ld: /tmp/go-link-1068906841/000042.o: in function `delHintCallback':
/config/go/pkg/mod/github.com/veandco/[email protected]/sdl/hints.c:18: undefined reference to `SDL_DelHintCallback'
/usr/lib/gcc-cross/aarch64-linux-gnu/9/../../../../aarch64-linux-gnu/bin/ld: /tmp/go-link-1068906841/000043.o: in function `LogSetOutputFunction':
/config/go/pkg/mod/github.com/veandco/[email protected]/sdl/log.c:5: undefined reference to `SDL_LogSetOutputFunction'
collect2: error: ld returned 1 exit status

Is there something I need to do to make sure cgo can see the the libs in ~/.local? I have PKG_CONFIG_PATH="$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH" exported. I'm also trying to compile SDL 2.0.7 which is the SDL included in the embedded device I'm working with, would I need to use a more updated SDL? I would assume if I am statically compiling against those SDL libs the SDL version wouldn't matter, so I'll try that next.

n-i-x avatar Sep 04 '22 17:09 n-i-x

I also wonder if I just need to compile SDL -image,-mixer,-ttf,-gfx to get all the references needed by the bindings... I'll make my way through compiling those libs for aarch64 as well. I really appreciate you helping me through this.

n-i-x avatar Sep 04 '22 17:09 n-i-x

Hi @n-i-x, the SDL2 build instructions written above are for dynamic compilation as we don't have static libraries for android arm64 yet. There shouldn't be a need to install the other SDL2 libraries such as SDL2_image if only sdl is imported.

All SDL2 versions should also work with the binding according to the Github Actions test but perhaps there's special case with android arm64. If so, could you let me know how to reproduce your environment?

veeableful avatar Sep 06 '22 14:09 veeableful