forked from shaka-project/shaka-packager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotobuf.cmake
86 lines (76 loc) · 3.13 KB
/
protobuf.cmake
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
86
# Copyright 2022 Google LLC. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
# Define a custom function to create protobuf libraries. This is similar to
# the one defined in the CMake FindProtobuf.cmake module, but allows us to more
# easily hook into the protobuf submodule to do the work instead of searching
# for a system-wide installation. Generates both C++ library targets and
# Python proto modules.
function(add_proto_library NAME)
cmake_parse_arguments(PARSE_ARGV
# How many arguments to skip (1 for the library name)
1
# Prefix for automatic variables created by the parser
ADD_PROTO_LIBRARY
# Options for the function, passed directly to add_library
"STATIC;SHARED;MODULE;EXCLUDE_FROM_ALL"
# One-value arguments
""
# Multi-value arguments
"")
if(${ADD_PROTO_LIBRARY_STATIC})
set(ADD_PROTO_LIBRARY_TYPE STATIC)
elseif(${ADD_PROTO_LIBRARY_SHARED})
set(ADD_PROTO_LIBRARY_TYPE SHARED)
elseif(${ADD_PROTO_LIBRARY_MODULE})
set(ADD_PROTO_LIBRARY_TYPE MODULE)
else()
set(ADD_PROTO_LIBRARY_TYPE)
endif()
if(${ADD_PROTO_LIBRARY_EXCLUDE_FROM_ALL})
set(ADD_PROTO_LIBRARY_EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
else()
set(ADD_PROTO_LIBRARY_EXCLUDE_FROM_ALL)
endif()
set(ADD_PROTO_LIBRARY_GENERATED_SOURCES)
foreach(_path ${ADD_PROTO_LIBRARY_UNPARSED_ARGUMENTS})
get_filename_component(_dir ${_path} DIRECTORY)
get_filename_component(_basename ${_path} NAME_WLE)
set(_generated_cpp "${CMAKE_CURRENT_BINARY_DIR}/${_dir}/${_basename}.pb.cc")
set(_generated_h "${CMAKE_CURRENT_BINARY_DIR}/${_dir}/${_basename}.pb.h")
list(APPEND ADD_PROTO_LIBRARY_GENERATED_SOURCES
${_generated_cpp} ${_generated_h})
add_custom_command(
OUTPUT ${_generated_cpp} ${_generated_h}
COMMAND protoc
ARGS
-I${CMAKE_CURRENT_SOURCE_DIR}/${_dir}
--cpp_out=${CMAKE_CURRENT_BINARY_DIR}/${_dir}
--python_out=${CMAKE_CURRENT_BINARY_DIR}/${_dir}
${CMAKE_CURRENT_SOURCE_DIR}/${_path}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_path} protoc
COMMENT "Running protocol buffer compiler on ${_path}"
VERBATIM)
endforeach()
add_library(${NAME}
${ADD_PROTO_LIBRARY_TYPE}
${ADD_PROTO_LIBRARY_EXCLUDE_FROM_ALL}
${ADD_PROTO_LIBRARY_GENERATED_SOURCES})
target_link_libraries(${NAME} libprotobuf)
# Anyone who depends on this proto library will need this include directory.
target_include_directories(${NAME} PUBLIC "${CMAKE_BINARY_DIR}")
# Anyone who depends on this proto library will also need to have these
# warnings suppressed from the generated headers.
if(MSVC)
# Integer truncation warnings
target_compile_options(${NAME} PUBLIC /wd4244 /wd4267)
# Unused parameter warnings
target_compile_options(${NAME} PUBLIC /wd4100)
else()
target_compile_options(${NAME} PUBLIC -Wno-shorten-64-to-32)
target_compile_options(${NAME} PUBLIC -Wno-unused-parameter)
target_compile_options(${NAME} PUBLIC -Wno-stringop-overflow)
endif()
endfunction()