nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 192 lines 5.4 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 if [ -f "target/bin/$crate_name_.wasm" ]; then 64 mv target/bin/$crate_name_.wasm target/bin/$crate_name.wasm 65 else 66 mv target/bin/$crate_name_ target/bin/$crate_name 67 fi 68 fi 69} 70 71build_lib_test() { 72 local file="$1" 73 EXTRA_RUSTC_FLAGS="--test $EXTRA_RUSTC_FLAGS" build_lib "$1" "$2" 74} 75 76build_bin_test() { 77 local crate="$1" 78 local file="$2" 79 EXTRA_RUSTC_FLAGS="--test $EXTRA_RUSTC_FLAGS" build_bin "$1" "$2" 80} 81 82build_bin_test_file() { 83 local file="$1" 84 local derived_crate_name="${file//\//_}" 85 # Make sure to strip the top level `tests` directory: see #204051. Note that 86 # a forward slash has now become an underscore due to the substitution 87 # above. 88 derived_crate_name=${derived_crate_name#"tests_"} 89 derived_crate_name="${derived_crate_name%.rs}" 90 build_bin_test "$derived_crate_name" "$file" 91} 92 93# Add additional link options that were provided by the build script. 94setup_link_paths() { 95 EXTRA_LIB="" 96 if [[ -e target/link_ ]]; then 97 EXTRA_BUILD="$(cat target/link_) $EXTRA_BUILD" 98 fi 99 100 echo "$EXTRA_LINK_SEARCH" | while read i; do 101 if [[ ! -z "$i" ]]; then 102 for library in $i; do 103 echo "-L $library" >> target/link 104 L=$(echo $library | sed -e "s#$(pwd)/target/build#$lib/lib#") 105 echo "-L $L" >> target/link.final 106 done 107 fi 108 done 109 echo "$EXTRA_LINK_LIBS" | while read i; do 110 if [[ ! -z "$i" ]]; then 111 for library in $i; do 112 echo "-l $library" >> target/link 113 done 114 fi 115 done 116 117 if [[ -e target/link ]]; then 118 tr '\n' ' ' < target/link > target/link_ 119 LINK=$(cat target/link_) 120 fi 121 122 # Add "rustc-cdylib-link-arg" as linker arguments 123 # https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-cdylib-link-arg 124 if [[ -n "$CRATE_TYPE_IS_CDYLIB" ]]; then 125 EXTRA_BUILD+=" $EXTRA_CDYLIB_LINK_ARGS" 126 fi 127} 128 129search_for_bin_path() { 130 # heuristic to "guess" the correct source file as found in cargo: 131 # https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325 132 133 BIN_NAME=$1 134 BIN_NAME_=$(echo $BIN_NAME | tr '-' '_') 135 136 # the first two cases are the "new" default IIRC 137 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" ) 138 139 if ! [ -e "$LIB_PATH" -o -e src/lib.rs -o -e "src/$LIB_NAME.rs" ]; then 140 # if this is not a library the following path is also valid 141 FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "${FILES[@]}" ) 142 fi 143 144 for file in "${FILES[@]}"; 145 do 146 echo "checking file $file" 147 # first file that exists wins 148 if [[ -e "$file" ]]; then 149 BIN_PATH="$file" 150 break 151 fi 152 done 153 154 if [[ -z "$BIN_PATH" ]]; then 155 echo_error "ERROR: failed to find file for binary target: $BIN_NAME" >&2 156 exit 1 157 fi 158} 159 160# Extracts cargo_toml_path of the matching crate. 161matching_cargo_toml_path() { 162 local manifest_path="$1" 163 local expected_crate_name="$2" 164 165 # If the Cargo.toml is not a workspace root, 166 # it will only contain one package in ".packages" 167 # because "--no-deps" suppressed dependency resolution. 168 # 169 # But to make it more general, we search for a matching 170 # crate in all packages and use the manifest path that 171 # is referenced there. 172 cargo metadata --no-deps --format-version 1 \ 173 --manifest-path "$manifest_path" \ 174 | jq -r '.packages[] 175 | select( .name == "'$expected_crate_name'") 176 | .manifest_path' 177} 178 179# Find a Cargo.toml in the current or any sub directory 180# with a matching crate name. 181matching_cargo_toml_dir() { 182 local expected_crate_name="$1" 183 184 find -L -name Cargo.toml | sort | while read manifest_path; do 185 echo "...checking manifest_path $manifest_path" >&2 186 local matching_path="$(matching_cargo_toml_path "$manifest_path" "$expected_crate_name")" 187 if [ -n "${matching_path}" ]; then 188 echo "$(dirname $matching_path)" 189 break 190 fi 191 done 192}