blob: d81dc459b6a54134c629d7464681e826f6dc4d26 [file] [log] [blame]
Konstantin Podsvirov0be07892015-08-02 16:31:28 +03001if(PROTOBUF_SRC_ROOT_FOLDER)
2 message(AUTHOR_WARNING "Variable PROTOBUF_SRC_ROOT_FOLDER defined, but not"
3 " used in CONFIG mode")
4endif()
5
6function(PROTOBUF_GENERATE_CPP SRCS HDRS)
7 if(NOT ARGN)
8 message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
9 return()
10 endif()
11
12 if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
13 # Create an include path for each file specified
14 foreach(FIL ${ARGN})
15 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
16 get_filename_component(ABS_PATH ${ABS_FIL} PATH)
17 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
18 if(${_contains_already} EQUAL -1)
19 list(APPEND _protobuf_include_path -I ${ABS_PATH})
20 endif()
21 endforeach()
22 else()
23 set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
24 endif()
25
Konstantin Podsvirov5500b062015-08-03 12:10:55 +030026 # Add well-known type protos include path
Konstantin Podsvirovebfca172015-08-03 11:58:48 +030027 list(APPEND _protobuf_include_path
28 -I "${_PROTOBUF_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@")
29
Konstantin Podsvirov0be07892015-08-02 16:31:28 +030030 if(DEFINED PROTOBUF_IMPORT_DIRS)
31 foreach(DIR ${PROTOBUF_IMPORT_DIRS})
32 get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
33 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
34 if(${_contains_already} EQUAL -1)
35 list(APPEND _protobuf_include_path -I ${ABS_PATH})
36 endif()
37 endforeach()
38 endif()
39
40 set(${SRCS})
41 set(${HDRS})
42 foreach(FIL ${ARGN})
43 get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
44 get_filename_component(FIL_WE ${FIL} NAME_WE)
45
46 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
47 list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
48
49 add_custom_command(
50 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
51 "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
52 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
53 ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
54 DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
55 COMMENT "Running C++ protocol buffer compiler on ${FIL}"
56 VERBATIM)
57 endforeach()
58
59 set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
60 set(${SRCS} ${${SRCS}} PARENT_SCOPE)
61 set(${HDRS} ${${HDRS}} PARENT_SCOPE)
62endfunction()
63
64# Internal function: search for normal library as well as a debug one
65# if the debug one is specified also include debug/optimized keywords
66# in *_LIBRARIES variable
67function(_protobuf_find_libraries name filename)
68 get_target_property(${name}_LIBRARY lib${filename}
69 IMPORTED_LOCATION_RELEASE)
Konstantin Podsvirovebfca172015-08-03 11:58:48 +030070 set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
Konstantin Podsvirov0be07892015-08-02 16:31:28 +030071 get_target_property(${name}_LIBRARY_DEBUG lib${filename}
72 IMPORTED_LOCATION_DEBUG)
Konstantin Podsvirovebfca172015-08-03 11:58:48 +030073 set(${name}_LIBRARY_DEBUG "${${name}_LIBRARY_DEBUG}" PARENT_SCOPE)
Konstantin Podsvirov0be07892015-08-02 16:31:28 +030074
75 if(NOT ${name}_LIBRARY_DEBUG)
76 # There is no debug library
77 set(${name}_LIBRARY_DEBUG ${${name}_LIBRARY} PARENT_SCOPE)
78 set(${name}_LIBRARIES ${${name}_LIBRARY} PARENT_SCOPE)
79 else()
80 # There IS a debug library
81 set(${name}_LIBRARIES
82 optimized ${${name}_LIBRARY}
83 debug ${${name}_LIBRARY_DEBUG}
84 PARENT_SCOPE
85 )
86 endif()
87endfunction()
88
89# Internal function: find threads library
90function(_protobuf_find_threads)
91 set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
92 find_package(Threads)
93 if(Threads_FOUND)
94 list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
95 set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
96 endif()
97endfunction()
98
99#
100# Main.
101#
102
103# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
104# for each directory where a proto file is referenced.
105if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
106 set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
107endif()
108
109# The Protobuf library
110_protobuf_find_libraries(PROTOBUF protobuf)
111
112# The Protobuf Lite library
113_protobuf_find_libraries(PROTOBUF_LITE protobuf-lite)
114
115# The Protobuf Protoc Library
116_protobuf_find_libraries(PROTOBUF_PROTOC protoc)
117
118if(UNIX)
119 _protobuf_find_threads()
120endif()
121
122# Set the include directory
Konstantin Podsvirovebfca172015-08-03 11:58:48 +0300123set(PROTOBUF_INCLUDE_DIR "${_PROTOBUF_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@")
Konstantin Podsvirov0be07892015-08-02 16:31:28 +0300124
125# Set the protoc Executable
126get_target_property(PROTOBUF_PROTOC_EXECUTABLE protoc
127 IMPORTED_LOCATION_RELEASE)
Konstantin Podsvirovebfca172015-08-03 11:58:48 +0300128if(NOT PROTOBUF_PROTOC_EXECUTABLE)
129 get_target_property(PROTOBUF_PROTOC_EXECUTABLE protoc
130 IMPORTED_LOCATION_DEBUG)
131endif()
Konstantin Podsvirov0be07892015-08-02 16:31:28 +0300132
133include(FindPackageHandleStandardArgs)
134FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
135 PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
136
137if(PROTOBUF_FOUND)
138 set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
139endif()