Merge pull request #182273 from mdarocha/dotnet-self-contained-build

buildDotnetModule: add option to make a self-contained build
This commit is contained in:
Sandro 2022-07-28 11:59:09 +02:00 committed by GitHub
commit f358b0d40d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 2 deletions

View file

@ -87,6 +87,7 @@ To package Dotnet applications, you can use `buildDotnetModule`. This has simila
* `executables` is used to specify which executables get wrapped to `$out/bin`, relative to `$out/lib/$pname`. If this is unset, all executables generated will get installed. If you do not want to install any, set this to `[]`. This gets done in the `preFixup` phase. * `executables` is used to specify which executables get wrapped to `$out/bin`, relative to `$out/lib/$pname`. If this is unset, all executables generated will get installed. If you do not want to install any, set this to `[]`. This gets done in the `preFixup` phase.
* `runtimeDeps` is used to wrap libraries into `LD_LIBRARY_PATH`. This is how dotnet usually handles runtime dependencies. * `runtimeDeps` is used to wrap libraries into `LD_LIBRARY_PATH`. This is how dotnet usually handles runtime dependencies.
* `buildType` is used to change the type of build. Possible values are `Release`, `Debug`, etc. By default, this is set to `Release`. * `buildType` is used to change the type of build. Possible values are `Release`, `Debug`, etc. By default, this is set to `Release`.
* `selfContainedBuild` allows to enable the [self-contained](https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained) build flag. By default, it is set to false and generated applications have a dependency on the selected dotnet runtime. If enabled, the dotnet runtime is bundled into the executable and the built app has no dependency on Dotnet.
* `dotnet-sdk` is useful in cases where you need to change what dotnet SDK is being used. * `dotnet-sdk` is useful in cases where you need to change what dotnet SDK is being used.
* `dotnet-runtime` is useful in cases where you need to change what dotnet runtime is being used. This can be either a regular dotnet runtime, or an aspnetcore. * `dotnet-runtime` is useful in cases where you need to change what dotnet runtime is being used. This can be either a regular dotnet runtime, or an aspnetcore.
* `dotnet-test-sdk` is useful in cases where unit tests expect a different dotnet SDK. By default, this is set to the `dotnet-sdk` attribute. * `dotnet-test-sdk` is useful in cases where unit tests expect a different dotnet SDK. By default, this is set to the `dotnet-sdk` attribute.

View file

@ -55,6 +55,8 @@
# The type of build to perform. This is passed to `dotnet` with the `--configuration` flag. Possible values are `Release`, `Debug`, etc. # The type of build to perform. This is passed to `dotnet` with the `--configuration` flag. Possible values are `Release`, `Debug`, etc.
, buildType ? "Release" , buildType ? "Release"
# If set to true, builds the application as a self-contained - removing the runtime dependency on dotnet
, selfContainedBuild ? false
# The dotnet SDK to use. # The dotnet SDK to use.
, dotnet-sdk ? dotnetCorePackages.sdk_6_0 , dotnet-sdk ? dotnetCorePackages.sdk_6_0
# The dotnet runtime to use. # The dotnet runtime to use.

View file

@ -14,6 +14,12 @@ dotnetBuildHook() {
parallelBuildFlag="false" parallelBuildFlag="false"
fi fi
if [ "${selfContainedBuild-}" ]; then
dotnetBuildFlags+=("--self-contained")
else
dotnetBuildFlags+=("--no-self-contained")
fi
if [ "${version-}" ]; then if [ "${version-}" ]; then
versionFlag="-p:Version=${version-}" versionFlag="-p:Version=${version-}"
fi fi

View file

@ -4,8 +4,12 @@ makeWrapperArgs=( ${makeWrapperArgs-} )
# First argument is the executable you want to wrap, # First argument is the executable you want to wrap,
# the second is the destination for the wrapper. # the second is the destination for the wrapper.
wrapDotnetProgram() { wrapDotnetProgram() {
if [ ! "${selfContainedBuild-}" ]; then
dotnetRootFlag=("--set" "DOTNET_ROOT" "@dotnetRuntime@")
fi
makeWrapper "$1" "$2" \ makeWrapper "$1" "$2" \
--set "DOTNET_ROOT" "@dotnetRuntime@" \ "${dotnetRootFlag[@]}" \
--suffix "LD_LIBRARY_PATH" : "@runtimeDeps@" \ --suffix "LD_LIBRARY_PATH" : "@runtimeDeps@" \
"${gappsWrapperArgs[@]}" \ "${gappsWrapperArgs[@]}" \
"${makeWrapperArgs[@]}" "${makeWrapperArgs[@]}"

View file

@ -6,6 +6,12 @@ dotnetInstallHook() {
runHook preInstall runHook preInstall
if [ "${selfContainedBuild-}" ]; then
dotnetInstallFlags+=("--self-contained")
else
dotnetInstallFlags+=("--no-self-contained")
fi
for project in ${projectFile[@]}; do for project in ${projectFile[@]}; do
env \ env \
dotnet publish "$project" \ dotnet publish "$project" \
@ -15,7 +21,6 @@ dotnetInstallHook() {
--output "$out/lib/${pname}" \ --output "$out/lib/${pname}" \
--configuration "@buildType@" \ --configuration "@buildType@" \
--no-build \ --no-build \
--no-self-contained \
${dotnetInstallFlags[@]} \ ${dotnetInstallFlags[@]} \
${dotnetFlags[@]} ${dotnetFlags[@]}
done done