1425bb815Sopenharmony_ci#!/usr/bin/env python
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
4425bb815Sopenharmony_ci#
5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
7425bb815Sopenharmony_ci# You may obtain a copy of the License at
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10425bb815Sopenharmony_ci#
11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
15425bb815Sopenharmony_ci# limitations under the License.
16425bb815Sopenharmony_cifrom __future__ import print_function
17425bb815Sopenharmony_ci
18425bb815Sopenharmony_ciimport argparse
19425bb815Sopenharmony_ciimport fnmatch
20425bb815Sopenharmony_ciimport logging
21425bb815Sopenharmony_ciimport os
22425bb815Sopenharmony_ciimport re
23425bb815Sopenharmony_ciimport sys
24425bb815Sopenharmony_ci
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_ciclass SourceMerger(object):
27425bb815Sopenharmony_ci    # pylint: disable=too-many-instance-attributes
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_ci    _RE_INCLUDE = re.compile(r'\s*#include ("|<)(.*?)("|>)\n$')
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_ci    def __init__(self, h_files, extra_includes=None, remove_includes=None, add_lineinfo=False):
32425bb815Sopenharmony_ci        self._log = logging.getLogger('sourcemerger')
33425bb815Sopenharmony_ci        self._last_builtin = None
34425bb815Sopenharmony_ci        self._processed = []
35425bb815Sopenharmony_ci        self._output = []
36425bb815Sopenharmony_ci        self._h_files = h_files
37425bb815Sopenharmony_ci        self._extra_includes = extra_includes or []
38425bb815Sopenharmony_ci        self._remove_includes = remove_includes
39425bb815Sopenharmony_ci        self._add_lineinfo = add_lineinfo
40425bb815Sopenharmony_ci        # The copyright will be loaded from the first input file
41425bb815Sopenharmony_ci        self._copyright = {'lines': [], 'loaded': False}
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_ci    def _process_non_include(self, line, file_level):
44425bb815Sopenharmony_ci        # Special case #2: Builtin include header name usage
45425bb815Sopenharmony_ci        if line.strip() == "#include BUILTIN_INC_HEADER_NAME":
46425bb815Sopenharmony_ci            assert self._last_builtin is not None, 'No previous BUILTIN_INC_HEADER_NAME definition'
47425bb815Sopenharmony_ci            self._log.debug('[%d] Detected usage of BUILTIN_INC_HEADER_NAME, including: %s',
48425bb815Sopenharmony_ci                            file_level, self._last_builtin)
49425bb815Sopenharmony_ci            self.add_file(self._h_files[self._last_builtin])
50425bb815Sopenharmony_ci            # return from the function as we have processed the included file
51425bb815Sopenharmony_ci            return
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_ci        # Special case #1: Builtin include header name definition
54425bb815Sopenharmony_ci        if line.startswith('#define BUILTIN_INC_HEADER_NAME '):
55425bb815Sopenharmony_ci            # the line is in this format: #define BUILTIN_INC_HEADER_NAME "<filename>"
56425bb815Sopenharmony_ci            self._last_builtin = line.split('"', 2)[1]
57425bb815Sopenharmony_ci            self._log.debug('[%d] Detected definition of BUILTIN_INC_HEADER_NAME: %s',
58425bb815Sopenharmony_ci                            file_level, self._last_builtin)
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci        # the line is not anything special, just push it into the output
61425bb815Sopenharmony_ci        self._output.append(line)
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_ci    def _emit_lineinfo(self, line_number, filename):
64425bb815Sopenharmony_ci        if not self._add_lineinfo:
65425bb815Sopenharmony_ci            return
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci        normalized_path = repr(os.path.normpath(filename))[1:-1]
68425bb815Sopenharmony_ci        line_info = '#line %d "%s"\n' % (line_number, normalized_path)
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_ci        if self._output and self._output[-1].startswith('#line'):
71425bb815Sopenharmony_ci            # Avoid emitting multiple line infos in sequence, just overwrite the last one
72425bb815Sopenharmony_ci            self._output[-1] = line_info
73425bb815Sopenharmony_ci        else:
74425bb815Sopenharmony_ci            self._output.append(line_info)
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci    def add_file(self, filename, file_level=0):
77425bb815Sopenharmony_ci        if os.path.basename(filename) in self._processed:
78425bb815Sopenharmony_ci            self._log.warning('Tried to to process an already processed file: "%s"', filename)
79425bb815Sopenharmony_ci            return
80425bb815Sopenharmony_ci
81425bb815Sopenharmony_ci        if not file_level:
82425bb815Sopenharmony_ci            self._log.debug('Adding file: "%s"', filename)
83425bb815Sopenharmony_ci
84425bb815Sopenharmony_ci        file_level += 1
85425bb815Sopenharmony_ci
86425bb815Sopenharmony_ci        # mark the start of the new file in the output
87425bb815Sopenharmony_ci        self._emit_lineinfo(1, filename)
88425bb815Sopenharmony_ci
89425bb815Sopenharmony_ci        line_idx = 0
90425bb815Sopenharmony_ci        with open(filename, 'r') as input_file:
91425bb815Sopenharmony_ci            in_copyright = False
92425bb815Sopenharmony_ci            for line in input_file:
93425bb815Sopenharmony_ci                line_idx += 1
94425bb815Sopenharmony_ci
95425bb815Sopenharmony_ci                if not in_copyright and line.startswith('/* Copyright '):
96425bb815Sopenharmony_ci                    in_copyright = True
97425bb815Sopenharmony_ci                    if not self._copyright['loaded']:
98425bb815Sopenharmony_ci                        self._copyright['lines'].append(line)
99425bb815Sopenharmony_ci                    continue
100425bb815Sopenharmony_ci
101425bb815Sopenharmony_ci                if in_copyright:
102425bb815Sopenharmony_ci                    if not self._copyright['loaded']:
103425bb815Sopenharmony_ci                        self._copyright['lines'].append(line)
104425bb815Sopenharmony_ci
105425bb815Sopenharmony_ci                    if line.strip().endswith('*/'):
106425bb815Sopenharmony_ci                        in_copyright = False
107425bb815Sopenharmony_ci                        self._copyright['loaded'] = True
108425bb815Sopenharmony_ci                        # emit a line info so the line numbering can be tracked correctly
109425bb815Sopenharmony_ci                        self._emit_lineinfo(line_idx + 1, filename)
110425bb815Sopenharmony_ci
111425bb815Sopenharmony_ci                    continue
112425bb815Sopenharmony_ci
113425bb815Sopenharmony_ci                # check if the line is an '#include' line
114425bb815Sopenharmony_ci                match = SourceMerger._RE_INCLUDE.match(line)
115425bb815Sopenharmony_ci                if not match:
116425bb815Sopenharmony_ci                    # the line is not a header
117425bb815Sopenharmony_ci                    self._process_non_include(line, file_level)
118425bb815Sopenharmony_ci                    continue
119425bb815Sopenharmony_ci
120425bb815Sopenharmony_ci                if match.group(1) == '<':
121425bb815Sopenharmony_ci                    # found a "global" include
122425bb815Sopenharmony_ci                    self._output.append(line)
123425bb815Sopenharmony_ci                    continue
124425bb815Sopenharmony_ci
125425bb815Sopenharmony_ci                name = match.group(2)
126425bb815Sopenharmony_ci
127425bb815Sopenharmony_ci                if name in self._remove_includes:
128425bb815Sopenharmony_ci                    self._log.debug('[%d] Removing include line (%s:%d): %s',
129425bb815Sopenharmony_ci                                    file_level, filename, line_idx, line.strip())
130425bb815Sopenharmony_ci                    # emit a line info so the line numbering can be tracked correctly
131425bb815Sopenharmony_ci                    self._emit_lineinfo(line_idx + 1, filename)
132425bb815Sopenharmony_ci                    continue
133425bb815Sopenharmony_ci
134425bb815Sopenharmony_ci                if name not in self._h_files:
135425bb815Sopenharmony_ci                    self._log.warning('[%d] Include not found: "%s" in "%s:%d"',
136425bb815Sopenharmony_ci                                      file_level, name, filename, line_idx)
137425bb815Sopenharmony_ci                    self._output.append(line)
138425bb815Sopenharmony_ci                    continue
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_ci                if name in self._processed:
141425bb815Sopenharmony_ci                    self._log.debug('[%d] Already included: "%s"',
142425bb815Sopenharmony_ci                                    file_level, name)
143425bb815Sopenharmony_ci                    # emit a line info so the line numbering can be tracked correctly
144425bb815Sopenharmony_ci                    self._emit_lineinfo(line_idx + 1, filename)
145425bb815Sopenharmony_ci                    continue
146425bb815Sopenharmony_ci
147425bb815Sopenharmony_ci                self._log.debug('[%d] Including: "%s"',
148425bb815Sopenharmony_ci                                file_level, self._h_files[name])
149425bb815Sopenharmony_ci                self.add_file(self._h_files[name], file_level)
150425bb815Sopenharmony_ci
151425bb815Sopenharmony_ci                # mark the continuation of the current file in the output
152425bb815Sopenharmony_ci                self._emit_lineinfo(line_idx + 1, filename)
153425bb815Sopenharmony_ci
154425bb815Sopenharmony_ci                if not name.endswith('.inc.h'):
155425bb815Sopenharmony_ci                    # if the included file is not a "*.inc.h" file mark it as processed
156425bb815Sopenharmony_ci                    self._processed.append(name)
157425bb815Sopenharmony_ci
158425bb815Sopenharmony_ci        file_level -= 1
159425bb815Sopenharmony_ci        if not filename.endswith('.inc.h'):
160425bb815Sopenharmony_ci            self._processed.append(os.path.basename(filename))
161425bb815Sopenharmony_ci
162425bb815Sopenharmony_ci    def write_output(self, out_fp):
163425bb815Sopenharmony_ci        for line in self._copyright['lines']:
164425bb815Sopenharmony_ci            out_fp.write(line)
165425bb815Sopenharmony_ci
166425bb815Sopenharmony_ci        for include in self._extra_includes:
167425bb815Sopenharmony_ci            out_fp.write('#include "%s"\n' % include)
168425bb815Sopenharmony_ci
169425bb815Sopenharmony_ci        for line in self._output:
170425bb815Sopenharmony_ci            out_fp.write(line)
171425bb815Sopenharmony_ci
172425bb815Sopenharmony_ci
173425bb815Sopenharmony_cidef match_files(base_dir, pattern):
174425bb815Sopenharmony_ci    """
175425bb815Sopenharmony_ci    Return the files matching the given pattern.
176425bb815Sopenharmony_ci
177425bb815Sopenharmony_ci    :param base_dir: directory to search in
178425bb815Sopenharmony_ci    :param pattern: file pattern to use
179425bb815Sopenharmony_ci    :returns generator: the generator which iterates the matching file names
180425bb815Sopenharmony_ci    """
181425bb815Sopenharmony_ci    for path, _, files in os.walk(base_dir):
182425bb815Sopenharmony_ci        for name in files:
183425bb815Sopenharmony_ci            if fnmatch.fnmatch(name, pattern):
184425bb815Sopenharmony_ci                yield os.path.join(path, name)
185425bb815Sopenharmony_ci
186425bb815Sopenharmony_ci
187425bb815Sopenharmony_cidef collect_files(base_dir, pattern):
188425bb815Sopenharmony_ci    """
189425bb815Sopenharmony_ci    Collect files in the provided base directory given a file pattern.
190425bb815Sopenharmony_ci    Will collect all files in the base dir recursively.
191425bb815Sopenharmony_ci
192425bb815Sopenharmony_ci    :param base_dir: directory to search in
193425bb815Sopenharmony_ci    :param pattern: file pattern to use
194425bb815Sopenharmony_ci    :returns dictionary: a dictionary file base name -> file path mapping
195425bb815Sopenharmony_ci    """
196425bb815Sopenharmony_ci    name_mapping = {}
197425bb815Sopenharmony_ci    for fname in match_files(base_dir, pattern):
198425bb815Sopenharmony_ci        name = os.path.basename(fname)
199425bb815Sopenharmony_ci
200425bb815Sopenharmony_ci        if name in name_mapping:
201425bb815Sopenharmony_ci            print('Duplicate name detected: "%s" and "%s"' % (name, name_mapping[name]))
202425bb815Sopenharmony_ci            continue
203425bb815Sopenharmony_ci
204425bb815Sopenharmony_ci        name_mapping[name] = fname
205425bb815Sopenharmony_ci
206425bb815Sopenharmony_ci    return name_mapping
207425bb815Sopenharmony_ci
208425bb815Sopenharmony_ci
209425bb815Sopenharmony_cidef run_merger(args):
210425bb815Sopenharmony_ci    h_files = collect_files(args.base_dir, '*.h')
211425bb815Sopenharmony_ci    c_files = collect_files(args.base_dir, '*.c')
212425bb815Sopenharmony_ci
213425bb815Sopenharmony_ci    for name in args.remove_include:
214425bb815Sopenharmony_ci        c_files.pop(name, '')
215425bb815Sopenharmony_ci        h_files.pop(name, '')
216425bb815Sopenharmony_ci
217425bb815Sopenharmony_ci    merger = SourceMerger(h_files, args.push_include, args.remove_include, args.add_lineinfo)
218425bb815Sopenharmony_ci    for input_file in args.input_files:
219425bb815Sopenharmony_ci        merger.add_file(input_file)
220425bb815Sopenharmony_ci
221425bb815Sopenharmony_ci    if args.append_c_files:
222425bb815Sopenharmony_ci        # if the input file is in the C files list it should be removed to avoid
223425bb815Sopenharmony_ci        # double inclusion of the file
224425bb815Sopenharmony_ci        for input_file in args.input_files:
225425bb815Sopenharmony_ci            input_name = os.path.basename(input_file)
226425bb815Sopenharmony_ci            c_files.pop(input_name, '')
227425bb815Sopenharmony_ci
228425bb815Sopenharmony_ci        # Add the C files in reverse order to make sure that builtins are
229425bb815Sopenharmony_ci        # not at the beginning.
230425bb815Sopenharmony_ci        for fname in sorted(c_files.values(), reverse=True):
231425bb815Sopenharmony_ci            merger.add_file(fname)
232425bb815Sopenharmony_ci
233425bb815Sopenharmony_ci    with open(args.output_file, 'w') as output:
234425bb815Sopenharmony_ci        merger.write_output(output)
235425bb815Sopenharmony_ci
236425bb815Sopenharmony_ci
237425bb815Sopenharmony_cidef main():
238425bb815Sopenharmony_ci    parser = argparse.ArgumentParser(description='Merge source/header files.')
239425bb815Sopenharmony_ci    parser.add_argument('--base-dir', metavar='DIR', type=str, dest='base_dir',
240425bb815Sopenharmony_ci                        help='', default=os.path.curdir)
241425bb815Sopenharmony_ci    parser.add_argument('--input', metavar='FILES', type=str, action='append', dest='input_files',
242425bb815Sopenharmony_ci                        help='Main input source/header files', default=[])
243425bb815Sopenharmony_ci    parser.add_argument('--output', metavar='FILE', type=str, dest='output_file',
244425bb815Sopenharmony_ci                        help='Output source/header file')
245425bb815Sopenharmony_ci    parser.add_argument('--append-c-files', dest='append_c_files', default=False,
246425bb815Sopenharmony_ci                        action='store_true', help='Enable auto inclusion of c files under the base-dir')
247425bb815Sopenharmony_ci    parser.add_argument('--remove-include', action='append', default=[])
248425bb815Sopenharmony_ci    parser.add_argument('--push-include', action='append', default=[])
249425bb815Sopenharmony_ci    parser.add_argument('--add-lineinfo', action='store_true', default=False,
250425bb815Sopenharmony_ci                        help='Enable #line macro insertion into the generated sources')
251425bb815Sopenharmony_ci    parser.add_argument('--verbose', '-v', action='store_true', default=False)
252425bb815Sopenharmony_ci
253425bb815Sopenharmony_ci    args = parser.parse_args()
254425bb815Sopenharmony_ci
255425bb815Sopenharmony_ci    log_level = logging.WARNING
256425bb815Sopenharmony_ci    if args.verbose:
257425bb815Sopenharmony_ci        log_level = logging.DEBUG
258425bb815Sopenharmony_ci
259425bb815Sopenharmony_ci    logging.basicConfig(level=log_level)
260425bb815Sopenharmony_ci    logging.debug('Starting merge with args: %s', str(sys.argv))
261425bb815Sopenharmony_ci
262425bb815Sopenharmony_ci    run_merger(args)
263425bb815Sopenharmony_ci
264425bb815Sopenharmony_ci
265425bb815Sopenharmony_ciif __name__ == "__main__":
266425bb815Sopenharmony_ci    main()
267