xref: /aosp_15_r20/external/protobuf/cmake/protobuf-module.cmake.in (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1# This file contains backwards compatibility patches for various legacy functions and variables
2# Functions
3
4function(PROTOBUF_GENERATE_CPP SRCS HDRS)
5  cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO" "" ${ARGN})
6
7  set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}")
8  if(NOT _proto_files)
9    message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
10    return()
11  endif()
12
13  if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
14    set(_append_arg APPEND_PATH)
15  endif()
16
17  if(DEFINED Protobuf_IMPORT_DIRS)
18    set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
19  endif()
20
21  set(_outvar)
22  protobuf_generate(${_append_arg} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
23
24  set(${SRCS})
25  set(${HDRS})
26  foreach(_file ${_outvar})
27    if(_file MATCHES "cc$")
28      list(APPEND ${SRCS} ${_file})
29    else()
30      list(APPEND ${HDRS} ${_file})
31    endif()
32  endforeach()
33  set(${SRCS} ${${SRCS}} PARENT_SCOPE)
34  set(${HDRS} ${${HDRS}} PARENT_SCOPE)
35endfunction()
36
37function(PROTOBUF_GENERATE_PYTHON SRCS)
38  if(NOT ARGN)
39    message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
40    return()
41  endif()
42
43  if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
44    set(_append_arg APPEND_PATH)
45  endif()
46
47  if(DEFINED Protobuf_IMPORT_DIRS)
48    set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
49  endif()
50
51  set(_outvar)
52  protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
53  set(${SRCS} ${_outvar} PARENT_SCOPE)
54endfunction()
55
56# Environment
57
58# Backwards compatibility
59# Define camel case versions of input variables
60foreach(UPPER
61    PROTOBUF_SRC_ROOT_FOLDER
62    PROTOBUF_IMPORT_DIRS
63    PROTOBUF_DEBUG
64    PROTOBUF_LIBRARY
65    PROTOBUF_PROTOC_LIBRARY
66    PROTOBUF_INCLUDE_DIR
67    PROTOBUF_PROTOC_EXECUTABLE
68    PROTOBUF_LIBRARY_DEBUG
69    PROTOBUF_PROTOC_LIBRARY_DEBUG
70    PROTOBUF_LITE_LIBRARY
71    PROTOBUF_LITE_LIBRARY_DEBUG
72    )
73    if (DEFINED ${UPPER})
74        string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
75        if (NOT DEFINED ${Camel})
76            set(${Camel} ${${UPPER}})
77        endif()
78    endif()
79endforeach()
80
81if(DEFINED Protobuf_SRC_ROOT_FOLDER)
82  message(AUTHOR_WARNING "Variable Protobuf_SRC_ROOT_FOLDER defined, but not"
83    " used in CONFIG mode")
84endif()
85
86include(SelectLibraryConfigurations)
87
88# Internal function: search for normal library as well as a debug one
89#    if the debug one is specified also include debug/optimized keywords
90#    in *_LIBRARIES variable
91function(_protobuf_find_libraries name filename)
92  if(${name}_LIBRARIES)
93    # Use result recorded by a previous call.
94  elseif(${name}_LIBRARY)
95    # Honor cache entry used by CMake 3.5 and lower.
96    set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
97  elseif(TARGET protobuf::lib${filename})
98    get_target_property(${name}_LIBRARY_RELEASE protobuf::lib${filename}
99      LOCATION_RELEASE)
100    get_target_property(${name}_LIBRARY_RELWITHDEBINFO protobuf::lib${filename}
101      LOCATION_RELWITHDEBINFO)
102    get_target_property(${name}_LIBRARY_MINSIZEREL protobuf::lib${filename}
103      LOCATION_MINSIZEREL)
104    get_target_property(${name}_LIBRARY_DEBUG protobuf::lib${filename}
105      LOCATION_DEBUG)
106
107    select_library_configurations(${name})
108    set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
109    set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
110  endif()
111endfunction()
112
113#
114# Main.
115#
116
117# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
118# for each directory where a proto file is referenced.
119if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
120  set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
121endif()
122
123# The Protobuf library
124_protobuf_find_libraries(Protobuf protobuf)
125
126# The Protobuf Lite library
127_protobuf_find_libraries(Protobuf_LITE protobuf-lite)
128
129# The Protobuf Protoc Library
130_protobuf_find_libraries(Protobuf_PROTOC protoc)
131
132# Set the include directory
133get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
134  INTERFACE_INCLUDE_DIRECTORIES)
135
136# Set the protoc Executable
137if(NOT Protobuf_PROTOC_EXECUTABLE AND TARGET protobuf::protoc)
138  get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
139    IMPORTED_LOCATION_RELEASE)
140  if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
141    get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
142      IMPORTED_LOCATION_RELWITHDEBINFO)
143  endif()
144  if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
145    get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
146      IMPORTED_LOCATION_MINSIZEREL)
147  endif()
148  if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
149    get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
150      IMPORTED_LOCATION_DEBUG)
151  endif()
152  if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
153    get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
154      IMPORTED_LOCATION_NOCONFIG)
155  endif()
156endif()
157
158# Version info variable
159set(Protobuf_VERSION "@protobuf_VERSION@")
160
161include(FindPackageHandleStandardArgs)
162FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
163    REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
164    VERSION_VAR Protobuf_VERSION
165)
166
167# Backwards compatibility
168# Define upper case versions of output variables
169foreach(Camel
170    Protobuf_VERSION
171    Protobuf_SRC_ROOT_FOLDER
172    Protobuf_IMPORT_DIRS
173    Protobuf_DEBUG
174    Protobuf_INCLUDE_DIRS
175    Protobuf_LIBRARIES
176    Protobuf_PROTOC_LIBRARIES
177    Protobuf_LITE_LIBRARIES
178    Protobuf_LIBRARY
179    Protobuf_PROTOC_LIBRARY
180    Protobuf_INCLUDE_DIR
181    Protobuf_PROTOC_EXECUTABLE
182    Protobuf_LIBRARY_DEBUG
183    Protobuf_PROTOC_LIBRARY_DEBUG
184    Protobuf_LITE_LIBRARY
185    Protobuf_LITE_LIBRARY_DEBUG
186    )
187    string(TOUPPER ${Camel} UPPER)
188    set(${UPPER} ${${Camel}})
189endforeach()
190