summaryrefslogtreecommitdiff
path: root/pkgs/development/mobile/androidenv/ndk-bundle/default.nix
blob: d0767ddc142bedad14985cc11239c24dee502568 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
{
  stdenv,
  lib,
  pkgs,
  pkgsHostHost,
  makeWrapper,
  autoPatchelfHook,
  deployAndroidPackage,
  package,
  os,
  arch,
  platform-tools,
  meta,
}:

let
  runtime_paths =
    lib.makeBinPath (
      with pkgsHostHost;
      [
        coreutils
        file
        findutils
        gawk
        gnugrep
        gnused
        jdk_headless
        python3
        which
      ]
    )
    + ":${platform-tools}/platform-tools";
in
deployAndroidPackage rec {
  inherit package os arch;
  nativeBuildInputs = [
    makeWrapper
  ]
  ++ lib.optionals stdenv.hostPlatform.isLinux [ autoPatchelfHook ];
  autoPatchelfIgnoreMissingDeps = [ "*" ];
  buildInputs = lib.optionals (os == "linux") [
    pkgs.zlib
    pkgs.libcxx
    (lib.getLib stdenv.cc.cc)
  ];

  patchElfBnaries = ''
    # Patch the executables of the toolchains, but not the libraries -- they are needed for crosscompiling
    if [ -d $out/libexec/android-sdk/ndk-bundle/toolchains/renderscript/prebuilt/linux-x86_64/lib64 ]; then
      addAutoPatchelfSearchPath $out/libexec/android-sdk/ndk-bundle/toolchains/renderscript/prebuilt/linux-x86_64/lib64
    fi

    if [ -d $out/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64 ]; then
      addAutoPatchelfSearchPath $out/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64
    fi

    if [ -d $out/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib ]; then
      addAutoPatchelfSearchPath $out/libexec/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib
    fi

    find toolchains -type d -name bin -or -name lib64 -or -name lib | while read dir; do
      autoPatchelf "$dir"
    done

    # Patch executables
    if [ -d prebuilt/linux-x86_64 ]; then
      autoPatchelf prebuilt/linux-x86_64
    fi
  '';

  patchOsAgnostic = ''
    patchShebangs .

    # TODO: allow this stuff
    rm -rf docs tests

    # Ndk now has a prebuilt toolchains inside, the file layout has changed, we do a symlink
    # to still support the old standalone toolchains builds.
    if [ -d $out/libexec/android-sdk/${package.path} ] && [ ! -d $out/libexec/android-sdk/ndk-bundle ]; then
      ln -sf $out/libexec/android-sdk/${package.path} $out/libexec/android-sdk/ndk-bundle
    elif [ ! -d $out/libexec/android-sdk/ndk-bundle ]; then
      echo "The ndk-bundle layout has changed. The nix expressions have to be updated!"
      exit 1
    fi

    # fix ineffective PROGDIR / MYNDKDIR determination
    for progname in ndk-build; do
      sed -i -e 's|^PROGDIR=`dirname $0`|PROGDIR=`dirname $(readlink -f $(which $0))`|' $progname
    done

    # wrap
    for progname in ndk-build; do
      wrapProgram "$(pwd)/$progname" --prefix PATH : "${runtime_paths}"
    done

    # make some executables available in PATH
    mkdir -p $out/bin
    for progname in ndk-build; do
      ln -sf ../libexec/android-sdk/ndk-bundle/$progname $out/bin/$progname
    done
  '';

  patchInstructions =
    patchOsAgnostic + lib.optionalString stdenv.hostPlatform.isLinux patchElfBnaries;

  noAuditTmpdir = true; # Audit script gets invoked by the build/ component in the path for the make standalone script

  inherit meta;
}