Go to the documentation of this file.
28 """A tool to convert {WORKSPACE, BUILD} -> CMakeLists.txt.
30 This tool is very upb-specific at the moment, and should not be seen as a
31 generic Bazel -> CMake converter.
34 from __future__
import absolute_import
35 from __future__
import division
36 from __future__
import print_function
43 return [dep[1:]
for dep
in deps]
46 return name.endswith(
".c")
or name.endswith(
".cc")
53 if "deps" not in kwargs:
55 self.
converter.toplevel +=
"target_link_libraries(%s%s\n %s)\n" % (
65 if kwargs[
"name"].endswith(
"amalgamation"):
67 if kwargs[
"name"] ==
"upbc_generator":
69 if kwargs[
"name"] ==
"lupb":
71 files = kwargs.get(
"srcs", []) + kwargs.get(
"hdrs", [])
73 pregenerated_files = [
74 "CMakeLists.txt",
"descriptor.upb.h",
"descriptor.upb.c"
77 if os.path.basename(file)
in pregenerated_files:
78 found_files.append(
"../cmake/" + file)
80 found_files.append(
"../" + file)
82 if list(filter(IsSourceFile, files)):
84 self.converter.toplevel +=
"add_library(%s\n %s)\n" % (
86 "\n ".join(found_files)
88 self._add_deps(kwargs)
93 self.converter.toplevel +=
"add_library(%s INTERFACE)\n" % (
96 self._add_deps(kwargs,
" INTERFACE")
204 self.converter.prelude +=
"project(%s)\n" % (kwargs[
"name"])
205 self.converter.prelude +=
"set(CMAKE_C_STANDARD 99)\n"
238 class Converter(object):
246 "prelude": converter.prelude,
247 "toplevel": converter.toplevel,
250 template = textwrap.dedent(
"""\
251 # This file was generated from BUILD using tools/make_cmakelists.py.
253 cmake_minimum_required(VERSION 3.1)
255 if(${CMAKE_VERSION} VERSION_LESS 3.12)
256 cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
258 cmake_policy(VERSION 3.12)
261 cmake_minimum_required (VERSION 3.0)
262 cmake_policy(SET CMP0048 NEW)
266 # Prevent CMake from setting -rdynamic on Linux (!!).
267 SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
268 SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
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."
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")
285 # Implement ASAN/UBSAN options
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")
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")
300 include_directories(..)
301 include_directories(../cmake)
302 include_directories(${CMAKE_CURRENT_BINARY_DIR})
305 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup -flat_namespace")
307 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id")
321 ret[
"UPB_DEFAULT_COPTS"] = []
322 ret[
"UPB_DEFAULT_CPPOPTS"] = []
324 if not k.startswith(
"_"):
325 ret[k] = getattr(obj, k);
333 with open(sys.argv[1],
"w")
as f:
334 f.write(converter.convert())
def upb_proto_reflection_library(self, **kwargs)
def __init__(self, converter)
def sh_test(self, **kwargs)
def __init__(self, converter)
def generated_file_staleness_test(self, **kwargs)
def make_shell_script(self, **kwargs)
def proto_library(self, **kwargs)
def upb_proto_library_copts(self, **kwargs)
def upb_fasttable_enabled(self, **kwargs)
def workspace(self, **kwargs)
def config_setting(self, **kwargs)
def cc_proto_library(self, **kwargs)
def cc_binary(self, **kwargs)
def cc_library(self, **kwargs)
def http_archive(self, **kwargs)
def _add_deps(self, kwargs, keyword="")
def genrule(self, **kwargs)
def rules_fuzzing_init(self)
def upb_amalgamation(self, **kwargs)
def cc_test(self, **kwargs)
def register_toolchains(self, toolchain)
def rules_fuzzing_dependencies(self)
def system_python(self, **kwargs)
def py_binary(self, **kwargs)
def cc_fuzz_test(self, **kwargs)
def select(self, arg_dict)
int read(izstream &zs, T *x, Items items)
def licenses(self, *args)
def exports_files(self, files, **kwargs)
def lua_proto_library(self, **kwargs)
def upb_proto_library(self, **kwargs)
def new_git_repository(self, **kwargs)
def filegroup(self, **kwargs)
def py_library(self, **kwargs)
def bazel_version_repository(self, **kwargs)
def upb_proto_srcs(self, **kwargs)
def git_repository(self, **kwargs)
grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:30