• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# Copyright 2016 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# ==============================================================================
17
18# Compile the nsync library for the platforms given as arguments.
19
20set -e
21
22prog=compile_nsync.sh
23android_api_version=21
24default_android_arch=armeabi-v7a
25default_ios_arch="x86_64 armv7 armv7s arm64"
26
27usage="usage: $prog [-t linux|ios|android|macos|native]
28        [-a architecture] [-v android_api_version]
29
30A script to build nsync for tensorflow.
31This script can be run on Linux or MacOS host platforms, and can target
32Linux, MacOS, iOS, or Android.
33
34Options:
35-t target_platform
36The default target platform is the native host platform.
37
38-a architecture
39For Android and iOS target platforms, specify which architecture
40to target.
41For iOS, the default is: $default_ios_arch.
42For Android, the default is: $default_android_arch.
43
44-v android_api_version
45Specify the Android API version; the default is $android_api_version."
46
47# Deduce host platform.
48host_platform=
49nsync_path=
50case `uname -s` in
51Linux)  host_platform=linux  android_host=linux;;
52Darwin) host_platform=macos  android_host=darwin;;
53*)      echo "$prog: can't deduce host platform" >&2; exit 2;;
54esac
55host_arch=`uname -m`
56case "$host_arch" in i[345678]86) host_arch=x86_32;; esac
57
58# Parse command line.
59target_platform=native   # Default is to build for the host.
60target_arch=default
61while
62        arg="${1-}"
63        case "$arg" in
64        -*)     case "$arg" in -*t*) target_platform="${2?"$usage"}"; shift; esac
65                case "$arg" in -*a*) target_arch="${2?"$usage"}"; shift; esac
66                case "$arg" in -*v*) android_api_version="${2?"$usage"}"; shift; esac
67                case "$arg" in -*[!atv]*) echo "$usage" >&2; exit 2;; esac;;
68        "")     break;;
69        *)      echo "$usage" >&2; exit 2;;
70        esac
71do
72        shift
73done
74
75# Sanity check the target platform.
76case "$target_platform" in
77native) target_platform="$host_platform";;
78esac
79
80# Change directory to the root of the source tree.
81SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
82cd "${SCRIPT_DIR}/../../.."
83
84nsync_builds_dir=tensorflow/contrib/makefile/downloads/nsync/builds
85
86case "$target_platform" in
87ios)            case "$target_arch" in
88                default) archs="$default_ios_arch";;
89                *)       archs="$target_arch";;
90                esac
91                ;;
92android)        case "$target_arch" in
93                default) archs="$default_android_arch";;
94                *)       archs="$target_arch";;
95                esac
96                ;;
97*)              archs="$target_arch";;
98esac
99
100# For ios, the library names for the CPU types accumulate in $platform_libs
101platform_libs=
102
103# Compile nsync.
104for arch in $archs; do
105        nsync_platform_dir="$nsync_builds_dir/$arch.$target_platform.c++11"
106
107        # Get Makefile for target.
108        case "$target_platform" in
109        linux)  makefile='
110                        CC=${CC_PREFIX} g++
111                        PLATFORM_CPPFLAGS=-DNSYNC_USE_CPP11_TIMEPOINT -DNSYNC_ATOMIC_CPP11 \
112                                          -I../../platform/c++11.futex \
113                                          -I../../platform/c++11 -I../../platform/gcc \
114                                          -I../../platform/posix -pthread
115                        PLATFORM_CFLAGS=-std=c++11 -Werror -Wall -Wextra -pedantic
116                        PLATFORM_LDFLAGS=-pthread
117                        MKDEP=${CC} -M -std=c++11
118                        PLATFORM_C=../../platform/linux/src/nsync_semaphore_futex.c \
119                                   ../../platform/c++11/src/per_thread_waiter.cc \
120                                   ../../platform/c++11/src/yield.cc \
121                                   ../../platform/c++11/src/time_rep_timespec.cc \
122                                   ../../platform/c++11/src/nsync_panic.cc
123                        PLATFORM_OBJS=nsync_semaphore_futex.o per_thread_waiter.o yield.o \
124                                      time_rep_timespec.o nsync_panic.o
125                        TEST_PLATFORM_C=../../platform/c++11/src/start_thread.cc
126                        TEST_PLATFORM_OBJS=start_thread.o
127                        include ../../platform/posix/make.common
128                        include dependfile
129                ';;
130
131        ios)    arch_flags=
132                case "$arch" in
133                x86_64)
134                        arch_flags="$arch_flags -mios-simulator-version-min=8.0"
135                        arch_flags="$arch_flags -isysroot $(xcrun --sdk iphonesimulator --show-sdk-path)"
136                        ;;
137                *)
138                        arch_flags="$arch_flags -miphoneos-version-min=8.0"
139                        arch_flags="$arch_flags -isysroot $(xcrun --sdk iphoneos --show-sdk-path)"
140                        ;;
141                esac
142                makefile='
143                        CC=${CC_PREFIX} clang++
144                        PLATFORM_CPPFLAGS=-DNSYNC_USE_CPP11_TIMEPOINT -DNSYNC_ATOMIC_CPP11 \
145                                          -I../../platform/c++11 -I../../platform/gcc_no_tls \
146                                          -I../../platform/macos -I../../platform/posix -pthread
147                        PLATFORM_CFLAGS=-arch '"$arch"' -fno-exceptions -stdlib=libc++ \
148                                        -fembed-bitcode '"$arch_flags"' -fPIC -x c++ \
149                                        -std=c++11 -Werror -Wall -Wextra -pedantic
150                        PLATFORM_LDFLAGS=-pthread
151                        MKDEP=${CC} -x c++ -M -std=c++11
152                        PLATFORM_C=../../platform/posix/src/clock_gettime.c \
153                                   ../../platform/c++11/src/nsync_semaphore_mutex.cc \
154                                   ../../platform/posix/src/per_thread_waiter.c \
155                                   ../../platform/c++11/src/yield.cc \
156                                   ../../platform/c++11/src/time_rep_timespec.cc \
157                                   ../../platform/c++11/src/nsync_panic.cc
158                        PLATFORM_OBJS=clock_gettime.o nsync_semaphore_mutex.o per_thread_waiter.o \
159                                      yield.o time_rep_timespec.o nsync_panic.o
160                        TEST_PLATFORM_C=../../platform/c++11/src/start_thread.cc
161                        TEST_PLATFORM_OBJS=start_thread.o
162                        include ../../platform/posix/make.common
163                        include dependfile
164                ';;
165
166        macos)  makefile='
167                        CC=${CC_PREFIX} clang++
168                        PLATFORM_CPPFLAGS=-DNSYNC_USE_CPP11_TIMEPOINT -DNSYNC_ATOMIC_CPP11 \
169                                          -I../../platform/c++11 -I../../platform/gcc \
170                                          -I../../platform/macos -I../../platform/posix -pthread
171                        PLATFORM_CFLAGS=-x c++ -std=c++11 -Werror -Wall -Wextra -pedantic
172                        PLATFORM_LDFLAGS=-pthread
173                        MKDEP=${CC} -x c++ -M -std=c++11
174                        PLATFORM_C=../../platform/posix/src/clock_gettime.c \
175                                   ../../platform/c++11/src/nsync_semaphore_mutex.cc \
176                                   ../../platform/posix/src/per_thread_waiter.c \
177                                   ../../platform/c++11/src/yield.cc \
178                                   ../../platform/c++11/src/time_rep_timespec.cc \
179                                   ../../platform/c++11/src/nsync_panic.cc
180                        PLATFORM_OBJS=clock_gettime.o nsync_semaphore_mutex.o per_thread_waiter.o \
181                                      yield.o time_rep_timespec.o nsync_panic.o
182                        TEST_PLATFORM_C=../../platform/c++11/src/start_thread.cc
183                        TEST_PLATFORM_OBJS=start_thread.o
184                        include ../../platform/posix/make.common
185                        include dependfile
186                ';;
187
188        android)
189                # The Android build uses many different names for the same
190                # platform in different parts of the tree, so things get messy here.
191
192                # Make $android_os_arch be the OS-arch name for the host
193                # binaries used in the NDK tree.
194                case "$host_platform" in
195                linux)  android_os_arch=linux;;
196                macos)  android_os_arch=darwin;;
197                *)      android_os_arch="$host_platform";;
198                esac
199                case "$host_arch" in
200                x86_32) android_os_arch="$android_os_arch"-x86;;
201                *)      android_os_arch="$android_os_arch-$host_arch";;
202                esac
203
204                case "$arch" in
205                arm64-v8a)              toolchain="aarch64-linux-android-4.9"
206                                        sysroot_arch="arm64"
207                                        bin_prefix="aarch64-linux-android"
208                                        march_option=
209                                        ;;
210                armeabi)                toolchain="arm-linux-androideabi-4.9"
211                                        sysroot_arch="arm"
212                                        bin_prefix="arm-linux-androideabi"
213                                        march_option=
214                                        ;;
215                armeabi-v7a)            toolchain="arm-linux-androideabi-4.9"
216                                        sysroot_arch="arm"
217                                        bin_prefix="arm-linux-androideabi"
218                                        march_option="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"
219                                        ;;
220                armeabi-v7a-hard)       toolchain="arm-linux-androideabi-4.9"
221                                        sysroot_arch="arm"
222                                        bin_prefix="arm-linux-androideabi"
223                                        march_option="-march=armv7-a -mfpu=neon"
224                                        ;;
225                mips)                   toolchain="mipsel-linux-android-4.9"
226                                        sysroot_arch="mips"
227                                        bin_prefix="mipsel-linux-android"
228                                        march_option=
229                                        ;;
230                mips64)                 toolchain="mips64el-linux-android-4.9"
231                                        sysroot_arch="mips64"
232                                        bin_prefix="mips64el-linux-android"
233                                        march_option=
234                                        ;;
235                x86)                    toolchain="x86-4.9"
236                                        sysroot_arch="x86"
237                                        bin_prefix="i686-linux-android"
238                                        march_option=
239                                        ;;
240                x86_64)                 toolchain="x86_64-4.9"
241                                        sysroot_arch="x86_64"
242                                        bin_prefix="x86_64-linux-android"
243                                        march_option=
244                                        ;;
245                *)                      echo "android is not supported for $arch" >&2
246                                        echo "$usage" >&2
247                                        exit 2
248                                        ;;
249                esac
250
251
252                android_target_platform=armeabi
253                case "$NDK_ROOT" in
254                "")     echo "$prog: requires \$NDK_ROOT for android build" >&2
255                        exit 2;;
256                esac
257
258                makefile='
259			AR := ${NDK_ROOT}/toolchains/'"$toolchain"'/prebuilt/'"$android_os_arch"'/bin/'"$bin_prefix"'-ar
260                        CC=${CC_PREFIX} \
261                           ${NDK_ROOT}/toolchains/'"$toolchain"'/prebuilt/'"$android_os_arch"'/bin/'"$bin_prefix"'-g++
262                        PLATFORM_CPPFLAGS=--sysroot \
263                                          $(NDK_ROOT)/platforms/android-'"$android_api_version"'/arch-'"$sysroot_arch"' \
264                                          -DNSYNC_USE_CPP11_TIMEPOINT -DNSYNC_ATOMIC_CPP11 \
265                                          -I$(NDK_ROOT)/sources/android/support/include \
266                                          -I$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/include \
267                                          -I$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/libs/'"$arch"'/include \
268                                          -I../../platform/c++11 -I../../platform/gcc \
269                                          -I../../platform/posix -pthread
270                        PLATFORM_CFLAGS=-std=c++11 -Wno-narrowing '"$march_option"' -fPIE -fPIC
271                        PLATFORM_LDFLAGS=-pthread
272                        MKDEP=${CC} -M -std=c++11
273                        PLATFORM_C=../../platform/c++11/src/nsync_semaphore_mutex.cc \
274                                   ../../platform/posix/src/per_thread_waiter.c \
275                                   ../../platform/c++11/src/yield.cc \
276                                   ../../platform/c++11/src/time_rep_timespec.cc \
277                                   ../../platform/c++11/src/nsync_panic.cc
278                        PLATFORM_OBJS=nsync_semaphore_mutex.o per_thread_waiter.o yield.o \
279                                      time_rep_timespec.o nsync_panic.o
280                        TEST_PLATFORM_C=../../platform/c++11/src/start_thread.cc
281                        TEST_PLATFORM_OBJS=start_thread.o
282                        include ../../platform/posix/make.common
283                        include dependfile
284                ';;
285
286                *)      echo "$usage" >&2; exit 2;;
287        esac
288
289        if [ ! -d "$nsync_platform_dir" ]; then
290                mkdir "$nsync_platform_dir"
291                echo "$makefile" | sed $'s,^[ \t]*,,' > "$nsync_platform_dir/Makefile"
292                touch "$nsync_platform_dir/dependfile"
293        fi
294        if (cd "$nsync_platform_dir" && make depend nsync.a >&2); then
295                case "$target_platform" in
296                ios)    platform_libs="$platform_libs '$nsync_platform_dir/nsync.a'";;
297                *)      echo "$nsync_platform_dir/nsync.a";;
298                esac
299        else
300                exit 2  # The if-statement suppresses the "set -e" on the "make".
301        fi
302done
303
304case "$target_platform" in
305ios)    nsync_platform_dir="$nsync_builds_dir/lipo.$target_platform.c++11"
306        if [ -d "$nsync_platform_dir" ]; then
307            rm -rf "$nsync_platform_dir"
308        fi
309        mkdir "$nsync_platform_dir"
310        eval lipo $platform_libs -create -output '$nsync_platform_dir/nsync.a'
311        echo "$nsync_platform_dir/nsync.a"
312        ;;
313esac
314