Home | History | Annotate | Download | only in makefile
      1 # This Makefile compiles a library containing the C++ runtime for the TensorFlow
      2 # library. It's designed for use on platforms with limited resources where
      3 # running a full Bazel build would be prohibitive, or for cross-compilation onto
      4 # embedded systems. It includes only a bare-bones set of functionality.
      5 #
      6 # The default setup below is aimed at Unix-like devices, and should work on
      7 # modern Linux and OS X distributions without changes.
      8 #
      9 # If you have another platform, you'll need to take a careful look at the
     10 # compiler flags and folders defined below. They're separated into two sections,
     11 # the first for the host (the machine you're compiling on) and the second for
     12 # the target (the machine you want the program to run on).
     13 
     14 SHELL := /bin/bash
     15 
     16 # Host compilation settings
     17 
     18 # Find where we're running from, so we can store generated files here.
     19 ifeq ($(origin MAKEFILE_DIR), undefined)
     20 	MAKEFILE_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
     21 endif
     22 
     23 HAS_GEN_HOST_PROTOC := \
     24 $(shell test -f $(MAKEFILE_DIR)/gen/protobuf-host/bin/protoc && echo "true" ||\
     25 echo "false")
     26 
     27 # Hexagon integration
     28 ifdef HEXAGON_LIBS
     29 	LIBGEMM_WRAPPER := $(HEXAGON_LIBS)/libhexagon_controller.so
     30 	ifeq ($(shell test -f $(LIBGEMM_WRAPPER) 2> /dev/null; echo $$?), 0)
     31     $(info "Use hexagon libs at " $(LIBGEMM_WRAPPER))
     32 	else
     33     $(error "hexagon libs not found at " $(LIBGEMM_WRAPPER))
     34 	endif
     35 	ifdef HEXAGON_INCLUDE
     36 		ifeq ($(shell test -d $(HEXAGON_INCLUDE) 2> /dev/null; echo $$?), 0)
     37       $(info "Use hexagon libs at " $(HEXAGON_INCLUDE))
     38 		else
     39       $(error "hexagon libs not found at " $(HEXAGON_INCLUDE))
     40 		endif
     41 	else
     42     $(error "HEXAGON_INCLUDE is not set.")
     43 	endif
     44 	ifneq ($(TARGET),ANDROID)
     45     $(error "hexagon is only supported on Android")
     46 	endif
     47 endif # HEXAGON_LIBS
     48 
     49 # If ANDROID_TYPES is not set assume __ANDROID_TYPES_SLIM__
     50 ifeq ($(ANDROID_TYPES),)
     51 	ANDROID_TYPES := -D__ANDROID_TYPES_SLIM__
     52 endif
     53 
     54 # Try to figure out the host system
     55 HOST_OS :=
     56 ifeq ($(OS),Windows_NT)
     57 	HOST_OS = WINDOWS
     58 else
     59 	UNAME_S := $(shell uname -s)
     60 	ifeq ($(UNAME_S),Linux)
     61 	        HOST_OS := LINUX
     62 	endif
     63 	ifeq ($(UNAME_S),Darwin)
     64 		HOST_OS := OSX
     65 	endif
     66 endif
     67 
     68 HOST_ARCH := $(shell if [[ $(shell uname -m) =~ i[345678]86 ]]; then echo x86_32; else echo $(shell uname -m); fi)
     69 
     70 # Where compiled objects are stored.
     71 HOST_OBJDIR := $(MAKEFILE_DIR)/gen/host_obj/
     72 HOST_BINDIR := $(MAKEFILE_DIR)/gen/host_bin/
     73 HOST_GENDIR := $(MAKEFILE_DIR)/gen/host_obj/
     74 
     75 # Settings for the host compiler.
     76 HOST_CXX := $(CC_PREFIX) gcc
     77 HOST_CXXFLAGS := --std=c++11
     78 HOST_LDOPTS :=
     79 ifeq ($(HAS_GEN_HOST_PROTOC),true)
     80 	HOST_LDOPTS += -L$(MAKEFILE_DIR)/gen/protobuf-host/lib
     81 endif
     82 HOST_LDOPTS += -L/usr/local/lib
     83 
     84 HOST_INCLUDES := \
     85 -I. \
     86 -I$(MAKEFILE_DIR)/../../../ \
     87 -I$(MAKEFILE_DIR)/downloads/ \
     88 -I$(MAKEFILE_DIR)/downloads/eigen \
     89 -I$(MAKEFILE_DIR)/downloads/gemmlowp \
     90 -I$(MAKEFILE_DIR)/downloads/nsync/public \
     91 -I$(MAKEFILE_DIR)/downloads/fft2d \
     92 -I$(HOST_GENDIR)
     93 ifeq ($(HAS_GEN_HOST_PROTOC),true)
     94 	HOST_INCLUDES += -I$(MAKEFILE_DIR)/gen/protobuf-host/include
     95 endif
     96 # This is at the end so any globally-installed frameworks like protobuf don't
     97 # override local versions in the source tree.
     98 HOST_INCLUDES += -I/usr/local/include
     99 
    100 HOST_LIBS := \
    101 $(HOST_NSYNC_LIB) \
    102 -lstdc++ \
    103 -lprotobuf \
    104 -lpthread \
    105 -lm \
    106 -lz
    107 
    108 # If we're on Linux, also link in the dl library.
    109 ifeq ($(HOST_OS),LINUX)
    110 	HOST_LIBS += -ldl -lpthread
    111 endif
    112 
    113 # If we're on a Pi, link in pthreads and dl
    114 ifeq ($(HOST_OS),PI)
    115 	HOST_LIBS += -ldl -lpthread
    116 endif
    117 
    118 
    119 # proto_text is a tool that converts protobufs into a form we can use more
    120 # compactly within TensorFlow. It's a bit like protoc, but is designed to
    121 # produce a much more minimal result so we can save binary space.
    122 # We have to build it on the host system first so that we can create files
    123 # that are needed for the runtime building.
    124 PROTO_TEXT := $(HOST_BINDIR)proto_text
    125 # The list of dependencies is derived from the Bazel build file by running
    126 # the gen_file_lists.sh script on a system with a working Bazel setup.
    127 PROTO_TEXT_CC_FILES := $(shell cat $(MAKEFILE_DIR)/proto_text_cc_files.txt)
    128 PROTO_TEXT_PB_CC_LIST := $(shell cat $(MAKEFILE_DIR)/proto_text_pb_cc_files.txt)
    129 PROTO_TEXT_PB_H_LIST := $(shell cat $(MAKEFILE_DIR)/proto_text_pb_h_files.txt)
    130 
    131 # Locations of the intermediate files proto_text generates.
    132 PROTO_TEXT_PB_H_FILES := $(addprefix $(HOST_GENDIR), $(PROTO_TEXT_PB_H_LIST))
    133 PROTO_TEXT_CC_OBJS := $(addprefix $(HOST_OBJDIR), $(PROTO_TEXT_CC_FILES:.cc=.o))
    134 PROTO_TEXT_PB_OBJS := $(addprefix $(HOST_OBJDIR), $(PROTO_TEXT_PB_CC_LIST:.cc=.o))
    135 PROTO_TEXT_OBJS := $(PROTO_TEXT_CC_OBJS) $(PROTO_TEXT_PB_OBJS)
    136 
    137 # Target device settings.
    138 
    139 # Default to running on the same system we're compiling on.
    140 # You should override TARGET on the command line if you're cross-compiling, e.g.
    141 # make -f tensorflow/contrib/makefile/Makefile TARGET=ANDROID
    142 TARGET := $(HOST_OS)
    143 
    144 # Where compiled objects are stored.
    145 GENDIR := $(MAKEFILE_DIR)/gen/
    146 OBJDIR := $(GENDIR)obj/
    147 LIBDIR := $(GENDIR)lib/
    148 BINDIR := $(GENDIR)bin/
    149 PBTGENDIR := $(GENDIR)proto_text/
    150 PROTOGENDIR := $(GENDIR)proto/
    151 DEPDIR := $(GENDIR)dep/
    152 $(shell mkdir -p $(DEPDIR) >/dev/null)
    153 
    154 # Settings for the target compiler.
    155 CXX := $(CC_PREFIX) gcc
    156 OPTFLAGS := -O2
    157 
    158 ifneq ($(TARGET),ANDROID)
    159   OPTFLAGS += -march=native
    160 endif
    161 
    162 CXXFLAGS := --std=c++11 -DIS_SLIM_BUILD -fno-exceptions -DNDEBUG $(OPTFLAGS)
    163 LDFLAGS := \
    164 -L/usr/local/lib
    165 DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
    166 
    167 INCLUDES := \
    168 -I. \
    169 -I$(MAKEFILE_DIR)/downloads/ \
    170 -I$(MAKEFILE_DIR)/downloads/eigen \
    171 -I$(MAKEFILE_DIR)/downloads/gemmlowp \
    172 -I$(MAKEFILE_DIR)/downloads/nsync/public \
    173 -I$(MAKEFILE_DIR)/downloads/fft2d \
    174 -I$(PROTOGENDIR) \
    175 -I$(PBTGENDIR)
    176 ifeq ($(HAS_GEN_HOST_PROTOC),true)
    177 	INCLUDES += -I$(MAKEFILE_DIR)/gen/protobuf-host/include
    178 endif
    179 # This is at the end so any globally-installed frameworks like protobuf don't
    180 # override local versions in the source tree.
    181 INCLUDES += -I/usr/local/include
    182 
    183 LIBS := \
    184 $(TARGET_NSYNC_LIB) \
    185 -lstdc++ \
    186 -lprotobuf \
    187 -lz \
    188 -lm
    189 
    190 ifeq ($(HAS_GEN_HOST_PROTOC),true)
    191 	PROTOC := $(MAKEFILE_DIR)/gen/protobuf-host/bin/protoc
    192 else
    193 	PROTOC := protoc
    194 endif
    195 
    196 $(info PROTOC = "$(PROTOC)")
    197 $(info CC_PREFIX = "$(CC_PREFIX)")
    198 
    199 PROTOCFLAGS :=
    200 AR := ar
    201 ARFLAGS := -r
    202 LIBFLAGS :=
    203 
    204 # If we're on OS X, make sure that globals aren't stripped out.
    205 ifeq ($(TARGET),OSX)
    206 ifeq ($(HAS_GEN_HOST_PROTOC),true)
    207 	LIBFLAGS += -L$(MAKEFILE_DIR)/gen/protobuf-host/lib
    208 	export LD_LIBRARY_PATH=$(MAKEFILE_DIR)/gen/protobuf-host/lib
    209 endif
    210 	LDFLAGS += -all_load
    211 endif
    212 # Make sure that we don't strip global constructors on Linux.
    213 ifeq ($(TARGET),LINUX)
    214 ifeq ($(HAS_GEN_HOST_PROTOC),true)
    215 	LIBFLAGS += -L$(MAKEFILE_DIR)/gen/protobuf-host/lib
    216 	export LD_LIBRARY_PATH=$(MAKEFILE_DIR)/gen/protobuf-host/lib
    217 endif
    218 	CXXFLAGS += -fPIC
    219 	LIBFLAGS += -Wl,--allow-multiple-definition -Wl,--whole-archive
    220 	LDFLAGS := -Wl,--no-whole-archive
    221 endif
    222 # If we're on Linux, also link in the dl library.
    223 ifeq ($(TARGET),LINUX)
    224 	LIBS += -ldl -lpthread
    225 endif
    226 # If we're cross-compiling for the Raspberry Pi, use the right gcc.
    227 ifeq ($(TARGET),PI)
    228 	CXXFLAGS += $(ANDROID_TYPES) -DRASPBERRY_PI
    229 	LDFLAGS := -Wl,--no-whole-archive
    230 	LIBS += -ldl -lpthread
    231 	LIBFLAGS += -Wl,--allow-multiple-definition -Wl,--whole-archive
    232 endif
    233 
    234 # Set up Android building
    235 # LINT.IfChange
    236 ifeq ($(TARGET),ANDROID)
    237 # Override NDK_ROOT on the command line with your own NDK location, e.g.
    238 # make -f tensorflow/contrib/makefile/Makefile TARGET=ANDROID \
    239 # NDK_ROOT=/path/to/your/ndk
    240 # You need to have an Android version of the protobuf libraries compiled to link
    241 # in. The compile_android_protobuf.sh script may help.
    242 
    243 	ANDROID_HOST_OS_ARCH :=
    244 	ifeq ($(HOST_OS),LINUX)
    245 		ANDROID_HOST_OS_ARCH=linux
    246 	endif
    247 	ifeq ($(HOST_OS),OSX)
    248 		ANDROID_HOST_OS_ARCH=darwin
    249 	endif
    250 	ifeq ($(HOST_OS),WINDOWS)
    251     $(error "windows is not supported.")
    252 	endif
    253 
    254 	ifeq ($(HOST_ARCH),x86_32)
    255 		ANDROID_HOST_OS_ARCH := $(ANDROID_HOST_OS_ARCH)-x86
    256 	else
    257 		ANDROID_HOST_OS_ARCH := $(ANDROID_HOST_OS_ARCH)-$(HOST_ARCH)
    258 	endif
    259     
    260 	ifndef ANDROID_ARCH
    261 		ANDROID_ARCH := armeabi-v7a
    262 	endif
    263 
    264 	ifeq ($(ANDROID_ARCH),arm64-v8a)
    265 		TOOLCHAIN := aarch64-linux-android-4.9
    266 		SYSROOT_ARCH := arm64
    267 		BIN_PREFIX := aarch64-linux-android
    268 		MARCH_OPTION :=
    269 	endif
    270 	ifeq ($(ANDROID_ARCH),armeabi)
    271 		TOOLCHAIN := arm-linux-androideabi-4.9
    272 		SYSROOT_ARCH := arm
    273 		BIN_PREFIX := arm-linux-androideabi
    274 		MARCH_OPTION :=
    275 	endif
    276 	ifeq ($(ANDROID_ARCH),armeabi-v7a)
    277 		TOOLCHAIN := arm-linux-androideabi-4.9
    278 		SYSROOT_ARCH := arm
    279 		BIN_PREFIX := arm-linux-androideabi
    280 		MARCH_OPTION := -march=armv7-a -mfloat-abi=softfp -mfpu=neon
    281 	endif
    282 	ifeq ($(ANDROID_ARCH),mips)
    283 		TOOLCHAIN := mipsel-linux-android-4.9
    284 		SYSROOT_ARCH := mips
    285 		BIN_PREFIX := mipsel-linux-android
    286 		MARCH_OPTION :=
    287 	endif
    288 	ifeq ($(ANDROID_ARCH),mips64)
    289 		TOOLCHAIN := mips64el-linux-android-4.9
    290 		SYSROOT_ARCH := mips64
    291 		BIN_PREFIX := mips64el-linux-android
    292 		MARCH_OPTION :=
    293 	endif
    294 	ifeq ($(ANDROID_ARCH),x86)
    295 		TOOLCHAIN := x86-4.9
    296 		SYSROOT_ARCH := x86
    297 		BIN_PREFIX := i686-linux-android
    298 		MARCH_OPTION :=
    299 	endif
    300 	ifeq ($(ANDROID_ARCH),x86_64)
    301 		TOOLCHAIN := x86_64-4.9
    302 		SYSROOT_ARCH := x86_64
    303 		BIN_PREFIX := x86_64-linux-android
    304 		MARCH_OPTION :=
    305 	endif
    306     
    307 	ifndef NDK_ROOT
    308     $(error "NDK_ROOT is not defined.")
    309 	endif
    310 	CXX := $(CC_PREFIX) $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-g++
    311 	CC := $(CC_PREFIX) $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-gcc
    312 	CXXFLAGS +=\
    313 --sysroot $(NDK_ROOT)/platforms/android-21/arch-$(SYSROOT_ARCH) \
    314 -Wno-narrowing \
    315 -fomit-frame-pointer \
    316 $(MARCH_OPTION) \
    317 -fPIE \
    318 -fPIC
    319 	INCLUDES = \
    320 -I$(NDK_ROOT)/sources/android/support/include \
    321 -I$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/include \
    322 -I$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/libs/$(ANDROID_ARCH)/include \
    323 -I. \
    324 -I$(MAKEFILE_DIR)/downloads/ \
    325 -I$(MAKEFILE_DIR)/downloads/eigen \
    326 -I$(MAKEFILE_DIR)/downloads/gemmlowp \
    327 -I$(MAKEFILE_DIR)/downloads/nsync/public \
    328 -I$(MAKEFILE_DIR)/downloads/fft2d \
    329 -I$(MAKEFILE_DIR)/gen/protobuf_android/$(ANDROID_ARCH)/include \
    330 -I$(PROTOGENDIR) \
    331 -I$(PBTGENDIR)
    332 
    333 	LIBS := \
    334 $(TARGET_NSYNC_LIB) \
    335 -lgnustl_static \
    336 -lprotobuf \
    337 -llog \
    338 -lz \
    339 -lm \
    340 -ldl \
    341 -latomic
    342 
    343 	LD := $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/$(BIN_PREFIX)/bin/ld
    344 
    345 	LDFLAGS := \
    346 $(MARCH_OPTION) \
    347 -L$(MAKEFILE_DIR)/gen/protobuf_android/$(ANDROID_ARCH)/lib \
    348 -L$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/libs/$(ANDROID_ARCH) \
    349 -fPIE \
    350 -pie \
    351 -v
    352 
    353 	AR := $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-ar
    354 	ARFLAGS := r
    355 	LIBFLAGS += -Wl,--allow-multiple-definition -Wl,--whole-archive
    356 
    357 	ifdef HEXAGON_LIBS
    358 		INCLUDES += -I$(HEXAGON_INCLUDE)
    359 		LIBS += -lhexagon_controller
    360 		LDFLAGS += -L$(HEXAGON_LIBS)
    361 		CXXFLAGS += -DUSE_HEXAGON_LIBS
    362 
    363 # CAVEAT: We should disable TENSORFLOW_DISABLE_META while running
    364 # quantized_matmul on Android because it crashes in
    365 # MultiThreadGemm in tensorflow/core/kernels/meta_support.cc
    366 # See http://b/33270149
    367 # TODO(satok): Remove once it's fixed
    368 		CXXFLAGS += -DTENSORFLOW_DISABLE_META
    369 
    370 # Declare __ANDROID_TYPES_FULL__ to enable required types for hvx
    371 		CXXFLAGS += -D__ANDROID_TYPES_FULL__
    372 	endif
    373 
    374 	ifdef ENABLE_EXPERIMENTAL_HEXNN_OPS
    375 		CXXFLAGS += -DENABLE_EXPERIMENTAL_HEXNN_OPS
    376 	endif
    377 
    378 	ifeq ($(BUILD_FOR_TEGRA),1)
    379 		NVCC := $(JETPACK)/cuda/bin/nvcc
    380 		NVCCFLAGS := -x=cu -D__CUDACC__ -DNVCC -DANDROID_TEGRA -ccbin $(NDK_ROOT)/toolchains/$(TOOLCHAIN)/prebuilt/$(ANDROID_HOST_OS_ARCH)/bin/$(BIN_PREFIX)-g++ --std c++11 --expt-relaxed-constexpr -m64 -gencode arch=compute_53,\"code=sm_53\" -gencode arch=compute_62,\"code=sm_62\" -DEIGEN_AVOID_STL_ARRAY -DTENSORFLOW_USE_EIGEN_THREADPOOL -DLANG_CXX11 -DEIGEN_HAS_C99_MATH -DGOOGLE_CUDA=1 -DTF_EXTRA_CUDA_CAPABILITIES=5.3
    381 		CXXFLAGS4NVCC =\
    382 -DIS_SLIM_BUILD \
    383 -DANDROID_TEGRA \
    384 -fno-exceptions \
    385 -DNDEBUG $(OPTFLAGS) \
    386 -march=armv8-a \
    387 -fPIE \
    388 -D__ANDROID_TYPES_FULL__ \
    389 --sysroot $(NDK_ROOT)/platforms/android-21/arch-arm64
    390 
    391 		CXXFLAGS +=\
    392 -DGOOGLE_CUDA=1 \
    393 -D__ANDROID_TYPES_FULL__ \
    394 -DANDROID_TEGRA \
    395 -DEIGEN_AVOID_STL_ARRAY \
    396 -DEIGEN_HAS_C99_MATH \
    397 -DLANG_CXX11 -DTENSORFLOW_USE_EIGEN_THREADPOOL -DTF_EXTRA_CUDA_CAPABILITIES=5.3
    398 
    399 		INCLUDES += \
    400 -Itensorflow/core/kernels \
    401 -I$(MAKEFILE_DIR)/downloads/cub \
    402 -I$(MAKEFILE_DIR)/downloads/cub/cub_archive/cub/device \
    403 -Ithird_party/toolchains/gpus/cuda \
    404 -I$(JETPACK)/cuda/include \
    405 -I$(JETPACK) \
    406 -I$(JETPACK)/cuDNN/aarch64 \
    407 -I$(JETPACK)/cuda/extras/CUPTI/include
    408 
    409 
    410 		CUDA_LIBS := \
    411 -ltfcuda \
    412 -lcudart_static \
    413 -lcudnn \
    414 -lcublas_static \
    415 -lcufftw_static \
    416 -lcusolver_static \
    417 -lcusparse_static \
    418 -lcufft \
    419 -lcuda \
    420 -lculibos \
    421 -lcurand_static
    422 
    423 		OBJDIR := $(OBJDIR)android_arm64-v8a/
    424 		LIBDIR := $(LIBDIR)android_arm64-v8a/
    425 		BINDIR := $(BINDIR)android_arm64-v8a/
    426 		DEPDIR := $(DEPDIR)android_arm64-v8a/
    427 
    428 		TEGRA_LIBS := \
    429 -L$(JETPACK)/cuda/targets/aarch64-linux-androideabi/lib \
    430 -L$(JETPACK)/cuda/targets/aarch64-linux-androideabi/lib/stubs \
    431 -L$(JETPACK)/cuda/targets/aarch64-linux-androideabi/lib64 \
    432 -L$(JETPACK)/cuda/targets/aarch64-linux-androideabi/lib64/stubs \
    433 -L$(JETPACK)/cuDNN/aarch64/cuda/lib64 \
    434 -L$(LIBDIR)
    435 
    436 		CUDA_LIB_DEPS := $(LIBDIR)libtfcuda.a
    437 	else
    438 		OBJDIR := $(OBJDIR)android_$(ANDROID_ARCH)/
    439 		LIBDIR := $(LIBDIR)android_$(ANDROID_ARCH)/
    440 		BINDIR := $(BINDIR)android_$(ANDROID_ARCH)/
    441 		DEPDIR := $(DEPDIR)android_$(ANDROID_ARCH)/
    442 	endif # ifeq ($(BUILD_FOR_TEGRA),1)
    443 endif  # ANDROID
    444 # LINT.ThenChange(//tensorflow/contrib/android/cmake/CMakeLists.txt)
    445 
    446 # Settings for iOS.
    447 ifeq ($(TARGET),IOS)
    448 	IPHONEOS_PLATFORM := $(shell xcrun --sdk iphoneos --show-sdk-platform-path)
    449 	IPHONEOS_SYSROOT := $(shell xcrun --sdk iphoneos --show-sdk-path)
    450 	IPHONESIMULATOR_PLATFORM := $(shell xcrun --sdk iphonesimulator \
    451 	--show-sdk-platform-path)
    452 	IPHONESIMULATOR_SYSROOT := $(shell xcrun --sdk iphonesimulator \
    453 	--show-sdk-path)
    454 	IOS_SDK_VERSION := $(shell xcrun --sdk iphoneos --show-sdk-version)
    455 	MIN_SDK_VERSION := 9.0
    456 # Override IOS_ARCH with ARMV7, ARMV7S, ARM64, or I386.
    457 	IOS_ARCH := X86_64
    458 	ifeq ($(IOS_ARCH),ARMV7)
    459 		CXXFLAGS += -miphoneos-version-min=$(MIN_SDK_VERSION) \
    460 		-arch armv7 \
    461 		-fembed-bitcode \
    462 		-D__thread=thread_local \
    463 		-DUSE_GEMM_FOR_CONV \
    464 		-Wno-c++11-narrowing \
    465 		-mno-thumb \
    466 		-DTF_LEAN_BINARY \
    467 		$(ANDROID_TYPES) \
    468 		-fno-exceptions \
    469 		-isysroot \
    470 		${IPHONEOS_SYSROOT}
    471 		LDFLAGS := -arch armv7 \
    472 		-fembed-bitcode \
    473 		-miphoneos-version-min=${MIN_SDK_VERSION} \
    474 		-framework Accelerate \
    475 		-Xlinker -S \
    476 		-Xlinker -x \
    477 		-Xlinker -dead_strip \
    478 		-all_load \
    479 		-L$(GENDIR)protobuf_ios/lib \
    480 		-lz
    481 	endif
    482 	ifeq ($(IOS_ARCH),ARMV7S)
    483 		CXXFLAGS += -miphoneos-version-min=$(MIN_SDK_VERSION) \
    484 		-arch armv7s \
    485 		-fembed-bitcode \
    486 		-D__thread=thread_local \
    487 		-DUSE_GEMM_FOR_CONV \
    488 		-Wno-c++11-narrowing \
    489 		-mno-thumb \
    490 		-DTF_LEAN_BINARY \
    491 		$(ANDROID_TYPES) \
    492 		-fno-exceptions \
    493 		-isysroot \
    494 		${IPHONEOS_SYSROOT}
    495 		LDFLAGS := -arch armv7s \
    496 		-fembed-bitcode \
    497 		-miphoneos-version-min=${MIN_SDK_VERSION} \
    498 		-framework Accelerate \
    499 		-Xlinker -S \
    500 		-Xlinker -x \
    501 		-Xlinker -dead_strip \
    502 		-all_load \
    503 		-L$(GENDIR)protobuf_ios/lib \
    504 		-lz
    505 	endif
    506 	ifeq ($(IOS_ARCH),ARM64)
    507 		CXXFLAGS += -miphoneos-version-min=$(MIN_SDK_VERSION) \
    508 		-arch arm64 \
    509 		-fembed-bitcode \
    510 		-D__thread=thread_local \
    511 		-DUSE_GEMM_FOR_CONV \
    512 		-Wno-c++11-narrowing \
    513 		-DTF_LEAN_BINARY \
    514 		$(ANDROID_TYPES) \
    515 		-fno-exceptions \
    516 		-isysroot \
    517 		${IPHONEOS_SYSROOT}
    518 		LDFLAGS := -arch arm64 \
    519 		-fembed-bitcode \
    520 		-miphoneos-version-min=${MIN_SDK_VERSION} \
    521 		-framework Accelerate \
    522 		-Xlinker -S \
    523 		-Xlinker -x \
    524 		-Xlinker -dead_strip \
    525 		-all_load \
    526 		-L$(GENDIR)protobuf_ios/lib \
    527 		-lz
    528 	endif
    529 	ifeq ($(IOS_ARCH),I386)
    530 		CXXFLAGS += -mios-simulator-version-min=$(MIN_SDK_VERSION) \
    531 		-arch i386 \
    532 		-mno-sse \
    533 		-fembed-bitcode \
    534 		-D__thread=thread_local \
    535 		-DUSE_GEMM_FOR_CONV \
    536 		-Wno-c++11-narrowing \
    537 		-DTF_LEAN_BINARY \
    538 		$(ANDROID_TYPES) \
    539 		-fno-exceptions \
    540 		-isysroot \
    541 		${IPHONESIMULATOR_SYSROOT}
    542 		LDFLAGS := -arch i386 \
    543 		-fembed-bitcode \
    544 		-mios-simulator-version-min=${MIN_SDK_VERSION} \
    545 		-framework Accelerate \
    546 		-Xlinker -S \
    547 		-Xlinker -x \
    548 		-Xlinker -dead_strip \
    549 		-all_load \
    550 		-L$(GENDIR)protobuf_ios/lib \
    551 		-lz
    552 	endif
    553 	ifeq ($(IOS_ARCH),X86_64)
    554 		CXXFLAGS += -mios-simulator-version-min=$(MIN_SDK_VERSION) \
    555 		-arch x86_64 \
    556 		-fembed-bitcode \
    557 		-D__thread=thread_local \
    558 		-DUSE_GEMM_FOR_CONV \
    559 		-Wno-c++11-narrowing \
    560 		-DTF_LEAN_BINARY \
    561 		$(ANDROID_TYPES) \
    562 		-fno-exceptions \
    563 		-isysroot \
    564 		${IPHONESIMULATOR_SYSROOT}
    565 		LDFLAGS := -arch x86_64 \
    566 		-fembed-bitcode \
    567 		-mios-simulator-version-min=${MIN_SDK_VERSION} \
    568 		-framework Accelerate \
    569 		-Xlinker -S \
    570 		-Xlinker -x \
    571 		-Xlinker -dead_strip \
    572 		-all_load \
    573 		-L$(GENDIR)protobuf_ios/lib \
    574 		-lz
    575 	endif
    576 	OBJDIR := $(OBJDIR)ios_$(IOS_ARCH)/
    577 	LIBDIR := $(LIBDIR)ios_$(IOS_ARCH)/
    578 	BINDIR := $(BINDIR)ios_$(IOS_ARCH)/
    579 	DEPDIR := $(DEPDIR)ios_$(IOS_ARCH)/
    580 endif
    581 
    582 # This library is the main target for this makefile. It will contain a minimal
    583 # runtime that can be linked in to other programs.
    584 LIB_NAME := libtensorflow-core.a
    585 LIB_PATH := $(LIBDIR)$(LIB_NAME)
    586 
    587 # A small example program that shows how to link against the library.
    588 BENCHMARK_NAME := $(BINDIR)benchmark
    589 
    590 # What sources we want to compile, derived from the main Bazel build using the
    591 # gen_file_lists.sh script.
    592 
    593 CORE_CC_ALL_SRCS := \
    594 $(wildcard tensorflow/core/*.cc) \
    595 $(wildcard tensorflow/core/common_runtime/*.cc) \
    596 $(wildcard tensorflow/core/framework/*.cc) \
    597 $(wildcard tensorflow/core/graph/*.cc) \
    598 $(wildcard tensorflow/core/grappler/*.cc) \
    599 $(wildcard tensorflow/core/grappler/*/*.cc) \
    600 $(wildcard tensorflow/core/lib/*/*.cc) \
    601 $(wildcard tensorflow/core/platform/*.cc) \
    602 $(wildcard tensorflow/core/platform/*/*.cc) \
    603 $(wildcard tensorflow/core/platform/*/*/*.cc) \
    604 $(wildcard tensorflow/core/util/*.cc) \
    605 $(wildcard tensorflow/core/util/*/*.cc) \
    606 tensorflow/core/util/version_info.cc
    607 # Remove duplicates (for version_info.cc)
    608 CORE_CC_ALL_SRCS := $(sort $(CORE_CC_ALL_SRCS))
    609 
    610 CORE_CC_EXCLUDE_SRCS_NON_GPU := \
    611 $(wildcard tensorflow/core/*/*test.cc) \
    612 $(wildcard tensorflow/core/*/*testutil*) \
    613 $(wildcard tensorflow/core/*/*testlib*) \
    614 $(wildcard tensorflow/core/*/*main.cc) \
    615 $(wildcard tensorflow/core/*/*/*test.cc) \
    616 $(wildcard tensorflow/core/*/*/*testutil*) \
    617 $(wildcard tensorflow/core/*/*/*testlib*) \
    618 $(wildcard tensorflow/core/*/*/*main.cc) \
    619 $(wildcard tensorflow/core/debug/*.cc) \
    620 $(wildcard tensorflow/core/framework/op_gen_lib.cc) \
    621 $(wildcard tensorflow/core/graph/dot.*) \
    622 $(wildcard tensorflow/core/lib/db/*) \
    623 $(wildcard tensorflow/core/lib/gif/*) \
    624 $(wildcard tensorflow/core/lib/io/zlib*) \
    625 $(wildcard tensorflow/core/lib/io/record*) \
    626 $(wildcard tensorflow/core/lib/jpeg/*) \
    627 $(wildcard tensorflow/core/lib/png/*) \
    628 $(wildcard tensorflow/core/util/events_writer.*) \
    629 $(wildcard tensorflow/core/util/reporter.*) \
    630 $(wildcard tensorflow/core/platform/default/test_benchmark.*) \
    631 $(wildcard tensorflow/core/platform/cloud/*) \
    632 $(wildcard tensorflow/core/platform/google/*) \
    633 $(wildcard tensorflow/core/platform/google/*/*) \
    634 $(wildcard tensorflow/core/platform/jpeg.*) \
    635 $(wildcard tensorflow/core/platform/png.*) \
    636 $(wildcard tensorflow/core/platform/s3/*) \
    637 $(wildcard tensorflow/core/platform/windows/*) \
    638 $(wildcard tensorflow/core/grappler/inputs/trivial_test_graph_input_yielder.*) \
    639 $(wildcard tensorflow/core/grappler/inputs/file_input_yielder.*) \
    640 $(wildcard tensorflow/core/grappler/clusters/single_machine.*) \
    641 tensorflow/core/util/cuda_kernel_helper_test.cu.cc
    642 
    643 CORE_CC_EXCLUDE_SRCS := \
    644 $(CORE_CC_EXCLUDE_SRCS_NON_GPU) \
    645 $(wildcard tensorflow/core/platform/stream_executor.*) \
    646 $(wildcard tensorflow/core/platform/default/cuda_libdevice_path.*) \
    647 $(wildcard tensorflow/core/platform/cuda.h) \
    648 $(wildcard tensorflow/core/platform/cuda_libdevice_path.*) \
    649 $(wildcard tensorflow/core/user_ops/*.cu.cc) \
    650 $(wildcard tensorflow/core/common_runtime/gpu/*) \
    651 $(wildcard tensorflow/core/common_runtime/gpu_device_factory.*)
    652 
    653 ifeq ($(BUILD_FOR_TEGRA),1)
    654 CORE_CC_ALL_SRCS := $(CORE_CC_ALL_SRCS) \
    655 tensorflow/core/kernels/concat_lib_gpu.cc \
    656 tensorflow/core/kernels/cuda_solvers.cc \
    657 tensorflow/core/kernels/cudnn_pooling_gpu.cc \
    658 tensorflow/core/kernels/dense_update_functor.cc \
    659 tensorflow/core/kernels/fractional_avg_pool_op.cc \
    660 tensorflow/core/kernels/fractional_max_pool_op.cc \
    661 tensorflow/core/kernels/fractional_pool_common.cc \
    662 tensorflow/core/kernels/pooling_ops_3d.cc \
    663 tensorflow/core/kernels/sparse_fill_empty_rows_op.cc \
    664 tensorflow/core/kernels/list_kernels.cc \
    665 $(wildcard tensorflow/core/common_runtime/gpu/*.cc) \
    666 $(wildcard tensorflow/stream_executor/*.cc) \
    667 $(wildcard tensorflow/stream_executor/*/*.cc)
    668 
    669 CORE_CC_EXCLUDE_SRCS := \
    670 $(CORE_CC_EXCLUDE_SRCS_NON_GPU)
    671 
    672 CUDA_CC_SRCS := $(wildcard tensorflow/core/kernels/*.cu.cc)
    673 CUDA_CC_OBJS := $(addprefix $(OBJDIR), $(CUDA_CC_SRCS:.cc=.o))
    674 endif  # TEGRA
    675 
    676 # Filter out all the excluded files.
    677 TF_CC_SRCS := $(filter-out $(CORE_CC_EXCLUDE_SRCS), $(CORE_CC_ALL_SRCS))
    678 # Add in any extra files that don't fit the patterns easily
    679 TF_CC_SRCS += tensorflow/contrib/makefile/downloads/fft2d/fftsg.c
    680 # Also include the op and kernel definitions.
    681 TF_CC_SRCS += $(shell cat $(MAKEFILE_DIR)/tf_op_files.txt)
    682 PBT_CC_SRCS := $(shell cat $(MAKEFILE_DIR)/tf_pb_text_files.txt)
    683 PROTO_SRCS := $(shell cat $(MAKEFILE_DIR)/tf_proto_files.txt)
    684 BENCHMARK_SRCS := \
    685 tensorflow/core/util/reporter.cc \
    686 tensorflow/tools/benchmark/benchmark_model.cc \
    687 tensorflow/tools/benchmark/benchmark_model_main.cc
    688 
    689 ifdef HEXAGON_LIBS
    690 	TF_CC_SRCS += \
    691 tensorflow/cc/framework/scope.cc \
    692 tensorflow/cc/framework/ops.cc \
    693 tensorflow/cc/ops/const_op.cc \
    694 tensorflow/core/kernels/hexagon/graph_transfer_utils.cc \
    695 tensorflow/core/kernels/hexagon/graph_transferer.cc \
    696 tensorflow/core/kernels/hexagon/hexagon_control_wrapper.cc \
    697 tensorflow/core/kernels/hexagon/hexagon_ops_definitions.cc \
    698 tensorflow/core/kernels/hexagon/hexagon_remote_fused_graph_executor_build.cc
    699 endif
    700 
    701 # File names of the intermediate files target compilation generates.
    702 TF_CC_OBJS := $(addprefix $(OBJDIR), \
    703 $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(TF_CC_SRCS))))
    704 PBT_GEN_FILES := $(addprefix $(PBTGENDIR), $(PBT_CC_SRCS))
    705 PBT_OBJS := $(addprefix $(OBJDIR), $(PBT_CC_SRCS:.cc=.o))
    706 PROTO_CC_SRCS := $(addprefix $(PROTOGENDIR), $(PROTO_SRCS:.proto=.pb.cc))
    707 PROTO_OBJS := $(addprefix $(OBJDIR), $(PROTO_SRCS:.proto=.pb.o))
    708 LIB_OBJS := $(PROTO_OBJS) $(TF_CC_OBJS) $(PBT_OBJS)
    709 BENCHMARK_OBJS := $(addprefix $(OBJDIR), $(BENCHMARK_SRCS:.cc=.o))
    710 
    711 .PHONY: clean cleantarget
    712 
    713 # The target that's compiled if there's no command-line arguments.
    714 all: $(LIB_PATH) $(BENCHMARK_NAME)
    715 
    716 # Rules for target compilation.
    717 
    718 
    719 .phony_version_info:
    720 tensorflow/core/util/version_info.cc: .phony_version_info
    721 	tensorflow/tools/git/gen_git_source.sh $@
    722 
    723 # Gathers together all the objects we've compiled into a single '.a' archive.
    724 $(LIB_PATH): $(LIB_OBJS)
    725 	@mkdir -p $(dir $@)
    726 	$(AR) $(ARFLAGS) $(LIB_PATH) $(LIB_OBJS)
    727 
    728 $(BENCHMARK_NAME): $(BENCHMARK_OBJS) $(LIB_PATH) $(CUDA_LIB_DEPS)
    729 	@mkdir -p $(dir $@)
    730 	$(CXX) $(CXXFLAGS) $(INCLUDES) \
    731 	-o $(BENCHMARK_NAME) $(BENCHMARK_OBJS) \
    732 	$(LIBFLAGS) $(TEGRA_LIBS) $(LIB_PATH) $(LDFLAGS) $(LIBS) $(CUDA_LIBS)
    733 
    734 # NVCC compilation rules for Tegra
    735 ifeq ($(BUILD_FOR_TEGRA),1)
    736 $(OBJDIR)%.cu.o: %.cu.cc
    737 	@mkdir -p $(dir $@)
    738 	@mkdir -p $(dir $(DEPDIR)$*)
    739 	$(NVCC) $(NVCCFLAGS) -Xcompiler "$(CXXFLAGS4NVCC) $(DEPFLAGS)" $(INCLUDES) -c $< -o $@
    740 
    741 $(LIBDIR)libtfcuda.a: $(CUDA_CC_OBJS)
    742 	@mkdir -p $(dir $@)
    743 	$(AR) $(ARFLAGS) $@ $(CUDA_CC_OBJS)
    744 endif
    745 
    746 # Matches on the normal hand-written TensorFlow C++ source files.
    747 $(OBJDIR)%.o: %.cc | $(PBT_GEN_FILES)
    748 	@mkdir -p $(dir $@)
    749 	@mkdir -p $(dir $(DEPDIR)$*)
    750 	$(CXX) $(CXXFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@
    751 	@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
    752 
    753 # Matches on plain C files.
    754 $(OBJDIR)%.o: %.c
    755 	@mkdir -p $(dir $@)
    756 	@mkdir -p $(dir $(DEPDIR)$*)
    757 	$(CXX) $(patsubst --std=c++11,--std=c99, $(CXXFLAGS)) -x c $(DEPFLAGS) \
    758 $(INCLUDES) -c $< -o $@
    759 	@mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
    760 
    761 # Compiles C++ source files that have been generated by protoc.
    762 $(OBJDIR)%.pb.o: $(PROTOGENDIR)%.pb.cc
    763 	@mkdir -p $(dir $@)
    764 	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
    765 
    766 # Builds C++ code from proto files using protoc.
    767 $(PROTOGENDIR)%.pb.cc $(PROTOGENDIR)%.pb.h: %.proto
    768 	@mkdir -p $(dir $@)
    769 	$(PROTOC) $(PROTOCFLAGS) $< --cpp_out $(PROTOGENDIR)
    770 
    771 # Uses proto_text to generate minimal pb_text C++ files from protos.
    772 $(PBTGENDIR)%.pb_text.cc $(PBTGENDIR)%.pb_text.h $(PBTGENDIR)%.pb_text-impl.h: %.proto | $(PROTO_TEXT)
    773 	@mkdir -p $(dir $@)
    774 	$(PROTO_TEXT) \
    775 	$(PBTGENDIR)tensorflow/core \
    776 	tensorflow/core/ \
    777 	tensorflow/tools/proto_text/placeholder.txt \
    778 	$<
    779 
    780 # Compiles the C++ source files created by proto_text.
    781 $(OBJDIR)%.pb_text.o: $(PBTGENDIR)%.pb_text.cc
    782 	@mkdir -p $(dir $@)
    783 	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
    784 
    785 # Makes sure that we don't compile the protoc-generated C++ sources before they
    786 # and the proto_text files have been created.
    787 $(PROTO_OBJS): $(PROTO_CC_SRCS) $(PBT_GEN_FILES)
    788 
    789 # Host compilation rules.
    790 
    791 # For normal manually-created TensorFlow C++ source files.
    792 $(HOST_OBJDIR)%.o: %.cc
    793 	@mkdir -p $(dir $@)
    794 	$(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_INCLUDES) -c $< -o $@
    795 
    796 # Compiles object code from protoc-built C++ source files.
    797 $(HOST_OBJDIR)%.pb.o: $(HOST_GENDIR)%.pb.cc
    798 	@mkdir -p $(dir $@)
    799 	$(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_INCLUDES) -c $< -o $@
    800 
    801 # Ensures we wait until proto_text has generated the .h files from protos before
    802 # we compile the C++.
    803 $(PROTO_TEXT_OBJS) : $(PROTO_TEXT_PB_H_FILES)
    804 
    805 # Runs proto_text to generate C++ source files from protos.
    806 $(PROTO_TEXT): $(PROTO_TEXT_OBJS) $(PROTO_TEXT_PB_H_FILES)
    807 	@mkdir -p $(dir $@)
    808 	$(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_INCLUDES) \
    809 	-o $(PROTO_TEXT) $(PROTO_TEXT_OBJS) $(HOST_LDOPTS) $(HOST_LIBS)
    810 
    811 # Compiles the C++ source files from protos using protoc.
    812 $(HOST_GENDIR)%.pb.cc $(HOST_GENDIR)%.pb.h: %.proto
    813 	@mkdir -p $(dir $@)
    814 	$(PROTOC) $(PROTOCFLAGS) $< --cpp_out $(HOST_GENDIR)
    815 
    816 # Gets rid of all generated files.
    817 clean:
    818 	rm -rf $(MAKEFILE_DIR)/gen
    819 	rm -rf tensorflow/core/util/version_info.cc
    820 
    821 # Gets rid of all generated files except protobuf libs generated
    822 # before calling make.  This allows users not to recompile proto libs everytime.
    823 clean_except_protobuf_libs:
    824 	find $(MAKEFILE_DIR)/gen -mindepth 1 -maxdepth 1 ! -name "protobuf*" -exec rm -r "{}" \;
    825 	rm -rf tensorflow/core/util/version_info.cc
    826 
    827 # Gets rid of target files only, leaving the host alone. Also leaves the lib
    828 # directory untouched deliberately, so we can persist multiple architectures
    829 # across builds for iOS and Android.
    830 cleantarget:
    831 	rm -rf $(OBJDIR)
    832 	rm -rf $(BINDIR)
    833 	rm -rf $(LIBDIR)
    834 
    835 $(DEPDIR)/%.d: ;
    836 .PRECIOUS: $(DEPDIR)/%.d
    837 
    838 -include $(patsubst %,$(DEPDIR)/%.d,$(basename $(TF_CC_SRCS)))
    839 
    840 ifdef SUB_MAKEFILES
    841   $(warning "include sub makefiles, must not contain white spaces in the path:" $(SUB_MAKEFILES))
    842   include $(SUB_MAKEFILES)
    843 endif
    844