xref: /aosp_15_r20/external/pytorch/cmake/public/protobuf.cmake (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# ---[ Protobuf
2
3# We will try to use the config mode first, and then manual find.
4find_package(Protobuf CONFIG QUIET)
5if(NOT Protobuf_FOUND)
6  find_package(Protobuf MODULE QUIET)
7endif()
8
9if((TARGET protobuf::libprotobuf OR TARGET protobuf::libprotobuf-lite) AND TARGET protobuf::protoc)
10  # Hooray. This is the most ideal situation, meaning that you either have a
11  # Protobuf config file installed (like on Windows), or you are using a
12  # modern CMake that ships with a FindProtobuf.cmake file that produces
13  # modern targets.
14  message(STATUS "Caffe2: Found protobuf with new-style protobuf targets.")
15elseif(Protobuf_FOUND OR PROTOBUF_FOUND)
16  # If the modern targets are not present, we will generate them for you for
17  # backward compatibility. This is backported from CMake's new FindProtobuf.cmake
18  # content.
19  if((NOT PROTOBUF_LIBRARY) AND (NOT PROTOBUF_LITE_LIBRARY))
20    message(FATAL_ERROR
21        "Caffe2: Found protobuf with old style targets, but could not find targets."
22        " PROTOBUF_LIBRARY: " ${PROTOBUF_LIBRARY}
23        " PROTOBUF_LITE_LIBRARY: " ${PROTOBUF_LITE_LIBRARY}
24        " Protobuf_LIBRARY: " ${Protobuf_LIBRARY}
25        " Protobuf_LITE_LIBRARY: " ${Protobuf_LITE_LIBRARY})
26  endif()
27  message(STATUS "Caffe2: Found protobuf with old-style protobuf targets.")
28
29  if(PROTOBUF_LIBRARY)
30    if(NOT TARGET protobuf::libprotobuf)
31      add_library(protobuf::libprotobuf UNKNOWN IMPORTED)
32      set_target_properties(protobuf::libprotobuf PROPERTIES
33          INTERFACE_INCLUDE_DIRECTORIES "${PROTOBUF_INCLUDE_DIRS}")
34    endif()
35    if(EXISTS "${PROTOBUF_LIBRARY}")
36      set_target_properties(protobuf::libprotobuf PROPERTIES
37          IMPORTED_LOCATION "${PROTOBUF_LIBRARY}")
38    endif()
39    if(EXISTS "${PROTOBUF_LIBRARY_RELEASE}")
40      set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
41          IMPORTED_CONFIGURATIONS RELEASE)
42      set_target_properties(protobuf::libprotobuf PROPERTIES
43          IMPORTED_LOCATION_RELEASE "${PROTOBUF_LIBRARY_RELEASE}")
44    endif()
45    if(EXISTS "${PROTOBUF_LIBRARY_DEBUG}")
46      set_property(TARGET protobuf::libprotobuf APPEND PROPERTY
47          IMPORTED_CONFIGURATIONS DEBUG)
48      set_target_properties(protobuf::libprotobuf PROPERTIES
49          IMPORTED_LOCATION_DEBUG "${PROTOBUF_LIBRARY_DEBUG}")
50    endif()
51  endif()
52
53  if(PROTOBUF_LITE_LIBRARY)
54    if(NOT TARGET protobuf::libprotobuf-lite)
55      add_library(protobuf::libprotobuf-lite UNKNOWN IMPORTED)
56      set_target_properties(protobuf::libprotobuf-lite PROPERTIES
57          INTERFACE_INCLUDE_DIRECTORIES "${PROTOBUF_INCLUDE_DIRS}")
58    endif()
59    if(EXISTS "${PROTOBUF_LITE_LIBRARY}")
60      set_target_properties(protobuf::libprotobuf-lite PROPERTIES
61          IMPORTED_LOCATION "${PROTOBUF_LITE_LIBRARY}")
62    endif()
63    if(EXISTS "${PROTOBUF_LITE_LIBRARY_RELEASE}")
64      set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
65          IMPORTED_CONFIGURATIONS RELEASE)
66      set_target_properties(protobuf::libprotobuf-lite PROPERTIES
67          IMPORTED_LOCATION_RELEASE "${PROTOBUF_LITE_LIBRARY_RELEASE}")
68    endif()
69    if(EXISTS "${PROTOBUF_LITE_LIBRARY_DEBUG}")
70      set_property(TARGET protobuf::libprotobuf-lite APPEND PROPERTY
71          IMPORTED_CONFIGURATIONS DEBUG)
72      set_target_properties(protobuf::libprotobuf-lite PROPERTIES
73          IMPORTED_LOCATION_DEBUG "${PROTOBUF_LITE_LIBRARY_DEBUG}")
74    endif()
75  endif()
76
77  if(PROTOBUF_PROTOC_EXECUTABLE)
78    if(NOT TARGET protobuf::protoc)
79      add_executable(protobuf::protoc IMPORTED)
80    endif()
81    set_property(TARGET protobuf::protoc PROPERTY
82        IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE})
83  endif()
84endif()
85
86# After above, we should have the protobuf related target now.
87if((NOT TARGET protobuf::libprotobuf) AND (NOT TARGET protobuf::libprotobuf-lite))
88  message(WARNING
89      "Protobuf cannot be found. Depending on whether you are building Caffe2 "
90      "or a Caffe2 dependent library, the next warning / error will give you "
91      "more info.")
92endif()
93