• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protobuf
2#
3#
4# PROTOBUF_LIBRARY - Link this to use protobuf
5#
6if(NOT TARGET protobuf::libprotobuf)
7    set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable protobuf test")
8    set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "Gen shared library")
9    set(_ms_tmp_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
10
11    string(REPLACE " -Wall" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
12    string(REPLACE " -Werror" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
13
14    add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/protobuf/cmake ${CMAKE_BINARY_DIR}/protobuf)
15
16    set(CMAKE_CXX_FLAGS ${_ms_tmp_CMAKE_CXX_FLAGS})
17endif()
18
19include_directories(${CMAKE_CURRENT_LIST_DIR}/../third_party/protobuf/src)
20
21set(PROTOBUF_LIBRARY protobuf::libprotobuf)
22set(PROTOC_EXECUTABLE $<TARGET_FILE:protobuf::protoc>)
23
24function(ms_protobuf_generate c_var h_var)
25    if(NOT ARGN)
26        message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files")
27        return()
28    endif()
29
30    set(${c_var})
31    set(${h_var})
32
33    foreach(file ${ARGN})
34        get_filename_component(abs_file ${file} ABSOLUTE)
35        get_filename_component(file_name ${file} NAME_WE)
36        get_filename_component(file_dir ${abs_file} PATH)
37        file(RELATIVE_PATH rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${file_dir})
38
39
40        list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc")
41        list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h")
42
43        add_custom_command(
44                OUTPUT "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc"
45                "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h"
46                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
47                COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/${rel_path}"
48                COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
49                DEPENDS protobuf::protoc ${abs_file}
50                COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM)
51    endforeach()
52
53    set_source_files_properties(${${c_var}} ${${h_var}} PROPERTIES GENERATED TRUE)
54    set(${c_var} ${${c_var}} PARENT_SCOPE)
55    set(${h_var} ${${h_var}} PARENT_SCOPE)
56
57endfunction()
58
59function(ms_protobuf_generate_py c_var h_var py_var)
60    if(NOT ARGN)
61        message(SEND_ERROR "Error: ms_protobuf_generate() called without any proto files")
62        return()
63    endif()
64
65    set(${c_var})
66    set(${h_var})
67    set(${py_var})
68
69    foreach(file ${ARGN})
70        get_filename_component(abs_file ${file} ABSOLUTE)
71        get_filename_component(file_name ${file} NAME_WE)
72        get_filename_component(file_dir ${abs_file} PATH)
73        file(RELATIVE_PATH rel_path ${CMAKE_CURRENT_SOURCE_DIR} ${file_dir})
74
75
76        list(APPEND ${c_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc")
77        list(APPEND ${h_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h")
78        list(APPEND ${py_var} "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py")
79
80        add_custom_command(
81                OUTPUT "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.cc"
82                "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}.pb.h"
83                "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py"
84                WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
85                COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/${rel_path}"
86                COMMAND protobuf::protoc -I${file_dir} --cpp_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
87                COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
88                COMMAND protobuf::protoc -I${file_dir} --python_out=${CMAKE_BINARY_DIR}/${rel_path} ${abs_file}
89                COMMAND perl -pi -e "s/import (.+_pb2.*)/from . import \\1/"
90                        "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py"
91                COMMAND cp "${CMAKE_BINARY_DIR}/${rel_path}/${file_name}_pb2.py"
92                        "${PROJECT_SOURCE_DIR}/mindspore/train/"
93                DEPENDS protobuf::protoc ${abs_file}
94                COMMENT "Running C++ protocol buffer compiler on ${file}" VERBATIM)
95    endforeach()
96
97    set_source_files_properties(${${c_var}} ${${h_var}} ${${py_var}} PROPERTIES GENERATED TRUE)
98    set(${c_var} ${${c_var}} PARENT_SCOPE)
99    set(${h_var} ${${h_var}} PARENT_SCOPE)
100    set(${py_var} ${${py_var}} PARENT_SCOPE)
101
102endfunction()
103