blob: 113629689c9299134cf079390beca482b86e76c0 [file] [log] [blame]
Feng Xiao9086d962016-07-13 13:47:51 -07001# Minimum CMake required
2cmake_minimum_required(VERSION 2.8.12)
3
4# Project
5project(protobuf-examples)
6
7# Find required protobuf package
8find_package(protobuf CONFIG REQUIRED)
9
10if(protobuf_VERBOSE)
Jiamin Shen0fd83582021-04-21 06:15:30 +080011 message(STATUS "Using Protocol Buffers ${protobuf_VERSION}")
Feng Xiao9086d962016-07-13 13:47:51 -070012endif()
13
14set(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
17if(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()
25endif()
26
27foreach(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 Xiao9086d962016-07-13 13:47:51 -070035 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 Gray03367702017-05-30 16:49:18 -070045 protobuf_generate(TARGET ${executable_name})
Feng Xiao9086d962016-07-13 13:47:51 -070046 endif()
47
48endforeach()