protobuf.cmake 3.9 KB

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