cargo icon indicating copy to clipboard operation
cargo copied to clipboard

config.toml support [env] with [env.<cfg>]

Open eric5f3759df opened this issue 3 years ago • 15 comments

Problem

some build need arch assotiated targe environment var. like rusty_v8 RUSTY_V8_ARCHIVE, so it's usefull for [env] supporting multi arch.

Proposed Solution

No response

Notes

No response

eric5f3759df avatar Jan 08 '22 14:01 eric5f3759df

I guess cargo maintainers here possibly have little knowledge about V8 and Deno stuff. It would be great if you can provide more contexts, such like

  • What exactly you're trying to resolve. A concrete example and links are great.
  • Why the feature should be built-in Cargo.
  • What the current solution/workaround looks like without this feature.

This would help not only maintainers but other random contributors to come up with better ideas. 😁

weihanglo avatar Jan 11 '22 17:01 weihanglo

eg:

[env]
RUSTY_V8_ARCHIVE = { value = "assets/rusty_v8_mirror/v0.42.0/librusty_v8_release_aarch64-apple-darwin.a", relative = true }

[env.'cfg(target_os="android")']
RUSTY_V8_ARCHIVE = { value = "assets/rusty_v8_mirror/v0.42.0/librusty_v8_release_aarch64-linux-android.a", relative = true }

Gaubee avatar Apr 28 '22 01:04 Gaubee

I have the same problem but with openssl crate. We use prebuilt versions on openssl libs and different platforms/architectures have different folders.

So ideally I'd need something like this:

[env.'cfg(target_os = "macos")']
OPENSSL_STATIC = "1"
OPENSSL_DIR = { value = "../PrecompiledLibs/MacOS/OpenSSL/openssl-1.1.1l/release", relative = true }

[env.'cfg(target = "aarch64-linux-android")']
OPENSSL_STATIC = "1"
OPENSSL_DIR = { value = "../PrecompiledLibs/Android/OpenSSL/openssl-1.1.1l/release", relative = true }

# etc for windows and so on

MatrixDev avatar Oct 20 '22 15:10 MatrixDev

Similar issue here using BoringSSL ... I wanted to set environment variables differently for each target platform. Details here: https://github.com/rust-lang/cargo/issues/11385

nmittler avatar Nov 17 '22 04:11 nmittler

IMHO it would be much much better if we could just setup all these configs (including most of the cargo.toml) from the rs script (similar to what gradle does). This would simplify non-trivial configuration so much....

Because currently instead of "everything is rust" it is "everything is rust hidden behind toml and shell scripts" :(

MatrixDev avatar Nov 17 '22 08:11 MatrixDev

if we could just setup all these configs…

For configuring environment variables, it's available today in build script with cargo:rustc-env=VAR=VALUE.

weihanglo avatar Nov 17 '22 08:11 weihanglo

@weihanglo, correct me if I'm wrong, but does it propagate to the libraries before their build.rs is executed?

MatrixDev avatar Nov 17 '22 09:11 MatrixDev

@weihanglo Not really. This does not propagate to dependencies, so you can not just define OPENSSL_DIR from a build.rs.

oblique avatar Nov 17 '22 09:11 oblique

THank you all. That is absolutely the limitation of build script today. Configuration is the current way to deal with that.

weihanglo avatar Nov 17 '22 09:11 weihanglo

@weihanglo, you can't deal with that with configuration, because configuration doesn't accept cfg params. please read my comment above from 28 days ago.

MatrixDev avatar Nov 17 '22 09:11 MatrixDev

Sorry for not being clear. I didn't mean Cargo already support the feature described in this issue.

Let me summarize:

  • Build script can set environment variables but only for the crate it associates with.
  • Configuration can set environment variables for all rustc invocations but cannot differentiate cfg(…).

weihanglo avatar Nov 17 '22 09:11 weihanglo

@weihanglo makes sense. Do we have a path forward on this then?

nmittler avatar Nov 17 '22 14:11 nmittler

Any update on this or a suggestion of where changes would need to be made to implement this functionality?

mikemorris avatar Jul 05 '23 17:07 mikemorris

As a workaround, I use a Makefile that defines the needed enivironment variables with the command. In my case, I need to choose which OpenCV libraries to link against.

# Makefile
all: test cross

test:
	OPENCV_LINK_LIBS=opencv_core,opencv_imgproc,opencv_highgui OPENCV_LINK_PATHS=/usr/local/lib/ OPENCV_INCLUDE_PATHS=/usr/local/include/opencv4/ cargo test

cross:
	OPENCV_LINK_LIBS=opencv_core,opencv_imgproc,opencv_highgui OPENCV_LINK_PATHS=/home/cendo/aarch64-unknown-linux-gnu/lib/ OPENCV_INCLUDE_PATHS=/home/cendo/aarch64-unknown-linux-gnu/include/opencv4/ cross build --target aarch64-unknown-linux-gnu

.PHONY: test cross

Kamicosi avatar Mar 20 '24 17:03 Kamicosi

Another workaround:

Set the RUSTC_WRAPPER environment variable or the build.rustc-wrapper Cargo config variable to point to a custom shell script:

#!/bin/zsh

target_option_idx=${@[(Ie)--target]}

if [[ ${target_option_idx} -ne 0 ]]; then
    target_value_idx=$((target_option_idx + 1))
    target_value=$@[target_value_idx]

    if [[ "${target_value}" = "<triple>" ]]; then
        exec /usr/bin/env <TARGET_ENV_VAR>=<TARGET_ENV_VALUE> "$@"
    fi
fi

exec /usr/bin/env "$@"

Kentzo avatar May 10 '24 03:05 Kentzo