`setup.sh` extension to "fixup" "no-install" targets

Hey so this is a quirk I’ve bumped into with several builds in the past; the automake name, “no-install” targets, is a convenient description of what I’m talking about - but this category of target appears in all manner of builds.

As a quick refresher, “no-install” targets are generally scripts or binaries produced during a build, which are used to create generated sources, but these targets are never installed.

When using Nix I often find that it’s necessary to split a buildPhase into multiple parts, or else build and fixup these “no-install” targets during preBuild ( which has to be done much more cautiously because of makeFlagsArray propagation ).

I was wondering if anyone was aware of an existing idiom/pattern for handling these types of targets. If one doesn’t exist, I wanted to spitball ideas on possibly extending setup.sh with a new setup-hook, or the addition of a new phase between preBuild and buildPhase - perhaps nativeBuildPhase or something ( that name could use some work-shopping ).

The reason fixup routines such as running patchelf or patchShebangs are a headache is really because if we use the standard make targets such make all or make install; we lack a clean way to hook the Makefile to add fixup for any “no-install” targets. The approach I currently take is something to the effect of ( this is to illustrate the process, I know that this isn’t ideal ):

stdenv.mkDerivation {

  /* ... */

  noInstallTargets = [ "foo" "bar.sh" ];

  buildPhase = ''
    runHook preBuild
    : ${makeFlags=}
    if [[ -z "$makeFlags" && -z "${makefile:-}" && ! ( -e Makefile || -e makefile || -e GNUmakefile ) ]]; then
      echo "no Makefile, doing nothing"
    else
      foundMakefile=1
      local flagsArray=(
        ${enableParallelBuilding:+-j${NIX_BUILD_CORES} -l${NIX_BUILD_CORES}}
        SHELL=$SHELL
        $makeFlags "${makeFlagsArray[@]}"
        $buildFlags "${buildFlagsArray[@]}"
      )
      echoCmd 'build flags' "${flagsArray[@]}"
      # Create no-install targets first
      if [[ -n "$noInstallTargets" ]]; then
        make ${makefile:+-f $makefile} "${flagsArray[@]}" $noInstallTargets
        # This is a really bad way to decide how to fixup - that's not really the point of this example though
        for t in $noInstallTargest; do
          if file -Lb $t|grep -q '^ASCII text'; then
            patchShebangs --build $t
          else
            autoPatchelf $t
          fi
        done
        # Run "regular" build
        make ${makefile:+-f $makefile} "${flagsArray[@]}"
        unset flagsArray
      fi
      runHook PostBuild
    }
  '';

  /* ... */

}