about summary refs log tree commit diff
path: root/pkgs/applications/audio/famistudio/build-native-wrapper.nix
blob: ddee5bf5712dcde101ce70c1043b8eb604faff44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{ depname
, version
, src
, sourceRoot
, stdenv
, lib
, patches ? []
, extraPostPatch ? ""
, buildInputs ? []
}:

let
  rebuildscriptName = if stdenv.hostPlatform.isLinux then
    "build_linux"
  else if stdenv.hostPlatform.isDarwin then
    "build_macos"
  else throw "Don't know how to rebuild FamiStudio's vendored ${depname} for ${stdenv.hostPlatform.system}";
in
stdenv.mkDerivation {
  pname = "famistudio-nativedep-${depname}";
  inherit version src sourceRoot patches buildInputs;

  postPatch = let
    libnameBase = lib.optionalString stdenv.hostPlatform.isLinux "lib" + depname;
  in ''
    # Use one name for build script, eases with patching
    mv ${rebuildscriptName}.sh build.sh

    # Scripts use hardcoded compilers and try to copy built libraries into FamiStudio's build tree
    # Not all scripts use the same compiler, so don't fail on replacing that
    substituteInPlace build.sh \
      --replace-fail '../../FamiStudio/' "$out/lib/" \
      --replace-quiet 'g++' "$CXX"

    # Replacing gcc via sed, would break -static-libgcc otherwise
    sed -i -e "s/^gcc/$CC/g" build.sh
  '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
    # Darwin rebuild scripts try to make a universal2 dylib
    # - build dylib for non-hostPlatform
    # - copy built library into special directory for later packaging script
    # - join two dylibs together into a universal2 dylib
    # Remove everything we don't need
    sed -ri \
      -e '/-target ${if stdenv.hostPlatform.isx86_64 then "arm64" else "x86_64"}/d' \
      -e '/..\/..\/Setup/d' \
      build.sh

    # Replace joining multi-arch dylibs with copying dylib for target arch
    substituteInPlace build.sh \
      --replace-fail 'lipo -create -output ${libnameBase}.dylib' 'cp ${libnameBase}_${if stdenv.hostPlatform.isx86_64 then "x86_64" else "arm64"}.dylib ${libnameBase}.dylib #'
  '' + extraPostPatch;

  dontConfigure = true;
  dontInstall = true; # rebuild script automatically installs

  buildPhase = ''
    runHook preBuild

    mkdir -p $out/lib

    # Delete all prebuilt libraries, make sure everything is rebuilt
    find . -name '*.so' -or -name '*.dylib' -or -name '*.a' -delete

    # When calling normally, an error won't cause derivation to fail
    source ./build.sh

    runHook postBuild
  '';
}