summaryrefslogtreecommitdiff
path: root/infra/nncc/cmake/packages/ProtobufConfig.cmake
blob: 9064d114048645b5ed1abfff84912346f4b6dec4 (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
# NOTE This function is unused, but remains for future reference
function(_Protobuf_module_import)
  # Let's use find_package here not to export unnecessary definitions
  find_package(Protobuf MODULE QUIET)

  if(NOT PROTOBUF_FOUND)
    set(Protobuf_FOUND FALSE PARENT_SCOPE)
    return()
  endif(NOT PROTOBUF_FOUND)

  if(NOT TARGET protoc)
    add_executable(protoc IMPORTED)
    set_target_properties(protoc PROPERTIES IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE})
   endif(NOT TARGET protoc)

  if(NOT TARGET libprotobuf)
    add_library(libprotobuf INTERFACE)
    target_include_directories(libprotobuf INTERFACE ${PROTOBUF_INCLUDE_DIRS})
    target_link_libraries(libprotobuf INTERFACE ${PROTOBUF_LIBRARIES})
  endif(NOT TARGET libprotobuf)

  set(Protobuf_FOUND TRUE PARENT_SCOPE)
endfunction(_Protobuf_module_import)

function(_Protobuf_import)
  # Let's use find_package here not to export unnecessary definitions
  # NOTE Here we use "exact" match to avoid possible infinite loop
  find_package(protobuf EXACT 3.5.2 QUIET)

  if(NOT protobuf_FOUND)
    set(Protobuf_FOUND FALSE PARENT_SCOPE)
    return()
  endif(NOT protobuf_FOUND)

  if(NOT TARGET libprotobuf)
    add_library(libprotobuf INTERFACE)
    target_link_libraries(libprotobuf INTERFACE protobuf::libprotobuf)
  endif(NOT TARGET libprotobuf)

  set(Protobuf_FOUND TRUE PARENT_SCOPE)
endfunction(_Protobuf_import)

function(_Protobuf_build)
  if(NOT BUILD_PROTOBUF)
    return()
  endif(NOT BUILD_PROTOBUF)

  nncc_find_package(ProtobufSource QUIET)

  if(NOT ProtobufSource_FOUND)
    # Source is not available
    return()
  endif(NOT ProtobufSource_FOUND)

  # TODO Introduce helper functions
  set(PROTOBUF_BUILD_DIR "${CMAKE_BINARY_DIR}/externals/PROTOBUF/build")
  set(PROTOBUF_INSTALL_DIR "${NNCC_OVERLAY_DIR}")

  set(STAMP_PATH "${PROTOBUF_INSTALL_DIR}/PROTOBUF.stamp")
  set(LOG_PATH "${PROTOBUF_INSTALL_DIR}/PROTOBUF.log")

  if(EXISTS ${STAMP_PATH})
    return()
  endif(EXISTS ${STAMP_PATH})

  message(STATUS "Build Protocol Buffer from ${ProtobufSource_DIR}")

  file(MAKE_DIRECTORY ${PROTOBUF_BUILD_DIR})
  file(MAKE_DIRECTORY ${PROTOBUF_INSTALL_DIR})

  # NOTE Do NOT retry Protocol Buffer build
  file(WRITE "${STAMP_PATH}")

  execute_process(COMMAND ${CMAKE_COMMAND}
                            -DCMAKE_INSTALL_PREFIX=${PROTOBUF_INSTALL_DIR}
                            -DCMAKE_BUILD_TYPE=Release
                            -DCMAKE_CXX_FLAGS="-fPIC"
                            -Dprotobuf_BUILD_TESTS=OFF
                            -Dprotobuf_WITH_ZLIB=OFF
                            "${ProtobufSource_DIR}/cmake"
                  OUTPUT_FILE ${LOG_PATH}
                  ERROR_FILE ${LOG_PATH}
                  WORKING_DIRECTORY ${PROTOBUF_BUILD_DIR}
                  RESULT_VARIABLE CONFIGURE_EXITCODE)

  if(NOT CONFIGURE_EXITCODE EQUAL 0)
    message(FATAL_ERROR "Fail to configure Protocol Buffer (check '${LOG_PATH}' for details)")
  endif(NOT CONFIGURE_EXITCODE EQUAL 0)

  execute_process(COMMAND ${CMAKE_COMMAND} --build . -- install
                  OUTPUT_FILE ${LOG_PATH}
                  ERROR_FILE ${LOG_PATH}
                  WORKING_DIRECTORY ${PROTOBUF_BUILD_DIR}
                  RESULT_VARIABLE BUILD_AND_INSTALL_EXITCODE)

  if(NOT BUILD_AND_INSTALL_EXITCODE EQUAL 0)
    message(FATAL_ERROR "Fail to build/install Protocol Buffer (check '${LOG_PATH}' for details)")
  endif(NOT BUILD_AND_INSTALL_EXITCODE EQUAL 0)

  message(STATUS "Succeeded in building Protocol Buffer")
endfunction(_Protobuf_build)

_Protobuf_build()
_Protobuf_import()

if(Protobuf_FOUND)
  function(Protobuf_Generate PREFIX OUTPUT_DIR PROTO_DIR)
    get_filename_component(abs_output_dir ${OUTPUT_DIR} ABSOLUTE)
    get_filename_component(abs_proto_dir ${PROTO_DIR} ABSOLUTE)

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

    foreach(proto ${ARGN})
      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}")
      list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb.h")
      list(APPEND OUTPUT_FILES "${abs_output_dir}/${dir}/${fil_we}.pb.cc")
    endforeach()

    add_custom_command(OUTPUT ${OUTPUT_FILES}
                       COMMAND ${CMAKE_COMMAND} -E make_directory "${abs_output_dir}"
                       COMMAND "$<TARGET_FILE:protobuf::protoc>" --cpp_out "${abs_output_dir}" -I "${abs_proto_dir}" ${PROTO_FILES}
                       DEPENDS ${PROTO_FILES})

    set(${PREFIX}_SOURCES ${OUTPUT_FILES} PARENT_SCOPE)
    set(${PREFIX}_INCLUDE_DIRS ${abs_output_dir} PARENT_SCOPE)
    set(${PREFIX}_LIBRARIES protobuf::libprotobuf PARENT_SCOPE)
  endfunction(Protobuf_Generate)
endif(Protobuf_FOUND)