blob: efed6faf4ea0280851a99282728cf01bf92e1878 [file] [log] [blame]
mtkleindf5b7602015-08-17 15:02:57 -07001#!/usr/bin/env python
2#
3# Copyright 2015 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8# This script does a very rough simulation of BUILD file expansion,
9# mostly to see the effects of glob().
10
11# We start by adding some symbols to our namespace that BUILD.public calls.
12
halcanarybf3dde22015-08-18 08:35:45 -070013import glob
benjaminwagner6f6bef82015-10-15 08:09:44 -070014import os
halcanarybf3dde22015-08-18 08:35:45 -070015import pprint
benjaminwagner6f6bef82015-10-15 08:09:44 -070016import re
halcanarybf3dde22015-08-18 08:35:45 -070017
18def noop(*args, **kwargs):
mtkleindf5b7602015-08-17 15:02:57 -070019 pass
20
benjaminwagner86ea33e2015-10-26 10:46:25 -070021def select_simulator(d):
22 result = []
23 for k in d:
24 result.append("*** BEGIN %s ***" % k)
25 result.extend(d[k])
26 result.append("*** END %s ***" % k)
27 return result
28
benjaminwagner6f6bef82015-10-15 08:09:44 -070029DOUBLE_STAR_RE = re.compile(r'/\*\*/')
30STAR_RE = re.compile(r'\*')
31DOUBLE_STAR_PLACEHOLDER = "xxxdoublestarxxx"
32STAR_PLACEHOLDER = "xxxstarxxx"
33
34# Returns a set of files that match pattern.
35def BUILD_glob_single(pattern):
36 if pattern.find('**') < 0:
37 # If pattern doesn't include **, glob.glob more-or-less does the right
38 # thing.
39 return glob.glob(pattern)
40 # First transform pattern into a regexp.
41 # Temporarily remove ** and *.
42 pattern2 = DOUBLE_STAR_RE.sub(DOUBLE_STAR_PLACEHOLDER, pattern)
43 pattern3 = STAR_RE.sub(STAR_PLACEHOLDER, pattern2)
44 # Replace any regexp special characters.
45 pattern4 = re.escape(pattern3)
46 # Replace * with [^/]* and ** with .*.
47 pattern5 = pattern4.replace(STAR_PLACEHOLDER, '[^/]*')
48 pattern6 = pattern5.replace(DOUBLE_STAR_PLACEHOLDER, '.*/')
49 # Anchor the match at the beginning and end.
50 pattern7 = "^" + pattern6 + "$"
51 pattern_re = re.compile(pattern7)
52 matches = set()
53 for root, _, files in os.walk('.'):
54 for fname in files:
55 # Remove initial "./".
56 path = os.path.join(root, fname)[2:]
57 if pattern_re.match(path):
58 matches.add(path)
59 return matches
60
mtkleindf5b7602015-08-17 15:02:57 -070061# Simulates BUILD file glob().
halcanarybf3dde22015-08-18 08:35:45 -070062def BUILD_glob(include, exclude=()):
mtkleindf5b7602015-08-17 15:02:57 -070063 files = set()
64 for pattern in include:
benjaminwagner6f6bef82015-10-15 08:09:44 -070065 files.update(BUILD_glob_single(pattern))
mtkleindf5b7602015-08-17 15:02:57 -070066 for pattern in exclude:
benjaminwagner6f6bef82015-10-15 08:09:44 -070067 files.difference_update(BUILD_glob_single(pattern))
mtkleindf5b7602015-08-17 15:02:57 -070068 return list(sorted(files))
69
halcanarybf3dde22015-08-18 08:35:45 -070070# With these namespaces, we can treat BUILD.public as if it were
71# Python code. This pulls its variable definitions (SRCS, HDRS,
72# DEFINES, etc.) into local_names.
73global_names = {
benjaminwagner6f6bef82015-10-15 08:09:44 -070074 'cc_library': noop,
75 'cc_test': noop,
benjaminwagner86ea33e2015-10-26 10:46:25 -070076 'exports_files': noop,
halcanarybf3dde22015-08-18 08:35:45 -070077 'glob': BUILD_glob,
benjaminwagner86ea33e2015-10-26 10:46:25 -070078 'select': select_simulator,
benjaminwagner39e7aa42015-11-18 13:26:10 -080079 'BASE_DIR': '',
80 'BASE_EXTERNAL_DEPS_ANDROID': [],
81 'BASE_EXTERNAL_DEPS_IOS': [],
82 'BASE_EXTERNAL_DEPS_UNIX': [],
83 'CONDITION_ANDROID': 'CONDITION_ANDROID',
84 'CONDITION_IOS': 'CONDITION_IOS',
benjaminwagner6f6bef82015-10-15 08:09:44 -070085 'DM_EXTERNAL_DEPS': [],
benjaminwagner8a3760f2015-10-29 13:40:27 -070086 'EXTERNAL_DEPS_ALL': [],
benjaminwagner2211a7b2015-12-01 11:12:05 -080087 'EXTERNAL_INCLUDES': [],
halcanarybf3dde22015-08-18 08:35:45 -070088}
89local_names = {}
90execfile('BUILD.public', global_names, local_names)
mtkleindf5b7602015-08-17 15:02:57 -070091
mtkleindf5b7602015-08-17 15:02:57 -070092with open('tools/BUILD.public.expected', 'w') as out:
93 print >>out, "This file is auto-generated by tools/BUILD_simulator.py."
94 print >>out, "It expands BUILD.public to make it easy to see changes."
halcanarybf3dde22015-08-18 08:35:45 -070095 for name, value in sorted(local_names.items()):
96 print >>out, name, '= ',
97 pprint.pprint(value, out)