diff options
author | Andreas Rammhold <andreas@rammhold.de> | 2018-09-06 15:03:13 +0200 |
---|---|---|
committer | Andreas Rammhold <andreas@rammhold.de> | 2018-09-13 20:28:39 +0200 |
commit | 0c50140da5bcc55c588ac64bfcedac4699a43711 (patch) | |
tree | 9129a962ea5611f2d5f5f94d5f169974692bdb4a /pkgs/build-support/rust/build-rust-crate.nix | |
parent | ee21f64f6a6c555418905288a5ee2c072531d4f9 (diff) | |
download | nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.tar nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.tar.gz nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.tar.bz2 nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.tar.lz nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.tar.xz nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.tar.zst nixlib-0c50140da5bcc55c588ac64bfcedac4699a43711.zip |
buildRustCrate: add heuristic to picking the right source files
Cargo has a few odd (old) ways of picking source files if the `bin.path` attribute isn't given in the Cargo.toml. This commit adds support for some of those. The previous behaviour always defaulted to `src/main.rs` which was not always the right choice. Since there is look-ahead into the unpacked sources before running the actual builder the path selection logic has to be embedded within the build script. `buildRustCrate` currently supports two ways of running building binaries when processing a crate: - Explicit definition of all the binaries (& optionally the paths to their respective `main.rs`) and, - if not binary was explictly configured all files matching the patterns `src/main.rs`, `src/bin/*.rs`. When the explicit list is given without path information paths are now being picked from a list of candidates. The first match wins. The order is the same as within the cargo compatibility code. If the crate does not provide any libraries the path `src/{bin_name}.rs` is also considered. All underscores within the binary names are translated into dashes (`-`) before the lookups are made. This seems to be a common convention.
Diffstat (limited to 'pkgs/build-support/rust/build-rust-crate.nix')
-rw-r--r-- | pkgs/build-support/rust/build-rust-crate.nix | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/pkgs/build-support/rust/build-rust-crate.nix b/pkgs/build-support/rust/build-rust-crate.nix index 6605aa27b21e..cfdf38958a8d 100644 --- a/pkgs/build-support/rust/build-rust-crate.nix +++ b/pkgs/build-support/rust/build-rust-crate.nix @@ -282,14 +282,48 @@ let makeDeps = dependencies: tr '\n' ' ' < target/link > target/link_ LINK=$(cat target/link_) - fi + fi mkdir -p target/bin - echo "${crateBin}" | sed -n 1'p' | tr ',' '\n' | while read BIN; do - if [[ ! -z "$BIN" ]]; then - build_bin $BIN - fi + printf "%s\n" "${crateBin}" | head -n1 | tr -s ',' '\n' | while read -r BIN_NAME BIN_PATH; do + # filter empty entries / empty "lines" + if [[ -z "$BIN_NAME" ]]; then + continue + fi + + if [[ -z "$BIN_PATH" ]]; then + # heuristic to "guess" the correct source file as found in cargo: + # https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325 + + # the first two cases are the "new" default IIRC + BIN_NAME_=$(echo $BIN_NAME | sed -e 's/-/_/g') + FILES="src/bin/$BIN_NAME_.rs src/bin/$BIN_NAME_/main.rs src/bin/main.rs src/main.rs" + + if ! [ -e "${libPath}" -o -e src/lib.rs -o -e "src/${libName}.rs" ]; then + # if this is not a library the following path is also valid + FILES="src/$BIN_NAME_.rs $FILES" + fi + + echo $FILES + for file in $FILES; + do + echo "checking file $file" + # first file that exists wins + if [[ -e "$file" ]]; then + BIN_PATH="$file" + break + fi + done + + if [[ -z "$BIN_PATH" ]]; then + echo "failed to find file for binary target: $BIN_NAME" >&2 + exit 1 + fi + fi + build_bin $BIN_NAME $BIN_PATH done + + ${lib.optionalString (crateBin == "") '' if [[ -e src/main.rs ]]; then build_bin ${crateName} src/main.rs @@ -388,11 +422,10 @@ stdenv.mkDerivation (rec { metadata = builtins.substring 0 10 (builtins.hashString "sha256" (crateName + "-" + crateVersion + "___" + toString crateFeatures + "___" + depsMetadata )); crateBin = if crate ? crateBin then - builtins.foldl' (bins: bin: - let name = - lib.strings.replaceStrings ["-"] ["_"] - (if bin ? name then bin.name else crateName); - path = if bin ? path then bin.path else "src/main.rs"; + builtins.foldl' (bins: bin: let + _name = (if bin ? name then bin.name else crateName); + name = lib.strings.replaceStrings ["-"] ["_"] _name; + path = if bin ? path then bin.path else ""; in bins + (if bin == "" then "" else ",") + "${name} ${path}" |