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

Cargo overwrites project target when used as a library #11245

Closed
Nariod opened this issue Oct 16, 2022 · 2 comments
Closed

Cargo overwrites project target when used as a library #11245

Nariod opened this issue Oct 16, 2022 · 2 comments
Labels
C-bug Category: bug

Comments

@Nariod
Copy link

Nariod commented Oct 16, 2022

Problem

When called as a library, cargo is overwriting the build configuration of the target project.

I'm developing a "RustPacker" project which takes another Rust project (created via cargo new), edits some files and then compiles it.
The function responsible for compiling the target is the following:

fn compiler(path_to_cargo_project: &mut PathBuf) -> Result<(), Box<dyn std::error::Error>> {
    path_to_cargo_project.push("Cargo.toml");
    let absolute_toml_path = path_to_cargo_project.absolutize()?;
    let config: Config = Config::default()?;
    let ws = Workspace::new(&absolute_toml_path, &config)?;
    let mut compile_options: CompileOptions = CompileOptions::new(&config , CompileMode::Build)?;
    compile_options.build_config.requested_profile = InternedString::new("release");
    compile(&ws, &compile_options)?;

    Ok(())
}

Even if the target Rust project contains a ".cargo/config" file forcing build target as "x86_64-pc-windows-gnu", cargo attempts to build the project to Linux binary when run an a Linux box.

Steps

  1. On a Linux box : install required dependencies cross-compilation to Windows (gcc, mingw64, Rust...)
  2. git clone https://github.com/Nariod/RustPacker.git
  3. cd RustPacker
  4. Execute cargo run -- -f calc.bin -i ct
  5. Target project fails to compile

Same steps do work when run on a Windows box.

Full Rust backtrace:

nariod@toolbox ~/D/RustPacker (main) [101]> cargo run -- -f calc.bin -i ct
   Compiling RustPacker v0.1.0 (/home/nariod/Dev/RustPacker)
warning: fields `encryption`, `sandbox` and `output` are never read
  --> src/arg_parser.rs:12:5
   |
9  | pub struct Order {
   |            ----- fields in this struct
...
12 |     encryption: Option<Encryption>,
   |     ^^^^^^^^^^
13 |     sandbox: Option<bool>,
   |     ^^^^^^^
14 |     output: Option<String>,
   |     ^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default
   = note: `Order` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: variants `Xor` and `Aes` are never constructed
  --> src/arg_parser.rs:25:5
   |
24 | pub enum Encryption {
   |          ---------- variants in this enum
25 |     Xor,
   |     ^^^
26 |     Aes,
   |     ^^^
   |
   = note: `Encryption` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: crate `RustPacker` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `rust_packer`

warning: `RustPacker` (bin "RustPacker") generated 3 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 4.90s
     Running `target/debug/RustPacker -f calc.bin -i ct`
Entering main function
[+] Parsing arguments..
[+] Done parsing arguments!
[+] Reading binary file..
[+] Done reading binary file!
[+] Assembling Rust code..
[+] Creating output folder: output_ftvzsbkjqpai
[+] Done assembling Rust code!
warning: unused manifest key: build
    Updating crates.io index
   Compiling windows v0.42.0
     Running `rustc --crate-name windows --edition=2018 /home/nariod/.cargo/registry/src/github.com-1ecc6299db9ec823/windows-0.42.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=z -C panic=abort -C linker-plugin-lto -C codegen-units=1 --cfg 'feature="Win32"' --cfg 'feature="Win32_Foundation"' --cfg 'feature="Win32_Security"' --cfg 'feature="Win32_System"' --cfg 'feature="Win32_System_Diagnostics"' --cfg 'feature="Win32_System_Diagnostics_Debug"' --cfg 'feature="Win32_System_Memory"' --cfg 'feature="Win32_System_Threading"' --cfg 'feature="Win32_UI"' --cfg 'feature="Win32_UI_Input"' --cfg 'feature="Win32_UI_Input_Pointer"' --cfg 'feature="default"' -C metadata=e2de51f36e1f5dd4 -C extra-filename=-e2de51f36e1f5dd4 --out-dir /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps -C strip=symbols -L dependency=/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps --cap-lints allow`
   Compiling createThread v0.1.0 (/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai)
     Running `rustc --crate-name createThread --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C opt-level=z -C panic=abort -C lto -C codegen-units=1 -C metadata=e2f2c3dfaf31f328 -C extra-filename=-e2f2c3dfaf31f328 --out-dir /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps -C strip=symbols -L dependency=/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps --extern windows=/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/libwindows-e2de51f36e1f5dd4.rlib`
warning: crate `createThread` should have a snake case name
  |
  = note: `#[warn(non_snake_case)]` on by default
  = help: convert the identifier to snake case: `create_thread`

error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcYvN1dc/symbols.o" "/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps" "-L" "/home/nariod/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,--end-group" "-Wl,-Bstatic" "/home/nariod/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-ff283b4bf550fa1c.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/nariod/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-Wl,--strip-all" "-nodefaultlibs"
  = note: /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `createThread::main':
          createThread.471884d7-cgu.0:(.text._ZN12createThread4main17h2538442e26ffae01E+0x145): undefined reference to `VirtualAlloc'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN12createThread4main17h2538442e26ffae01E+0x175): undefined reference to `VirtualProtect'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN12createThread4main17h2538442e26ffae01E+0x18a): undefined reference to `CreateThread'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN12createThread4main17h2538442e26ffae01E+0x1a0): undefined reference to `WaitForSingleObject'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `windows::core::strings::bstr::BSTR::len':
          createThread.471884d7-cgu.0:(.text._ZN7windows4core7strings4bstr4BSTR3len17h547ef90ef7898e20E+0x8): undefined reference to `SysStringLen'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `<windows::core::error::Error as core::fmt::Debug>::fmt':
          createThread.471884d7-cgu.0:(.text._ZN64_$LT$windows..core..error..Error$u20$as$u20$core..fmt..Debug$GT$3fmt17hccc9a089a744ec55E+0x18d): undefined reference to `FormatMessageW'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `windows::core::heap::heap_free':
          createThread.471884d7-cgu.0:(.text._ZN7windows4core4heap9heap_free17hbfd356c3b50dbd1eE+0x6): undefined reference to `GetProcessHeap'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN7windows4core4heap9heap_free17hbfd356c3b50dbd1eE+0x15): undefined reference to `HeapFree'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `<windows::core::strings::bstr::BSTR as core::ops::drop::Drop>::drop':
          createThread.471884d7-cgu.0:(.text._ZN76_$LT$windows..core..strings..bstr..BSTR$u20$as$u20$core..ops..drop..Drop$GT$4drop17hf709e30b9044ac8fE+0x7): undefined reference to `SysFreeString'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `windows::core::strings::hstring::HSTRING::from_wide':
          createThread.471884d7-cgu.0:(.text._ZN7windows4core7strings7hstring7HSTRING9from_wide17h3c8f0080fe568f2fE+0x23): undefined reference to `GetProcessHeap'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN7windows4core7strings7hstring7HSTRING9from_wide17h3c8f0080fe568f2fE+0x31): undefined reference to `HeapAlloc'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `<windows::core::error::Error as core::convert::From<windows::core::hresult::HRESULT>>::from':
          createThread.471884d7-cgu.0:(.text._ZN106_$LT$windows..core..error..Error$u20$as$u20$core..convert..From$LT$windows..core..hresult..HRESULT$GT$$GT$4from17hb352c7a3d653b3c3E+0x181): undefined reference to `LoadLibraryA'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN106_$LT$windows..core..error..Error$u20$as$u20$core..convert..From$LT$windows..core..hresult..HRESULT$GT$$GT$4from17hb352c7a3d653b3c3E+0x199): undefined reference to `GetProcAddress'
          /usr/bin/ld: createThread.471884d7-cgu.0:(.text._ZN106_$LT$windows..core..error..Error$u20$as$u20$core..convert..From$LT$windows..core..hresult..HRESULT$GT$$GT$4from17hb352c7a3d653b3c3E+0x1bd): undefined reference to `FreeLibrary'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `windows::core::error::GetErrorInfo':
          createThread.471884d7-cgu.0:(.text._ZN7windows4core5error12GetErrorInfo17h6548fa75fd2fb9e5E+0x15): undefined reference to `GetErrorInfo'
          /usr/bin/ld: /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/createThread-e2f2c3dfaf31f328.createThread.471884d7-cgu.0.rcgu.o: in function `windows::core::error::Error::from_win32':
          createThread.471884d7-cgu.0:(.text._ZN7windows4core5error5Error10from_win3217hafd5987e6fcc0f26E+0x3): undefined reference to `GetLastError'
          collect2: error: ld returned 1 exit status
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `createThread` (bin "createThread") generated 1 warning
error: could not compile `createThread` due to previous error; 1 warning emitted

Caused by:
  process didn't exit successfully: `rustc --crate-name createThread --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C opt-level=z -C panic=abort -C lto -C codegen-units=1 -C metadata=e2f2c3dfaf31f328 -C extra-filename=-e2f2c3dfaf31f328 --out-dir /home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps -C strip=symbols -L dependency=/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps --extern windows=/home/nariod/Dev/RustPacker/output_ftvzsbkjqpai/target/release/deps/libwindows-e2de51f36e1f5dd4.rlib` (exit status: 1)
thread 'main' panicked at '1 job failed', src/compiler.rs:27:21
stack backtrace:
   0:     0x564233b39fed - std::backtrace_rs::backtrace::libunwind::trace::h9135f25bc195152c
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x564233b39fed - std::backtrace_rs::backtrace::trace_unsynchronized::h015ee85be510df51
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x564233b39fed - std::sys_common::backtrace::_print_fmt::h5fad03caa9652a2c
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x564233b39fed - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h2b42ca28d244e5c7
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x564233b6319c - core::fmt::write::h401e827d053130ed
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/fmt/mod.rs:1198:17
   5:     0x564233b34d41 - std::io::Write::write_fmt::hffec93268f5cde32
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/io/mod.rs:1672:15
   6:     0x564233b3c005 - std::sys_common::backtrace::_print::h180c4c706ee1d3fb
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x564233b3c005 - std::sys_common::backtrace::print::hd0c35d18765761c9
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x564233b3c005 - std::panicking::default_hook::{{closure}}::h1f023310983bc730
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:295:22
   9:     0x564233b3bd21 - std::panicking::default_hook::h188fec3334afd5be
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:314:9
  10:     0x564233b3c596 - std::panicking::rust_panic_with_hook::hf26e9d4f97b40096
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:698:17
  11:     0x564233b3c487 - std::panicking::begin_panic_handler::{{closure}}::hfab912107608087a
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:588:13
  12:     0x564233b3a4e4 - std::sys_common::backtrace::__rust_end_short_backtrace::h434b685ce8d9965b
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:138:18
  13:     0x564233b3c1b9 - rust_begin_unwind
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:584:5
  14:     0x564232402b83 - core::panicking::panic_fmt::ha6dc7f2ab2479463
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:142:14
  15:     0x564232412968 - RustPacker::compiler::meta_compiler::ha2ba1a72af4d3bc0
                               at /home/nariod/Dev/RustPacker/src/compiler.rs:27:21
  16:     0x5642324045dd - RustPacker::main::h6668d9f06a3c105b
                               at /home/nariod/Dev/RustPacker/src/main.rs:15:5
  17:     0x56423241072b - core::ops::function::FnOnce::call_once::h8212e74aaff01037
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5
  18:     0x56423240f94e - std::sys_common::backtrace::__rust_begin_short_backtrace::h2d09a8407fcaacf2
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/sys_common/backtrace.rs:122:18
  19:     0x56423240f4c1 - std::rt::lang_start::{{closure}}::hc6ed1754b0418ad6
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/rt.rs:145:18
  20:     0x564233b2e4ae - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::hcdfee62722e5e4b8
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:280:13
  21:     0x564233b2e4ae - std::panicking::try::do_call::h84ca51609826746f
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:492:40
  22:     0x564233b2e4ae - std::panicking::try::hd58075e533b8e0cb
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:456:19
  23:     0x564233b2e4ae - std::panic::catch_unwind::h1ebac24d83cb6ce2
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panic.rs:137:14
  24:     0x564233b2e4ae - std::rt::lang_start_internal::{{closure}}::h0145388a1edd1640
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/rt.rs:128:48
  25:     0x564233b2e4ae - std::panicking::try::do_call::h7630182e534a0a32
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:492:40
  26:     0x564233b2e4ae - std::panicking::try::h05b6544f0c6331dc
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:456:19
  27:     0x564233b2e4ae - std::panic::catch_unwind::h77b2ba8fd3309f34
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panic.rs:137:14
  28:     0x564233b2e4ae - std::rt::lang_start_internal::h6612c8a7a6861b8b
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/rt.rs:128:20
  29:     0x56423240f490 - std::rt::lang_start::h9e9abbb21e79c908
                               at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/rt.rs:144:17
  30:     0x5642324046ac - main
  31:     0x7f4304e90550 - __libc_start_call_main
  32:     0x7f4304e90609 - __libc_start_main_impl
  33:     0x564232402ee5 - _start
  34:                0x0 - <unknown>

Possible Solution(s)

As of today, I can circumvent the problem by:

  • compiling on a Windows box, as the expected arch and the host arch are the same
  • call cargo build as an OS command instead of using cargo as a library

Notes

The error was raised when using this project: https://github.com/Nariod/RustPacker

Version

cargo 1.64.0 (387270bc7 2022-09-16)
release: 1.64.0
commit-hash: 387270bc7f446d17869c7f208207c73231d6a252
commit-date: 2022-09-16
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1q)
os: Fedora 36.0.0 [64-bit]
@Nariod Nariod added the C-bug Category: bug label Oct 16, 2022
@weihanglo
Copy link
Member

Config starts probing .cargo/config.toml from current working directory1 and then up to root. This is documented in here. I guess that's why config file from a template never got read. Could you try switching working directory to where the temp template is before calling Config::default?

FWIW, there is a meta issue about config search. #10952 also deals with setting work directory before build gets started.

Footnotes

  1. https://github.com/rust-lang/cargo/blob/afadab23a5ede0d51201379ae87aee7d846926a6/src/cargo/util/config/mod.rs#L314-L314

@Nariod
Copy link
Author

Nariod commented Oct 20, 2022

Config starts probing .cargo/config.toml from current working directory1 and then up to root. This is documented in here. I guess that's why config file from a template never got read. Could you try switching working directory to where the temp template is before calling Config::default?

FWIW, there is a meta issue about config search. #10952 also deals with setting work directory before build gets started.

Thank you very much, the curdir trick worked !

Footnotes

  1. https://github.com/rust-lang/cargo/blob/afadab23a5ede0d51201379ae87aee7d846926a6/src/cargo/util/config/mod.rs#L314-L314

@Nariod Nariod closed this as completed Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants