1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15######################################################## 16# tf_cc_framework library 17######################################################## 18set(tf_cc_framework_srcs 19 "${tensorflow_source_dir}/tensorflow/cc/framework/ops.h" 20 "${tensorflow_source_dir}/tensorflow/cc/framework/ops.cc" 21 "${tensorflow_source_dir}/tensorflow/cc/framework/scope.h" 22 "${tensorflow_source_dir}/tensorflow/cc/framework/scope_internal.h" 23 "${tensorflow_source_dir}/tensorflow/cc/framework/scope.cc" 24) 25 26add_library(tf_cc_framework OBJECT ${tf_cc_framework_srcs}) 27 28add_dependencies(tf_cc_framework tf_core_framework) 29 30######################################################## 31# tf_cc_op_gen_main library 32######################################################## 33set(tf_cc_op_gen_main_srcs 34 "${tensorflow_source_dir}/tensorflow/cc/framework/cc_op_gen.cc" 35 "${tensorflow_source_dir}/tensorflow/cc/framework/cc_op_gen_main.cc" 36 "${tensorflow_source_dir}/tensorflow/cc/framework/cc_op_gen.h" 37) 38 39add_library(tf_cc_op_gen_main OBJECT ${tf_cc_op_gen_main_srcs}) 40 41add_dependencies(tf_cc_op_gen_main tf_core_framework) 42 43######################################################## 44# tf_gen_op_wrapper_cc executables 45######################################################## 46 47# create directory for ops generated files 48set(cc_ops_target_dir ${CMAKE_CURRENT_BINARY_DIR}/tensorflow/cc/ops) 49 50add_custom_target(create_cc_ops_header_dir 51 COMMAND ${CMAKE_COMMAND} -E make_directory ${cc_ops_target_dir} 52) 53 54set(tf_cc_ops_generated_files) 55 56set(tf_cc_op_lib_names 57 ${tf_op_lib_names} 58 "user_ops" 59) 60foreach(tf_cc_op_lib_name ${tf_cc_op_lib_names}) 61 # Using <TARGET_OBJECTS:...> to work around an issue where no ops were 62 # registered (static initializers dropped by the linker because the ops 63 # are not used explicitly in the *_gen_cc executables). 64 add_executable(${tf_cc_op_lib_name}_gen_cc 65 $<TARGET_OBJECTS:tf_cc_op_gen_main> 66 $<TARGET_OBJECTS:tf_${tf_cc_op_lib_name}> 67 $<TARGET_OBJECTS:tf_core_lib> 68 $<TARGET_OBJECTS:tf_core_framework> 69 ) 70 71 target_link_libraries(${tf_cc_op_lib_name}_gen_cc PRIVATE 72 tf_protos_cc 73 ${tensorflow_EXTERNAL_LIBRARIES} 74 ) 75 76 set(cc_ops_include_internal 0) 77 if(${tf_cc_op_lib_name} STREQUAL "sendrecv_ops") 78 set(cc_ops_include_internal 1) 79 endif() 80 81 add_custom_command( 82 OUTPUT ${cc_ops_target_dir}/${tf_cc_op_lib_name}.h 83 ${cc_ops_target_dir}/${tf_cc_op_lib_name}.cc 84 ${cc_ops_target_dir}/${tf_cc_op_lib_name}_internal.h 85 ${cc_ops_target_dir}/${tf_cc_op_lib_name}_internal.cc 86 COMMAND ${tf_cc_op_lib_name}_gen_cc ${cc_ops_target_dir}/${tf_cc_op_lib_name}.h ${cc_ops_target_dir}/${tf_cc_op_lib_name}.cc ${cc_ops_include_internal} ${tensorflow_source_dir}/tensorflow/core/api_def/base_api 87 DEPENDS ${tf_cc_op_lib_name}_gen_cc create_cc_ops_header_dir 88 ) 89 90 list(APPEND tf_cc_ops_generated_files ${cc_ops_target_dir}/${tf_cc_op_lib_name}.h) 91 list(APPEND tf_cc_ops_generated_files ${cc_ops_target_dir}/${tf_cc_op_lib_name}.cc) 92 list(APPEND tf_cc_ops_generated_files ${cc_ops_target_dir}/${tf_cc_op_lib_name}_internal.h) 93 list(APPEND tf_cc_ops_generated_files ${cc_ops_target_dir}/${tf_cc_op_lib_name}_internal.cc) 94endforeach() 95 96 97 98######################################################## 99# tf_cc_ops library 100######################################################## 101add_library(tf_cc_ops OBJECT 102 ${tf_cc_ops_generated_files} 103 "${tensorflow_source_dir}/tensorflow/cc/ops/const_op.h" 104 "${tensorflow_source_dir}/tensorflow/cc/ops/const_op.cc" 105 "${tensorflow_source_dir}/tensorflow/cc/ops/standard_ops.h" 106) 107 108######################################################## 109# tf_cc_while_loop library 110######################################################## 111add_library(tf_cc_while_loop OBJECT 112 "${tensorflow_source_dir}/tensorflow/cc/ops/while_loop.h" 113 "${tensorflow_source_dir}/tensorflow/cc/ops/while_loop.cc" 114) 115 116add_dependencies(tf_cc_while_loop tf_core_framework tf_cc_ops) 117 118######################################################## 119# tf_cc library 120######################################################## 121file(GLOB_RECURSE tf_cc_srcs 122 "${tensorflow_source_dir}/tensorflow/cc/client/*.h" 123 "${tensorflow_source_dir}/tensorflow/cc/client/*.cc" 124 "${tensorflow_source_dir}/tensorflow/cc/gradients/*.h" 125 "${tensorflow_source_dir}/tensorflow/cc/gradients/*.cc" 126 "${tensorflow_source_dir}/tensorflow/cc/training/*.h" 127 "${tensorflow_source_dir}/tensorflow/cc/training/*.cc" 128) 129 130set(tf_cc_srcs 131 ${tf_cc_srcs} 132 "${tensorflow_source_dir}/tensorflow/cc/framework/grad_op_registry.h" 133 "${tensorflow_source_dir}/tensorflow/cc/framework/grad_op_registry.cc" 134 "${tensorflow_source_dir}/tensorflow/cc/framework/gradient_checker.h" 135 "${tensorflow_source_dir}/tensorflow/cc/framework/gradient_checker.cc" 136 "${tensorflow_source_dir}/tensorflow/cc/framework/gradients.h" 137 "${tensorflow_source_dir}/tensorflow/cc/framework/gradients.cc" 138 "${tensorflow_source_dir}/tensorflow/cc/framework/while_gradients.h" 139 "${tensorflow_source_dir}/tensorflow/cc/framework/while_gradients.cc" 140) 141 142file(GLOB_RECURSE tf_cc_test_srcs 143 "${tensorflow_source_dir}/tensorflow/cc/*test*.cc" 144) 145 146list(REMOVE_ITEM tf_cc_srcs ${tf_cc_test_srcs}) 147 148add_library(tf_cc OBJECT ${tf_cc_srcs}) 149add_dependencies(tf_cc tf_cc_framework tf_cc_ops) 150 151if (WIN32) 152 set (pywrap_tensorflow_lib "${CMAKE_CURRENT_BINARY_DIR}/$(Configuration)/pywrap_tensorflow_internal.lib") 153else (WIN32) 154 set (pywrap_tensorflow_lib "${CMAKE_CURRENT_BINARY_DIR}/libpywrap_tensorflow_internal${CMAKE_SHARED_LIBRARY_SUFFIX}") 155endif (WIN32) 156add_custom_target(tf_extension_ops) 157 158function(AddUserOps) 159 cmake_parse_arguments(_AT "" "" "TARGET;SOURCES;GPUSOURCES;DEPENDS;DISTCOPY" ${ARGN}) 160 if (tensorflow_ENABLE_GPU AND _AT_GPUSOURCES) 161 # if gpu build is enabled and we have gpu specific code, 162 # hint to cmake that this needs to go to nvcc 163 set (gpu_source ${_AT_GPUSOURCES}) 164 set (gpu_lib "${_AT_TARGET}_gpu") 165 set_source_files_properties(${gpu_source} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ) 166 cuda_compile(gpu_lib ${gpu_source}) 167 endif() 168 # create shared library from source and cuda obj 169 add_library(${_AT_TARGET} SHARED ${_AT_SOURCES} ${gpu_lib}) 170 target_link_libraries(${_AT_TARGET} ${pywrap_tensorflow_lib}) 171 if (tensorflow_ENABLE_GPU AND _AT_GPUSOURCES) 172 # some ops call out to cuda directly; need to link libs for the cuda dlls 173 target_link_libraries(${_AT_TARGET} ${CUDA_LIBRARIES}) 174 endif() 175 if (_AT_DISTCOPY) 176 add_custom_command(TARGET ${_AT_TARGET} POST_BUILD 177 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${_AT_TARGET}> ${_AT_DISTCOPY}/) 178 endif() 179 if (_AT_DEPENDS) 180 add_dependencies(${_AT_TARGET} ${_AT_DEPENDS}) 181 endif() 182 # make sure TF_COMPILE_LIBRARY is not defined for this target 183 get_target_property(target_compile_flags ${_AT_TARGET} COMPILE_FLAGS) 184 if(target_compile_flags STREQUAL "target_compile_flags-NOTFOUND") 185 if (WIN32) 186 set(target_compile_flags "/UTF_COMPILE_LIBRARY") 187 else (WIN32) 188 # gcc uses UTF as default 189 set(target_compile_flags "-finput-charset=UTF-8") 190 endif (WIN32) 191 else() 192 if (WIN32) 193 set(target_compile_flags "${target_compile_flags} /UTF_COMPILE_LIBRARY") 194 else (WIN32) 195 # gcc uses UTF as default 196 set(target_compile_flags "${target_compile_flags} -finput-charset=UTF-8") 197 endif (WIN32) 198 endif() 199 set_target_properties(${_AT_TARGET} PROPERTIES COMPILE_FLAGS ${target_compile_flags}) 200 add_dependencies(tf_extension_ops ${_AT_TARGET}) 201endfunction(AddUserOps) 202