forked from google/bazel-to-cmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bazel_to_cmake.py
executable file
·417 lines (316 loc) · 12.2 KB
/
bazel_to_cmake.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
# Copyright 2019 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.
"""A script to convert Bazel build systems to CMakeLists.txt.
See README.md for more information.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import textwrap
import ast
import os
from glob import glob
from pathlib import Path
def StripColons(deps):
return [item.replace(":", "") for item in deps]
def GetTargetName(names):
ret_val = []
for item in names:
if ":" in item:
ret_val.append(item[item.rfind(":")+1:])
elif "/" in item:
ret_val.append(item[item.rfind("/")+1:])
else:
ret_val.append(item)
return ret_val
def IsSourceFile(name):
endings = [".c", ".cc", ".cpp"]
return any(n in name for n in endings)
class BuildFileFunctions(object):
def __init__(self, converter):
self.converter = converter
self.globs = {} # needed to expand global vars on the fly, i.e. through load()
def _append_toplevel(self, string):
self.converter.append_current_toplevel(string)
def _add_deps(self, kwargs, keyword=""):
if "deps" not in kwargs:
return
string = "target_link_libraries(%s%s\n %s)\n" % (
kwargs["name"],
keyword,
# "\n ".join(StripColons(kwargs["deps"]))
"\n ".join(GetTargetName(kwargs["deps"]))
)
self._append_toplevel(string)
def _target_include_directories(self, **kwargs):
copts = kwargs.get("copts", [])
for opt in copts:
if opt.startswith("-I"):
string = "target_include_directories(%s PUBLIC %s)\n" % (
kwargs["name"],
opt[2:],
)
self._append_toplevel(string)
def load(self, file, *args):
file = file.replace("//:", "./")
with open(file, 'r') as f:
prev_dir = dir()
exec(f.read())
delta_dir = [item for item in dir() if item not in prev_dir]
delta_dir.remove("prev_dir")
for var in delta_dir:
self.globs[var] = eval(var)
def cc_library(self, **kwargs):
if kwargs["name"] == "amalgamation" or kwargs["name"] == "upbc_generator":
return
files = kwargs.get("srcs", []) + kwargs.get("hdrs", [])
if True or filter(IsSourceFile, files):
# Has sources, make this a normal library.
string = "add_library(%s\n %s)\n" % (
kwargs["name"],
"\n ".join(files)
)
self._append_toplevel(string)
self._add_deps(kwargs)
self._target_include_directories(**kwargs)
string = "SET_TARGET_PROPERTIES(%s PROPERTIES LINKER_LANGUAGE CXX)\n" % kwargs[
"name"]
self._append_toplevel(string)
else:
# Header-only library, have to do a couple things differently.
# For some info, see:
# http://mariobadr.com/creating-a-header-only-library-with-cmake.html
string = "add_library(%s INTERFACE)\n" % (
kwargs["name"]
)
self._append_toplevel(string)
# TODO: if it is a relative path, its not allowed with target_sources. Maybe with include_...?
string = "target_sources(%s INTERFACE\n %s)\n" % (
kwargs["name"],
# "\n ".join(StripColons(files))
"\n ".join(files)
)
self._append_toplevel(string)
self._add_deps(kwargs, " INTERFACE")
def cc_binary(self, **kwargs):
files = kwargs.get("srcs", []) + kwargs.get("hdrs", [])
string = "add_executable(%s\n %s)\n" % (
kwargs["name"],
"\n ".join(files)
)
self._append_toplevel(string)
self._add_deps(kwargs)
self._target_include_directories(**kwargs)
def cc_test(self, **kwargs):
# Disable this until we properly support upb_proto_library().
string = "add_executable(%s\n %s)\n" % (
kwargs["name"],
"\n ".join(kwargs["srcs"])
)
self._append_toplevel(string)
string = "add_test(NAME %s COMMAND %s)\n" % (
kwargs["name"],
kwargs["name"],
)
self._append_toplevel(string)
if "data" in kwargs:
for data_dep in kwargs["data"]:
string = textwrap.dedent("""\
add_custom_command(
TARGET %s POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/%s
${CMAKE_CURRENT_BINARY_DIR}/%s)\n""" % (
kwargs["name"], data_dep, data_dep
))
self._append_toplevel(string)
self._add_deps(kwargs)
self._target_include_directories(**kwargs)
def py_library(self, **kwargs):
pass
def py_binary(self, **kwargs):
pass
def lua_cclibrary(self, **kwargs):
pass
def lua_library(self, **kwargs):
pass
def lua_binary(self, **kwargs):
pass
def lua_test(self, **kwargs):
pass
def sh_test(self, **kwargs):
pass
def make_shell_script(self, **kwargs):
pass
def exports_files(self, files, **kwargs):
pass
def proto_library(self, **kwargs):
pass
def generated_file_staleness_test(self, **kwargs):
pass
def upb_amalgamation(self, **kwargs):
pass
def upb_proto_library(self, **kwargs):
pass
def upb_proto_reflection_library(self, **kwargs):
pass
def genrule(self, **kwargs):
pass
def config_setting(self, **kwargs):
pass
def select(self, arg_dict):
return []
def glob(self, *args):
return []
def licenses(self, *args):
pass
def map_dep(self, arg):
return arg
def package(self, **kwargs):
pass
class WorkspaceFileFunctions(object):
def __init__(self, converter):
self.converter = converter
def load(self, file, *args):
pass
def workspace(self, **kwargs):
self.converter.prelude += "project(%s C CXX)\n" % (kwargs["name"])
def http_archive(self, **kwargs):
pass
def git_repository(self, **kwargs):
pass
def new_local_repository(self, **kwargs):
pass
class Converter(object):
def __init__(self):
self.prelude = ""
self.toplevel = {}
self.include_subdirs = {}
self.if_lua = ""
self.current_build_file = ""
def append_current_toplevel(self, string):
if self.current_build_file not in self.toplevel:
self.toplevel[self.current_build_file] = ""
self.toplevel[self.current_build_file] = self.toplevel[self.current_build_file] + string
def append_current_subdir(self, file):
if self.current_build_file not in self.include_subdirs:
self.include_subdirs[self.current_build_file] = []
self.include_subdirs[self.current_build_file].append(file)
def convert_prelude(self):
return self.prelude_template % {"prelude": self.prelude}
def convert_toplevel(self):
include_dirs = ""
if self.current_build_file in self.include_subdirs:
for directory in self.include_subdirs[self.current_build_file]:
include_dirs += "add_subdirectory(%s)\n" % directory.replace(
".", "").replace("/", "")
return "%s\n%s" % (include_dirs, self.toplevel[self.current_build_file])
prelude_template = textwrap.dedent("""\
# This file was generated from BUILD using tools/make_cmakelists.py.
cmake_minimum_required(VERSION 3.1)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.12)
endif()
cmake_minimum_required (VERSION 3.0)
cmake_policy(SET CMP0048 NEW)
%(prelude)s
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
# Prevent CMake from setting -rdynamic on Linux (!!).
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
# Set default build type.
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
"Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
FORCE)
endif()
# When using Ninja, compiler output won't be colorized without this.
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(-fdiagnostics-color=always SUPPORTS_COLOR_ALWAYS)
if(SUPPORTS_COLOR_ALWAYS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
endif()
# Implement ASAN/UBSAN options
if(UPB_ENABLE_ASAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()
if(UPB_ENABLE_UBSAN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
endif()
#include_directories(.)
#include_directories(${CMAKE_CURRENT_BINARY_DIR})
if(APPLE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined dynamic_lookup -flat_namespace")
elseif(UNIX)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id")
endif()
enable_testing()
""")
def convert(self):
return self.convert_prelude() + self.convert_toplevel()
data = {}
converter = Converter()
def GetDict(obj):
ret = {}
for k in dir(obj):
# if k == "variables":
# for v in obj.variables:
# ret[v] = getattr(obj.variables, v)
if not k.startswith("_"):
ret[k] = getattr(obj, k)
return ret
def ParseBuildFile(path, converter):
cmake_created = False
for directory in glob(path+"*/"):
if not os.path.islink(Path(directory)):
cmake_created_in_dir = ParseBuildFile(directory, converter)
if cmake_created_in_dir:
cmake_created = True
converter.current_build_file = os.path.join(path, "BUILD")
converter.append_current_subdir(directory)
if "BUILD" in os.listdir(path):
file = os.path.join(path, "BUILD")
converter.current_build_file = file
code_block = compile(open(file).read(), file, 'exec')
build = BuildFileFunctions(converter)
code_globals = GetDict(build)
build.globs = code_globals
exec(code_block, code_globals)
return True
return cmake_created
globs = GetDict(converter)
exec(open("WORKSPACE").read(), GetDict(WorkspaceFileFunctions(converter)))
ParseBuildFile("./", converter)
to_create_files = set(converter.include_subdirs.keys())
to_create_files.update(set(converter.toplevel.keys()))
for affected_subdirs in to_create_files:
with open(affected_subdirs.replace("BUILD", sys.argv[1]), "w") as f:
converter.current_build_file = affected_subdirs
if "./BUILD" == affected_subdirs:
f.write(converter.convert())
else:
f.write(converter.convert_toplevel())
# with open(sys.argv[1], "w") as f:
# f.write(converter.convert())