-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-python-for-iOS
More file actions
executable file
·213 lines (159 loc) · 4.79 KB
/
build-python-for-iOS
File metadata and controls
executable file
·213 lines (159 loc) · 4.79 KB
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
#!/bin/bash
#
# BRIEF:
# This script is used to retrieve and build a static library of python interpreter for iOS.
#
# DESCRIPTION:
# This script retrieves the python source files and cross compile patch from internet, and
# build a universal (for iphone-os and iphone-simulator) static library, libpythonX.Y.a.
# The static library contains only the interpreter. In order to the the scripts run, you
# still need to
# * Copy all the headers at ./install/include/ to your project.
# * Copy all the necessary modules at $BUILD_FOLDER/Lib to some place your app can
# access at run time, and notify the interpreter about the path with Py_SetPythonHome
# before calling Py_Initialize.
# If your Python Home is at <py-home>, the modules should be put at
# <py-home>/lib/pythonX.Y
# None of the C modules is compiled.
#
# AUTHOR (GitHub homepage):
# http://github.com/wewei
#
# CREDIT TO:
# http://randomsplat.com/id5-cross-compiling-python-for-embedded-linux.html
# http://latenitesoft.blogspot.com/2008/10/iphone-programming-tips-building-unix.html
# https://github.com/cobbal/python-for-iphone
#
# Version information
SOURCE_VERSION=2.7.3
MIN_IOS_VERSION="5.0"
# Library information
LIBRARY_VERSION=${SOURCE_VERSION%.*}
LIBRARY_NAME="libpython$LIBRARY_VERSION.a"
LIBRARY_NAME_FOR_IPHONEOS="libpython$LIBRARY_VERSION.ios.a"
LIBRARY_NAME_FOR_SIMULATOR="libpython$LIBRARY_VERSION.sim.a"
# Resource information
PATCH_NAME="Python-$SOURCE_VERSION-xcompile.patch"
PATCH_URL="https://raw.github.com/wewei/Build-Python-for-iOS/master/$PATCH_NAME"
SOURCE_PACK_NAME="Python-$SOURCE_VERSION.tar.bz2"
SOURCE_PACK_URL="http://python.org/ftp/python/$SOURCE_VERSION/$SOURCE_PACK_NAME"
# Build information
BUILD_FOLDER="Python-$SOURCE_VERSION"
HOST_PYTHON="hostpython"
HOST_PGEN="Parser/hostpgen"
# To prepare the resource files
function retrieve_file_if_not_exists ()
{
local fileName=$(basename "$1")
if [ ! -e $fileName ]; then
curl -O $1
fi
}
function unpack_source_files()
{
tar jxf $SOURCE_PACK_NAME
}
function prepare_source_files()
{
retrieve_file_if_not_exists $PATCH_URL
retrieve_file_if_not_exists $SOURCE_PACK_URL
unpack_source_files
}
# To build the host utilites (host python, host pgen)
function build_host_utilities()
{
./configure
make python.exe Parser/pgen
}
function rename_host_utilities()
{
mv python.exe $HOST_PYTHON
mv Parser/pgen $HOST_PGEN
}
function generate_host_utilities()
{
pushd $BUILD_FOLDER
if [[ ! -x $HOST_PYTHON || ! -x $HOST_PGEN ]]; then
build_host_utilities
rename_host_utilities
make distclean
fi
popd
}
# To apply the patch
function apply_cross_compile_patch()
{
pushd $BUILD_FOLDER
patch -p1 < ../$PATCH_NAME
popd
}
# To build the libraries
function fix_libgcc_cannot_find_error()
{
local sdkroot=$1
mkdir -p "extralibs"
ln -fs "$sdkroot/usr/lib/libgcc_s.1.dylib" extralibs/libgcc_s.10.4.dylib
}
function cross_compile_library()
{
local sdkroot=$(xcodebuild -version -sdk "$sdk" Path)
local cc=$(xcrun -find -sdk "$sdk" llvm-gcc)
local cpp=$(xcrun -find -sdk "$sdk" cpp)
local cflags="-arch $arch -isysroot $sdkroot -miphoneos-version-min=$MIN_IOS_VERSION"
local ldflags="-arch $arch -isysroot $sdkroot -static-libgcc -miphoneos-version-min=$MIN_IOS_VERSION"
fix_libgcc_cannot_find_error $sdkroot
ldflags="$ldflags -Lextralibs/"
CC="$cc" \
CPP="$cpp" \
CFLAGS="$cflags" \
LDFLAGS="$ldflags" \
./configure --disable-toolbox-glue --host=$arch-apple-darwin
HOSTPYTHON=./$HOST_PYTHON \
HOSTPGEN=./$HOST_PGEN \
CROSS_COMPILE_TARGET=yes \
make $LIBRARY_NAME
}
function generate_library_for_iphoneos()
{
if [[ ! -e $LIBRARY_NAME_FOR_IPHONEOS ]]; then
pushd $BUILD_FOLDER
arch="armv7" \
sdk="iphoneos" \
cross_compile_library
cp $LIBRARY_NAME ../$LIBRARY_NAME_FOR_IPHONEOS
make inclinstall prefix="$PWD/../install"
#make distclean
popd
fi
}
function generate_library_for_simulator()
{
if [[ ! -e $LIBRARY_NAME_FOR_SIMULATOR ]]; then
pushd $BUILD_FOLDER
arch="i386" \
sdk="iphonesimulator" \
cross_compile_library
cp $LIBRARY_NAME ../$LIBRARY_NAME_FOR_SIMULATOR
make distclean
popd
fi
}
# To generate the universal library
function generate_universal_library()
{
lipo -create \
$LIBRARY_NAME_FOR_IPHONEOS \
$LIBRARY_NAME_FOR_SIMULATOR \
-output $LIBRARY_NAME
}
# The main entry
function build_python_for_iOS()
{
prepare_source_files
generate_host_utilities
apply_cross_compile_patch
generate_library_for_simulator
generate_library_for_iphoneos
generate_universal_library
}
build_python_for_iOS