aboutsummaryrefslogtreecommitdiff
path: root/third_party/nanopb/CMakeLists.txt
blob: b3868f80040b36eda5b26e4ecb2ce2687db22b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

include($ENV{PW_ROOT}/pw_build/pigweed.cmake)
include($ENV{PW_ROOT}/pw_protobuf_compiler/proto.cmake)

set(dir_pw_third_party_nanopb "" CACHE PATH "Path to the Nanopb installation.")
option(pw_third_party_nanopb_ADD_SUBDIRECTORY
    "Whether to add the dir_pw_third_party_nanopb subdirectory" OFF)

if("${dir_pw_third_party_nanopb}" STREQUAL "")
  pw_add_error_target(pw_third_party.nanopb
    MESSAGE
      "Attempted to use nanopb without configuring it, see "
      "pigweed.dev/third_party/nanopb/"
  )
  return()
elseif(pw_third_party_nanopb_ADD_SUBDIRECTORY)
  add_subdirectory("${dir_pw_third_party_nanopb}" third_party/nanopb)
endif()

set(nanopb_main_library
    $<IF:$<TARGET_EXISTS:protobuf-nanopb-static>,protobuf-nanopb-static,protobuf-nanopb>)

add_library(pw_third_party.nanopb INTERFACE)
target_link_libraries(pw_third_party.nanopb INTERFACE "${nanopb_main_library}")
target_include_directories(pw_third_party.nanopb
  INTERFACE
    "${dir_pw_third_party_nanopb}"
)

pw_proto_library(pw_third_party.nanopb.proto
  SOURCES
    "${dir_pw_third_party_nanopb}/generator/proto/nanopb.proto"
  STRIP_PREFIX
    "${dir_pw_third_party_nanopb}/generator/proto"
)

# Generates nanopb_pb2.py, which is needed to compile protobufs with Nanopb.
add_custom_command(
  COMMAND
    python3
    "${CMAKE_CURRENT_LIST_DIR}/generate_nanopb_proto.py"
    --nanopb-root "${dir_pw_third_party_nanopb}"
    --protoc-binary "$ENV{PW_PIGWEED_CIPD_INSTALL_DIR}/bin/protoc"
  DEPENDS
    "${CMAKE_CURRENT_LIST_DIR}/generate_nanopb_proto.py"
    "${dir_pw_third_party_nanopb}/pb.h"
    "${dir_pw_third_party_nanopb}/pb_common.h"
    "${dir_pw_third_party_nanopb}/pb_decode.h"
    "${dir_pw_third_party_nanopb}/pb_encode.h"
    "${dir_pw_third_party_nanopb}/pb_common.c"
    "${dir_pw_third_party_nanopb}/pb_decode.c"
    "${dir_pw_third_party_nanopb}/pb_encode.c"
    "${dir_pw_third_party_nanopb}/generator/nanopb_generator.py"
    "${dir_pw_third_party_nanopb}/generator/proto/google/protobuf"
    "${dir_pw_third_party_nanopb}/generator/proto/google/protobuf/descriptor.proto"
    "${dir_pw_third_party_nanopb}/generator/proto/__init__.py"
    "${dir_pw_third_party_nanopb}/generator/proto/nanopb.proto"
    "${dir_pw_third_party_nanopb}/generator/proto/_utils.py"
    "${dir_pw_third_party_nanopb}/generator/protoc-gen-nanopb"
    "${dir_pw_third_party_nanopb}/generator/nanopb_generator.py2"
    "${dir_pw_third_party_nanopb}/generator/protoc-gen-nanopb-py2"
    "${dir_pw_third_party_nanopb}/generator/protoc"
    "${dir_pw_third_party_nanopb}/generator/protoc-gen-nanopb.bat"
    "${dir_pw_third_party_nanopb}/generator/protoc.bat"
    "$ENV{PW_PIGWEED_CIPD_INSTALL_DIR}/bin/protoc"
  OUTPUT
    "${dir_pw_third_party_nanopb}/generator/proto/nanopb_pb2.py"
)
add_custom_target(pw_third_party.nanopb.generate_proto
  DEPENDS
    "${dir_pw_third_party_nanopb}/generator/proto/nanopb_pb2.py"
)