-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh
executable file
·137 lines (107 loc) · 3.37 KB
/
run.sh
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
#!/bin/bash
#
# See the documentation in README.md. When you're ready, just execute ./run.sh
# and the output will be placed in a new directory named 'build'.
#
# these tools will be used
gcc=`which gcc`
make=`which make`
patch=`which patch`
head=`which head`
tar=`which tar`
makeParallel=-j8
# get the location of this script
scriptDir=$(cd $(dirname $0) && pwd)
# create the output directory
outDir=$scriptDir/build
mkdir -p $outDir
# define some useful variables
filesDir=$scriptDir/files
pyversion=2.7
pythonPackageName=Python-2.7.3
numpyPackageName=numpy-1.6.2
pythonSourceDir=$outDir/$pythonPackageName
numpySourceDir=$outDir/$numpyPackageName
installDir=$outDir/install
python=$installDir/bin/python
freezeOutputDir=$outDir/freezeOutput
# remove python related environment variables
unset PYTHONHOME
unset PYTHONPATH
unset PYTHONSTARTUP
############################
# define the script routines
extractAndBuildPython()
{
cd $outDir
$tar -zxf $filesDir/$pythonPackageName.tgz
cd $pythonSourceDir
# patch python import
$patch -p0 < $filesDir/patch-python-import.diff
# setup builtin modules
cp $filesDir/Setup.local ./Modules/
./configure install --disable-shared --prefix $installDir || exit
$make $makeParallel || exit
$make install || exit
}
extractAndBuildNumpy()
{
cd $outDir
$tar -zxf $filesDir/$numpyPackageName.tar.gz
cd $numpyPackageName
$python ./setup.py install --prefix $installDir || exit
}
runFreezeTool()
{
targetScript=$filesDir/freezeInputScript.py
freezeScript=$pythonSourceDir/Tools/freeze/freeze.py
ignores="-X codecs -X copy -X distutils -X encodings -X locale -X macpath -X ntpath -X os2emxpath -X popen2 -X pydoc -X readline -X _warnings"
export PYTHONHOME=$installDir
export PYTHONPATH=$installDir/lib/python$pyversion/site-packages
$python -S $freezeScript -o $freezeOutputDir -p $pythonSourceDir $ignores $targetScript || exit
unset PYTHONHOME
unset PYTHONPATH
# convert frozen.c to frozen.h by removing the "int main()" function at the end
cd $freezeOutputDir
numberOfLines=$(cat frozen.c | wc -l)
numberOfLinesToKeep=$(expr $numberOfLines - 9)
$head -n $numberOfLinesToKeep frozen.c > frozen.h
}
buildHelloNumpy()
{
cd $outDir
cp -r $filesDir/helloNumpy ./
cd helloNumpy
pythonInclude=$installDir/include/python$pyversion
pythonLibrary=$installDir/lib/libpython$pyversion.a
numpyBuildDir=$numpySourceDir/build
# glob numpy .o files and frozen .c source files
numpyObjects=$(find $numpyBuildDir -name \*.o)
frozenSources=$(find $freezeOutputDir -name M_\*.c)
# osx and linux take slightly different linking flags
if [ "$(uname)" == "Darwin" ]; then
linkFlags=""
requiredBlasLibrary="-framework Accelerate"
else
linkFlags="-Xlinker -export-dynamic"
requiredBlasLibrary=""
fi
requiredLibs="-lpthread -lm -ldl -lutil"
# compile hello.c
$gcc $linkFlags -o hello hello.c -I$pythonInclude $pythonLibrary $requiredLibs
# compile hello.c with HELLO_FROZEN defined
# this build includes the numpy object files and frozen source files
$gcc $linkFlags -DHELLO_FROZEN -o helloFrozen hello.c -I$pythonInclude -I$freezeOutputDir \
$frozenSources \
$numpyObjects \
$pythonLibrary \
$requiredBlasLibrary \
$requiredLibs
}
##########################
# call the script routines
set -x
extractAndBuildPython
extractAndBuildNumpy
runFreezeTool
buildHelloNumpy