NethermindEth / juno

Starknet client implementation.
https://juno.nethermind.io
Apache License 2.0
409 stars 174 forks source link

Crosscompiling binaries not working #235

Closed stdevMac closed 2 years ago

stdevMac commented 2 years ago

Describe the bug When we are trying to build the binaries using cross-compilation, we cant

To Reproduce Steps to reproduce the behavior:

  1. run ./scripts/go-executable-build.sh github.com/NethermindEth/juno/cmd/juno

You will get an error similar to this:

go build github.com/torquem-ch/mdbx-go/mdbx: build constraints exclude all Go files in /Users/maceo/go/pkg/mod/github.com/torquem-ch/mdbx-go@v0.24.2/mdbx

However, in those architectures, without cross-compilation, the building works without issues.

Expected behavior Build a set of binaries for the different architectures that we support

smishy05 commented 2 years ago

My machine - Ubuntu 22.04 (linux/amd64) Successful crosscompile - linux/amd64, linux/arm64, windows/amd64 Unsuccessful crosscompile - windows/386, darwin/amd64, arm/64

Reason for this issue

mdbx uses C for its working due to which we have to enable CGO while crosscompiling. This makes crosscompilation complicated and I had to use toolchain for each of the configuration.

Now, I will describe the procedure for each of them.

linux/amd64

Not much of an issue (cos my machine has the same configuration). Command was:

env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o ./.dist/juno-linux-amd64 github.com/NethermindEth/juno/cmd/juno

If you are not using the same configuration, then I believe, you will have to use this command:

env CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-pc-linux-gcc go build -o ./.dist/juno-linux-amd64 github.com/NethermindEth/juno/cmd/juno

linux/arm64

You will need a toolchain for doing this and it is aarch64-linux-gnu-gcc. Therefore, you will have to install it using the command:

sudo apt-get install gcc-aarch64-linux-gnu

After you are done with this, use the following command for crosscompilation:

env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go build -o ./.dist/juno-linux-arm64 github.com/NethermindEth/juno/cmd/juno

You will get the binary for this configuration in the .dist folder.

windows/amd64

Just like the previous case, you will need a toolchain. Use this command:

sudo apt-get install gcc-mingw-w64-x86-64

Then, use this command for installation: After you are done with this, use the following command for crosscompilation:

env CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -o ./.dist/juno-windows-amd64.exe github.com/NethermindEth/juno/cmd/juno

I was successful again with this.

windows/386

I failed here. I used this link. So, I first installed the toolchain using:

sudo apt-get install gcc-mingw-w64-i686

Then, I tried crosscompiling using the command:

env CGO_ENABLED=1 GOOS=windows GOARCH=386 CC=i686-w64-mingw32-gcc go build -o ./.dist/juno-windows-386.exe github.com/NethermindEth/juno/cmd/juno

But this time I got an error saying this:

# github.com/torquem-ch/mdbx-go/mdbx
mdbx.c: In function ‘mdbx_cursor_get_batch’:
mdbx.c:18477:10: error: ‘EINVAL’ undeclared (first use in this function)
18477 |     rc = EINVAL;
      |          ^~~~~~
mdbx.c:18477:10: note: each undeclared identifier is reported only once for each function it appears in
An error has occurred! Aborting the script execution...

I am not sure how to resolve this because this seems to be an issue in mdbx. Any advice would be welcome.

darwin/amd64

After the error in Windows, I thought of trying out darwin and thankfully, it turned out to be the most complex one. If you run it without the toolchain, then you would get this error:

# runtime/cgo
gcc: error: unrecognized command-line option '-arch'
An error has occurred! Aborting the script execution...

Then, I followed the instructions from here. But, when I tried running the script to get the sdk:

## Building xar (master) ##

From https://github.com/tpoechtrager/xar
 * branch            master     -> FETCH_HEAD
Already on 'master'
Your branch is up to date with 'origin/master'.
From https://github.com/tpoechtrager/xar
 * branch            master     -> FETCH_HEAD
Already up to date.

## Nothing to do ##

## Building pbzx (master) ##

From https://github.com/tpoechtrager/pbzx
 * branch            master     -> FETCH_HEAD
Already on 'master'
Your branch is up to date with 'origin/master'.
From https://github.com/tpoechtrager/pbzx
 * branch            master     -> FETCH_HEAD
Already up to date.
clang -O2 -Wall -I /home/shailesh1998/Downloads/osxcross-master/target/include -L /home/shailesh1998/Downloads/osxcross-master/target/lib pbzx.c -o /home/shailesh1998/Downloads/osxcross-master/target/SDK/tools/bin/pbzx -llzma -lxar -Wl,-rpath,/home/shailesh1998/Downloads/osxcross-master/target/lib
/usr/bin/ld: warning: libcrypto.so.1.1, needed by /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libicui18n.so.58, needed by /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libicuuc.so.58, needed by /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libicudata.so.58, needed by /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_MD_type@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_MD_CTX_md@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_get_digestbyname@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_MD_CTX_new@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_MD_CTX_free@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_DigestInit_ex@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_MD_size@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `OPENSSL_init_crypto@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_DigestUpdate@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `EVP_DigestFinal_ex@OPENSSL_1_1_0'
/usr/bin/ld: /home/shailesh1998/Downloads/osxcross-master/target/lib/libxar.so: undefined reference to `OBJ_nid2ln@OPENSSL_1_1_0'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Couldn't find anything for this. But the author of this answer was kind enough to put this. I thought since now I have the required executables, I would be able to do it. I went with the command:

env CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC=/home/shailesh1998/Downloads/osxcross-target-master/bin/o64-clang CXX=/home/shailesh1998/Downloads/osxcross-target-master/bin/o64-clang++ go build -o ./.dist/juno-darwin-amd64 github.com/NethermindEth/juno/cmd/juno

But, this didn't work out again:

# runtime/cgo
/usr/bin/ld: unrecognized option '-dynamic'
/usr/bin/ld: use the --help option for usage information
clang: error: linker command failed with exit code 1 (use -v to see invocation)
An error has occurred! Aborting the script execution...

Then, I found out goreleaser and this tutorial. But again, I ended up with this:

⨯ release failed after 41.23s error=failed to build for darwin_arm64: exit status 2: # runtime/cgo
gcc: error: unrecognized command-line option '-arch'

Then, I found out zig. This failed with windows/386 and I couldn't try it with darwin because it needs the sdk (link) which I could not generate.

For darwin, I tried installing llvm and reinstalling clang but nothing worked out. I have hit a dead end while trying to resolve the errors that I am facing :( .

darwin/arm64

Same as darwin/amd64

Any comments on the errors and possible directions would be extremely helpful!