CMakeLists.txt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Minimum CMake required
  2. cmake_minimum_required(VERSION 2.8.12)
  3. # Project
  4. project(protobuf-examples)
  5. # Find required protobuf package
  6. find_package(protobuf CONFIG REQUIRED)
  7. if(protobuf_VERBOSE)
  8. message(STATUS "Using Protocol Buffers ${Protobuf_VERSION}")
  9. endif()
  10. set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
  11. # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
  12. if(MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  13. foreach(flag_var
  14. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  15. CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  16. if(${flag_var} MATCHES "/MD")
  17. string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  18. endif(${flag_var} MATCHES "/MD")
  19. endforeach()
  20. endif()
  21. foreach(example add_person list_people)
  22. set(${example}_SRCS ${example}.cc)
  23. set(${example}_PROTOS addressbook.proto)
  24. #Code Generation
  25. if(protobuf_MODULE_COMPATIBLE) #Legacy Support
  26. protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS})
  27. list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS})
  28. else()
  29. foreach(proto_file ${${example}_PROTOS})
  30. get_filename_component(proto_file_abs ${proto_file} ABSOLUTE)
  31. get_filename_component(basename ${proto_file} NAME_WE)
  32. set(generated_files ${basename}.pb.cc ${basename}.pb.h)
  33. list(APPEND ${example}_SRCS ${generated_files})
  34. add_custom_command(
  35. OUTPUT ${generated_files}
  36. COMMAND protobuf::protoc
  37. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I ${CMAKE_CURRENT_SOURCE_DIR} ${proto_file_abs}
  38. COMMENT "Generating ${generated_files} from ${proto_file}"
  39. VERBATIM
  40. )
  41. endforeach()
  42. endif()
  43. #Executable setup
  44. set(executable_name ${example}_cpp)
  45. add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS})
  46. if(protobuf_MODULE_COMPATIBLE) #Legacy mode
  47. target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS})
  48. target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES})
  49. else()
  50. target_link_libraries(${executable_name} protobuf::libprotobuf)
  51. endif()
  52. endforeach()