Skip to content

Commit bdefea1

Browse files
committed
wrote base for building recipes. include hostpython and python recipes
1 parent 6234a5d commit bdefea1

11 files changed

+1325
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
*.swp
3+
*.swo
4+
*~

README

Whitespace-only changes.

README.rst

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Python for Android
2+
==================
3+
4+
Python for android is a project to create your own Python distribution
5+
including the modules you want, and create an apk including python, libs, and
6+
your application.
7+
8+
In terms of comparaison, you can check how Python for android can be useful
9+
compared to other projects.
10+
11+
+--------------------+---------------+---------------+----------------+--------------+
12+
| Project | Native Python | GUI libraries | APK generation | Custom build |
13+
+====================+===============+===============+================+==============+
14+
| Python for android | Yes | Yes | Yes | Yes |
15+
| PGS4A | Yes | Yes | Yes | No |
16+
| Android scripting | No | No | No | No |
17+
| Python on a chip | No | No | No | No |
18+
+--------------------+---------------+---------------+----------------+--------------+
19+
20+
21+
Prerequisites
22+
-------------
23+
24+
WARNING: the current version is working only on Ubuntu oneiric (11.10). We
25+
don't provide support on other platform. If it doesn't work, send us patch, not
26+
bug report.
27+
28+
You need the minimal environment for building python. Note that other libraries
29+
might need other tools::
30+
31+
sudo apt-get install build-essential patch
32+
33+
You must have android SDK and NDK. You can download them at::
34+
35+
http://developer.android.com/sdk/index.html
36+
http://developer.android.com/sdk/ndk/index.html
37+
38+
If it's your very first time into android sdk, don't forget to follow
39+
documentation for recommended components at::
40+
41+
http://developer.android.com/sdk/installing.html#which
42+
43+
You need to download at least one platform into your environment, so
44+
that you will be able to compile your application and set up an Android
45+
Virtual Device (AVD) to run it on (in the emulator). To start with,
46+
just download the latest version of the platform. Later, if you plan to
47+
publish your application, you will want to download other platforms as
48+
well, so that you can test your application on the full range of
49+
Android platform versions that your application supports.
50+
51+
After installing them, export both installation path::
52+
53+
export ANDROIDSDK=/path/to/android-sdk
54+
export ANDROIDNDK=/path/to/android-ndk
55+
56+
# examples
57+
export ANDROIDSDK="/home/tito/code/android/android-sdk-linux_86"
58+
export ANDROIDNDK="/home/tito/code/android/android-ndk-r5b"
59+
60+
61+
Usage
62+
-----
63+
64+
Step 1, compile the toolchain::
65+
66+
MODULES="openssl pygame" ./configure.sh
67+
68+
Step 2, package your application::
69+
70+
...
71+
72+
73+
Related project
74+
---------------
75+
76+
- PGS4A: http://pygame.renpy.org/
77+
- Android scripting: http://code.google.com/p/android-scripting/
78+
- Python on a chip: http://code.google.com/p/python-on-a-chip/

configure.sh

+290
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
#!/bin/bash
2+
#------------------------------------------------------------------------------
3+
#
4+
# Python for android
5+
# https://github.com/tito/python-for-android
6+
#
7+
#------------------------------------------------------------------------------
8+
9+
# Modules
10+
MODULES=$MODULES
11+
12+
# Paths
13+
ROOT_PATH="$(dirname $(readlink -f $0))"
14+
RECIPES_PATH="$ROOT_PATH/recipes"
15+
BUILD_PATH="$ROOT_PATH/build"
16+
PACKAGES_PATH="$BUILD_PATH/packages"
17+
18+
# Internals
19+
CRED="\x1b[31;01m"
20+
CBLUE="\x1b[34;01m"
21+
CGRAY="\x1b[30;01m"
22+
CRESET="\x1b[39;49;00m"
23+
24+
#set -x
25+
26+
function try () {
27+
"$@" || exit -1
28+
}
29+
30+
function info() {
31+
echo -e "$CBLUE"$@"$CRESET";
32+
}
33+
34+
function error() {
35+
echo -e "$CRED"$@"$CRESET";
36+
}
37+
38+
function debug() {
39+
echo -e "$CGRAY"$@"$CRESET";
40+
}
41+
42+
function get_directory() {
43+
case $1 in
44+
*.tar.gz) directory=$(basename $1 .tar.gz) ;;
45+
*.tgz) directory=$(basename $1 .tgz) ;;
46+
*.tar.bz2) directory=$(basename $1 .tar.bz2) ;;
47+
*.tbz2) directory=$(basename $1 .tbz2) ;;
48+
*.zip) directory=$(basename $1 .zip) ;;
49+
*)
50+
error "Unknown file extension $1"
51+
exit -1
52+
;;
53+
esac
54+
echo $directory
55+
}
56+
57+
function push_arm() {
58+
info "Entering in ARM enviromnent"
59+
# ANDROIDSDK / ANDROIDNDK
60+
#export NDK="$PGS4A_ROOT/android-ndk-r5b"
61+
#export SDK="$PGS4A_ROOT/android-sdk-linux_86/"
62+
export NDKPLATFORM="$ANDROIDNDK/platforms/android-5/arch-arm"
63+
64+
# save for pop
65+
export OLD_PATH=$PATH
66+
export OLD_ARCH=$ARCH
67+
export OLD_CFLAGS=$CFLAGS
68+
export OLD_CXXFLAGS=$CXXFLAGS
69+
export OLD_CC=$CC
70+
export OLD_CXX=$CXX
71+
export OLD_AR=$AR
72+
export OLD_RANLIB=$RANLIB
73+
export OLD_STRIP=$STRIP
74+
export OLD_MAKE=$MAKE
75+
76+
export PATH="$NDK/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin/:$NDK:$SDK/tools:$PATH"
77+
export ARCH="armeabi"
78+
#export ARCH="armeabi-v7a"
79+
# to override the default optimization, set OFLAG
80+
#export OFLAG="-Os"
81+
#export OFLAG="-O2"
82+
83+
export CFLAGS="-mandroid $OFLAG -fomit-frame-pointer --sysroot $NDKPLATFORM"
84+
if [ $ARCH == "armeabi-v7a" ]; then
85+
CFLAGS+=" -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -mthumb"
86+
fi
87+
export CXXFLAGS="$CFLAGS"
88+
89+
export CC="arm-eabi-gcc $CFLAGS"
90+
export CXX="arm-eabi-g++ $CXXFLAGS"
91+
export AR="arm-eabi-ar"
92+
export RANLIB="arm-eabi-ranlib"
93+
export STRIP="arm-eabi-strip --strip-unneeded"
94+
export MAKE="make -j5"
95+
96+
# Use ccache ?
97+
which ccache &>/dev/null
98+
if [ $? -eq 0 ]; then
99+
export CC="ccache $CC"
100+
export CXX="ccache $CXX"
101+
fi
102+
}
103+
104+
function pop_arm() {
105+
info "Leaving ARM enviromnent"
106+
export PATH=$OLD_PATH
107+
export ARCH=$OLD_ARCH
108+
export CFLAGS=$OLD_CFLAGS
109+
export CXXFLAGS=$OLD_CXXFLAGS
110+
export CC=$OLD_CC
111+
export CXX=$OLD_CXX
112+
export AR=$OLD_AR
113+
export RANLIB=$OLD_RANLIB
114+
export STRIP=$OLD_STRIP
115+
export MAKE=$OLD_MAKE
116+
}
117+
118+
function run_prepare() {
119+
info "Check enviromnent"
120+
if [ "X$ANDROIDSDK" == "X" ]; then
121+
error "No ANDROIDSDK environment set, abort"
122+
exit -1
123+
fi
124+
if [ "X$ANDROIDNDK" == "X" ]; then
125+
error "No ANDROIDNDK environment set, abort"
126+
exit -1
127+
fi
128+
129+
info "Check mandatory tools"
130+
# ensure that some tools are existing
131+
for tool in md5sum tar bzip2 unzip make gcc g++; do
132+
which $tool &>/dev/null
133+
if [ $? -ne 0 ]; then
134+
error "Tool $tool is missing"
135+
exit -1
136+
fi
137+
done
138+
139+
# create build directory if not found
140+
if [ ! -d $BUILD_PATH ]; then
141+
mkdir -p $BUILD_PATH
142+
mkdir -p $PACKAGES_PATH
143+
fi
144+
}
145+
146+
function run_source_modules() {
147+
for module in hostpython python $MODULES; do
148+
recipe=$RECIPES_PATH/$module/recipe.sh
149+
if [ ! -f $recipe ]; then
150+
error "Recipe $module does not exit"
151+
exit -1
152+
fi
153+
source $RECIPES_PATH/$module/recipe.sh
154+
done
155+
}
156+
157+
# order modules by their priority
158+
function run_order_modules() {
159+
cd $BUILD_PATH
160+
filename=$RANDOM.order
161+
if [ -f $filename ]; then
162+
rm $filename
163+
fi
164+
165+
for module in hostpython python $MODULES; do
166+
# get priority
167+
priority="PRIORITY_$module"
168+
priority=${!priority}
169+
# write on the file
170+
echo "$priority $module" >> $filename
171+
done
172+
173+
# update modules by priority
174+
MODULES=$(sort -n $filename|cut -d\ -f2)
175+
info "Module order is $MODULES"
176+
177+
# remove temporary filename
178+
rm $filename
179+
}
180+
181+
function run_get_deps() {
182+
info "Run get dependencies"
183+
184+
for module in $MODULES; do
185+
# download dependencies for this module
186+
debug "Download dependencies for $module"
187+
188+
url="URL_$module"
189+
url=${!url}
190+
md5="MD5_$module"
191+
md5=${!md5}
192+
filename=$(basename $url)
193+
do_download=1
194+
195+
if [ ! -d "$BUILD_PATH/$module" ]; then
196+
try mkdir -p $BUILD_PATH/$module
197+
fi
198+
cd $PACKAGES_PATH
199+
200+
# check if the file is already present
201+
if [ -f $filename ]; then
202+
203+
# check if the md5 is correct
204+
current_md5=$(md5sum $filename | cut -d\ -f1)
205+
if [ "X$current_md5" == "X$md5" ]; then
206+
# correct, no need to download
207+
do_download=0
208+
else
209+
# invalid download, remove the file
210+
error "Module $module have invalid md5, redownload."
211+
rm $filename
212+
fi
213+
fi
214+
215+
# download if needed
216+
if [ $do_download -eq 1 ]; then
217+
info "Downloading $url"
218+
try wget $url
219+
else
220+
debug "Module $module already downloaded"
221+
fi
222+
223+
# check md5
224+
current_md5=$(md5sum $filename | cut -d\ -f1)
225+
if [ "X$current_md5" != "X$md5" ]; then
226+
error "File $filename md5 check failed (got $current_md5 instead of $md5)."
227+
error "Ensure the file is correctly downloaded, and update MD5S_$module"
228+
exit -1
229+
fi
230+
231+
# if already decompress, forget it
232+
cd $BUILD_PATH/$module
233+
directory=$(get_directory $filename)
234+
if [ -d $directory ]; then
235+
continue
236+
fi
237+
238+
# decompress
239+
case $filename in
240+
*.tar.gz|*.tgz ) try tar xzf $PACKAGES_PATH/$filename ;;
241+
*.tar.bz2|*.tbz2 ) try tar xjf $PACKAGES_PATH/$filename ;;
242+
*.zip ) try unzip x $PACKAGES_PATH/$filename ;;
243+
esac
244+
done
245+
}
246+
247+
function run_prebuild() {
248+
info "Run prebuild"
249+
cd $BUILD_PATH
250+
for module in $MODULES; do
251+
fn=$(echo prebuild_$module)
252+
debug "Call $fn"
253+
$fn
254+
done
255+
}
256+
257+
function run_build() {
258+
info "Run build"
259+
cd $BUILD_PATH
260+
for module in $MODULES; do
261+
fn=$(echo build_$module)
262+
debug "Call $fn"
263+
$fn
264+
done
265+
}
266+
267+
function run_postbuild() {
268+
info "Run postbuild"
269+
cd $BUILD_PATH
270+
for module in $MODULES; do
271+
fn=$(echo postbuild_$module)
272+
debug "Call $fn"
273+
$fn
274+
done
275+
}
276+
277+
function run() {
278+
run_prepare
279+
run_source_modules
280+
run_order_modules
281+
run_get_deps
282+
run_prebuild
283+
run_build
284+
run_postbuild
285+
info "All done !"
286+
}
287+
288+
# Do the build
289+
run
290+

0 commit comments

Comments
 (0)