Project

General

Profile

NDKBuild » History » Version 18

Wolfgang Wiedmeyer, 04/16/2017 02:56 PM
link to ndk issue

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