summaryrefslogtreecommitdiff
path: root/infra/nncc/cmake/packages/TensorFlowProtoText-1.12/TensorFlowProtoTextConfig.cmake
blob: 5963ce4181d825795419372f0c9e498e7cbfaf32 (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
function(_TensorFlowProtoText_import)
  macro(require_package PKGNAME)
    nncc_find_package(${PKGNAME} ${ARGN} QUIET)
    if(NOT ${PKGNAME}_FOUND)
      message(STATUS "Found TensorFlowProtoText: FALSE (${PKGNAME} is missing)")
      set(TensorFlowProtoText_FOUND FALSE PARENT_SCOPE)
      return()
    endif(NOT ${PKGNAME}_FOUND)
  endmacro(require_package)

  require_package(TensorFlowSource EXACT 1.12)
  require_package(Abseil)
  require_package(Eigen)
  require_package(Protobuf)
  require_package(GoogleDoubleConversion)
  require_package(GoogleNSync)

  if(NOT TARGET tensorflow-prototext-1.12)
    nncc_include(ExternalProjectTools)
    add_extdirectory("${CMAKE_CURRENT_LIST_DIR}/build" TensorFlowProtoText-1.12)
  endif(NOT TARGET tensorflow-prototext-1.12)

  set(TensorFlowProtoText_FOUND TRUE PARENT_SCOPE)
endfunction(_TensorFlowProtoText_import)

_TensorFlowProtoText_import()

if(TensorFlowProtoText_FOUND)
  # CMAKE_CURRENT_LIST_DIR
  #
  # ... The value has dynamic scope. ... Therefore the value of the variable inside a macro
  # or function is the directory of the file invoking the bottom-most entry on the call stack,
  # not the directory of the file containing the macro or function definition.
  #
  # Reference: https://cmake.org/cmake/help/v3.1/variable/CMAKE_CURRENT_LIST_DIR.html
  set(TENSORLFLOW_PROTO_TEXT_1_12_CMAKE_DIR
    "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL
    "Where to find make_directories"
  )

  # Comments from "gen_proto_text_functions.cc"
  # >
  # > Main program to take input protos and write output pb_text source files that
  # > contain generated proto text input and output functions.
  # >
  # > Main expects:
  # > - First argument is output path
  # > - Second argument is the relative path of the protos to the root. E.g.,
  # >   for protos built by a rule in tensorflow/core, this will be
  # >   tensorflow/core.
  # > - Then any number of source proto file names, plus one source name must be
  # >   placeholder.txt from this gen tool's package.  placeholder.txt is
  # >   ignored for proto resolution, but is used to determine the root at which
  # >   the build tool has placed the source proto files.
  # >
  function(ProtoText_Generate PREFIX OUTPUT_DIR)
    # THIS SHOULD SUCCEED!
    nncc_find_package(TensorFlowSource EXACT 1.12 REQUIRED)

    set(OUTPUT_REL "tensorflow")
    set(PROTO_DIR "${TensorFlowSource_DIR}")

    set(PROTO_INPUTS ${ARGN})
    list(APPEND PROTO_INPUTS "tensorflow/tools/proto_text/placeholder.txt")

    get_filename_component(abs_output_dir ${OUTPUT_DIR} ABSOLUTE)
    get_filename_component(abs_proto_dir ${TensorFlowSource_DIR} ABSOLUTE)

    # Let's reset variables before using them
    # NOTE This DOES NOT AFFECT variables in the parent scope
    unset(PROTO_FILES)
    unset(OUTPUT_DIRS)
    unset(OUTPUT_FILES)

    foreach(proto ${PROTO_INPUTS})
      get_filename_component(fil "${proto}" NAME)
      get_filename_component(dir "${proto}" DIRECTORY)

      get_filename_component(fil_we "${fil}" NAME_WE)

      get_filename_component(abs_fil "${abs_proto_base}/${proto}" ABSOLUTE)
      get_filename_component(abs_dir "${abs_fil}" DIRECTORY)

      list(APPEND PROTO_FILES "${abs_proto_dir}/${proto}")

      if(NOT ${fil} STREQUAL "placeholder.txt")
        list(APPEND OUTPUT_DIRS "${abs_output_dir}/${dir}")
        list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb_text.h")
        list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb_text-impl.h")
        list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb_text.cc")
      endif(NOT ${fil} STREQUAL "placeholder.txt")
    endforeach()

    add_custom_command(OUTPUT ${OUTPUT_FILES}
      # "make_directory" in CMake 3.1 cannot create multiple directories at once.
      # COMMAND ${CMAKE_COMMAND} -E make_directory ${OUTPUT_DIRS}
      COMMAND "${TENSORLFLOW_PROTO_TEXT_1_12_CMAKE_DIR}/make_directories.sh" ${OUTPUT_DIRS}
      COMMAND "$<TARGET_FILE:tensorflow-prototext-1.12>" "${abs_output_dir}/${OUTPUT_REL}" "${OUTPUT_REL}" ${PROTO_FILES}
      DEPENDS ${PROTO_FILES})

    set(${PREFIX}_SOURCES ${OUTPUT_FILES} PARENT_SCOPE)
    set(${PREFIX}_INCLUDE_DIRS ${abs_output_dir} PARENT_SCOPE)
  endfunction(ProtoText_Generate)
endif(TensorFlowProtoText_FOUND)