summary refs log tree commit diff
path: root/pkgs/development/mobile/androidenv/androidsdk.nix
blob: 5f61f25d54eeaf8f129bfa12275fadd22bf9c029 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{ stdenv, stdenv_32bit, fetchurl, fetchzip, unzip, makeWrapper
, platformTools, buildTools, support, supportRepository, platforms, sysimages, addons, sources
, libX11, libXext, libXrender, libxcb, libXau, libXdmcp, libXtst, libGLU_combined, alsaLib
, freetype, fontconfig, glib, gtk2, atk, file, jdk, coreutils, libpulseaudio, dbus
, zlib, glxinfo, xkeyboardconfig
, includeSources
}:
{ platformVersions, abiVersions, useGoogleAPIs, useExtraSupportLibs ? false
, useGooglePlayServices ? false, useInstantApps ? false }:

let inherit (stdenv.lib) makeLibraryPath;

    googleRepository = let version = "gms_v9_rc41_wear_2_0_rc6";
      in fetchzip rec {
        url = "https://dl-ssl.google.com/android/repository/google_m2repository_${version}.zip";
        sha256 = "0k99xmynv0k62d301zx5jnjkddflr51i5lb02l9incg7m5cn8kzx";
      };

in

stdenv.mkDerivation rec {
  name = "android-sdk-${version}";
  version = "25.2.5";

  src = if (stdenv.hostPlatform.system == "i686-linux" || stdenv.hostPlatform.system == "x86_64-linux")
    then fetchurl {
      url = "https://dl.google.com/android/repository/tools_r${version}-linux.zip";
      sha256 = "0gnk49pkwy4m0nqwm1xnf3w4mfpi9w0kk7841xlawpwbkj0icxap";
    }
    else if stdenv.hostPlatform.system == "x86_64-darwin" then fetchurl {
      url = "http://dl.google.com/android/repository/tools_r${version}-macosx.zip";
      sha256 = "0yg7wjmyw70xsh8k4hgbqb5rilam2a94yc8dwbh7fjwqcmpxgwqb";
    }
    else throw "platform not ${stdenv.hostPlatform.system} supported!";

  buildCommand = ''
    mkdir -p $out/libexec
    cd $out/libexec
    unpackFile $src
    cd tools

    for f in android traceview draw9patch hierarchyviewer monitor ddms screenshot2 uiautomatorviewer monkeyrunner jobb lint
    do
        sed -i -e "s|/bin/ls|${coreutils}/bin/ls|" "$f"
    done

    ${stdenv.lib.optionalString (stdenv.hostPlatform.system == "i686-linux" || stdenv.hostPlatform.system == "x86_64-linux")
    ''
      # There are a number of native binaries. We must patch them to let them find the interpreter and libstdc++

      for i in mksdcard
      do
          patchelf --set-interpreter ${stdenv_32bit.cc.libc.out}/lib/ld-linux.so.2 $i
          patchelf --set-rpath ${stdenv_32bit.cc.cc.lib}/lib $i
      done

      ${stdenv.lib.optionalString (stdenv.hostPlatform.system == "x86_64-linux") ''
        for i in bin64/{mkfs.ext4,fsck.ext4,e2fsck,tune2fs,resize2fs}
        do
            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
      ''}

      ${stdenv.lib.optionalString (stdenv.hostPlatform.system == "x86_64-linux") ''
        # We must also patch the 64-bit emulator instances, if needed

        for i in emulator emulator64-arm emulator64-mips emulator64-x86 emulator64-crash-service emulator-check qemu/linux-x86_64/qemu-system-*
        do
            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
      ''}

      # The following scripts used SWT and wants to dynamically load some GTK+ stuff.
      # Creating these wrappers ensure that they can be found:

      wrapProgram `pwd`/android \
        --prefix PATH : ${jdk}/bin \
        --prefix LD_LIBRARY_PATH : ${makeLibraryPath [ glib gtk2 libXtst ]}

      wrapProgram `pwd`/uiautomatorviewer \
        --prefix PATH : ${jdk}/bin \
        --prefix LD_LIBRARY_PATH : ${stdenv.lib.makeLibraryPath [ glib gtk2 libXtst ]}

      wrapProgram `pwd`/hierarchyviewer \
        --prefix PATH : ${jdk}/bin \
        --prefix LD_LIBRARY_PATH : ${stdenv.lib.makeLibraryPath [ glib gtk2 libXtst ]}

      # The emulators need additional libraries, which are dynamically loaded => let's wrap them

      ${stdenv.lib.optionalString (stdenv.hostPlatform.system == "x86_64-linux") ''
        for i in emulator emulator64-arm emulator64-mips emulator64-x86 emulator64-crash-service
        do
            wrapProgram `pwd`/$i \
              --prefix PATH : ${stdenv.lib.makeBinPath [ file glxinfo ]} \
              --suffix LD_LIBRARY_PATH : `pwd`/lib64:`pwd`/lib64/qt/lib:${makeLibraryPath [ stdenv.cc.cc libX11 libxcb libXau libXdmcp libXext libGLU_combined alsaLib zlib libpulseaudio dbus.lib ]} \
              --suffix QT_XKB_CONFIG_ROOT : ${xkeyboardconfig}/share/X11/xkb
        done
      ''}
    ''}

    patchShebangs .

    ${if stdenv.hostPlatform.system == "i686-linux" then
      ''
        # The monitor requires some more patching

        cd lib/monitor-x86
        patchelf --set-interpreter ${stdenv.cc.libc.out}/lib/ld-linux.so.2 monitor
        patchelf --set-rpath ${makeLibraryPath [ libX11 libXext libXrender freetype fontconfig ]} libcairo-swt.so

        wrapProgram `pwd`/monitor \
          --prefix LD_LIBRARY_PATH : ${makeLibraryPath [ gtk2 atk stdenv.cc.cc libXtst ]}

        cd ../..
      ''
      else if stdenv.hostPlatform.system == "x86_64-linux" then
      ''
        # The monitor requires some more patching

        cd lib/monitor-x86_64
        patchelf --set-interpreter ${stdenv.cc.libc.out}/lib/ld-linux-x86-64.so.2 monitor
        patchelf --set-rpath ${makeLibraryPath [ libX11 libXext libXrender freetype fontconfig ]} libcairo-swt.so

        wrapProgram `pwd`/monitor \
          --prefix LD_LIBRARY_PATH : ${makeLibraryPath [ gtk2 atk stdenv.cc.cc libXtst ]}

        cd ../..
      ''
      else ""}

    # Symlink the other sub packages

    cd ..
    ln -s ${platformTools}/platform-tools
    ln -s ${buildTools}/build-tools
    ln -s ${support}/support

    # Symlink required Google API add-ons

    mkdir -p add-ons
    cd add-ons

    ${if useGoogleAPIs then
        stdenv.lib.concatMapStrings (platformVersion:
        if (builtins.hasAttr ("google_apis_"+platformVersion) addons) then
          let
            googleApis = builtins.getAttr ("google_apis_"+platformVersion) addons;
          in
          "ln -s ${googleApis}/* addon-google_apis-${platformVersion}\n"
        else "") platformVersions
      else ""}

    cd ..

    # Symlink required extras

    mkdir -p extras/android
    cd extras/android

    ln -s ${supportRepository}/m2repository

    ${if useExtraSupportLibs then
       "ln -s ${addons.android_support_extra}/support ."
     else ""}

    cd ..
    mkdir -p google
    cd google

    ${if useGooglePlayServices then
       "ln -s ${addons.google_play_services}/google-play-services google_play_services"
     else ""}

    ${stdenv.lib.optionalString useInstantApps
       "ln -s ${addons.instant_apps}/whsdk instantapps"}

    ln -s ${googleRepository} m2repository

    cd ../..

    # Symlink required sources
    mkdir -p sources
    cd sources

    ${if includeSources then
        stdenv.lib.concatMapStrings (platformVersion:
        if (builtins.hasAttr ("source_"+platformVersion) sources) then
          let
            source = builtins.getAttr ("source_"+platformVersion) sources;
          in
          "ln -s ${source}/* android-${platformVersion}\n"
        else "") platformVersions
      else ""}

    cd ..

    # Symlink required platforms

    mkdir -p platforms
    cd platforms

    ${stdenv.lib.concatMapStrings (platformVersion:
      if (builtins.hasAttr ("platform_"+platformVersion) platforms) then
        let
          platform = builtins.getAttr ("platform_"+platformVersion) platforms;
        in
        "ln -s ${platform}/* android-${platformVersion}\n"
      else ""
    ) platformVersions}

    cd ..

    # Symlink required system images

    mkdir -p system-images
    cd system-images

    ${stdenv.lib.concatMapStrings (abiVersion:
      stdenv.lib.concatMapStrings (platformVersion:
        if (builtins.hasAttr ("sysimg_" + abiVersion + "_" + platformVersion) sysimages) then
          let
            sysimg = builtins.getAttr ("sysimg_" + abiVersion + "_" + platformVersion) sysimages;
          in
          ''
            mkdir -p android-${platformVersion}
            cd android-${platformVersion}
            ln -s ${sysimg}/*
            cd ..
          ''
        else ""
      ) platformVersions
    ) abiVersions}

    # Create wrappers to the most important tools and platform tools so that we can run them if the SDK is in our PATH

    mkdir -p $out/bin

    for i in $out/libexec/tools/*
    do
        if [ ! -d $i ] && [ -x $i ]
        then
            ln -sf $i $out/bin/$(basename $i)
        fi
    done

    for i in $out/libexec/platform-tools/*
    do
        if [ ! -d $i ] && [ -x $i ]
        then
            ln -sf $i $out/bin/$(basename $i)
        fi
    done

    for i in $out/libexec/build-tools/*/*
    do
        if [ ! -d $i ] && [ -x $i ]
        then
            ln -sf $i $out/bin/$(basename $i)
        fi
    done
  '';

  buildInputs = [ unzip makeWrapper ];

  meta = {
    platforms = stdenv.lib.platforms.unix;
    hydraPlatforms = [];
    license = stdenv.lib.licenses.unfree;
  };
}