forked from aptogo/OpenCVForiPhone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencvbuild.sh
executable file
·141 lines (117 loc) · 4.89 KB
/
opencvbuild.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
137
138
139
140
141
#!/bin/sh
################################################################################
# OpenCVBuild.sh - build OpenCV for iOS
#
# Usage: ./OpenCVBuild.sh <opencv source directory> <build directory>
#
# This script creates three library/header packages and one framework in the
# build directory:
#
# OpenCV_iPhoneSimulator - Library and headers for simulator
# OpenCV_iPhoneOS - Library and headers for device
# OpenCV_Universal - Universal libaries and headers (for simulator AND device)
# OpenCV.framework - Universal libraries and headers combined into an iOS framework
#
# OpenCV source can be obtained using Subversion:
#
# svn co https://code.ros.org/svn/opencv/trunk
#
# (c)2011 Robin Summerhill, Aptogo Limited. http://aptogo.co.uk
# This script is distributed under the GPLv2 licence.
# Based on work by Eugene Khvedchenya (OpenCV build) and Diney Bomfim (iOS framework)
#
set -e
set -u
if [ $# -ne 2 ]
then
echo "Syntax: $0 <opencv source directory> <build directory>"
exit
fi
CONFIGURATION=Release
FRAMEWORK_NAME=OpenCV
# Absolute path to the source code directory.
D=`dirname "$1"`
B=`basename "$1"`
SRC_DIR="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"
# Absolute path to build directory
D=`dirname "$2"`
B=`basename "$2"`
BUILD_DIR="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"
# Åbsolute path to temporary build directory
TEMP_BUILD_DIR="$BUILD_DIR/tmp"
# Final installation locations for packages
INSTALL_DIR="$BUILD_DIR"
IPHONE_SIMULATOR_INSTALL_DIR="$INSTALL_DIR/OpenCV_iPhoneSimulator"
IPHONE_OS_INSTALL_DIR="$INSTALL_DIR/OpenCV_iPhoneOS"
UNIVERSAL_INSTALL_DIR="$INSTALL_DIR/OpenCV_Universal"
FRAMEWORK_DIR="$INSTALL_DIR/$FRAMEWORK_NAME.framework"
echo "OpenCV source :" $SRC_DIR
echo "Build directory :" $BUILD_DIR
################################################################################
# Clean previous build
rm -rf $TEMP_BUILD_DIR
rm -rf $IPHONE_SIMULATOR_INSTALL_DIR
rm -rf $IPHONE_OS_INSTALL_DIR
rm -rf $UNIVERSAL_INSTALL_DIR
rm -rf $FRAMEWORK_DIR
mkdir -p $BUILD_DIR
mkdir -p $TEMP_BUILD_DIR
# Build is performed in TEMP_BUILD_DIR
cd $TEMP_BUILD_DIR
################################################################################
# Build for simulator
echo "Building for iphone simulator"
cmake -GXcode \
-DCMAKE_TOOLCHAIN_FILE=$SRC_DIR/ios/cmake/Toolchains/Toolchain-iPhoneSimulator_Xcode.cmake \
-DCMAKE_INSTALL_PREFIX=$IPHONE_SIMULATOR_INSTALL_DIR \
-DOPENCV_BUILD_3RDPARTY_LIBS=YES \
-DCMAKE_XCODE_ATTRIBUTE_GCC_VERSION="com.apple.compilers.llvmgcc42" \
$SRC_DIR
xcodebuild -sdk iphonesimulator -configuration $CONFIGURATION -target install
################################################################################
# Copy third party and opencv libs:
cp -f $TEMP_BUILD_DIR/3rdparty/lib/$CONFIGURATION/*.a $IPHONE_SIMULATOR_INSTALL_DIR/lib/
cp -f $TEMP_BUILD_DIR/lib/$CONFIGURATION/*.a $IPHONE_SIMULATOR_INSTALL_DIR/lib/
################################################################################
# Build for device
echo "Building for iphone device"
cmake -GXcode \
-DCMAKE_TOOLCHAIN_FILE=$SRC_DIR/ios/cmake/Toolchains/Toolchain-iPhoneDevice_Xcode.cmake \
-DCMAKE_INSTALL_PREFIX=$IPHONE_OS_INSTALL_DIR \
-DOPENCV_BUILD_3RDPARTY_LIBS=YES \
-DCMAKE_XCODE_ATTRIBUTE_GCC_VERSION="com.apple.compilers.llvmgcc42" \
$SRC_DIR
xcodebuild -sdk iphoneos -configuration $CONFIGURATION -target install
################################################################################
# Copy third party and opencv libs:
cp -f $TEMP_BUILD_DIR/3rdparty/lib/$CONFIGURATION/*.a $IPHONE_OS_INSTALL_DIR/lib/
cp -f $TEMP_BUILD_DIR/lib/$CONFIGURATION/*.a $IPHONE_OS_INSTALL_DIR/lib/
################################################################################
# Create universal installation package
mkdir -p $UNIVERSAL_INSTALL_DIR
mkdir -p $UNIVERSAL_INSTALL_DIR/lib
cp -R $IPHONE_SIMULATOR_INSTALL_DIR/include $UNIVERSAL_INSTALL_DIR/include
cp -R $IPHONE_SIMULATOR_INSTALL_DIR/share $UNIVERSAL_INSTALL_DIR/share
# Create fat binaries for OpenCV libraries
for FILE in `ls $IPHONE_OS_INSTALL_DIR/lib/ | grep .a`
do
lipo $IPHONE_OS_INSTALL_DIR/lib/$FILE \
$IPHONE_SIMULATOR_INSTALL_DIR/lib/$FILE \
-create -output $UNIVERSAL_INSTALL_DIR/lib/$FILE
done
################################################################################
# Create iOS framework
mkdir -p $FRAMEWORK_DIR
# Combine all libraries into one - required for framework
libtool -o $FRAMEWORK_DIR/$FRAMEWORK_NAME $UNIVERSAL_INSTALL_DIR/lib/*.a 2> /dev/null
# Copy public headers into framework
cp -R $UNIVERSAL_INSTALL_DIR/include $FRAMEWORK_DIR/Headers
# Fix-up header files to use standard framework-style include paths
for FILE in `find "$FRAMEWORK_DIR/Headers" -type f`
do
sed -i "" 's:#include "opencv2/\(.*\)":#include <OpenCV/opencv2/\1>:' "$FILE"
done
################################################################################
# Finished
echo "$0 completed successfully"
exit 0