1cmake_minimum_required(VERSION 2.8.12) 2 3project(nanopb C) 4 5set(nanopb_VERSION_STRING nanopb-0.4.7) 6set(nanopb_SOVERSION 0) 7 8string(REPLACE "nanopb-" "" nanopb_VERSION ${nanopb_VERSION_STRING}) 9 10option(BUILD_SHARED_LIBS "Build shared libraries" OFF) 11option(BUILD_STATIC_LIBS "Build static libraries" ON) 12 13option(nanopb_BUILD_RUNTIME "Build the headers and libraries needed at runtime" ON) 14option(nanopb_BUILD_GENERATOR "Build the protoc plugin for code generation" ON) 15option(nanopb_MSVC_STATIC_RUNTIME "Link static runtime libraries" ON) 16 17find_program(nanopb_PROTOC_PATH protoc HINTS generator-bin generator) 18if(NOT EXISTS ${nanopb_PROTOC_PATH}) 19 message(FATAL_ERROR "protoc compiler not found") 20endif() 21 22if(NOT DEFINED CMAKE_DEBUG_POSTFIX) 23 set(CMAKE_DEBUG_POSTFIX "d") 24endif() 25 26include(GNUInstallDirs) 27 28if(MSVC AND nanopb_MSVC_STATIC_RUNTIME) 29 foreach(flag_var 30 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE 31 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO) 32 if(${flag_var} MATCHES "/MD") 33 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 34 endif(${flag_var} MATCHES "/MD") 35 endforeach(flag_var) 36endif() 37 38if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR) 39 set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/nanopb") 40endif() 41 42find_package(Python REQUIRED COMPONENTS Interpreter) 43execute_process( 44 COMMAND ${Python_EXECUTABLE} -c 45 "import os.path, sys, sysconfig; print(os.path.relpath(sysconfig.get_path('purelib'), start=sys.prefix))" 46 OUTPUT_VARIABLE PYTHON_INSTDIR 47 OUTPUT_STRIP_TRAILING_WHITESPACE 48) 49 50if(nanopb_BUILD_GENERATOR) 51 set(generator_protos nanopb) 52 53 foreach(generator_proto IN LISTS generator_protos) 54 string(REGEX REPLACE "([^;]+)" "${PROJECT_SOURCE_DIR}/generator/proto/\\1.proto" generator_proto_file "${generator_proto}") 55 string(REGEX REPLACE "([^;]+)" "\\1_pb2.py" generator_proto_py_file "${generator_proto}") 56 add_custom_command( 57 OUTPUT ${generator_proto_py_file} 58 COMMAND ${nanopb_PROTOC_PATH} --python_out=${PROJECT_BINARY_DIR} -I${PROJECT_SOURCE_DIR}/generator/proto ${generator_proto_file} 59 DEPENDS ${generator_proto_file} 60 ) 61 add_custom_target("generate_${generator_proto_py_file}" ALL DEPENDS ${generator_proto_py_file}) 62 install( 63 FILES ${PROJECT_BINARY_DIR}/${generator_proto_py_file} 64 ${generator_proto_file} 65 DESTINATION ${PYTHON_INSTDIR}/proto/ 66 ) 67 endforeach() 68 69 install( FILES generator/proto/_utils.py 70 DESTINATION ${PYTHON_INSTDIR}/proto/ ) 71endif() 72 73if(WIN32) 74 install( 75 PROGRAMS 76 generator/nanopb_generator.py 77 generator/protoc-gen-nanopb.bat 78 DESTINATION ${CMAKE_INSTALL_BINDIR} 79 ) 80else() 81 install( 82 PROGRAMS 83 generator/nanopb_generator.py 84 generator/protoc-gen-nanopb 85 DESTINATION ${CMAKE_INSTALL_BINDIR} 86 ) 87endif() 88 89if(nanopb_BUILD_RUNTIME) 90 if(BUILD_SHARED_LIBS) 91 add_library(protobuf-nanopb SHARED 92 pb.h 93 pb_common.h 94 pb_common.c 95 pb_encode.h 96 pb_encode.c 97 pb_decode.h 98 pb_decode.c) 99 set_target_properties(protobuf-nanopb PROPERTIES 100 SOVERSION ${nanopb_SOVERSION}) 101 install(TARGETS protobuf-nanopb EXPORT nanopb-targets 102 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 103 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 104 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 105 target_include_directories(protobuf-nanopb INTERFACE 106 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 107 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> 108 ) 109 endif() 110 111 if(BUILD_STATIC_LIBS) 112 add_library(protobuf-nanopb-static STATIC 113 pb.h 114 pb_common.h 115 pb_common.c 116 pb_encode.h 117 pb_encode.c 118 pb_decode.h 119 pb_decode.c) 120 set_target_properties(protobuf-nanopb-static PROPERTIES 121 OUTPUT_NAME protobuf-nanopb) 122 install(TARGETS protobuf-nanopb-static EXPORT nanopb-targets 123 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) 124 target_include_directories(protobuf-nanopb-static INTERFACE 125 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> 126 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> 127 ) 128 endif() 129 130 configure_file(extra/nanopb-config-version.cmake.in 131 nanopb-config-version.cmake @ONLY) 132 133 install(EXPORT nanopb-targets 134 DESTINATION ${CMAKE_INSTALL_CMAKEDIR} 135 NAMESPACE nanopb::) 136 137 install(FILES extra/nanopb-config.cmake 138 ${CMAKE_CURRENT_BINARY_DIR}/nanopb-config-version.cmake 139 DESTINATION ${CMAKE_INSTALL_CMAKEDIR}) 140 141 install(FILES pb.h pb_common.h pb_encode.h pb_decode.h 142 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 143endif() 144