aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/cmake/tf_cc_ops.cmake
blob: 8a9b2083aef5f146c64d574b3f9931b4917e8f84 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
########################################################
# tf_cc_op_gen_main library
########################################################
set(tf_cc_op_gen_main_srcs
    "${tensorflow_source_dir}/tensorflow/cc/ops/cc_op_gen.cc"
    "${tensorflow_source_dir}/tensorflow/cc/ops/cc_op_gen_main.cc"
    "${tensorflow_source_dir}/tensorflow/cc/ops/cc_op_gen.h"
)

add_library(tf_cc_op_gen_main OBJECT ${tf_cc_op_gen_main_srcs})

add_dependencies(tf_cc_op_gen_main tf_core_framework)

target_include_directories(tf_cc_op_gen_main PRIVATE
    ${tensorflow_source_dir}
    ${eigen_INCLUDE_DIRS}
)

#target_link_libraries(tf_cc_op_gen_main
#    ${CMAKE_THREAD_LIBS_INIT}
#    ${PROTOBUF_LIBRARIES}
#    tf_protos_cc
#    tf_core_lib
#    tf_core_framework
#)

target_compile_options(tf_cc_op_gen_main PRIVATE
    -fno-exceptions
    -DEIGEN_AVOID_STL_ARRAY
)

# C++11
target_compile_features(tf_cc_op_gen_main PRIVATE
    cxx_rvalue_references
)

########################################################
# tf_gen_op_wrapper_cc executables
########################################################

#
#  # Run the op generator.
#  if name == "sendrecv_ops":
#    include_internal = "1"
#  else:
#    include_internal = "0"
#  native.genrule(
#      name=name + "_genrule",
#      outs=[out_ops_file + ".h", out_ops_file + ".cc"],
#      tools=[":" + tool],
#      cmd=("$(location :" + tool + ") $(location :" + out_ops_file + ".h) " +
#           "$(location :" + out_ops_file + ".cc) " + include_internal))



#def tf_gen_op_wrappers_cc(name,
#                          op_lib_names=[],
#                          other_srcs=[],
#                          other_hdrs=[],
#                          pkg=""):
#  subsrcs = other_srcs
#  subhdrs = other_hdrs
#  for n in op_lib_names:
#    tf_gen_op_wrapper_cc(n, "ops/" + n, pkg=pkg)
#    subsrcs += ["ops/" + n + ".cc"]
#    subhdrs += ["ops/" + n + ".h"]
#
#  native.cc_library(name=name,
#                    srcs=subsrcs,
#                    hdrs=subhdrs,
#                    deps=["//tensorflow/core:core_cpu"],
#                    copts=tf_copts(),
#                    alwayslink=1,)

# create directory for ops generated files
set(cc_ops_target_dir ${CMAKE_CURRENT_BINARY_DIR}/tensorflow/cc/ops)

add_custom_target(create_cc_ops_header_dir
    COMMAND ${CMAKE_COMMAND} -E make_directory ${cc_ops_target_dir}
)

set(tf_cc_ops_generated_files)

set(tf_cc_op_lib_names
    ${tf_op_lib_names}
    "user_ops"
)
foreach(tf_cc_op_lib_name ${tf_cc_op_lib_names})
    #tf_gen_op_wrapper_cc(name, out_ops_file, pkg=""):
    #  # Construct an op generator binary for these ops.
    #  tool = out_ops_file + "_gen_cc"  #example ops/array_ops_gen_cc
    #  native.cc_binary(
    #      name = tool,
    #      copts = tf_copts(),
    #      linkopts = ["-lm"],
    #      linkstatic = 1,   # Faster to link this one-time-use binary dynamically
    #      deps = (["//tensorflow/cc:cc_op_gen_main",
    #               pkg + ":" + name + "_op_lib"])
    #  )
 
    # Using <TARGET_OBJECTS:...> to work around an issue where no ops were
    # registered (static initializers dropped by the linker because the ops
    # are not used explicitly in the *_gen_cc executables).
    add_executable(${tf_cc_op_lib_name}_gen_cc
        $<TARGET_OBJECTS:tf_cc_op_gen_main>
        $<TARGET_OBJECTS:tf_${tf_cc_op_lib_name}>
        $<TARGET_OBJECTS:tf_core_lib>
        $<TARGET_OBJECTS:tf_core_framework>
    )

    target_include_directories(${tf_cc_op_lib_name}_gen_cc PRIVATE
        ${tensorflow_source_dir}
        ${eigen_INCLUDE_DIRS}
    )

    find_package(ZLIB REQUIRED)

    target_link_libraries(${tf_cc_op_lib_name}_gen_cc PRIVATE
        ${CMAKE_THREAD_LIBS_INIT}
        ${PROTOBUF_LIBRARIES}
        tf_protos_cc
        re2_lib
        ${jpeg_STATIC_LIBRARIES}
        ${png_STATIC_LIBRARIES}
        ${ZLIB_LIBRARIES}
    )

    target_compile_options(${tf_cc_op_lib_name}_gen_cc PRIVATE
        -fno-exceptions
        -DEIGEN_AVOID_STL_ARRAY
        -lm
    )

    # C++11
    target_compile_features(${tf_cc_op_lib_name}_gen_cc PRIVATE
        cxx_rvalue_references
    )

    set(cc_ops_include_internal 0)
    if(${tf_cc_op_lib_name} STREQUAL "sendrecv_ops")
        set(cc_ops_include_internal 1)
    endif()

    add_custom_command(
        OUTPUT ${cc_ops_target_dir}/${tf_cc_op_lib_name}.h
               ${cc_ops_target_dir}/${tf_cc_op_lib_name}.cc
        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}
        DEPENDS ${tf_cc_op_lib_name}_gen_cc create_cc_ops_header_dir
    )
    
    list(APPEND tf_cc_ops_generated_files ${cc_ops_target_dir}/${tf_cc_op_lib_name}.h)
    list(APPEND tf_cc_ops_generated_files ${cc_ops_target_dir}/${tf_cc_op_lib_name}.cc)
endforeach()


########################################################
# tf_cc_ops library
########################################################
add_library(tf_cc_ops OBJECT
    ${tf_cc_ops_generated_files}
    "${tensorflow_source_dir}/tensorflow/cc/ops/const_op.h"
    "${tensorflow_source_dir}/tensorflow/cc/ops/const_op.cc"
    "${tensorflow_source_dir}/tensorflow/cc/ops/standard_ops.h"
)

target_include_directories(tf_cc_ops PRIVATE
    ${tensorflow_source_dir}
    ${eigen_INCLUDE_DIRS}
)

#target_link_libraries(tf_cc_ops
#    ${CMAKE_THREAD_LIBS_INIT}
#    ${PROTOBUF_LIBRARIES}
#    tf_protos_cc
#    tf_core_lib
#    tf_core_cpu
#    tf_models_word2vec_ops
#)

target_compile_options(tf_cc_ops PRIVATE
    -fno-exceptions
    -DEIGEN_AVOID_STL_ARRAY
)

# C++11
target_compile_features(tf_cc_ops PRIVATE
    cxx_rvalue_references
)


#tf_gen_op_wrappers_cc(
#    name = "cc_ops",
#    op_lib_names = [
#        ...
#    ],
#    other_hdrs = [
#        "ops/const_op.h",
#        "ops/standard_ops.h",
#    ],
#    other_srcs = [
#        "ops/const_op.cc",
#    ] + glob(["ops/*_grad.cc"]),
#    pkg = "//tensorflow/core",
#)