nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at devShellTools-shell 188 lines 5.2 kB view raw
1echo_build_heading() { 2 if (( $# == 1 )); then 3 echo_colored "Building $1" 4 else 5 echo_colored "Building $1 ($2)" 6 fi 7} 8 9build_lib() { 10 lib_src=$1 11 echo_build_heading $lib_src ${libName} 12 13 noisily rustc \ 14 --crate-name $CRATE_NAME \ 15 $lib_src \ 16 --out-dir target/lib \ 17 -L dependency=target/deps \ 18 --cap-lints allow \ 19 $LINK \ 20 $EXTRA_LINK_ARGS \ 21 $EXTRA_LINK_ARGS_LIB \ 22 $LIB_RUSTC_OPTS \ 23 $BUILD_OUT_DIR \ 24 $EXTRA_BUILD \ 25 $EXTRA_FEATURES \ 26 $EXTRA_RUSTC_FLAGS \ 27 --color $colors 28 29 EXTRA_LIB=" --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-$metadata.rlib" 30 if [ -e target/deps/lib$CRATE_NAME-$metadata$LIB_EXT ]; then 31 EXTRA_LIB="$EXTRA_LIB --extern $CRATE_NAME=target/lib/lib$CRATE_NAME-$metadata$LIB_EXT" 32 fi 33} 34 35build_bin() { 36 local crate_name=$1 37 local crate_name_=$(echo $crate_name | tr '-' '_') 38 local main_file="" 39 40 if [[ ! -z $2 ]]; then 41 main_file=$2 42 fi 43 echo_build_heading $@ 44 noisily rustc \ 45 --crate-name $crate_name_ \ 46 $main_file \ 47 --crate-type bin \ 48 $BIN_RUSTC_OPTS \ 49 --out-dir target/bin \ 50 -L dependency=target/deps \ 51 $LINK \ 52 $EXTRA_LINK_ARGS \ 53 $EXTRA_LINK_ARGS_BINS \ 54 $EXTRA_LIB \ 55 --cap-lints allow \ 56 $BUILD_OUT_DIR \ 57 $EXTRA_BUILD \ 58 $EXTRA_FEATURES \ 59 $EXTRA_RUSTC_FLAGS \ 60 --color ${colors} \ 61 62 if [ "$crate_name_" != "$crate_name" ]; then 63 mv target/bin/$crate_name_ target/bin/$crate_name 64 fi 65} 66 67build_lib_test() { 68 local file="$1" 69 EXTRA_RUSTC_FLAGS="--test $EXTRA_RUSTC_FLAGS" build_lib "$1" "$2" 70} 71 72build_bin_test() { 73 local crate="$1" 74 local file="$2" 75 EXTRA_RUSTC_FLAGS="--test $EXTRA_RUSTC_FLAGS" build_bin "$1" "$2" 76} 77 78build_bin_test_file() { 79 local file="$1" 80 local derived_crate_name="${file//\//_}" 81 # Make sure to strip the top level `tests` directory: see #204051. Note that 82 # a forward slash has now become an underscore due to the substitution 83 # above. 84 derived_crate_name=${derived_crate_name#"tests_"} 85 derived_crate_name="${derived_crate_name%.rs}" 86 build_bin_test "$derived_crate_name" "$file" 87} 88 89# Add additional link options that were provided by the build script. 90setup_link_paths() { 91 EXTRA_LIB="" 92 if [[ -e target/link_ ]]; then 93 EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD" 94 fi 95 96 echo "$EXTRA_LINK_SEARCH" | while read i; do 97 if [[ ! -z "$i" ]]; then 98 for library in $i; do 99 echo "-L $library" >> target/link 100 L=$(echo $library | sed -e "s#$(pwd)/target/build#$lib/lib#") 101 echo "-L $L" >> target/link.final 102 done 103 fi 104 done 105 echo "$EXTRA_LINK_LIBS" | while read i; do 106 if [[ ! -z "$i" ]]; then 107 for library in $i; do 108 echo "-l $library" >> target/link 109 done 110 fi 111 done 112 113 if [[ -e target/link ]]; then 114 tr '\n' ' ' < target/link > target/link_ 115 LINK=$(cat target/link_) 116 fi 117 118 # Add "rustc-cdylib-link-arg" as linker arguments 119 # https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-cdylib-link-arg 120 if [[ -n "$CRATE_TYPE_IS_CDYLIB" ]]; then 121 EXTRA_BUILD+=" $EXTRA_CDYLIB_LINK_ARGS" 122 fi 123} 124 125search_for_bin_path() { 126 # heuristic to "guess" the correct source file as found in cargo: 127 # https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325 128 129 BIN_NAME=$1 130 BIN_NAME_=$(echo $BIN_NAME | tr '-' '_') 131 132 # the first two cases are the "new" default IIRC 133 FILES=( "src/bin/$BIN_NAME.rs" "src/bin/$BIN_NAME/main.rs" "src/bin/$BIN_NAME_.rs" "src/bin/$BIN_NAME_/main.rs" "src/bin/main.rs" "src/main.rs" ) 134 135 if ! [ -e "$LIB_PATH" -o -e src/lib.rs -o -e "src/$LIB_NAME.rs" ]; then 136 # if this is not a library the following path is also valid 137 FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "${FILES[@]}" ) 138 fi 139 140 for file in "${FILES[@]}"; 141 do 142 echo "checking file $file" 143 # first file that exists wins 144 if [[ -e "$file" ]]; then 145 BIN_PATH="$file" 146 break 147 fi 148 done 149 150 if [[ -z "$BIN_PATH" ]]; then 151 echo_error "ERROR: failed to find file for binary target: $BIN_NAME" >&2 152 exit 1 153 fi 154} 155 156# Extracts cargo_toml_path of the matching crate. 157matching_cargo_toml_path() { 158 local manifest_path="$1" 159 local expected_crate_name="$2" 160 161 # If the Cargo.toml is not a workspace root, 162 # it will only contain one package in ".packages" 163 # because "--no-deps" suppressed dependency resolution. 164 # 165 # But to make it more general, we search for a matching 166 # crate in all packages and use the manifest path that 167 # is referenced there. 168 cargo metadata --no-deps --format-version 1 \ 169 --manifest-path "$manifest_path" \ 170 | jq -r '.packages[] 171 | select( .name == "'$expected_crate_name'") 172 | .manifest_path' 173} 174 175# Find a Cargo.toml in the current or any sub directory 176# with a matching crate name. 177matching_cargo_toml_dir() { 178 local expected_crate_name="$1" 179 180 find -L -name Cargo.toml | sort | while read manifest_path; do 181 echo "...checking manifest_path $manifest_path" >&2 182 local matching_path="$(matching_cargo_toml_path "$manifest_path" "$expected_crate_name")" 183 if [ -n "${matching_path}" ]; then 184 echo "$(dirname $matching_path)" 185 break 186 fi 187 done 188}