Feng Xiao | 9086d96 | 2016-07-13 13:47:51 -0700 | [diff] [blame] | 1 | # Minimum CMake required |
| 2 | cmake_minimum_required(VERSION 2.8.12) |
| 3 | |
| 4 | # Project |
| 5 | project(protobuf-examples) |
| 6 | |
| 7 | # Find required protobuf package |
| 8 | find_package(protobuf CONFIG REQUIRED) |
| 9 | |
| 10 | if(protobuf_VERBOSE) |
Jiamin Shen | 0fd8358 | 2021-04-21 06:15:30 +0800 | [diff] [blame] | 11 | message(STATUS "Using Protocol Buffers ${protobuf_VERSION}") |
Feng Xiao | 9086d96 | 2016-07-13 13:47:51 -0700 | [diff] [blame] | 12 | endif() |
| 13 | |
| 14 | set(CMAKE_INCLUDE_CURRENT_DIR TRUE) |
| 15 | |
| 16 | # http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F |
| 17 | if(MSVC AND protobuf_MSVC_STATIC_RUNTIME) |
| 18 | foreach(flag_var |
| 19 | CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE |
| 20 | CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) |
| 21 | if(${flag_var} MATCHES "/MD") |
| 22 | string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") |
| 23 | endif(${flag_var} MATCHES "/MD") |
| 24 | endforeach() |
| 25 | endif() |
| 26 | |
| 27 | foreach(example add_person list_people) |
| 28 | set(${example}_SRCS ${example}.cc) |
| 29 | set(${example}_PROTOS addressbook.proto) |
| 30 | |
| 31 | #Code Generation |
| 32 | if(protobuf_MODULE_COMPATIBLE) #Legacy Support |
| 33 | protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS}) |
| 34 | list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS}) |
Feng Xiao | 9086d96 | 2016-07-13 13:47:51 -0700 | [diff] [blame] | 35 | endif() |
| 36 | |
| 37 | #Executable setup |
| 38 | set(executable_name ${example}_cpp) |
| 39 | add_executable(${executable_name} ${${example}_SRCS} ${${example}_PROTOS}) |
| 40 | if(protobuf_MODULE_COMPATIBLE) #Legacy mode |
| 41 | target_include_directories(${executable_name} PUBLIC ${PROTOBUF_INCLUDE_DIRS}) |
| 42 | target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES}) |
| 43 | else() |
| 44 | target_link_libraries(${executable_name} protobuf::libprotobuf) |
Walter Gray | 0336770 | 2017-05-30 16:49:18 -0700 | [diff] [blame] | 45 | protobuf_generate(TARGET ${executable_name}) |
Feng Xiao | 9086d96 | 2016-07-13 13:47:51 -0700 | [diff] [blame] | 46 | endif() |
| 47 | |
| 48 | endforeach() |