2015-08-17 22:02:57 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2015 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
# This script does a very rough simulation of BUILD file expansion,
|
|
|
|
# mostly to see the effects of glob().
|
|
|
|
|
|
|
|
# We start by adding some symbols to our namespace that BUILD.public calls.
|
|
|
|
|
2015-08-18 15:35:45 +00:00
|
|
|
import glob
|
2015-10-15 15:09:44 +00:00
|
|
|
import os
|
2015-08-18 15:35:45 +00:00
|
|
|
import pprint
|
2015-10-15 15:09:44 +00:00
|
|
|
import re
|
2015-08-18 15:35:45 +00:00
|
|
|
|
|
|
|
def noop(*args, **kwargs):
|
2015-08-17 22:02:57 +00:00
|
|
|
pass
|
|
|
|
|
2015-10-26 17:46:25 +00:00
|
|
|
def select_simulator(d):
|
|
|
|
result = []
|
|
|
|
for k in d:
|
|
|
|
result.append("*** BEGIN %s ***" % k)
|
|
|
|
result.extend(d[k])
|
|
|
|
result.append("*** END %s ***" % k)
|
|
|
|
return result
|
|
|
|
|
2015-10-15 15:09:44 +00:00
|
|
|
DOUBLE_STAR_RE = re.compile(r'/\*\*/')
|
|
|
|
STAR_RE = re.compile(r'\*')
|
|
|
|
DOUBLE_STAR_PLACEHOLDER = "xxxdoublestarxxx"
|
|
|
|
STAR_PLACEHOLDER = "xxxstarxxx"
|
|
|
|
|
|
|
|
# Returns a set of files that match pattern.
|
|
|
|
def BUILD_glob_single(pattern):
|
|
|
|
if pattern.find('**') < 0:
|
|
|
|
# If pattern doesn't include **, glob.glob more-or-less does the right
|
|
|
|
# thing.
|
|
|
|
return glob.glob(pattern)
|
|
|
|
# First transform pattern into a regexp.
|
|
|
|
# Temporarily remove ** and *.
|
|
|
|
pattern2 = DOUBLE_STAR_RE.sub(DOUBLE_STAR_PLACEHOLDER, pattern)
|
|
|
|
pattern3 = STAR_RE.sub(STAR_PLACEHOLDER, pattern2)
|
|
|
|
# Replace any regexp special characters.
|
|
|
|
pattern4 = re.escape(pattern3)
|
|
|
|
# Replace * with [^/]* and ** with .*.
|
|
|
|
pattern5 = pattern4.replace(STAR_PLACEHOLDER, '[^/]*')
|
|
|
|
pattern6 = pattern5.replace(DOUBLE_STAR_PLACEHOLDER, '.*/')
|
|
|
|
# Anchor the match at the beginning and end.
|
|
|
|
pattern7 = "^" + pattern6 + "$"
|
|
|
|
pattern_re = re.compile(pattern7)
|
|
|
|
matches = set()
|
|
|
|
for root, _, files in os.walk('.'):
|
|
|
|
for fname in files:
|
|
|
|
# Remove initial "./".
|
|
|
|
path = os.path.join(root, fname)[2:]
|
|
|
|
if pattern_re.match(path):
|
|
|
|
matches.add(path)
|
|
|
|
return matches
|
|
|
|
|
2015-08-17 22:02:57 +00:00
|
|
|
# Simulates BUILD file glob().
|
2015-08-18 15:35:45 +00:00
|
|
|
def BUILD_glob(include, exclude=()):
|
2015-08-17 22:02:57 +00:00
|
|
|
files = set()
|
|
|
|
for pattern in include:
|
2015-10-15 15:09:44 +00:00
|
|
|
files.update(BUILD_glob_single(pattern))
|
2015-08-17 22:02:57 +00:00
|
|
|
for pattern in exclude:
|
2015-10-15 15:09:44 +00:00
|
|
|
files.difference_update(BUILD_glob_single(pattern))
|
2015-08-17 22:02:57 +00:00
|
|
|
return list(sorted(files))
|
|
|
|
|
2015-08-18 15:35:45 +00:00
|
|
|
# With these namespaces, we can treat BUILD.public as if it were
|
|
|
|
# Python code. This pulls its variable definitions (SRCS, HDRS,
|
|
|
|
# DEFINES, etc.) into local_names.
|
|
|
|
global_names = {
|
2015-10-15 15:09:44 +00:00
|
|
|
'cc_library': noop,
|
|
|
|
'cc_test': noop,
|
2015-10-26 17:46:25 +00:00
|
|
|
'exports_files': noop,
|
2015-08-18 15:35:45 +00:00
|
|
|
'glob': BUILD_glob,
|
2015-10-26 17:46:25 +00:00
|
|
|
'select': select_simulator,
|
2015-10-15 15:09:44 +00:00
|
|
|
'BASE_DIR': "",
|
2015-10-26 17:46:25 +00:00
|
|
|
'CONDITION_ANDROID': "CONDITION_ANDROID",
|
2015-10-15 15:09:44 +00:00
|
|
|
'DM_EXTERNAL_DEPS': [],
|
2015-10-29 20:40:27 +00:00
|
|
|
'EXTERNAL_DEPS_ALL': [],
|
2015-10-26 17:46:25 +00:00
|
|
|
'EXTERNAL_DEPS_ANDROID': [],
|
|
|
|
'EXTERNAL_DEPS_UNIX': [],
|
2015-08-18 15:35:45 +00:00
|
|
|
}
|
|
|
|
local_names = {}
|
|
|
|
execfile('BUILD.public', global_names, local_names)
|
2015-08-17 22:02:57 +00:00
|
|
|
|
|
|
|
with open('tools/BUILD.public.expected', 'w') as out:
|
|
|
|
print >>out, "This file is auto-generated by tools/BUILD_simulator.py."
|
|
|
|
print >>out, "It expands BUILD.public to make it easy to see changes."
|
2015-08-18 15:35:45 +00:00
|
|
|
for name, value in sorted(local_names.items()):
|
|
|
|
print >>out, name, '= ',
|
|
|
|
pprint.pprint(value, out)
|