summary refs log tree commit diff
path: root/pkgs/development/mobile/androidenv/androidndk.nix
blob: 23ae4378dc68410a67aa1f825643845839792f03 (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
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
110
111
112
113
114
115
116
117
118
119
120
{ stdenv, fetchurl, zlib, ncurses5, unzip, lib, makeWrapper
, coreutils, file, findutils, gawk, gnugrep, gnused, jdk, which
, platformTools, python3, libcxx, version, sha1s, bash, runCommand
, fullNDK ? false # set to true if you want other parts of the NDK
                  # that is not used by Nixpkgs like sources,
                  # examples, docs, or LLVM toolchains
}:

let
  makeStandaloneToolchain = api: arch: let
      full_ndk = (ndk true);
    in runCommand "makeStandaloneToolchain-${version}" {} ''
      ${full_ndk}/libexec/${full_ndk.name}/build/tools/make_standalone_toolchain.py --api ${toString api} --arch ${arch} --install-dir $out
    '';
  ndk = fullNDK: stdenv.mkDerivation rec {
    name = "android-ndk-r${version}";
    inherit version;

    src = fetchurl {
      url = "https://dl.google.com/android/repository/${name}-${stdenv.hostPlatform.parsed.kernel.name}-${stdenv.hostPlatform.parsed.cpu.name}.zip";
      sha1 = sha1s.${stdenv.hostPlatform.system} or (throw "platform ${stdenv.hostPlatform.system} not supported!");
    };

    phases = "buildPhase";

    nativeBuildInputs = [ unzip makeWrapper file ];

    buildCommand = let
      bin_path = "$out/bin";
      pkg_path = "$out/libexec/${name}";
      sed_script_1 =
        "'s|^PROGDIR=`dirname $0`" +
        "|PROGDIR=`dirname $(readlink -f $(which $0))`|'";
      runtime_paths = (lib.makeBinPath [
        coreutils file findutils
        gawk gnugrep gnused
        jdk python3 which
      ]) + ":${platformTools}/platform-tools";
    in ''
      mkdir -pv $out/libexec
      cd $out/libexec
      unzip -qq $src

      # so that it doesn't fail because of read-only permissions set
      cd -
      ${if (version == "10e") then
          ''
            patch -p1 \
              --no-backup-if-mismatch \
              -d $out/libexec/${name} < ${ ./make-standalone-toolchain_r10e.patch }
          ''
        else
          ''
            patch -p1 \
              --no-backup-if-mismatch \
              -d $out/libexec/${name} < ${ ./. + "/make_standalone_toolchain.py_" + "${version}" + ".patch" }

            sed -i 's,#!/usr/bin/env python,#!${python3}/bin/python,g' ${pkg_path}/build/tools/make_standalone_toolchain.py
            sed -i 's,#!/bin/bash,#!${bash}/bin/bash,g' ${pkg_path}/build/tools/make_standalone_toolchain.py
            wrapProgram ${pkg_path}/build/tools/make_standalone_toolchain.py --prefix PATH : "${runtime_paths}"
          ''
      }

      patchShebangs ${pkg_path}

      cd ${pkg_path}

    '' + lib.optionalString (!fullNDK) ''
      # Steps to reduce output size
      rm -rf docs sources tests
      # We only support cross compiling with gcc for now
      rm -rf toolchains/*-clang* toolchains/llvm*
    '' +

    ''
      find ${pkg_path}/toolchains \( \
          \( -type f -a -name "*.so*" \) -o \
          \( -type f -a -perm -0100 \) \
          \) -exec patchelf --set-interpreter ${stdenv.cc.libc.out}/lib/ld-*so.? \
                            --set-rpath ${stdenv.lib.makeLibraryPath [ libcxx zlib ncurses5 ]} {} \;
      # fix ineffective PROGDIR / MYNDKDIR determination
      for i in ndk-build ${lib.optionalString (version == "10e") "ndk-gdb ndk-gdb-py"}
      do
          sed -i -e ${sed_script_1} $i
      done

      # wrap
      for i in ndk-build ${lib.optionalString (version == "10e") "ndk-gdb ndk-gdb-py ndk-which"}
      do
          wrapProgram "$(pwd)/$i" --prefix PATH : "${runtime_paths}"
      done

      ${stdenv.lib.optionalString (stdenv.hostPlatform.system == "x86_64-linux") ''
        for i in ${pkg_path}/prebuilt/linux-x86_64/bin/*
        do
            if ! isELF $i; then continue; fi
            patchelf --set-interpreter ${stdenv.cc.libc.out}/lib/ld-linux-x86-64.so.2 $i
            patchelf --set-rpath ${stdenv.cc.cc.lib}/lib64 $i
        done
      ''}

      # make some executables available in PATH
      mkdir -pv ${bin_path}
      for i in \
          ndk-build ${lib.optionalString (version == "10e") "ndk-depends ndk-gdb ndk-gdb-py ndk-gdb.py ndk-stack ndk-which"}
      do
          ln -sf ${pkg_path}/$i ${bin_path}/$i
      done
    '';

    meta = {
      platforms = builtins.attrNames sha1s;
      hydraPlatforms = [];
      license = stdenv.lib.licenses.asl20;
    };
  };
  passthru = {
    inherit makeStandaloneToolchain;
  };
in lib.extendDerivation true passthru (ndk fullNDK)