Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1# shellcheck shell=bash
2
3dotnetConfigurePhase() {
4 echo "Executing dotnetConfigureHook"
5
6 runHook preConfigure
7
8 local -a projectFiles flags runtimeIds
9 concatTo projectFiles dotnetProjectFiles dotnetTestProjectFiles
10 concatTo flags dotnetFlags dotnetRestoreFlags
11 concatTo runtimeIds dotnetRuntimeIds
12
13 if [[ -z ${enableParallelBuilding-} ]]; then
14 flags+=(--disable-parallel)
15 fi
16
17 if [[ -v dotnetSelfContainedBuild ]]; then
18 if [[ -n $dotnetSelfContainedBuild ]]; then
19 flags+=("-p:SelfContained=true")
20 else
21 flags+=("-p:SelfContained=false")
22 fi
23 fi
24
25 dotnetRestore() {
26 local -r projectFile="${1-}"
27 for runtimeId in "${runtimeIds[@]}"; do
28 dotnet restore ${1+"$projectFile"} \
29 -p:ContinuousIntegrationBuild=true \
30 -p:Deterministic=true \
31 -p:NuGetAudit=false \
32 --runtime "$runtimeId" \
33 "${flags[@]}"
34 done
35 }
36
37 if [[ -f .config/dotnet-tools.json || -f dotnet-tools.json ]]; then
38 dotnet tool restore
39 fi
40
41 # dotnetGlobalTool is set in buildDotnetGlobalTool to patch dependencies but
42 # avoid other project-specific logic. This is a hack, but the old behavior
43 # is worse as it relied on a bug: setting projectFile to an empty string
44 # made the hooks actually skip all project-specific logic. It’s hard to keep
45 # backwards compatibility with this odd behavior now since we are using
46 # arrays, so instead we just pass a variable to indicate that we don’t have
47 # projects.
48 if [[ -z ${dotnetGlobalTool-} ]]; then
49 if (( ${#projectFiles[@]} == 0 )); then
50 dotnetRestore
51 fi
52
53 local projectFile
54 for projectFile in "${projectFiles[@]}"; do
55 dotnetRestore "$projectFile"
56 done
57 fi
58
59 runHook postConfigure
60
61 echo "Finished dotnetConfigureHook"
62}
63
64if [[ -z "${dontDotnetConfigure-}" && -z "${configurePhase-}" ]]; then
65 configurePhase=dotnetConfigurePhase
66fi
67
68dotnetBuildPhase() {
69 echo "Executing dotnetBuildHook"
70
71 runHook preBuild
72
73 local -r dotnetBuildType="${dotnetBuildType-Release}"
74
75 local -a projectFiles flags runtimeIds
76 concatTo projectFiles dotnetProjectFiles dotnetTestProjectFiles
77 concatTo flags dotnetFlags dotnetBuildFlags
78 concatTo runtimeIds dotnetRuntimeIds
79
80 if [[ -n "${enableParallelBuilding-}" ]]; then
81 local -r maxCpuFlag="$NIX_BUILD_CORES"
82 local -r parallelBuildFlag="true"
83 else
84 local -r maxCpuFlag="1"
85 local -r parallelBuildFlag="false"
86 fi
87
88 if [[ -v dotnetSelfContainedBuild ]]; then
89 if [[ -n $dotnetSelfContainedBuild ]]; then
90 flags+=("-p:SelfContained=true")
91 else
92 flags+=("-p:SelfContained=false")
93 fi
94 fi
95
96 if [[ -n ${dotnetUseAppHost-} ]]; then
97 flags+=("-p:UseAppHost=true")
98 fi
99
100 if [[ -n ${version-} ]]; then
101 flags+=("-p:InformationalVersion=$version")
102 fi
103
104 if [[ -n ${versionForDotnet-} ]]; then
105 flags+=("-p:Version=$versionForDotnet")
106 fi
107
108 dotnetBuild() {
109 local -r projectFile="${1-}"
110
111 for runtimeId in "${runtimeIds[@]}"; do
112 local runtimeIdFlags=()
113 if [[ $projectFile == *.csproj || -n ${dotnetSelfContainedBuild-} ]]; then
114 runtimeIdFlags+=("--runtime" "$runtimeId")
115 fi
116
117 dotnet build ${1+"$projectFile"} \
118 -maxcpucount:"$maxCpuFlag" \
119 -p:BuildInParallel="$parallelBuildFlag" \
120 -p:ContinuousIntegrationBuild=true \
121 -p:Deterministic=true \
122 -p:OverwriteReadOnlyFiles=true \
123 --configuration "$dotnetBuildType" \
124 --no-restore \
125 "${runtimeIdFlags[@]}" \
126 "${flags[@]}"
127 done
128 }
129
130 if (( ${#projectFiles[@]} == 0 )); then
131 dotnetBuild
132 fi
133
134 local projectFile
135 for projectFile in "${projectFiles[@]}"; do
136 dotnetBuild "$projectFile"
137 done
138
139 runHook postBuild
140
141 echo "Finished dotnetBuildHook"
142}
143
144if [[ -z ${dontDotnetBuild-} && -z ${buildPhase-} ]]; then
145 buildPhase=dotnetBuildPhase
146fi
147
148dotnetCheckPhase() {
149 echo "Executing dotnetCheckHook"
150
151 runHook preCheck
152
153 local -r dotnetBuildType="${dotnetBuildType-Release}"
154
155 local -a projectFiles testProjectFiles testFilters disabledTests flags runtimeIds runtimeDeps
156 concatTo projectFiles dotnetProjectFiles
157 concatTo testProjectFiles dotnetTestProjectFiles
158 concatTo testFilters dotnetTestFilters
159 concatTo disabledTests dotnetDisabledTests
160 concatTo flags dotnetFlags dotnetTestFlags
161 concatTo runtimeIds dotnetRuntimeIds
162 concatTo runtimeDeps dotnetRuntimeDeps
163
164 if (( ${#disabledTests[@]} > 0 )); then
165 local disabledTestsFilters=("${disabledTests[@]/#/FullyQualifiedName!=}")
166 testFilters=( "${testFilters[@]}" "${disabledTestsFilters[@]//,/%2C}" )
167 fi
168
169 if (( ${#testFilters[@]} > 0 )); then
170 local OLDIFS="$IFS" IFS='&'
171 flags+=("--filter:${testFilters[*]}")
172 IFS="$OLDIFS"
173 fi
174
175 local libraryPath="${LD_LIBRARY_PATH-}"
176 if (( ${#runtimeDeps[@]} > 0 )); then
177 local libraryPaths=("${runtimeDeps[@]/%//lib}")
178 local OLDIFS="$IFS" IFS=':'
179 libraryPath="${libraryPaths[*]}${libraryPath:+':'}$libraryPath"
180 IFS="$OLDIFS"
181 fi
182
183 if [[ -n ${enableParallelBuilding-} ]]; then
184 local -r maxCpuFlag="$NIX_BUILD_CORES"
185 else
186 local -r maxCpuFlag="1"
187 fi
188
189 local projectFile runtimeId
190 for projectFile in "${testProjectFiles[@]-${projectFiles[@]}}"; do
191 for runtimeId in "${runtimeIds[@]}"; do
192 local runtimeIdFlags=()
193 if [[ $projectFile == *.csproj ]]; then
194 runtimeIdFlags=("--runtime" "$runtimeId")
195 fi
196
197 LD_LIBRARY_PATH=$libraryPath \
198 dotnet test "$projectFile" \
199 -maxcpucount:"$maxCpuFlag" \
200 -p:ContinuousIntegrationBuild=true \
201 -p:Deterministic=true \
202 --configuration "$dotnetBuildType" \
203 --no-restore \
204 --no-build \
205 --logger "console;verbosity=normal" \
206 "${runtimeIdFlags[@]}" \
207 "${flags[@]}"
208 done
209 done
210
211 runHook postCheck
212
213 echo "Finished dotnetCheckHook"
214}
215
216if [[ -z "${dontDotnetCheck-}" && -z "${checkPhase-}" ]]; then
217 checkPhase=dotnetCheckPhase
218fi
219
220# For compatibility, convert makeWrapperArgs to an array unless we are using
221# structured attributes. That is, we ensure that makeWrapperArgs is always an
222# array.
223# See https://github.com/NixOS/nixpkgs/blob/858f4db3048c5be3527e183470e93c1a72c5727c/pkgs/build-support/dotnet/build-dotnet-module/hooks/dotnet-fixup-hook.sh#L1-L3
224# and https://github.com/NixOS/nixpkgs/pull/313005#issuecomment-2175482920
225# shellcheck disable=2206
226if [[ -z $__structuredAttrs ]]; then
227 makeWrapperArgs=( ${makeWrapperArgs-} )
228fi
229
230# First argument is the executable you want to wrap,
231# the second is the destination for the wrapper.
232wrapDotnetProgram() {
233 local -r dotnetRuntime=@dotnetRuntime@
234 local -r wrapperPath=@wrapperPath@
235
236 # shellcheck disable=2016
237 local -r dotnetFromEnvScript='dotnetFromEnv() {
238 local dotnetPath
239 if command -v dotnet 2>&1 >/dev/null; then
240 dotnetPath=$(which dotnet) && \
241 dotnetPath=$(realpath "$dotnetPath") && \
242 dotnetPath=$(dirname "$dotnetPath") && \
243 export DOTNET_ROOT="$dotnetPath"
244 fi
245}
246dotnetFromEnv'
247
248 # shellcheck disable=2206
249 local -a runtimeDeps
250 concatTo runtimeDeps dotnetRuntimeDeps
251
252 local wrapperFlags=()
253 if (( ${#runtimeDeps[@]} > 0 )); then
254 local libraryPath=("${dotnetRuntimeDeps[@]/%//lib}")
255 local OLDIFS="$IFS" IFS=':'
256 wrapperFlags+=("--suffix" "LD_LIBRARY_PATH" ":" "${libraryPath[*]}")
257 IFS="$OLDIFS"
258 fi
259
260 if [[ -z ${dotnetSelfContainedBuild-} ]]; then
261 if [[ -n ${useDotnetFromEnv-} ]]; then
262 # if dotnet CLI is available, set DOTNET_ROOT based on it. Otherwise set to default .NET runtime
263 wrapperFlags+=("--suffix" "PATH" ":" "$wrapperPath")
264 wrapperFlags+=("--run" "$dotnetFromEnvScript")
265 if [[ -n $dotnetRuntime ]]; then
266 wrapperFlags+=("--set-default" "DOTNET_ROOT" "$dotnetRuntime/share/dotnet")
267 wrapperFlags+=("--suffix" "PATH" ":" "$dotnetRuntime/bin")
268 fi
269 elif [[ -n $dotnetRuntime ]]; then
270 wrapperFlags+=("--set" "DOTNET_ROOT" "$dotnetRuntime/share/dotnet")
271 wrapperFlags+=("--prefix" "PATH" ":" "$dotnetRuntime/bin")
272 fi
273 fi
274
275 # shellcheck disable=2154
276 makeWrapper "$1" "$2" \
277 "${wrapperFlags[@]}" \
278 "${gappsWrapperArgs[@]}" \
279 "${makeWrapperArgs[@]}"
280
281 echo "installed wrapper to $2"
282}
283
284dotnetFixupPhase() {
285 local -r dotnetInstallPath="${dotnetInstallPath-$out/lib/$pname}"
286
287 local executable executableBasename
288
289 # check if dotnetExecutables is declared (including empty values, in which case we generate no executables)
290 # shellcheck disable=2154
291 if declare -p dotnetExecutables &>/dev/null; then
292 # shellcheck disable=2206
293 local -a executables
294 concatTo executables dotnetExecutables
295 for executable in "${executables[@]}"; do
296 executableBasename=$(basename "$executable")
297
298 local path="$dotnetInstallPath/$executable"
299
300 if test -x "$path"; then
301 wrapDotnetProgram "$path" "$out/bin/$executableBasename"
302 else
303 echo "Specified binary \"$executable\" is either not an executable or does not exist!"
304 echo "Looked in $path"
305 exit 1
306 fi
307 done
308 else
309 while IFS= read -r -d '' executable; do
310 executableBasename=$(basename "$executable")
311 wrapDotnetProgram "$executable" "$out/bin/$executableBasename" \;
312 done < <(find "$dotnetInstallPath" ! -name "*.dll" -executable -type f -print0)
313 fi
314}
315
316if [[ -z "${dontFixup-}" && -z "${dontDotnetFixup-}" ]]; then
317 appendToVar preFixupPhases dotnetFixupPhase
318fi
319
320dotnetInstallPhase() {
321 echo "Executing dotnetInstallHook"
322
323 runHook preInstall
324
325 local -r dotnetInstallPath="${dotnetInstallPath-$out/lib/$pname}"
326 local -r dotnetBuildType="${dotnetBuildType-Release}"
327
328 local -a projectFiles flags installFlags packFlags runtimeIds
329 concatTo projectFiles dotnetProjectFiles
330 concatTo flags dotnetFlags
331 concatTo installFlags dotnetInstallFlags
332 concatTo packFlags dotnetPackFlags
333 concatTo runtimeIds dotnetRuntimeIds
334
335 if [[ -v dotnetSelfContainedBuild ]]; then
336 if [[ -n $dotnetSelfContainedBuild ]]; then
337 installFlags+=("--self-contained")
338 else
339 installFlags+=("--no-self-contained")
340 # https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained
341 # Trimming is only available for self-contained build, so force disable it here
342 installFlags+=("-p:PublishTrimmed=false")
343 fi
344 fi
345
346 if [[ -n ${dotnetUseAppHost-} ]]; then
347 installFlags+=("-p:UseAppHost=true")
348 fi
349
350 if [[ -n ${enableParallelBuilding-} ]]; then
351 local -r maxCpuFlag="$NIX_BUILD_CORES"
352 else
353 local -r maxCpuFlag="1"
354 fi
355
356 dotnetPublish() {
357 local -r projectFile="${1-}"
358
359 for runtimeId in "${runtimeIds[@]}"; do
360 runtimeIdFlags=()
361 if [[ $projectFile == *.csproj || -n ${dotnetSelfContainedBuild-} ]]; then
362 runtimeIdFlags+=("--runtime" "$runtimeId")
363 fi
364
365 dotnet publish ${1+"$projectFile"} \
366 -maxcpucount:"$maxCpuFlag" \
367 -p:ContinuousIntegrationBuild=true \
368 -p:Deterministic=true \
369 -p:OverwriteReadOnlyFiles=true \
370 --output "$dotnetInstallPath" \
371 --configuration "$dotnetBuildType" \
372 --no-restore \
373 --no-build \
374 "${runtimeIdFlags[@]}" \
375 "${flags[@]}" \
376 "${installFlags[@]}"
377 done
378 }
379
380 dotnetPack() {
381 local -r projectFile="${1-}"
382
383 for runtimeId in "${runtimeIds[@]}"; do
384 dotnet pack ${1+"$projectFile"} \
385 -maxcpucount:"$maxCpuFlag" \
386 -p:ContinuousIntegrationBuild=true \
387 -p:Deterministic=true \
388 -p:OverwriteReadOnlyFiles=true \
389 --output "$out/share/nuget/source" \
390 --configuration "$dotnetBuildType" \
391 --no-restore \
392 --no-build \
393 --runtime "$runtimeId" \
394 "${flags[@]}" \
395 "${packFlags[@]}"
396 done
397 }
398
399 if (( ${#projectFiles[@]} == 0 )); then
400 dotnetPublish
401 else
402 local projectFile
403 for projectFile in "${projectFiles[@]}"; do
404 dotnetPublish "$projectFile"
405 done
406 fi
407
408 if [[ -n ${packNupkg-} ]]; then
409 if (( ${#projectFiles[@]} == 0 )); then
410 dotnetPack
411 else
412 local projectFile
413 for projectFile in "${projectFiles[@]}"; do
414 dotnetPack "$projectFile"
415 done
416 fi
417 fi
418
419 runHook postInstall
420
421 echo "Finished dotnetInstallHook"
422}
423
424if [[ -z "${dontDotnetInstall-}" && -z "${installPhase-}" ]]; then
425 installPhase=dotnetInstallPhase
426fi