Project

General

Profile

NDKBuild » History » Version 17

Wolfgang Wiedmeyer, 04/16/2017 02:48 PM
typo in heading

1 1 Beuc Beuc
h1. Build a Native Developement Kit (NDK) with Replicant 4.2
2
3
h2. Principle of operation
4
5
The [[ReplicantSourceCode|Android 4.2 source code]] ships a @ndk@ directory with NDK build scripts.
6 9 Beuc Beuc
These tools download various upstream imports from https://git.androidproject.xxx/toolchain/XXX/ with optional commit date (no branches or tags), apply patches, and build them.
7 1 Beuc Beuc
8 2 Beuc Beuc
Note: as of 2015-07, the upcoming Android version moved to a different build system, and will populate $topdir/toolchain/ through @repo@ instead of invoking git in a temporary directory (i.e. ditched @download-toolchain-sources.sh@).
9
10 12 Beuc Beuc
h2. Recreate matching build environment
11 1 Beuc Beuc
12 12 Beuc Beuc
Replicant 4.2 is based on CyanogenMod 10.1 which is based on AOSP 4.2.2, released 2013-02.
13
The build environment used by NDK release managers should be Ubuntu LTS 12.04 (or maybe Ubuntu LTS 10.04).
14 2 Beuc Beuc
15 12 Beuc Beuc
To recreate it easily with LXC, follow:
16
* [[BuildEnvironments#Ubuntu-1204-Precise-with-LXC|Ubuntu 12.04 Precise with LXC]] (Trisquel support in progress)
17
* [[BuildEnvironments#Non-privileged-user-setup|Non-privileged user setup]]
18 3 Beuc Beuc
19
Note: compiling from Debian 8 will yield errors from texinfo (doc in binutils) and Perl (POD in LLVM) due to more recent, stricter versions of these.
20 2 Beuc Beuc
21
h2. Build dependencies
22
23
<pre>
24
# Base Android build dependencies (from https://source.android.com/source/initializing.html, section "Installing required packages (Ubuntu 12.04)")
25
# Dropping mingw to skip windows builds for now.
26
# Installing g++-multilib first otherwise apt-get complains.
27
apt-get install build-essential g++-multilib
28 13 Beuc Beuc
apt-get install wget git gnupg flex bison gperf \
29 2 Beuc Beuc
  zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev \
30
  libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 \
31
  libgl1-mesa-dev \
32
  python-markdown libxml2-utils xsltproc zlib1g-dev:i386
33
34
# Additional NDK build dependencies (completed from https://android.googlesource.com/platform/ndk/+/master/README.md)
35
apt-get install curl texinfo bison flex libtool pbzip2 groff
36
37
# Make 'bash' the default shell otherwise 'build-gabi++.sh' will fail
38
dpkg-reconfigure dash
39
</pre>
40
41
h2. Preparing the sources
42
43
Based on [[ReplicantSourceCode]] with additional configuration.
44 11 Beuc Beuc
Login as user @replicant@ and:
45 2 Beuc Beuc
46
<pre>
47
mkdir ~/tools/
48
cd ~/tools/
49
wget http://commondatastorage.googleapis.com/git-repo-downloads/repo
50
chmod a+x repo
51
cd ~
52
53
cat <<EOF >> ~/.bashrc
54
export USE_CCACHE=1
55
EOF
56 14 Beuc Beuc
exec bash
57 2 Beuc Beuc
58
# Avoid prompts
59
git config --global user.email "you@example.com"
60
git config --global user.name "Your Name"
61
git config --global color.ui true
62
63
mkdir replicant-4.2/
64
cd replicant-4.2/
65
../tools/repo init -u git://git.replicant.us/manifest.git -b replicant-4.2
66
../tools/repo sync -j4 
67
# > 1h, 27GB (inc. 17GB .repo)
68 14 Beuc Beuc
prebuilts/misc/linux-x86/ccache/ccache -M 10G  # unused in Trisquel 4.1?
69 2 Beuc Beuc
</pre>
70
71
h2. Determining the checkout date
72
73 7 Beuc Beuc
Since there's no NDK release tag for the @toolchain/@ Git checkouts, we need to find the right date.
74 2 Beuc Beuc
75
@cd ndk/ && git log@ says the last commit is 2012-10-05, so it's around NDK r8b (2012-07) and NDK r8c (2012-11).
76
Checking @build/tools/toolchain-patches/@ in the NDKs shows r8c has matching patches.
77
78
Also NDKs from google contain several @SOURCES@ files listing Git repos and commit IDs.
79 10 Beuc Beuc
With r8c's release date (@ls --full-time RELEASE.TXT@ => 2012-10-22 05:59), @build/tools/download-toolchain-sources.sh@ checkouts the same Git commit IDs, and the patches apply.
80 2 Beuc Beuc
Only, r8c references a LLVM Git repo, while our scripts download a tarball.
81
So our scripts match a NDK version between r8b and r8c, and can almost exactly reproduce r8c. Let's call it r8b2.
82
83 17 Wolfgang Wiedmeyer
h2. Building the NDK
84 2 Beuc Beuc
85
Based on @ndk/docs/DEVELOPMENT.html@, with much improvement :)
86
87
<pre>
88
export NDK=~/replicant-4.2/ndk
89
cd $NDK
90
91
# Allow checking build errors
92
export NDK_LOGFILE=$HOME/ndk.log
93
94
# Follow Python download redirection
95
sed -i -e 's/curl -S/curl -L -S/' build/tools/download-toolchain-sources.sh
96
97 10 Beuc Beuc
# Get the sources from toolchain/ repos, with r8c release date:
98 1 Beuc Beuc
bash $NDK/build/tools/download-toolchain-sources.sh --git-date='2012-10-22T05:59:40Z' ~/ndk-dl
99 14 Beuc Beuc
# ~5mn with good network, ndk-dl: 2.5GB
100 2 Beuc Beuc
101
# Define NDK_TMPDIR and and use --incremental so the build can be resumed on error
102
export NDK_TMPDIR=~/ndk-tmp
103
mkdir -p $NDK_TMPDIR/release-r8b2/
104
105 1 Beuc Beuc
# Build the release
106 2 Beuc Beuc
bash $NDK/build/tools/make-release.sh --toolchain-src-dir=$HOME/ndk-dl --release=r8b2 --incremental
107 14 Beuc Beuc
# 30mn with 4 cores, 28GB (inc. 17GB .repo), 2.5GB ndk-dl, ?GB ndk-tmp, 1GB /tmp
108 2 Beuc Beuc
</pre>
109
110 4 Beuc Beuc
The GNU/Linux NDK release is in @/tmp/ndk-replicant/release/android-ndk-r8b2-linux-x86.tar.bz2@ :)
111
112
h2. TODOs
113 1 Beuc Beuc
114 7 Beuc Beuc
h3. Mirror @toolchain/@ repos
115
116
@download-toolchain-sources.sh@'s default is @--git-base=https://android.googlesource.com/toolchain@.
117
Should we use a git.replicant.us repo? (note: there's no 'toolchain' there yet)
118
119 4 Beuc Beuc
h3. Windows build
120
121
Build deps:
122
<pre>
123
# windows build is triggered by the presence of mingw32
124
apt-get install mingw32 tofrodos
125
# and requires running maketab.exe
126
apt-get install wine
127
# I had troubles in LXC registering direct .exe execution:
128
# $ /tmp/ndk-replicant/tmp/build-3038/maketab.exe 
129 1 Beuc Beuc
# run-detectors: unable to find an interpreter for /tmp/ndk-replicant/tmp/build-3038/maketab.exe
130 4 Beuc Beuc
# Work-around:
131
update-binfmts --disable
132 14 Beuc Beuc
dpkg-reconfigure wine1.4 --pri=high  # or wine1.2 for Trisquel 4.1
133 4 Beuc Beuc
</pre>
134
135
Code fixes:
136
<pre>
137
# build-ndk-stack/elff/ produces a warning with mingw32, and uses -Werror
138
# cf. https://android.googlesource.com/platform/ndk/+/32e74f3f1b969ff65f037e1ee89e21a5cbc0ecf0
139
sed -i -e 's/-Werror//' sources/host-tools/ndk-stack/GNUMakefile
140
# TODO: maybe 'export EXTRA_CFLAGS=-Wall' would be enough?
141
</pre>
142
143
...
144 1 Beuc Beuc
145
<pre>
146
bash $NDK/build/tools/make-release.sh --toolchain-src-dir=$HOME/ndk-dl --release=r8b2 --incremental
147
# /home/replicant/ndk-dl/build/../binutils/binutils-2.21/gold/arm.cc:2171: internal compiler error: in make_rtl_for_nonlocal_decl, at cp/decl.c:4971
148
</pre>
149 14 Beuc Beuc
150
Exact same errors with Trisquel 4.1 (Ubuntu 10.04).  For reference, the build dependencies:
151
<pre>
152
# Based on https://web.archive.org/web/20121201011547/http://source.android.com/source/initializing.html
153
apt-get install gnupg flex bison gperf build-essential \
154
  zip curl zlib1g-dev libc6-dev lib32ncurses5-dev ia32-libs \
155
  x11proto-core-dev libx11-dev lib32readline5-dev lib32z-dev \
156
  libgl1-mesa-dev g++-multilib python-markdown \
157
  libxml2-utils xsltproc
158
</pre>
159
160
No idea how google made the windows build for r8c.  Maybe a later ndk.git has fixes?
161
162 4 Beuc Beuc
163 6 Beuc Beuc
h3. Use of prebuilts
164
165
Check how much of $topdir/prebuilts/ is used to compile the NDK. We shouldn't rely on untrusted binaries.
166 4 Beuc Beuc
167 15 Beuc Beuc
168 16 Beuc Beuc
h3. Other build method : directly use ndk.git and development.git
169 4 Beuc Beuc
170 7 Beuc Beuc
Attempt to rebuild r8e with the same build environment, but checkout-ing precise .git revisions from ndk.git and development.git:
171 4 Beuc Beuc
less patches + local LLVM repo + precise control over the ndk scripts revision
172
173
According to @ndk/docs/DEVELOPMENT.html@ one only needs:
174 9 Beuc Beuc
  git clone https://git.androidproject.xxx/platform/ndk.git ndk
175
  git clone https://git.androidproject.xxx/platform/development.git development
176 1 Beuc Beuc
177 16 Beuc Beuc
We wouldn't rely on exactly the build scripts shipped in Replicant 4.2, but official NDK releases are not sync'd with an Android release either.
178
This would allow us to reproduce an existing NDK release precisely.
179 15 Beuc Beuc
180 16 Beuc Beuc
h4. Other build method : rely on previously built NDK
181 6 Beuc Beuc
182 1 Beuc Beuc
@ndk/docs/DEVELOPMENT.html@ references using an existing (trusted) NDK to build the next one.
183 15 Beuc Beuc
Maybe this is how the NDK release team worked around the windows build errors (by not recompiling everything)?