make_cmakelists.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2009-2021, Google LLC
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Google LLC nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 """A tool to convert {WORKSPACE, BUILD} -> CMakeLists.txt.
29 
30 This tool is very upb-specific at the moment, and should not be seen as a
31 generic Bazel -> CMake converter.
32 """
33 
34 from __future__ import absolute_import
35 from __future__ import division
36 from __future__ import print_function
37 
38 import sys
39 import textwrap
40 import os
41 
42 def StripFirstChar(deps):
43  return [dep[1:] for dep in deps]
44 
45 def IsSourceFile(name):
46  return name.endswith(".c") or name.endswith(".cc")
47 
48 class BuildFileFunctions(object):
49  def __init__(self, converter):
50  self.converter = converter
51 
52  def _add_deps(self, kwargs, keyword=""):
53  if "deps" not in kwargs:
54  return
55  self.converter.toplevel += "target_link_libraries(%s%s\n %s)\n" % (
56  kwargs["name"],
57  keyword,
58  "\n ".join(StripFirstChar(kwargs["deps"]))
59  )
60 
61  def load(self, *args):
62  pass
63 
64  def cc_library(self, **kwargs):
65  if kwargs["name"].endswith("amalgamation"):
66  return
67  if kwargs["name"] == "upbc_generator":
68  return
69  if kwargs["name"] == "lupb":
70  return
71  files = kwargs.get("srcs", []) + kwargs.get("hdrs", [])
72  found_files = []
73  pregenerated_files = [
74  "CMakeLists.txt", "descriptor.upb.h", "descriptor.upb.c"
75  ]
76  for file in files:
77  if os.path.basename(file) in pregenerated_files:
78  found_files.append("../cmake/" + file)
79  else:
80  found_files.append("../" + file)
81 
82  if list(filter(IsSourceFile, files)):
83  # Has sources, make this a normal library.
84  self.converter.toplevel += "add_library(%s\n %s)\n" % (
85  kwargs["name"],
86  "\n ".join(found_files)
87  )
88  self._add_deps(kwargs)
89  else:
90  # Header-only library, have to do a couple things differently.
91  # For some info, see:
92  # http://mariobadr.com/creating-a-header-only-library-with-cmake.html
93  self.converter.toplevel += "add_library(%s INTERFACE)\n" % (
94  kwargs["name"]
95  )
96  self._add_deps(kwargs, " INTERFACE")
97 
98  def cc_binary(self, **kwargs):
99  pass
100 
101  def cc_test(self, **kwargs):
102  # Disable this until we properly support upb_proto_library().
103  # self.converter.toplevel += "add_executable(%s\n %s)\n" % (
104  # kwargs["name"],
105  # "\n ".join(kwargs["srcs"])
106  # )
107  # self.converter.toplevel += "add_test(NAME %s COMMAND %s)\n" % (
108  # kwargs["name"],
109  # kwargs["name"],
110  # )
111 
112  # if "data" in kwargs:
113  # for data_dep in kwargs["data"]:
114  # self.converter.toplevel += textwrap.dedent("""\
115  # add_custom_command(
116  # TARGET %s POST_BUILD
117  # COMMAND ${CMAKE_COMMAND} -E copy
118  # ${CMAKE_SOURCE_DIR}/%s
119  # ${CMAKE_CURRENT_BINARY_DIR}/%s)\n""" % (
120  # kwargs["name"], data_dep, data_dep
121  # ))
122 
123  # self._add_deps(kwargs)
124  pass
125 
126  def cc_fuzz_test(self, **kwargs):
127  pass
128 
129  def py_library(self, **kwargs):
130  pass
131 
132  def py_binary(self, **kwargs):
133  pass
134 
135  def lua_proto_library(self, **kwargs):
136  pass
137 
138  def sh_test(self, **kwargs):
139  pass
140 
141  def make_shell_script(self, **kwargs):
142  pass
143 
144  def exports_files(self, files, **kwargs):
145  pass
146 
147  def proto_library(self, **kwargs):
148  pass
149 
150  def cc_proto_library(self, **kwargs):
151  pass
152 
153  def generated_file_staleness_test(self, **kwargs):
154  pass
155 
156  def upb_amalgamation(self, **kwargs):
157  pass
158 
159  def upb_proto_library(self, **kwargs):
160  pass
161 
162  def upb_proto_library_copts(self, **kwargs):
163  pass
164 
165  def upb_proto_reflection_library(self, **kwargs):
166  pass
167 
168  def upb_proto_srcs(self, **kwargs):
169  pass
170 
171  def genrule(self, **kwargs):
172  pass
173 
174  def config_setting(self, **kwargs):
175  pass
176 
177  def upb_fasttable_enabled(self, **kwargs):
178  pass
179 
180  def select(self, arg_dict):
181  return []
182 
183  def glob(self, *args):
184  return []
185 
186  def licenses(self, *args):
187  pass
188 
189  def filegroup(self, **kwargs):
190  pass
191 
192  def map_dep(self, arg):
193  return arg
194 
195 
197  def __init__(self, converter):
198  self.converter = converter
199 
200  def load(self, *args):
201  pass
202 
203  def workspace(self, **kwargs):
204  self.converter.prelude += "project(%s)\n" % (kwargs["name"])
205  self.converter.prelude += "set(CMAKE_C_STANDARD 99)\n"
206 
207  def http_archive(self, **kwargs):
208  pass
209 
210  def git_repository(self, **kwargs):
211  pass
212 
213  def new_git_repository(self, **kwargs):
214  pass
215 
216  def bazel_version_repository(self, **kwargs):
217  pass
218 
219  def upb_deps(self):
220  pass
221 
222  def protobuf_deps(self):
223  pass
224 
226  pass
227 
229  pass
230 
231  def system_python(self, **kwargs):
232  pass
233 
234  def register_toolchains(self, toolchain):
235  pass
236 
237 
238 class Converter(object):
239  def __init__(self):
240  self.prelude = ""
241  self.toplevel = ""
242  self.if_lua = ""
243 
244  def convert(self):
245  return self.template % {
246  "prelude": converter.prelude,
247  "toplevel": converter.toplevel,
248  }
249 
250  template = textwrap.dedent("""\
251  # This file was generated from BUILD using tools/make_cmakelists.py.
252 
253  cmake_minimum_required(VERSION 3.1)
254 
255  if(${CMAKE_VERSION} VERSION_LESS 3.12)
256  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
257  else()
258  cmake_policy(VERSION 3.12)
259  endif()
260 
261  cmake_minimum_required (VERSION 3.0)
262  cmake_policy(SET CMP0048 NEW)
263 
264  %(prelude)s
265 
266  # Prevent CMake from setting -rdynamic on Linux (!!).
267  SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
268  SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
269 
270  # Set default build type.
271  if(NOT CMAKE_BUILD_TYPE)
272  message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
273  set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
274  "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
275  FORCE)
276  endif()
277 
278  # When using Ninja, compiler output won't be colorized without this.
279  include(CheckCXXCompilerFlag)
280  CHECK_CXX_COMPILER_FLAG(-fdiagnostics-color=always SUPPORTS_COLOR_ALWAYS)
281  if(SUPPORTS_COLOR_ALWAYS)
282  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
283  endif()
284 
285  # Implement ASAN/UBSAN options
286  if(UPB_ENABLE_ASAN)
287  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
288  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
289  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
290  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
291  endif()
292 
293  if(UPB_ENABLE_UBSAN)
294  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
295  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
296  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
297  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
298  endif()
299 
300  include_directories(..)
301  include_directories(../cmake)
302  include_directories(${CMAKE_CURRENT_BINARY_DIR})
303 
304  if(APPLE)
305  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup -flat_namespace")
306  elseif(UNIX)
307  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id")
308  endif()
309 
310  enable_testing()
311 
312  %(toplevel)s
313 
314  """)
315 
316 data = {}
317 converter = Converter()
318 
319 def GetDict(obj):
320  ret = {}
321  ret["UPB_DEFAULT_COPTS"] = [] # HACK
322  ret["UPB_DEFAULT_CPPOPTS"] = [] # HACK
323  for k in dir(obj):
324  if not k.startswith("_"):
325  ret[k] = getattr(obj, k);
326  return ret
327 
328 globs = GetDict(converter)
329 
330 exec(open("WORKSPACE").read(), GetDict(WorkspaceFileFunctions(converter)))
331 exec(open("BUILD").read(), GetDict(BuildFileFunctions(converter)))
332 
333 with open(sys.argv[1], "w") as f:
334  f.write(converter.convert())
make_cmakelists.BuildFileFunctions.upb_proto_reflection_library
def upb_proto_reflection_library(self, **kwargs)
Definition: make_cmakelists.py:165
make_cmakelists.WorkspaceFileFunctions.__init__
def __init__(self, converter)
Definition: make_cmakelists.py:197
make_cmakelists.BuildFileFunctions.sh_test
def sh_test(self, **kwargs)
Definition: make_cmakelists.py:138
make_cmakelists.Converter.__init__
def __init__(self)
Definition: make_cmakelists.py:239
make_cmakelists.BuildFileFunctions.glob
def glob(self, *args)
Definition: make_cmakelists.py:183
make_cmakelists.BuildFileFunctions.__init__
def __init__(self, converter)
Definition: make_cmakelists.py:49
make_cmakelists.BuildFileFunctions.generated_file_staleness_test
def generated_file_staleness_test(self, **kwargs)
Definition: make_cmakelists.py:153
make_cmakelists.BuildFileFunctions.make_shell_script
def make_shell_script(self, **kwargs)
Definition: make_cmakelists.py:141
make_cmakelists.BuildFileFunctions.proto_library
def proto_library(self, **kwargs)
Definition: make_cmakelists.py:147
make_cmakelists.Converter.prelude
prelude
Definition: make_cmakelists.py:240
make_cmakelists.Converter.template
template
Definition: make_cmakelists.py:250
make_cmakelists.BuildFileFunctions.upb_proto_library_copts
def upb_proto_library_copts(self, **kwargs)
Definition: make_cmakelists.py:162
make_cmakelists.WorkspaceFileFunctions.upb_deps
def upb_deps(self)
Definition: make_cmakelists.py:219
make_cmakelists.BuildFileFunctions.upb_fasttable_enabled
def upb_fasttable_enabled(self, **kwargs)
Definition: make_cmakelists.py:177
make_cmakelists.Converter.convert
def convert(self)
Definition: make_cmakelists.py:244
make_cmakelists.WorkspaceFileFunctions.workspace
def workspace(self, **kwargs)
Definition: make_cmakelists.py:203
make_cmakelists.BuildFileFunctions.config_setting
def config_setting(self, **kwargs)
Definition: make_cmakelists.py:174
make_cmakelists.GetDict
def GetDict(obj)
Definition: make_cmakelists.py:319
make_cmakelists.BuildFileFunctions.cc_proto_library
def cc_proto_library(self, **kwargs)
Definition: make_cmakelists.py:150
make_cmakelists.BuildFileFunctions.cc_binary
def cc_binary(self, **kwargs)
Definition: make_cmakelists.py:98
make_cmakelists.BuildFileFunctions.cc_library
def cc_library(self, **kwargs)
Definition: make_cmakelists.py:64
make_cmakelists.WorkspaceFileFunctions.http_archive
def http_archive(self, **kwargs)
Definition: make_cmakelists.py:207
make_cmakelists.BuildFileFunctions._add_deps
def _add_deps(self, kwargs, keyword="")
Definition: make_cmakelists.py:52
make_cmakelists.BuildFileFunctions.genrule
def genrule(self, **kwargs)
Definition: make_cmakelists.py:171
make_cmakelists.WorkspaceFileFunctions.rules_fuzzing_init
def rules_fuzzing_init(self)
Definition: make_cmakelists.py:228
make_cmakelists.Converter.if_lua
if_lua
Definition: make_cmakelists.py:242
make_cmakelists.BuildFileFunctions.upb_amalgamation
def upb_amalgamation(self, **kwargs)
Definition: make_cmakelists.py:156
make_cmakelists.BuildFileFunctions.cc_test
def cc_test(self, **kwargs)
Definition: make_cmakelists.py:101
make_cmakelists.WorkspaceFileFunctions.register_toolchains
def register_toolchains(self, toolchain)
Definition: make_cmakelists.py:234
make_cmakelists.WorkspaceFileFunctions.rules_fuzzing_dependencies
def rules_fuzzing_dependencies(self)
Definition: make_cmakelists.py:225
make_cmakelists.WorkspaceFileFunctions.protobuf_deps
def protobuf_deps(self)
Definition: make_cmakelists.py:222
make_cmakelists.IsSourceFile
def IsSourceFile(name)
Definition: make_cmakelists.py:45
make_cmakelists.WorkspaceFileFunctions.system_python
def system_python(self, **kwargs)
Definition: make_cmakelists.py:231
make_cmakelists.BuildFileFunctions.py_binary
def py_binary(self, **kwargs)
Definition: make_cmakelists.py:132
make_cmakelists.BuildFileFunctions.cc_fuzz_test
def cc_fuzz_test(self, **kwargs)
Definition: make_cmakelists.py:126
make_cmakelists.Converter.toplevel
toplevel
Definition: make_cmakelists.py:241
make_cmakelists.BuildFileFunctions
Definition: make_cmakelists.py:48
make_cmakelists.BuildFileFunctions.select
def select(self, arg_dict)
Definition: make_cmakelists.py:180
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
make_cmakelists.Converter
Definition: make_cmakelists.py:238
make_cmakelists.BuildFileFunctions.load
def load(self, *args)
Definition: make_cmakelists.py:61
make_cmakelists.WorkspaceFileFunctions
Definition: make_cmakelists.py:196
make_cmakelists.BuildFileFunctions.licenses
def licenses(self, *args)
Definition: make_cmakelists.py:186
make_cmakelists.StripFirstChar
def StripFirstChar(deps)
Definition: make_cmakelists.py:42
make_cmakelists.BuildFileFunctions.exports_files
def exports_files(self, files, **kwargs)
Definition: make_cmakelists.py:144
make_cmakelists.WorkspaceFileFunctions.load
def load(self, *args)
Definition: make_cmakelists.py:200
make_cmakelists.BuildFileFunctions.lua_proto_library
def lua_proto_library(self, **kwargs)
Definition: make_cmakelists.py:135
make_cmakelists.BuildFileFunctions.upb_proto_library
def upb_proto_library(self, **kwargs)
Definition: make_cmakelists.py:159
make_cmakelists.WorkspaceFileFunctions.new_git_repository
def new_git_repository(self, **kwargs)
Definition: make_cmakelists.py:213
make_cmakelists.BuildFileFunctions.filegroup
def filegroup(self, **kwargs)
Definition: make_cmakelists.py:189
open
#define open
Definition: test-fs.c:46
make_cmakelists.BuildFileFunctions.map_dep
def map_dep(self, arg)
Definition: make_cmakelists.py:192
make_cmakelists.BuildFileFunctions.py_library
def py_library(self, **kwargs)
Definition: make_cmakelists.py:129
make_cmakelists.WorkspaceFileFunctions.converter
converter
Definition: make_cmakelists.py:198
make_cmakelists.WorkspaceFileFunctions.bazel_version_repository
def bazel_version_repository(self, **kwargs)
Definition: make_cmakelists.py:216
make_cmakelists.BuildFileFunctions.upb_proto_srcs
def upb_proto_srcs(self, **kwargs)
Definition: make_cmakelists.py:168
make_cmakelists.WorkspaceFileFunctions.git_repository
def git_repository(self, **kwargs)
Definition: make_cmakelists.py:210
make_cmakelists.BuildFileFunctions.converter
converter
Definition: make_cmakelists.py:50


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:30