Home | History | Annotate | Download | only in makefile
      1 #!/bin/bash
      2 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 # ==============================================================================
     16 # Builds protobuf 3 for iOS.
     17 
     18 set -e
     19 
     20 if [[ -n MACOSX_DEPLOYMENT_TARGET ]]; then
     21     export MACOSX_DEPLOYMENT_TARGET=$(sw_vers -productVersion)
     22 fi
     23 
     24 usage() {
     25   echo "Usage: $(basename "$0") [-a]"
     26   echo "-a [build_arch] build for specified arch comma separate for multiple archs (eg: x86_64,arm64)"
     27   echo "default arch i386, x86_64, armv7, armv7s, arm64"
     28   exit 1
     29 }
     30 
     31 BUILD_TARGET="i386 x86_64 armv7 armv7s arm64"
     32 while getopts "a:" opt_name; do
     33   case "$opt_name" in
     34     a) BUILD_TARGET="${OPTARG}";;
     35     *) usage;;
     36   esac
     37 done
     38 shift $((OPTIND - 1))
     39 
     40 IFS=' ' read -r -a build_targets <<< "${BUILD_TARGET}"
     41 
     42 SCRIPT_DIR=$(cd `dirname $0` && pwd)
     43 source "${SCRIPT_DIR}/build_helper.subr"
     44 
     45 cd ${SCRIPT_DIR}
     46 
     47 HOST_GENDIR="$(pwd)/gen/protobuf-host"
     48 mkdir -p "${HOST_GENDIR}"
     49 if [[ ! -f "./downloads/protobuf/autogen.sh" ]]; then
     50     echo "You need to download dependencies before running this script." 1>&2
     51     echo "tensorflow/contrib/makefile/download_dependencies.sh" 1>&2
     52     exit 1
     53 fi
     54 
     55 JOB_COUNT="${JOB_COUNT:-$(get_job_count)}"
     56 
     57 GENDIR=$(pwd)/gen/protobuf_ios/
     58 LIBDIR=${GENDIR}lib
     59 mkdir -p ${LIBDIR}
     60 
     61 OSX_VERSION=darwin14.0.0
     62 
     63 IPHONEOS_PLATFORM=$(xcrun --sdk iphoneos --show-sdk-platform-path)
     64 IPHONEOS_SYSROOT=$(xcrun --sdk iphoneos --show-sdk-path)
     65 IPHONESIMULATOR_PLATFORM=$(xcrun --sdk iphonesimulator --show-sdk-platform-path)
     66 IPHONESIMULATOR_SYSROOT=$(xcrun --sdk iphonesimulator --show-sdk-path)
     67 IOS_SDK_VERSION=$(xcrun --sdk iphoneos --show-sdk-version)
     68 MIN_SDK_VERSION=8.0
     69 
     70 CFLAGS="-DNDEBUG -Os -pipe -fPIC -fno-exceptions"
     71 CXXFLAGS="${CFLAGS} -std=c++11 -stdlib=libc++"
     72 LDFLAGS="-stdlib=libc++"
     73 LIBS="-lc++ -lc++abi"
     74 
     75 cd downloads/protobuf
     76 PROTOC_PATH="${HOST_GENDIR}/bin/protoc"
     77 if [[ ! -f "${PROTOC_PATH}" || ${clean} == true ]]; then
     78   # Try building compatible protoc first on host
     79   echo "protoc not found at ${PROTOC_PATH}. Build it first."
     80   make_host_protoc "${HOST_GENDIR}"
     81 else
     82   echo "protoc found. Skip building host tools."
     83 fi
     84 
     85 # Remove old libs
     86 rm -f ${LIBDIR}/libprotobuf.a
     87 rm -f ${LIBDIR}/libprotobuf-lite.a
     88 
     89 ./autogen.sh
     90 if [ $? -ne 0 ]
     91 then
     92   echo "./autogen.sh command failed."
     93   exit 1
     94 fi
     95 
     96 package_pb_library() {
     97     pb_libs="${LIBDIR}/${1}/lib/libprotobuf.a"
     98     if [ -f "${LIBDIR}/libprotobuf.a" ]; then
     99         pb_libs="$pb_libs ${LIBDIR}/libprotobuf.a"
    100     fi
    101     lipo \
    102     $pb_libs \
    103     -create \
    104     -output ${LIBDIR}/libprotobuf.a
    105 
    106     pblite_libs="${LIBDIR}/${1}/lib/libprotobuf-lite.a"
    107     if [ -f "${LIBDIR}/libprotobuf-lite.a" ]; then
    108         pblite_libs="$pblite_libs ${LIBDIR}/libprotobuf-lite.a"
    109     fi
    110     lipo \
    111     $pblite_libs \
    112     -create \
    113     -output ${LIBDIR}/libprotobuf-lite.a
    114 }
    115 
    116 build_target() {
    117 case "$1" in
    118     i386)  make distclean
    119         ./configure \
    120         --host=i386-apple-${OSX_VERSION} \
    121         --disable-shared \
    122         --enable-cross-compile \
    123         --with-protoc="${PROTOC_PATH}" \
    124         --prefix=${LIBDIR}/iossim_386 \
    125         --exec-prefix=${LIBDIR}/iossim_386 \
    126         "CFLAGS=${CFLAGS} \
    127         -mios-simulator-version-min=${MIN_SDK_VERSION} \
    128         -arch i386 \
    129         -fembed-bitcode \
    130         -isysroot ${IPHONESIMULATOR_SYSROOT}" \
    131         "CXX=${CXX}" \
    132         "CXXFLAGS=${CXXFLAGS} \
    133         -mios-simulator-version-min=${MIN_SDK_VERSION} \
    134         -arch i386 \
    135         -fembed-bitcode \
    136         -isysroot \
    137         ${IPHONESIMULATOR_SYSROOT}" \
    138         LDFLAGS="-arch i386 \
    139         -fembed-bitcode \
    140         -mios-simulator-version-min=${MIN_SDK_VERSION} \
    141         ${LDFLAGS} \
    142         -L${IPHONESIMULATOR_SYSROOT}/usr/lib/ \
    143         -L${IPHONESIMULATOR_SYSROOT}/usr/lib/system" \
    144         "LIBS=${LIBS}"
    145         make -j"${JOB_COUNT}"
    146         make install
    147 
    148         package_pb_library "iossim_386"
    149         ;;
    150 
    151     x86_64) make distclean
    152         ./configure \
    153         --host=x86_64-apple-${OSX_VERSION} \
    154         --disable-shared \
    155         --enable-cross-compile \
    156         --with-protoc="${PROTOC_PATH}" \
    157         --prefix=${LIBDIR}/iossim_x86_64 \
    158         --exec-prefix=${LIBDIR}/iossim_x86_64 \
    159         "CFLAGS=${CFLAGS} \
    160         -mios-simulator-version-min=${MIN_SDK_VERSION} \
    161         -arch x86_64 \
    162         -fembed-bitcode \
    163         -isysroot ${IPHONESIMULATOR_SYSROOT}" \
    164         "CXX=${CXX}" \
    165         "CXXFLAGS=${CXXFLAGS} \
    166         -mios-simulator-version-min=${MIN_SDK_VERSION} \
    167         -arch x86_64 \
    168         -fembed-bitcode \
    169         -isysroot \
    170         ${IPHONESIMULATOR_SYSROOT}" \
    171         LDFLAGS="-arch x86_64 \
    172         -fembed-bitcode \
    173         -mios-simulator-version-min=${MIN_SDK_VERSION} \
    174         ${LDFLAGS} \
    175         -L${IPHONESIMULATOR_SYSROOT}/usr/lib/ \
    176         -L${IPHONESIMULATOR_SYSROOT}/usr/lib/system" \
    177         "LIBS=${LIBS}"
    178         make -j"${JOB_COUNT}"
    179         make install
    180 
    181         package_pb_library "iossim_x86_64"
    182         ;;
    183 
    184     armv7) make distclean
    185         ./configure \
    186         --host=armv7-apple-${OSX_VERSION} \
    187         --with-protoc="${PROTOC_PATH}" \
    188         --disable-shared \
    189         --prefix=${LIBDIR}/ios_arm7 \
    190         --exec-prefix=${LIBDIR}/ios_arm7 \
    191         "CFLAGS=${CFLAGS} \
    192         -miphoneos-version-min=${MIN_SDK_VERSION} \
    193         -arch armv7 \
    194         -fembed-bitcode \
    195         -isysroot ${IPHONEOS_SYSROOT}" \
    196         "CXX=${CXX}" \
    197         "CXXFLAGS=${CXXFLAGS} \
    198         -miphoneos-version-min=${MIN_SDK_VERSION} \
    199         -arch armv7 \
    200         -fembed-bitcode \
    201         -isysroot ${IPHONEOS_SYSROOT}" \
    202         LDFLAGS="-arch armv7 \
    203         -fembed-bitcode \
    204         -miphoneos-version-min=${MIN_SDK_VERSION} \
    205         ${LDFLAGS}" \
    206         "LIBS=${LIBS}"
    207         make -j"${JOB_COUNT}"
    208         make install
    209 
    210         package_pb_library "ios_arm7"
    211         ;;
    212 
    213     armv7s) make distclean
    214         ./configure \
    215         --host=armv7s-apple-${OSX_VERSION} \
    216         --with-protoc="${PROTOC_PATH}" \
    217         --disable-shared \
    218         --prefix=${LIBDIR}/ios_arm7s \
    219         --exec-prefix=${LIBDIR}/ios_arm7s \
    220         "CFLAGS=${CFLAGS} \
    221         -miphoneos-version-min=${MIN_SDK_VERSION} \
    222         -arch armv7s \
    223         -fembed-bitcode \
    224         -isysroot ${IPHONEOS_SYSROOT}" \
    225         "CXX=${CXX}" \
    226         "CXXFLAGS=${CXXFLAGS} \
    227         -miphoneos-version-min=${MIN_SDK_VERSION} \
    228         -arch armv7s \
    229         -fembed-bitcode \
    230         -isysroot ${IPHONEOS_SYSROOT}" \
    231         LDFLAGS="-arch armv7s \
    232         -fembed-bitcode \
    233         -miphoneos-version-min=${MIN_SDK_VERSION} \
    234         ${LDFLAGS}" \
    235         "LIBS=${LIBS}"
    236         make -j"${JOB_COUNT}"
    237         make install
    238 
    239         package_pb_library "ios_arm7s"
    240         ;;
    241 
    242     arm64) make distclean
    243         ./configure \
    244         --host=arm \
    245         --with-protoc="${PROTOC_PATH}" \
    246         --disable-shared \
    247         --prefix=${LIBDIR}/ios_arm64 \
    248         --exec-prefix=${LIBDIR}/ios_arm64 \
    249         "CFLAGS=${CFLAGS} \
    250         -miphoneos-version-min=${MIN_SDK_VERSION} \
    251         -arch arm64 \
    252         -fembed-bitcode \
    253         -isysroot ${IPHONEOS_SYSROOT}" \
    254         "CXXFLAGS=${CXXFLAGS} \
    255         -miphoneos-version-min=${MIN_SDK_VERSION} \
    256         -arch arm64 \
    257         -fembed-bitcode \
    258         -isysroot ${IPHONEOS_SYSROOT}" \
    259         LDFLAGS="-arch arm64 \
    260         -fembed-bitcode \
    261         -miphoneos-version-min=${MIN_SDK_VERSION} \
    262         ${LDFLAGS}" \
    263         "LIBS=${LIBS}"
    264         make -j"${JOB_COUNT}"
    265         make install
    266 
    267         package_pb_library "ios_arm64"
    268         ;;
    269     *)
    270         echo "Unknown ARCH"
    271         exit 1
    272         ;;
    273 esac
    274 }
    275 
    276 for build_element in "${build_targets[@]}"
    277 do
    278     echo "$build_element"
    279     build_target "$build_element"
    280 done
    281 
    282 file ${LIBDIR}/libprotobuf.a
    283 file ${LIBDIR}/libprotobuf-lite.a
    284 echo "Done building and packaging the libraries"
    285