rust-cross / cargo-zigbuild

Compile Cargo project with zig as linker
MIT License
1.43k stars 51 forks source link

Can't build libgit2-sys crate when specify x86_64-unknown-linux-gnu target from x86_64-unknown-linux-gnu host #159

Closed nui closed 1 year ago

nui commented 1 year ago

Got error while building with cargo zigbuild --release --target x86_64-unknown-linux-gnu in docker, but it works fine when use cargo zigbuild --release (not specify target)

It seems like this is a problem when host system is x86_64-unknown-linux-gnu and you build for this system. Building from apple m1 to x86_64-unknown-linux-gnu work fine.

I have included minimal reproducable example with dockerfile here, https://github.com/nui/cross-libgit2-issue

You can run docker build https://github.com/nui/cross-libgit2-issue.git to see full log.

#9 6.946   cargo:warning=In file included from libgit2/src/libgit2/transports/local.c:10:
#9 6.946 
#9 6.946   cargo:warning=In file included from libgit2/src/libgit2/pack-objects.h:17:
#9 6.946 
#9 6.946   cargo:warning=libgit2/src/util/zstream.h:12:10: fatal error: 'zlib.h' file not found
#9 6.946 
#9 6.946   cargo:warning=#include <zlib.h>
#9 6.946 
#9 6.946   cargo:warning=         ^~~~~~~~
#9 6.946 
#9 6.946   cargo:warning=In file included from libgit2/src/libgit2/transports/smart_protocol.c:17:
#9 6.946 
#9 6.946   cargo:warning=In file included from libgit2/src/libgit2/pack-objects.h:17:
#9 6.946 
#9 6.946   cargo:warning=libgit2/src/util/zstream.h:12:10: fatal error: 'zlib.h' file not found
#9 6.946 
#9 6.946   cargo:warning=#include <zlib.h>
#9 6.946 
#9 6.946   cargo:warning=         ^~~~~~~~
#9 6.946 
#9 6.946   cargo:warning=libgit2/src/libgit2/transports/smart_pkt.c:322:26: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion]
#9 6.946 
#9 6.946   cargo:warning=        data->seen_capabilities = 1;
#9 6.946 
#9 6.946   cargo:warning=                                ^ ~
#9 6.946 
#9 6.946   cargo:warning=1 error generated.
#9 6.946 
#9 6.946   cargo:warning=1 error generated.
#9 6.946 
#9 6.946   running: "/root/.cache/cargo-zigbuild/0.17.0/zigcc-x86_64-unknown-linux-gnu.sh" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/include" "-I" "libgit2/src/libgit2" "-I" "libgit2/src/util" "-I" "libgit2/deps/http-parser" "-I" "libgit2/deps/pcre" "-fvisibility=hidden" "-DGIT_REGEX_BUILTIN=1" "-DHAVE_STDINT_H=1" "-DHAVE_MEMMOVE=1" "-DNO_RECURSE=1" "-DNEWLINE=10" "-DPOSIX_MALLOC_THRESHOLD=10" "-DLINK_SIZE=2" "-DPARENS_NEST_LIMIT=250" "-DMATCH_LIMIT=10000000" "-DMATCH_LIMIT_RECURSION=MATCH_LIMIT" "-DMAX_NAME_SIZE=32" "-DMAX_NAME_COUNT=10000" "-DSHA1DC_NO_STANDARD_INCLUDES=1" "-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\"" "-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\"" "-o" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/build/libgit2/src/libgit2/streams/stransport.o" "-c" "libgit2/src/libgit2/streams/stransport.c"
#9 6.946   exit status: 0
#9 6.946   exit status: 0
#9 6.946   exit status: 0
#9 6.946   exit status: 0
#9 6.946   exit status: 1
#9 6.946   cargo:warning=ToolExecError: Command "/root/.cache/cargo-zigbuild/0.17.0/zigcc-x86_64-unknown-linux-gnu.sh" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/include" "-I" "libgit2/src/libgit2" "-I" "libgit2/src/util" "-I" "libgit2/deps/http-parser" "-I" "libgit2/deps/pcre" "-fvisibility=hidden" "-DGIT_REGEX_BUILTIN=1" "-DHAVE_STDINT_H=1" "-DHAVE_MEMMOVE=1" "-DNO_RECURSE=1" "-DNEWLINE=10" "-DPOSIX_MALLOC_THRESHOLD=10" "-DLINK_SIZE=2" "-DPARENS_NEST_LIMIT=250" "-DMATCH_LIMIT=10000000" "-DMATCH_LIMIT_RECURSION=MATCH_LIMIT" "-DMAX_NAME_SIZE=32" "-DMAX_NAME_COUNT=10000" "-DSHA1DC_NO_STANDARD_INCLUDES=1" "-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\"" "-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\"" "-o" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/build/libgit2/src/libgit2/transports/local.o" "-c" "libgit2/src/libgit2/transports/local.c" with args "zigcc-x86_64-unknown-linux-gnu.sh" did not execute successfully (status code exit status: 1).
#9 6.946   exit status: 1
#9 6.946   cargo:warning=ToolExecError: Command "/root/.cache/cargo-zigbuild/0.17.0/zigcc-x86_64-unknown-linux-gnu.sh" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/include" "-I" "libgit2/src/libgit2" "-I" "libgit2/src/util" "-I" "libgit2/deps/http-parser" "-I" "libgit2/deps/pcre" "-fvisibility=hidden" "-DGIT_REGEX_BUILTIN=1" "-DHAVE_STDINT_H=1" "-DHAVE_MEMMOVE=1" "-DNO_RECURSE=1" "-DNEWLINE=10" "-DPOSIX_MALLOC_THRESHOLD=10" "-DLINK_SIZE=2" "-DPARENS_NEST_LIMIT=250" "-DMATCH_LIMIT=10000000" "-DMATCH_LIMIT_RECURSION=MATCH_LIMIT" "-DMAX_NAME_SIZE=32" "-DMAX_NAME_COUNT=10000" "-DSHA1DC_NO_STANDARD_INCLUDES=1" "-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\"" "-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\"" "-o" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/build/libgit2/src/libgit2/transports/smart_protocol.o" "-c" "libgit2/src/libgit2/transports/smart_protocol.c" with args "zigcc-x86_64-unknown-linux-gnu.sh" did not execute successfully (status code exit status: 1).running: "/root/.cache/cargo-zigbuild/0.17.0/zigcc-x86_64-unknown-linux-gnu.sh" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/include" "-I" "libgit2/src/libgit2" "-I" "libgit2/src/util" "-I" "libgit2/deps/http-parser" "-I" "libgit2/deps/pcre" "-fvisibility=hidden" "-DGIT_REGEX_BUILTIN=1" "-DHAVE_STDINT_H=1" "-DHAVE_MEMMOVE=1" "-DNO_RECURSE=1" "-DNEWLINE=10" "-DPOSIX_MALLOC_THRESHOLD=10" "-DLINK_SIZE=2" "-DPARENS_NEST_LIMIT=250" "-DMATCH_LIMIT=10000000" "-DMATCH_LIMIT_RECURSION=MATCH_LIMIT" "-DMAX_NAME_SIZE=32" "-DMAX_NAME_COUNT=10000" "-DSHA1DC_NO_STANDARD_INCLUDES=1" "-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\"" "-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\"" "-o" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/build/libgit2/src/libgit2/streams/tls.o" "-c" "libgit2/src/libgit2/streams/tls.c"
#9 6.946   running: "/root/.cache/cargo-zigbuild/0.17.0/zigcc-x86_64-unknown-linux-gnu.sh" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-I" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/include" "-I" "libgit2/src/libgit2" "-I" "libgit2/src/util" "-I" "libgit2/deps/http-parser" "-I" "libgit2/deps/pcre" "-fvisibility=hidden" "-DGIT_REGEX_BUILTIN=1" "-DHAVE_STDINT_H=1" "-DHAVE_MEMMOVE=1" "-DNO_RECURSE=1" "-DNEWLINE=10" "-DPOSIX_MALLOC_THRESHOLD=10" "-DLINK_SIZE=2" "-DPARENS_NEST_LIMIT=250" "-DMATCH_LIMIT=10000000" "-DMATCH_LIMIT_RECURSION=MATCH_LIMIT" "-DMAX_NAME_SIZE=32" "-DMAX_NAME_COUNT=10000" "-DSHA1DC_NO_STANDARD_INCLUDES=1" "-DSHA1DC_CUSTOM_INCLUDE_SHA1_C=\"common.h\"" "-DSHA1DC_CUSTOM_INCLUDE_UBC_CHECK_C=\"common.h\"" "-o" "/workspace/target/x86_64-unknown-linux-gnu/release/build/libgit2-sys-fa328674ae2e267f/out/build/libgit2/deps/http-parser/http_parser.o" "-c" "libgit2/deps/http-parser/http_parser.c"
nui commented 1 year ago

Weird thing about this issue is that it works fine when not specify build target.

messense commented 1 year ago

If you don't specify build target, it's not cross compiling so it's gonna link with system zlib which generally works.

For cross compiling, you should static link zlib. See https://github.com/rust-lang/libz-sys/blob/0bb7bd4dbc31c7125a72fce3dcfa266658b7078f/Cargo.toml#L39-L66.

nui commented 1 year ago

Oh thanks for pointing me to correct direction.

This comment is for future rust people if you get hit by this problem.

Add this line to you Cargo.toml to enable static link zlib

libz-sys = { version = "1.1.12", features = ["static"] }