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