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

Use CARGO_CFG_TARGET_ENV to determine if the target env ABI is gnu #31

Conversation

MarijnS95
Copy link
Contributor

@MarijnS95 MarijnS95 commented Sep 15, 2023

Fixes #30

The unfortunately named target_cfg used in a cfg() macro / attribute is a host flag (as all other cfg()s) when a build script using this library is compiled for the host. Instead, the ENV of the compile target should be read from an environment variable (just like $TARGET) when this code is being run on the host to figure out what the target environment ABI is.

This causes a cross-compile via xwin (reported in #30) to invoke the glibc compile test even though windows doesn't provide glibc. When cross-compiling to x86_64-pc-windows-msvc target_env will still be "gnu" while CARGO_CFG_TARGET_ENV properly represents msvc.

Aside from this I do think that the compile-check is valid for MSVC, the warning in that error even suggests to include <string.h>, but the test .c file already does exactly this... Perhaps the arguments set up by xwin and/or the sccache wrapper there are incompatible with how we invoke the compiler for this test?

The unfortunately named `target_cfg` used in a `cfg()` macro / attribute
is a *host* flag (as all other `cfg()`s) when a build script _using
this library_ is compiled for the host.  Instead, the `ENV` of the
compile _target_ should be read from an environment variable (just like
`$TARGET`) when this code is being run _on the host_ to figure out what
the target environment ABI is.

This causes a cross-compile via `xwin` (reported in jean-airoldie#30) to invoke the
`glibc` compile test even though windows doesn't provide `glibc`.  When
cross-compiling to `x86_64-pc-windows-msvc` `target_env` will still be
`"gnu"` while `CARGO_CFG_TARGET_ENV` properly represents `msvc`.

Aside from this I do think that the compile-check is valid for MSVC,
the warning in that error even suggests to include `<string.h>`, but the
test `.c` file already does exactly this... Perhaps the arguments set up
by `xwin` and/or the `sccache` wrapper there are incompatible with how
we invoke the compiler for this test?
@MarijnS95 MarijnS95 force-pushed the pull-target-flags-from-environment branch from eeff12c to 1c4d412 Compare September 15, 2023 20:24
@MarijnS95 MarijnS95 changed the title Use CARGO_CFG_TARGET_ENV to determine if the TARGET is gnu Use CARGO_CFG_TARGET_ENV to determine if the _target_ env ABI is gnu Sep 15, 2023
@MarijnS95 MarijnS95 changed the title Use CARGO_CFG_TARGET_ENV to determine if the _target_ env ABI is gnu Use CARGO_CFG_TARGET_ENV to determine if the target env ABI is gnu Sep 15, 2023
@jean-airoldie
Copy link
Owner

The unfortunately named target_cfg used in a cfg() macro / attribute is a host flag (as all other cfg()s) when a build script using this library is compiled for the host. Instead, the ENV of the compile target should be read from an environment variable (just like $TARGET) when this code is being run on the host to figure out what the target environment ABI is.

Nice catch, I wasn't aware of this.

Aside from this I do think that the compile-check is valid for MSVC, the warning in that error even suggests to include <string.h>, but the test .c file already does exactly this...

Interesting, and slightly worrying.

@jean-airoldie
Copy link
Owner

jean-airoldie commented Sep 16, 2023

/home/sven/.cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.6+4.3.4/src/strlcpy.c(5,11): error: call to undeclared library function 'strlcpy' with type 'unsigned long long (char *, const char *, unsigned long long)'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
(void)strlcpy(buf, "a", 1);

The weird part is that the compiler seems to correctly infer the strlcpy signature as taking a size_t (unsigned long long), despite the fact that we provided it with a ambiguous constant integer. From my understanding it would infer that 1 is a int if it didn't have the definition.

I'll merge this, and we'll wait and see if this fixes #30. Its possible that whatever causes our test to be unable to find the <string.h> symbols in #30 might also cause the same issue when libzmq is compiled.

@jean-airoldie jean-airoldie merged commit 21056e9 into jean-airoldie:master Sep 16, 2023
10 checks passed
@MarijnS95 MarijnS95 deleted the pull-target-flags-from-environment branch September 16, 2023 07:28
@MarijnS95
Copy link
Contributor Author

@jean-airoldie not to mention the return type of this function. Perhaps this is not the complete error context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cross-compilation with cargo xwin fails when sccache is enabled with GLIBC>=2.38
2 participants