forked from clementfarabet/torch-ios
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_ios_framework
executable file
·184 lines (156 loc) · 5.92 KB
/
generate_ios_framework
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
#!/bin/bash
set -e
SCRIPT_DIR=$( cd $(dirname $0) ; pwd -P )
XCODE_ROOT=`xcode-select -print-path`
IPHONE_SDKVERSION=`xcodebuild -showsdks | grep iphoneos | egrep "[[:digit:]]+\.[[:digit:]]+" -o | tail -1`
ARM_DEV_CMD="xcrun --sdk iphoneos"
SIM_DEV_CMD="xcrun --sdk iphonesimulator"
EXTRA_FLAGS="-miphoneos-version-min=6.0 -fembed-bitcode"
configure_exports() {
if [[ $1 = arm* ]]; then
DEV_CMD=$ARM_DEV_CMD
else
DEV_CMD=$SIM_DEV_CMD
fi
if [[ $1 = armv7* ]]; then
NEON_FLAGS=" -D__NEON__ -mfpu=neon"
else
NEON_FLAGS=""
fi
export CXX="$DEV_CMD clang++ -arch $1 $EXTRA_FLAGS $NEON_FLAGS"
export CC="$DEV_CMD clang -arch $1 $EXTRA_FLAGS $NEON_FLAGS"
XCMAKE=`$DEV_CMD --find make`
XCPATH=`dirname $XCMAKE`
export PATH="$XCPATH:$PATH"
XCRANLIB=`$DEV_CMD --find ranlib`
XCPATH=`dirname $XCRANLIB`
export PATH="$XCPATH:$PATH"
}
generate_arch() {
configure_exports $1
rm -rf $SCRIPT_DIR/build
mkdir -p $SCRIPT_DIR/build
cd $SCRIPT_DIR/build
SROOT=`$DEV_CMD --show-sdk-path`
cmake .. -DCMAKE_INSTALL_PREFIX="$SCRIPT_DIR/installed/" -DCMAKE_OSX_SYSROOT=$SROOT
make install
cd $SCRIPT_DIR
mkdir -p $SCRIPT_DIR/framework/lua
mkdir -p $SCRIPT_DIR/framework/lib/$1
mkdir -p $SCRIPT_DIR/framework/include
# copy includes
cp -r $SCRIPT_DIR/installed/include/torch/* $SCRIPT_DIR/framework/include
rm -rf $SCRIPT_DIR/installed/
# copy libs
cp $SCRIPT_DIR/build/exe/lua/libtorch-lua-static.a $SCRIPT_DIR/framework/lib/$1/
cp $SCRIPT_DIR/build/lib/luaT/libluaT.a $SCRIPT_DIR/framework/lib/$1
cp $SCRIPT_DIR/build/lib/TH/libTH.a $SCRIPT_DIR/framework/lib/$1
cp $SCRIPT_DIR/build/pkg/torch/libtorch.a $SCRIPT_DIR/framework/lib/$1
cp $SCRIPT_DIR/build/3rdparty/nn/libnn.a $SCRIPT_DIR/framework/lib/$1
cp $SCRIPT_DIR/build/3rdparty/image/libimage.a $SCRIPT_DIR/framework/lib/$1
cp $SCRIPT_DIR/build/3rdparty/nnx/libnnx.a $SCRIPT_DIR/framework/lib/$1
cp $SCRIPT_DIR/build/3rdparty/cjson/libcjson.a $SCRIPT_DIR/framework/lib/$1
# copy lua scripts
mkdir -p $SCRIPT_DIR/framework/lua/torch
cp -r $SCRIPT_DIR/pkg/torch/*.lua $SCRIPT_DIR/framework/lua/torch/
mkdir -p $SCRIPT_DIR/framework/lua/dok
cp -r $SCRIPT_DIR/pkg/dok/*.lua $SCRIPT_DIR/framework/lua/dok/
mkdir -p $SCRIPT_DIR/framework/lua/nn
cp -r $SCRIPT_DIR/3rdparty/nn/*.lua $SCRIPT_DIR/framework/lua/nn/
mkdir -p $SCRIPT_DIR/framework/lua/nnx
cp -r $SCRIPT_DIR/3rdparty/nnx/*.lua $SCRIPT_DIR/framework/lua/nnx/
mkdir -p $SCRIPT_DIR/framework/lua/image
cp -r $SCRIPT_DIR/3rdparty/image/*.lua $SCRIPT_DIR/framework/lua/image/
}
extract_archive() {
mkdir $1
cd $1
ar -x ../lib$1.a
cd ..
}
extract_recombine() {
cd $SCRIPT_DIR/framework/lib/$1
extract_archive cjson
extract_archive image
extract_archive luaT
extract_archive nn
extract_archive nnx
extract_archive TH
extract_archive torch
extract_archive torch-lua-static
ar -qc libtorch-$1.a cjson/*.o image/*.o luaT/*.o nn/*.o nnx/*.o TH/*.o \
torch/*.o torch-lua-static/*.o
cd $SCRIPT_DIR
}
# $SCRIPT_DIR/framework/lib/armv7/libtorch-armv7.a \
# $SCRIPT_DIR/framework/lib/armv7s/libtorch-armv7s.a \
# $SCRIPT_DIR/framework/lib/i386/libtorch-i386.a \
# $SCRIPT_DIR/framework/lib/x86_64/libtorch-x86_64.a \
lipo_libs() {
$ARM_DEV_CMD lipo -create \
$SCRIPT_DIR/framework/lib/arm64/libtorch-arm64.a \
-output $SCRIPT_DIR/framework/lib/libtorch.a
}
rm -rf $SCRIPT_DIR/framework/lua
rm -rf $SCRIPT_DIR/framework/lib
rm -rf $SCRIPT_DIR/framework/include
rm -rf $SCRIPT_DIR/Torch.framework
echo "generating torch"
# generate_arch armv7
# generate_arch armv7s
generate_arch arm64
# generate_arch i386
# generate_arch x86_64
echo "extracting archives and recombining"
# extract_recombine armv7
# extract_recombine armv7s
extract_recombine arm64
# extract_recombine i386
# extract_recombine x86_64
echo "creating universal binary"
lipo_libs
echo "creating framework"
mkdir -p $SCRIPT_DIR/Torch.framework/Versions/A/Headers
cp $SCRIPT_DIR/framework/lib/libtorch.a \
$SCRIPT_DIR/Torch.framework/Versions/A/Torch
cp -r $SCRIPT_DIR/framework/include/* \
$SCRIPT_DIR/Torch.framework/Versions/A/Headers
cp -r $SCRIPT_DIR/framework/lua \
$SCRIPT_DIR/Torch.framework/Versions/A/Resources
cd $SCRIPT_DIR/Torch.framework/Versions
ln -s A Current
cd $SCRIPT_DIR/Torch.framework
ln -s Versions/Current/Torch Torch
ln -s Versions/Current/Headers Headers
ln -s Versions/Current/Resources Resources
echo "creating header files"
HEADER=$SCRIPT_DIR/Torch.framework/Versions/A/Headers/Torch.h
echo '#ifndef TORCH_IOS_FRAMEWORK_TORCH_H' > $HEADER
echo '#define TORCH_IOS_FRAMEWORK_TORCH_H' >> $HEADER
echo '#ifdef __cplusplus' >> $HEADER
echo 'extern "C" {' >> $HEADER
echo '#endif' >> $HEADER
echo '#include "TH/TH.h"' >> $HEADER
echo '#include "lua.h"' >> $HEADER
echo '#include "luaconf.h"' >> $HEADER
echo '#include "lauxlib.h"' >> $HEADER
echo '#include "luaT.h"' >> $HEADER
echo '#include "lualib.h"' >> $HEADER
echo '#ifdef __cplusplus' >> $HEADER
echo '}' >> $HEADER
echo '#endif' >> $HEADER
echo '#endif' >> $HEADER
echo >> $HEADER
cat $SCRIPT_DIR/Torch.framework/Versions/A/Headers/luaT.h | \
sed 's/include [<]/include "/g' | \
sed 's/[>]$/"/g' > \
$SCRIPT_DIR/Torch.framework/Versions/A/Headers/luaT-new.h
mv $SCRIPT_DIR/Torch.framework/Versions/A/Headers/luaT-new.h \
$SCRIPT_DIR/Torch.framework/Versions/A/Headers/luaT.h
echo "==> exported complete framework in Torch.framework"
echo " in Xcode just drag Torch.framework into"
echo " Build Phases > Link Binary with Libraries"
echo " and add path to Torch.framework parent folder in"
echo " Build Settings > Framework Search Paths"
echo
echo " Include headers with #include <Torch/Torch.h>"