diff options
Diffstat (limited to 'nixpkgs/pkgs/build-support/dart/build-dart-application/hooks')
5 files changed, 202 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-build-hook.sh b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-build-hook.sh new file mode 100644 index 000000000000..23ebfbd6e66e --- /dev/null +++ b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-build-hook.sh @@ -0,0 +1,34 @@ +# shellcheck shell=bash + +# Outputs line-separated "${dest}\t${source}" +_getDartEntryPoints() { + if [ -n "$dartEntryPoints" ]; then + @jq@ -r '(to_entries | map(.key + "\t" + .value) | join("\n"))' "$dartEntryPoints" + else + # The pubspec executables section follows the pattern: + # <output-bin-name>: [source-file-name] + # Where source-file-name defaults to output-bin-name if omited + @yq@ -r '(.executables | to_entries | map("bin/" + .key + "\t" + "bin/" + (.value // .key) + ".dart") | join("\n"))' pubspec.yaml + fi +} + +dartBuildHook() { + echo "Executing dartBuildHook" + + runHook preBuild + + while IFS=$'\t' read -ra target; do + dest="${target[0]}" + src="${target[1]}" + eval "$dartCompileCommand" "$dartOutputType" \ + -o "$dest" "${dartCompileFlags[@]}" "$src" "${dartJitFlags[@]}" + done < <(_getDartEntryPoints) + + runHook postBuild + + echo "Finished dartBuildHook" +} + +if [ -z "${dontDartBuild-}" ] && [ -z "${buildPhase-}" ]; then + buildPhase=dartBuildHook +fi diff --git a/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh new file mode 100644 index 000000000000..50754a7b56d4 --- /dev/null +++ b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-config-hook.sh @@ -0,0 +1,70 @@ +# shellcheck shell=bash + +dartConfigHook() { + echo "Executing dartConfigHook" + + echo "Setting up SDK" + eval "$sdkSetupScript" + + echo "Installing dependencies" + mkdir -p .dart_tool + cp "$packageConfig" .dart_tool/package_config.json + + packagePath() { + jq --raw-output --arg name "$1" '.packages.[] | select(.name == $name) .rootUri | sub("file://"; "")' .dart_tool/package_config.json + } + + # Runs a Dart executable from a package with a custom path. + # + # Usage: + # packageRunCustom <package> [executable] [bin_dir] + # + # By default, [bin_dir] is "bin", and [executable] is <package>. + # i.e. `packageRunCustom build_runner` is equivalent to `packageRunCustom build_runner build_runner bin`, which runs `bin/build_runner.dart` from the build_runner package. + packageRunCustom() { + local args=() + local passthrough=() + + while [ $# -gt 0 ]; do + if [ "$1" != "--" ]; then + args+=("$1") + shift + else + shift + passthrough=("$@") + break + fi + done + + local name="${args[0]}" + local path="${args[1]:-$name}" + local prefix="${args[2]:-bin}" + + dart --packages=.dart_tool/package_config.json "$(packagePath "$name")/$prefix/$path.dart" "${passthrough[@]}" + } + + # Runs a Dart executable from a package. + # + # Usage: + # packageRun <package> [-e executable] [...] + # + # To run an executable from an unconventional location, use packageRunCustom. + packageRun() { + local name="$1" + shift + + local executableName="$name" + if [ "$1" = "-e" ]; then + shift + executableName="$1" + shift + fi + + fileName="$(@yq@ --raw-output --arg name "$executableName" '.executables.[$name] // $name' "$(packagePath "$name")/pubspec.yaml")" + packageRunCustom "$name" "$fileName" -- "$@" + } + + echo "Finished dartConfigHook" +} + +postConfigureHooks+=(dartConfigHook) diff --git a/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-fixup-hook.sh b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-fixup-hook.sh new file mode 100644 index 000000000000..60bd74871c92 --- /dev/null +++ b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-fixup-hook.sh @@ -0,0 +1,35 @@ +# shellcheck shell=bash + +dartFixupHook() { + echo "Executing dartFixupHook" + + declare -a wrapProgramArgs + + # Add runtime library dependencies to the LD_LIBRARY_PATH. + # For some reason, the RUNPATH of the executable is not used to load dynamic libraries in dart:ffi with DynamicLibrary.open(). + # + # This could alternatively be fixed with patchelf --add-needed, but this would cause all the libraries to be opened immediately, + # which is not what application authors expect. + APPLICATION_LD_LIBRARY_PATH="" + for runtimeDependency in "${runtimeDependencies[@]}"; do + addToSearchPath APPLICATION_LD_LIBRARY_PATH "${runtimeDependency}/lib" + done + if [[ ! -z "$APPLICATION_LD_LIBRARY_PATH" ]]; then + wrapProgramArgs+=(--suffix LD_LIBRARY_PATH : \"$APPLICATION_LD_LIBRARY_PATH\") + fi + + if [[ ! -z "$extraWrapProgramArgs" ]]; then + wrapProgramArgs+=("$extraWrapProgramArgs") + fi + + if [ ${#wrapProgramArgs[@]} -ne 0 ]; then + for f in "$out"/bin/*; do + echo "Wrapping $f..." + eval "wrapProgram \"$f\" ${wrapProgramArgs[@]}" + done + fi + + echo "Finished dartFixupHook" +} + +postFixupHooks+=(dartFixupHook) diff --git a/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-install-hook.sh b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-install-hook.sh new file mode 100644 index 000000000000..349a0dfdef0e --- /dev/null +++ b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/dart-install-hook.sh @@ -0,0 +1,43 @@ +# shellcheck shell=bash + +dartInstallHook() { + echo "Executing dartInstallHook" + + runHook preInstall + + # Install snapshots and executables. + mkdir -p "$out" + while IFS=$'\t' read -ra target; do + dest="${target[0]}" + # Wrap with runtime command, if it's defined + if [ -n "$dartRuntimeCommand" ]; then + install -D "$dest" "$out/share/$dest" + makeWrapper "$dartRuntimeCommand" "$out/$dest" \ + --add-flags "$out/share/$dest" + else + install -Dm755 "$dest" "$out/$dest" + fi + done < <(_getDartEntryPoints) + + runHook postInstall + + echo "Finished dartInstallHook" +} + +dartInstallCacheHook() { + echo "Executing dartInstallCacheHook" + + # Install the package_config.json file. + mkdir -p "$pubcache" + cp .dart_tool/package_config.json "$pubcache/package_config.json" + + echo "Finished dartInstallCacheHook" +} + +if [ -z "${dontDartInstall-}" ] && [ -z "${installPhase-}" ]; then + installPhase=dartInstallHook +fi + +if [ -z "${dontDartInstallCache-}" ]; then + postInstallHooks+=(dartInstallCacheHook) +fi diff --git a/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/default.nix b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/default.nix new file mode 100644 index 000000000000..253d3132ad02 --- /dev/null +++ b/nixpkgs/pkgs/build-support/dart/build-dart-application/hooks/default.nix @@ -0,0 +1,20 @@ +{ lib, makeSetupHook, dart, yq, jq }: + +{ + dartConfigHook = makeSetupHook { + name = "dart-config-hook"; + substitutions.yq = "${yq}/bin/yq"; + substitutions.jq = "${jq}/bin/jq"; + } ./dart-config-hook.sh; + dartBuildHook = makeSetupHook { + name = "dart-build-hook"; + substitutions.yq = "${yq}/bin/yq"; + substitutions.jq = "${jq}/bin/jq"; + } ./dart-build-hook.sh; + dartInstallHook = makeSetupHook { + name = "dart-install-hook"; + } ./dart-install-hook.sh; + dartFixupHook = makeSetupHook { + name = "dart-fixup-hook"; + } ./dart-fixup-hook.sh; +} |