2019-04-18 19:51:19 +02:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
2017-04-05 01:09:31 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
2019-10-23 22:51:03 +02:00
|
|
|
try:
|
|
|
|
from builtins import str
|
|
|
|
except ImportError:
|
|
|
|
from __builtin__ import str
|
2017-04-05 01:09:31 +02:00
|
|
|
from targets_builder import TARGETSBuilder
|
2019-08-02 19:40:32 +02:00
|
|
|
import json
|
2017-04-05 01:09:31 +02:00
|
|
|
import os
|
|
|
|
import fnmatch
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from util import ColorString
|
|
|
|
|
2019-08-02 19:40:32 +02:00
|
|
|
# This script generates TARGETS file for Buck.
|
|
|
|
# Buck is a build tool specifying dependencies among different build targets.
|
|
|
|
# User can pass extra dependencies as a JSON object via command line, and this
|
|
|
|
# script can include these dependencies in the generate TARGETS file.
|
|
|
|
# Usage:
|
2020-05-29 20:24:19 +02:00
|
|
|
# $python3 buckifier/buckify_rocksdb.py
|
2019-08-02 19:40:32 +02:00
|
|
|
# (This generates a TARGET file without user-specified dependency for unit
|
|
|
|
# tests.)
|
2020-05-29 20:24:19 +02:00
|
|
|
# $python3 buckifier/buckify_rocksdb.py \
|
2019-08-02 19:40:32 +02:00
|
|
|
# '{"fake": { \
|
|
|
|
# "extra_deps": [":test_dep", "//fakes/module:mock1"], \
|
|
|
|
# "extra_compiler_flags": ["-DROCKSDB_LITE", "-Os"], \
|
|
|
|
# } \
|
|
|
|
# }'
|
|
|
|
# (Generated TARGETS file has test_dep and mock1 as dependencies for RocksDB
|
|
|
|
# unit tests, and will use the extra_compiler_flags to compile the unit test
|
|
|
|
# source.)
|
|
|
|
|
2017-04-05 01:09:31 +02:00
|
|
|
# tests to export as libraries for inclusion in other projects
|
|
|
|
_EXPORTED_TEST_LIBS = ["env_basic_test"]
|
|
|
|
|
|
|
|
# Parse src.mk files as a Dictionary of
|
|
|
|
# VAR_NAME => list of files
|
|
|
|
def parse_src_mk(repo_path):
|
|
|
|
src_mk = repo_path + "/src.mk"
|
|
|
|
src_files = {}
|
|
|
|
for line in open(src_mk):
|
|
|
|
line = line.strip()
|
|
|
|
if len(line) == 0 or line[0] == '#':
|
|
|
|
continue
|
|
|
|
if '=' in line:
|
|
|
|
current_src = line.split('=')[0].strip()
|
|
|
|
src_files[current_src] = []
|
|
|
|
elif '.cc' in line:
|
|
|
|
src_path = line.split('.cc')[0].strip() + '.cc'
|
|
|
|
src_files[current_src].append(src_path)
|
|
|
|
return src_files
|
|
|
|
|
|
|
|
|
|
|
|
# get all .cc / .c files
|
|
|
|
def get_cc_files(repo_path):
|
|
|
|
cc_files = []
|
2018-01-29 21:43:56 +01:00
|
|
|
for root, dirnames, filenames in os.walk(repo_path): # noqa: B007 T25377293 Grandfathered in
|
2017-04-05 01:09:31 +02:00
|
|
|
root = root[(len(repo_path) + 1):]
|
|
|
|
if "java" in root:
|
|
|
|
# Skip java
|
|
|
|
continue
|
|
|
|
for filename in fnmatch.filter(filenames, '*.cc'):
|
|
|
|
cc_files.append(os.path.join(root, filename))
|
|
|
|
for filename in fnmatch.filter(filenames, '*.c'):
|
|
|
|
cc_files.append(os.path.join(root, filename))
|
|
|
|
return cc_files
|
|
|
|
|
|
|
|
|
|
|
|
# Get tests from Makefile
|
|
|
|
def get_tests(repo_path):
|
|
|
|
Makefile = repo_path + "/Makefile"
|
|
|
|
|
|
|
|
# Dictionary TEST_NAME => IS_PARALLEL
|
|
|
|
tests = {}
|
|
|
|
|
|
|
|
found_tests = False
|
|
|
|
for line in open(Makefile):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("TESTS ="):
|
|
|
|
found_tests = True
|
|
|
|
elif found_tests:
|
|
|
|
if line.endswith("\\"):
|
|
|
|
# remove the trailing \
|
|
|
|
line = line[:-1]
|
|
|
|
line = line.strip()
|
|
|
|
tests[line] = False
|
|
|
|
else:
|
|
|
|
# we consumed all the tests
|
|
|
|
break
|
|
|
|
|
|
|
|
found_parallel_tests = False
|
|
|
|
for line in open(Makefile):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("PARALLEL_TEST ="):
|
|
|
|
found_parallel_tests = True
|
|
|
|
elif found_parallel_tests:
|
|
|
|
if line.endswith("\\"):
|
|
|
|
# remove the trailing \
|
|
|
|
line = line[:-1]
|
|
|
|
line = line.strip()
|
|
|
|
tests[line] = True
|
|
|
|
else:
|
|
|
|
# we consumed all the parallel tests
|
|
|
|
break
|
2019-04-18 19:51:19 +02:00
|
|
|
|
2017-04-05 01:09:31 +02:00
|
|
|
return tests
|
|
|
|
|
2019-08-02 19:40:32 +02:00
|
|
|
# Parse extra dependencies passed by user from command line
|
|
|
|
def get_dependencies():
|
|
|
|
deps_map = {
|
2019-10-23 22:51:03 +02:00
|
|
|
'': {
|
|
|
|
'extra_deps': [],
|
|
|
|
'extra_compiler_flags': []
|
2019-08-02 19:40:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
return deps_map
|
|
|
|
|
|
|
|
def encode_dict(data):
|
|
|
|
rv = {}
|
|
|
|
for k, v in data.items():
|
2019-10-23 22:51:03 +02:00
|
|
|
if isinstance(v, dict):
|
2019-08-02 19:40:32 +02:00
|
|
|
v = encode_dict(v)
|
|
|
|
rv[k] = v
|
|
|
|
return rv
|
|
|
|
extra_deps = json.loads(sys.argv[1], object_hook=encode_dict)
|
|
|
|
for target_alias, deps in extra_deps.items():
|
|
|
|
deps_map[target_alias] = deps
|
|
|
|
return deps_map
|
|
|
|
|
|
|
|
|
2017-04-05 01:09:31 +02:00
|
|
|
# Prepare TARGETS file for buck
|
2019-08-02 19:40:32 +02:00
|
|
|
def generate_targets(repo_path, deps_map):
|
2017-04-05 01:09:31 +02:00
|
|
|
print(ColorString.info("Generating TARGETS"))
|
|
|
|
# parsed src.mk file
|
|
|
|
src_mk = parse_src_mk(repo_path)
|
|
|
|
# get all .cc files
|
|
|
|
cc_files = get_cc_files(repo_path)
|
|
|
|
# get tests from Makefile
|
|
|
|
tests = get_tests(repo_path)
|
|
|
|
|
|
|
|
if src_mk is None or cc_files is None or tests is None:
|
|
|
|
return False
|
|
|
|
|
2020-06-05 21:14:42 +02:00
|
|
|
TARGETS = TARGETSBuilder("%s/TARGETS" % repo_path)
|
2020-05-20 20:35:28 +02:00
|
|
|
|
2017-04-05 01:09:31 +02:00
|
|
|
# rocksdb_lib
|
|
|
|
TARGETS.add_library(
|
|
|
|
"rocksdb_lib",
|
|
|
|
src_mk["LIB_SOURCES"] +
|
|
|
|
src_mk["TOOL_LIB_SOURCES"])
|
|
|
|
# rocksdb_test_lib
|
|
|
|
TARGETS.add_library(
|
|
|
|
"rocksdb_test_lib",
|
|
|
|
src_mk.get("MOCK_LIB_SOURCES", []) +
|
|
|
|
src_mk.get("TEST_LIB_SOURCES", []) +
|
2018-09-25 23:12:12 +02:00
|
|
|
src_mk.get("EXP_LIB_SOURCES", []) +
|
|
|
|
src_mk.get("ANALYZER_LIB_SOURCES", []),
|
2020-06-05 21:14:42 +02:00
|
|
|
[":rocksdb_lib"],
|
|
|
|
extra_external_deps=""" + [
|
|
|
|
("googletest", None, "gtest"),
|
|
|
|
]""")
|
2017-04-05 01:09:31 +02:00
|
|
|
# rocksdb_tools_lib
|
|
|
|
TARGETS.add_library(
|
|
|
|
"rocksdb_tools_lib",
|
|
|
|
src_mk.get("BENCH_LIB_SOURCES", []) +
|
2018-09-25 23:12:12 +02:00
|
|
|
src_mk.get("ANALYZER_LIB_SOURCES", []) +
|
2019-05-30 20:21:38 +02:00
|
|
|
["test_util/testutil.cc"],
|
2017-04-05 01:09:31 +02:00
|
|
|
[":rocksdb_lib"])
|
2019-10-22 04:38:42 +02:00
|
|
|
# rocksdb_stress_lib
|
2020-05-17 06:46:21 +02:00
|
|
|
TARGETS.add_rocksdb_library(
|
2019-10-22 04:38:42 +02:00
|
|
|
"rocksdb_stress_lib",
|
|
|
|
src_mk.get("ANALYZER_LIB_SOURCES", [])
|
|
|
|
+ src_mk.get('STRESS_LIB_SOURCES', [])
|
2020-05-17 06:46:21 +02:00
|
|
|
+ ["test_util/testutil.cc"])
|
2017-04-05 01:09:31 +02:00
|
|
|
|
2020-05-13 06:35:08 +02:00
|
|
|
print("Extra dependencies:\n{0}".format(json.dumps(deps_map)))
|
2017-04-05 01:09:31 +02:00
|
|
|
# test for every test we found in the Makefile
|
2019-08-02 19:40:32 +02:00
|
|
|
for target_alias, deps in deps_map.items():
|
|
|
|
for test in sorted(tests):
|
|
|
|
match_src = [src for src in cc_files if ("/%s.c" % test) in src]
|
|
|
|
if len(match_src) == 0:
|
|
|
|
print(ColorString.warning("Cannot find .cc file for %s" % test))
|
|
|
|
continue
|
|
|
|
elif len(match_src) > 1:
|
|
|
|
print(ColorString.warning("Found more than one .cc for %s" % test))
|
|
|
|
print(match_src)
|
|
|
|
continue
|
|
|
|
|
|
|
|
assert(len(match_src) == 1)
|
|
|
|
is_parallel = tests[test]
|
|
|
|
test_target_name = \
|
|
|
|
test if not target_alias else test + "_" + target_alias
|
|
|
|
TARGETS.register_test(
|
|
|
|
test_target_name,
|
|
|
|
match_src[0],
|
|
|
|
is_parallel,
|
2020-05-13 06:35:08 +02:00
|
|
|
json.dumps(deps['extra_deps']),
|
|
|
|
json.dumps(deps['extra_compiler_flags']))
|
2019-08-02 19:40:32 +02:00
|
|
|
|
|
|
|
if test in _EXPORTED_TEST_LIBS:
|
|
|
|
test_library = "%s_lib" % test_target_name
|
|
|
|
TARGETS.add_library(test_library, match_src, [":rocksdb_test_lib"])
|
2017-04-05 01:09:31 +02:00
|
|
|
TARGETS.flush_tests()
|
|
|
|
|
|
|
|
print(ColorString.info("Generated TARGETS Summary:"))
|
|
|
|
print(ColorString.info("- %d libs" % TARGETS.total_lib))
|
|
|
|
print(ColorString.info("- %d binarys" % TARGETS.total_bin))
|
|
|
|
print(ColorString.info("- %d tests" % TARGETS.total_test))
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def get_rocksdb_path():
|
|
|
|
# rocksdb = {script_dir}/..
|
|
|
|
script_dir = os.path.dirname(sys.argv[0])
|
|
|
|
script_dir = os.path.abspath(script_dir)
|
|
|
|
rocksdb_path = os.path.abspath(
|
|
|
|
os.path.join(script_dir, "../"))
|
|
|
|
|
|
|
|
return rocksdb_path
|
|
|
|
|
2020-05-20 20:35:28 +02:00
|
|
|
|
2017-04-05 01:09:31 +02:00
|
|
|
def exit_with_error(msg):
|
|
|
|
print(ColorString.error(msg))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-08-02 19:40:32 +02:00
|
|
|
deps_map = get_dependencies()
|
2017-04-05 01:09:31 +02:00
|
|
|
# Generate TARGETS file for buck
|
2019-08-02 19:40:32 +02:00
|
|
|
ok = generate_targets(get_rocksdb_path(), deps_map)
|
2017-04-05 01:09:31 +02:00
|
|
|
if not ok:
|
|
|
|
exit_with_error("Failed to generate TARGETS files")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|