Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
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
119search_for_bin_path() {
120 # heuristic to "guess" the correct source file as found in cargo:
121 # https://github.com/rust-lang/cargo/blob/90fc9f620190d5fa3c80b0c8c65a1e1361e6b8ae/src/cargo/util/toml/targets.rs#L308-L325
122
123 BIN_NAME=$1
124 BIN_NAME_=$(echo $BIN_NAME | tr '-' '_')
125
126 # the first two cases are the "new" default IIRC
127 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" )
128
129 if ! [ -e "$LIB_PATH" -o -e src/lib.rs -o -e "src/$LIB_NAME.rs" ]; then
130 # if this is not a library the following path is also valid
131 FILES=( "src/$BIN_NAME.rs" "src/$BIN_NAME_.rs" "${FILES[@]}" )
132 fi
133
134 for file in "${FILES[@]}";
135 do
136 echo "checking file $file"
137 # first file that exists wins
138 if [[ -e "$file" ]]; then
139 BIN_PATH="$file"
140 break
141 fi
142 done
143
144 if [[ -z "$BIN_PATH" ]]; then
145 echo_error "ERROR: failed to find file for binary target: $BIN_NAME" >&2
146 exit 1
147 fi
148}
149
150# Extracts cargo_toml_path of the matching crate.
151matching_cargo_toml_path() {
152 local manifest_path="$1"
153 local expected_crate_name="$2"
154
155 # If the Cargo.toml is not a workspace root,
156 # it will only contain one package in ".packages"
157 # because "--no-deps" suppressed dependency resolution.
158 #
159 # But to make it more general, we search for a matching
160 # crate in all packages and use the manifest path that
161 # is referenced there.
162 cargo metadata --no-deps --format-version 1 \
163 --manifest-path "$manifest_path" \
164 | jq -r '.packages[]
165 | select( .name == "'$expected_crate_name'")
166 | .manifest_path'
167}
168
169# Find a Cargo.toml in the current or any sub directory
170# with a matching crate name.
171matching_cargo_toml_dir() {
172 local expected_crate_name="$1"
173
174 find -L -name Cargo.toml | sort | while read manifest_path; do
175 echo "...checking manifest_path $manifest_path" >&2
176 local matching_path="$(matching_cargo_toml_path "$manifest_path" "$expected_crate_name")"
177 if [ -n "${matching_path}" ]; then
178 echo "$(dirname $matching_path)"
179 break
180 fi
181 done
182}