-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathCreateBazelConfig.cmake
154 lines (146 loc) · 5.63 KB
/
CreateBazelConfig.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# ~~~
# Copyright 2018 Google LLC
#
# 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.
# ~~~
function (google_cloud_cpp_bazel_copyright VAR YEAR)
set(text "# Copyright ${YEAR} Google LLC")
string(
CONCAT
text
"${text}"
[=[
#
# 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.
#
]=])
string(APPEND text "# DO NOT " "EDIT")
string(
APPEND
text
[=[ -- GENERATED BY CMake -- Change the CMakeLists.txt file if needed
]=])
set(${VAR}
"${text}"
PARENT_SCOPE)
endfunction ()
# Generate a Bazel configuration file with the headers and sources for a given
# target. The generated file can be loaded from a BUILD file to create the
# corresponding targets in Bazel.
function (create_bazel_config TARGET)
cmake_parse_arguments(_CREATE_BAZEL_CONFIG_OPT "" "YEAR" "" ${ARGN})
if ("${_CREATE_BAZEL_CONFIG_OPT_YEAR}" STREQUAL "")
set(_CREATE_BAZEL_CONFIG_OPT_YEAR "2018")
endif ()
if (NOT TARGET ${TARGET})
message(
FATAL_ERROR "create_bazel_config requires a target name: ${TARGET}")
endif ()
set(filename "${TARGET}.bzl")
set(H)
set(CC)
get_target_property(target_type ${TARGET} TYPE)
if (${target_type} STREQUAL "INTERFACE_LIBRARY")
get_target_property(sources ${TARGET} INTERFACE_SOURCES)
else ()
get_target_property(sources ${TARGET} SOURCES)
endif ()
foreach (src ${sources})
# Some files need to be specified with an absolute path (mainly sources
# for INTERFACE libraries). Compute the relative path because Bazel does
# not like absolute filenames.
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" relative "${src}")
string(FIND "${src}" "${CMAKE_CURRENT_BINARY_DIR}" in_binary_dir)
if ("${in_binary_dir}" EQUAL 0)
# Skip files in the binary directory, they are generated and handled
# differently by our Bazel BUILD files.
elseif ("${src}" MATCHES "\\.inc$")
list(APPEND H ${relative})
elseif ("${src}" MATCHES "\\.h$")
list(APPEND H ${relative})
elseif ("${src}" MATCHES "\\.cc$")
list(APPEND CC ${relative})
endif ()
endforeach ()
google_cloud_cpp_bazel_copyright(text "${_CREATE_BAZEL_CONFIG_OPT_YEAR}")
file(WRITE "${filename}" "${text}")
file(APPEND "${filename}" [=[
"""Automatically generated source lists for ]=])
file(APPEND "${filename}" ${TARGET} " - DO NOT " "EDIT")
file(
APPEND "${filename}"
[=[."""
]=])
file(APPEND "${filename}" "${TARGET}_hdrs = [\n")
foreach (src ${H})
file(APPEND "${filename}" " \"${src}\",\n")
endforeach ()
file(APPEND "${filename}" "]\n\n")
file(APPEND "${filename}" "${TARGET}_srcs = [\n")
foreach (src ${CC})
file(APPEND "${filename}" " \"${src}\",\n")
endforeach ()
file(APPEND "${filename}" "]\n")
endfunction ()
# Export a list to a .bzl file, mostly used to export names of unit tests.
function (export_list_to_bazel filename)
cmake_parse_arguments(_EXPORT_LIST_TO_BAZEL_OPT "" "YEAR" "" ${ARGN})
if ("${_EXPORT_LIST_TO_BAZEL_OPT_YEAR}" STREQUAL "")
set(_EXPORT_LIST_TO_BAZEL_OPT_YEAR "2018")
endif ()
google_cloud_cpp_bazel_copyright(text "${_EXPORT_LIST_TO_BAZEL_OPT_YEAR}")
file(WRITE "${filename}" "${text}")
file(APPEND "${filename}" [=[
"""Automatically generated unit tests list]=])
file(APPEND "${filename}" " - DO NOT " "EDIT")
file(APPEND "${filename}" [=[."""
]=])
foreach (VAR ${_EXPORT_LIST_TO_BAZEL_OPT_UNPARSED_ARGUMENTS})
file(APPEND "${filename}" "\n${VAR} = [\n")
foreach (item ${${VAR}})
file(APPEND "${filename}" " \"${item}\",\n")
endforeach ()
file(APPEND "${filename}" "]\n")
endforeach ()
endfunction ()
# Export a number of variables to a .bzl file, mostly used to export version
# information.
function (export_variables_to_bazel filename)
cmake_parse_arguments(_EXPORT_VARIABLES_TO_BAZEL_OPT "" "YEAR" "" ${ARGN})
if ("${_EXPORT_VARIABLES_TO_BAZEL_OPT_YEAR}" STREQUAL "")
set(_EXPORT_VARIABLES_TO_BAZEL_OPT_YEAR "2019")
endif ()
google_cloud_cpp_bazel_copyright(text
"${_EXPORT_VARIABLES_TO_BAZEL_OPT_YEAR}")
file(WRITE "${filename}" "${text}")
file(APPEND "${filename}" [=[
"""Automatically generated version numbers]=])
file(APPEND "${filename}" " - DO NOT " "EDIT")
file(
APPEND "${filename}"
[=[."""
]=])
foreach (item ${_EXPORT_VARIABLES_TO_BAZEL_OPT_UNPARSED_ARGUMENTS})
file(APPEND "${filename}" "${item} = \"${${item}}\"\n")
endforeach ()
endfunction ()