Skip to content
This repository was archived by the owner on Aug 16, 2019. It is now read-only.

Commit f9e89fd

Browse files
committed
it's working for python 2.7.5
1 parent 36b589f commit f9e89fd

16 files changed

+992
-50
lines changed

Makefile.pre.in-ios-2.7.5.patch

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--- Python-2.7.5/Makefile.pre.in 2013-05-12 11:32:49.000000000 +0800
2+
+++ Makefile.pre.in 2013-07-18 11:04:44.000000000 +0800
3+
@@ -227,6 +227,7 @@
4+
##########################################################################
5+
# Parser
6+
PGEN= Parser/pgen$(EXE)
7+
+PGEN_FOR_BUILD= $(PGEN)
8+
9+
PSRCS= \
10+
Parser/acceler.c \
11+
@@ -593,7 +594,7 @@
12+
$(GRAMMAR_H): $(GRAMMAR_INPUT) $(PGENSRCS)
13+
@$(MKDIR_P) Include
14+
$(MAKE) $(PGEN)
15+
- $(PGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)
16+
+ $(PGEN_FOR_BUILD) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)
17+
$(GRAMMAR_C): $(GRAMMAR_H) $(GRAMMAR_INPUT) $(PGENSRCS)
18+
$(MAKE) $(GRAMMAR_H)
19+
touch $(GRAMMAR_C)

Readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
A Python build script (with patches) for iOS
2+
============================================
3+
4+
Tested for Python 2.7.5, under Xcode 4.6.3 (iOS SDK 6.1)
5+
6+
Usage:
7+
-----
8+
9+
1. The script build.sh will clean current source directory (Python-2.7.5), download Python-2.7.5.tar.bz2 if not exists from python.org, and then extract the source tallball..
10+
2. The script will first build a native (x86) Python for HOST build.
11+
3. Build python for each i386 armv7 armv7s
12+
4. Combine all generated .a into one universal lib, one is libpython2.7.a, the other is liball_extensions.a, which are under build/universal directory. Then the script will clean .py and .pyc files under universal directory, only preserve .pyo lib files.
13+
5. Now, you can copy libpython2.7.a, liball_extensions.a, all include headers, all lib/python2.7/* into your own iOS project, and add header files into your search path, add .a files into your build phrase, depends on your need, add your required modules into your Bundle.
14+
6. Initialize your Python environment
15+
``` objc
16+
NSString * rootPath = [[NSBundle mainBundle] resourcePath];
17+
Py_SetPythonHome((char *)[rootPath cStringUsingEncoding:NSUTF8StringEncoding]);
18+
Py_OptimizeFlag = 1; //If you are using .pyo files
19+
Py_Initialize();
20+
```
21+
7. Enjoy Python on iOS!
22+
23+
24+

build.sh

Lines changed: 178 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,225 @@
77
# build python 2.7.5 for iOS
88
#
99

10-
# Clang
11-
# -Wno-unused-value -Wno-empty-body -Qunused-arguments
12-
13-
# LLVM 2.8
14-
# -no-integrated-as ----- for ctypes
15-
1610
SDKVERSION=6.1
11+
IPHONEOS_MIN_VERSION=5.0
12+
PYTHON_MAJOR_VER="2.7"
1713
PYTHONVER="2.7.5"
1814
PYTHONDIR="Python-$PYTHONVER"
15+
MAKEFILE_PRE_IN_PATCH="Makefile.pre.in-ios-$PYTHONVER.patch"
16+
CONFIGURE_PATCH="configure-ios-$PYTHONVER.patch"
17+
SETUP_PY_PATCH="setup.py-ios-$PYTHONVER.patch"
1918

20-
PWD=`pwd`
19+
PYFEATURES="--enable-shared=no --enable-profiling=no --enable-ipv6=yes --with-threads"
20+
OPTIONAL="--with-signal-module --disable-toolbox-glue"
2121

22-
if [ ! -d "$PYTHONDIR" ]; then
23-
echo "Please download python source code and extract it into $PYTHONDIR"
24-
exit 1
25-
fi
22+
PYTHON_DISABLED_MODULE_LIST=("_ctypes")
23+
24+
ARCHS=(i386 armv7 armv7s)
25+
#ARCHS=(i386)
26+
27+
ROOT=`pwd`
2628

2729
XCODE=`xcode-select -print-path`
2830

2931
if [ ! -d "$XCODE" ]; then
3032
echo "The Developer tools at $XCODE is not found"
3133
exit 1
3234
fi
35+
# build native machine version of Python
3336

34-
#ARCHS="i386 armv7 armv7s"
35-
ARCHS="i386"
36-
for ARCH in ${ARCHS}
37+
BUILD_PATH=$ROOT/build
38+
39+
if [ -d $BUILD_PATH ]; then
40+
echo "Cleaning build directory ..."
41+
rm -rf build
42+
fi
43+
44+
# Due to modifications to multiple files in Python dir, we need a clean source base
45+
if [ -d "$PYTHONDIR" ]; then
46+
echo "Cleaning $PYTHONDIR source ..."
47+
rm -rf "$PYTHONDIR"
48+
fi
49+
50+
if [ ! -f "Python-$PYTHONVER.tar.bz2" ]; then
51+
wget -c "http://python.org/ftp/python/$PYTHONVER/Python-$PYTHONVER.tar.bz2"
52+
fi
53+
echo "Extracting Python-$PYTHONVER.tar.bz2"
54+
tar xf "Python-$PYTHONVER.tar.bz2"
55+
56+
#####################################################################
57+
# for executing setup.py, a cross compliation need a native python first
58+
cd $ROOT/$PYTHONDIR
59+
./configure --prefix=$BUILD_PATH/macosx $PYFEATURES $OPTIONAL
60+
make && make install
61+
if [ 0 != $? ]; then
62+
echo "Making native error, exiting ..."
63+
exit 1
64+
fi
65+
cp Parser/pgen $BUILD_PATH/MacOSX/bin
66+
make clean
67+
cd $ROOT
68+
69+
PYTHON_FOR_BUILD=$BUILD_PATH/MacOSX/bin/python
70+
PGEN_FOR_BUILD=$BUILD_PATH/MacOSX/bin/pgen
71+
72+
####################################################################
73+
echo "patching configure and Makefile ..."
74+
cd $ROOT/$PYTHONDIR
75+
#hack: to use native build of pgen
76+
patch -Np0 <../$MAKEFILE_PRE_IN_PATCH
77+
#hack: 1. to add cross complication support to iOS/MacOSX
78+
# 2. to fix a bug for checking getaddrinfo
79+
patch -Np0 <../$CONFIGURE_PATCH
80+
#hack: 1. to add a enabled_module_list (not used at the moment)
81+
# 2. to work around to build static libaries of modules
82+
patch -Np0 <../$SETUP_PY_PATCH
83+
84+
#hack: to remove ctypes support
85+
LIST=""
86+
for module in ${PYTHON_DISABLED_MODULE_LIST[@]}
3787
do
38-
echo "building arch: $ARCH ..."
88+
LIST="\"$module\", $LIST"
89+
done
90+
91+
if [ "$LIST" != "" ]; then
92+
sed -e "s/\(disabled_module_list = \)\[\]/\1\[$LIST\]/" -i .org setup.py
93+
fi
3994

95+
cd $ROOT
96+
97+
##################################################################
98+
# configure needs config.site for cross compiling
99+
touch "$PYTHONDIR/config.site"
100+
echo "ac_cv_file__dev_ptmx=no" > "$ROOT/$PYTHONDIR/config.site"
101+
echo "ac_cv_file__dev_ptc=no" >> "$ROOT/$PYTHONDIR/config.site"
102+
export CONFIG_SITE="$ROOT/$PYTHONDIR/config.site"
103+
104+
for ARCH in ${ARCHS[@]}
105+
do
106+
echo ""
107+
echo "building arch: $ARCH ..."
108+
echo ""
109+
40110
unset CC
41111
unset CFLAGS
42112
unset CPP
43113
unset CPPFLAGS
44114
unset LD
45115
unset LDFLAGS
46-
116+
unset AR
117+
47118
if [ "$ARCH" == "i386" ]; then
48119
PLATFORM="iPhoneSimulator"
49120
else
50121
PLATFORM="iPhoneOS"
122+
CFLAGS="-mthumb "
51123
fi
52-
124+
125+
# adding -miphoneos-version-min to surpress compilation errors
53126
DEVELOPER="$XCODE/Platforms/$PLATFORM.platform/Developer"
54127
SDKROOT="$DEVELOPER/SDKs/$PLATFORM$SDKVERSION.sdk"
55128
export CC="$DEVELOPER/usr/bin/llvm-gcc"
56-
export CFLAGS="-arch $ARCH -I$SDKROOT/usr/include -isysroot $SDKROOT"
57-
129+
export CFLAGS+="-arch $ARCH -O2 -I$SDKROOT/usr/include -isysroot $SDKROOT -miphoneos-version-min=$IPHONEOS_MIN_VERSION -F$SDKROOT/System/Library/Frameworks"
130+
58131
export CPPFLAGS=$CFLAGS
59-
132+
60133
export CXX="$DEVELOPER/usr/bin/llvm-g++"
61134
export CXXFLAGS=$CPPFLAGS
62-
135+
63136
export LD="$DEVELOPER/usr/bin/ld"
64-
export LDFLAGS="-isysroot "$SDKROOT" -L$SDKROOT/usr/lib"
65-
137+
export LDFLAGS="-arch $ARCH -L$SDKROOT/usr/lib -miphoneos-version-min=$IPHONEOS_MIN_VERSION -F$SDKROOT/System/Library/Frameworks -L$SDKROOT/usr/lib/system"
138+
139+
export AR="$DEVELOPER/usr/bin/ar"
66140

67-
PYINSTDIR="--prefix=$PWD/python-staticlib-ios/$ARCH"
141+
PYINSTDIR="--prefix=$BUILD_PATH/$ARCH"
68142
# due to some bug of configure script, you must remove a "exit 1" when testing availablity of getaddrinfo
69-
patch -Np0 $PWD/$PYTHONDIR/configure <$PWD/configure.patch
70-
PYFEATURES="--enable-shared=no --enable-profiling=no --enable-ipv6=yes --with-threads"
71-
OPTIONAL="--with-system-ffi --with-signal-module --disable-toolbox-glue"
72-
BUILD_TARGET="`$CC -arch $ARCH -v 2>&1| sed -nE 's/Target: (.+)/\1/p'`"
73-
BUILD="--build $BUILD_TARGET"
74-
HOST="--host `$CC -v 2>&1 | sed -nE 's/Target: (.+)/\1/p'`"
143+
#BUILD_TARGET="`$CC -arch $ARCH -v 2>&1| sed -nE 's/Target: (.+)/\1/p'`"
144+
BUILD_TARGET="$ARCH-apple-darwin"
145+
BUILD="--build=$BUILD_TARGET"
146+
147+
HOST_PLATFORM="`$CC -v 2>&1 | sed -nE 's/Target: (.+)/\1/p'`"
148+
HOST="--host=$HOST_PLATFORM"
149+
75150
TARGET="--target $BUILD_TARGET"
76-
77-
cd $PYTHONDIR
78-
./configure $BUILD $HOST $TARGET $PYINSTDIR $PYFEATURES $OPTIONAL
79-
#$HOST $BUILD $TARGET"
80-
151+
152+
export _PYTHON_HOST_PLATFORM="$HOST_PLATFORM"
153+
154+
cd "$ROOT/$PYTHONDIR"
155+
PYTHON_FOR_BUILD="$PYTHON_FOR_BUILD" PGEN_FOR_BUILD="$PGEN_FOR_BUILD" ./configure $BUILD $HOST $TARGET $PYINSTDIR $PYFEATURES $OPTIONAL
156+
81157
if [ 0 != $? ]; then
82158
echo "exiting ..."
83-
cd $PWD
159+
cd $ROOT
84160
exit 1
85161
fi
86-
make && make install && make clean
87-
cd $PWD
162+
163+
164+
PYTHON_FOR_BUILD=$PYTHON_FOR_BUILD PGEN_FOR_BUILD=$PGEN_FOR_BUILD make libpython$PYTHON_MAJOR_VER.a sharedmods && make bininstall libinstall sharedinstall inclinstall
165+
if [ 0 != $? ]; then
166+
echo "making error, exiting ..."
167+
cd "$ROOT"
168+
exit 1
169+
fi
170+
echo ""
171+
echo "Building python modules ..."
172+
CC="$CC" CFLAGS="$CFLAGS" AR="$AR" $PYTHON_FOR_BUILD setup.py install_lib
173+
make clean
174+
cd "$ROOT"
88175
done
89176

177+
#if [ -f "$PYTHONDIR/config.site" ]; then
178+
# rm -f "$PYTHONDIR/config.site"
179+
#fi
180+
#export CONFIG_SITE=
181+
#
182+
#cd $ROOT/$PYTHONDIR
183+
#if [ -f "setup.py.org" ]; then
184+
# cp -f setup.py.org setup.py
185+
#fi
186+
#cd $ROOT
187+
188+
echo "Packaging ..."
189+
cd $BUILD_PATH
190+
191+
mkdir -p universal/{lib,include}
192+
LIBPYTHON_FILES=""
193+
EXTENSIONS_FILES=""
194+
for ARCH in ${ARCHS[@]}
195+
do
196+
LIBPYTHON_FILES+="$ARCH/lib/libpython$PYTHON_MAJOR_VER.a "
197+
# because building of static library is just a work around, the path for the
198+
# lib is not very proper. I don't want to make more modifications, so just
199+
# leave as it.
200+
EXTENSIONS_FILES+="$ARCH/lib/python$PYTHON_MAJOR_VER/lib-dynload/liball_extensions.a "
201+
done
202+
203+
echo "Combing multi-arch libpython$PYTHON_MAJOR_VER.a ..."
204+
lipo $LIBPYTHON_FILES -create -output universal/libpython$PYTHON_MAJOR_VER.a
205+
206+
if [ 0 != $? ]; then
207+
echo "Create universal lib error, exiting ..."
208+
cd "$ROOT"
209+
exit 1
210+
fi
211+
212+
echo "Combing multi-arch liball_extensions2.7.a ..."
213+
lipo $EXTENSIONS_FILES -create -output universal/liball_extensions$PYTHON_MAJOR_VER.a
214+
215+
if [ 0 != $? ]; then
216+
echo "Create universal lib error, exiting ..."
217+
cd "$ROOT"
218+
exit 1
219+
fi
220+
221+
ARCH=${ARCHS[0]}
222+
echo "Copying lib files ..."
223+
cp -r $ARCH/lib/python$PYTHON_MAJOR_VER universal/lib/
224+
cp -r $ARCH/include/python$PYTHON_MAJOR_VER universal/include/
225+
find universal -iname "*.pyc" -exec rm -f {} \;
226+
find universal -iname "*.py" -exec rm -f {} \;
227+
rm -rf universal/lib/python$PYTHON_MAJOR_VER/lib-dynload
228+
229+
cd $ROOT
90230
echo "Done."
91231

configure-ios-2.7.5.patch

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--- configure.org 2013-07-18 13:28:42.000000000 +0800
2+
+++ configure 2013-07-18 13:28:48.000000000 +0800
3+
@@ -3174,6 +3174,9 @@
4+
*-*-cygwin*)
5+
ac_sys_system=Cygwin
6+
;;
7+
+ *-*-darwin*)
8+
+ ac_sys_system=Darwin
9+
+ ;;
10+
*)
11+
# for now, limit cross builds to known configurations
12+
MACHDEP="unknown"
13+
@@ -3220,6 +3223,15 @@
14+
*-*-cygwin*)
15+
_host_cpu=
16+
;;
17+
+ *-*-darwin*)
18+
+ case "$host_cpu" in
19+
+ arm*)
20+
+ _host_cpu=arm
21+
+ ;;
22+
+ *)
23+
+ _host_cpu=$host_cpu
24+
+ esac
25+
+ ;;
26+
*)
27+
# for now, limit cross builds to known configurations
28+
MACHDEP="unknown"
29+
@@ -11476,7 +11488,7 @@
30+
then
31+
echo 'Fatal: You must get working getaddrinfo() function.'
32+
echo ' or you can specify "--disable-ipv6"'.
33+
- exit 1
34+
+ #exit 1
35+
fi
36+
else
37+

configure.patch

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)