1f92157deSopenharmony_ci#!/usr/bin/env python
2f92157deSopenharmony_ci#
3f92157deSopenharmony_ci# Copyright 2009, Google Inc.
4f92157deSopenharmony_ci# All rights reserved.
5f92157deSopenharmony_ci#
6f92157deSopenharmony_ci# Redistribution and use in source and binary forms, with or without
7f92157deSopenharmony_ci# modification, are permitted provided that the following conditions are
8f92157deSopenharmony_ci# met:
9f92157deSopenharmony_ci#
10f92157deSopenharmony_ci#     * Redistributions of source code must retain the above copyright
11f92157deSopenharmony_ci# notice, this list of conditions and the following disclaimer.
12f92157deSopenharmony_ci#     * Redistributions in binary form must reproduce the above
13f92157deSopenharmony_ci# copyright notice, this list of conditions and the following disclaimer
14f92157deSopenharmony_ci# in the documentation and/or other materials provided with the
15f92157deSopenharmony_ci# distribution.
16f92157deSopenharmony_ci#     * Neither the name of Google Inc. nor the names of its
17f92157deSopenharmony_ci# contributors may be used to endorse or promote products derived from
18f92157deSopenharmony_ci# this software without specific prior written permission.
19f92157deSopenharmony_ci#
20f92157deSopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21f92157deSopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22f92157deSopenharmony_ci# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23f92157deSopenharmony_ci# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24f92157deSopenharmony_ci# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25f92157deSopenharmony_ci# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26f92157deSopenharmony_ci# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27f92157deSopenharmony_ci# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28f92157deSopenharmony_ci# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29f92157deSopenharmony_ci# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30f92157deSopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31f92157deSopenharmony_ci"""fuse_gmock_files.py v0.1.0.
32f92157deSopenharmony_ci
33f92157deSopenharmony_ciFuses Google Mock and Google Test source code into two .h files and a .cc file.
34f92157deSopenharmony_ci
35f92157deSopenharmony_ciSYNOPSIS
36f92157deSopenharmony_ci       fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
37f92157deSopenharmony_ci
38f92157deSopenharmony_ci       Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
39f92157deSopenharmony_ci       code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
40f92157deSopenharmony_ci       directory, and generates three files:
41f92157deSopenharmony_ci       OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
42f92157deSopenharmony_ci       OUTPUT_DIR/gmock-gtest-all.cc.  Then you can build your tests
43f92157deSopenharmony_ci       by adding OUTPUT_DIR to the include search path and linking
44f92157deSopenharmony_ci       with OUTPUT_DIR/gmock-gtest-all.cc.  These three files contain
45f92157deSopenharmony_ci       everything you need to use Google Mock.  Hence you can
46f92157deSopenharmony_ci       "install" Google Mock by copying them to wherever you want.
47f92157deSopenharmony_ci
48f92157deSopenharmony_ci       GMOCK_ROOT_DIR can be omitted and defaults to the parent
49f92157deSopenharmony_ci       directory of the directory holding this script.
50f92157deSopenharmony_ci
51f92157deSopenharmony_ciEXAMPLES
52f92157deSopenharmony_ci       ./fuse_gmock_files.py fused_gmock
53f92157deSopenharmony_ci       ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
54f92157deSopenharmony_ci
55f92157deSopenharmony_ciThis tool is experimental.  In particular, it assumes that there is no
56f92157deSopenharmony_ciconditional inclusion of Google Mock or Google Test headers.  Please
57f92157deSopenharmony_cireport any problems to googlemock@googlegroups.com.  You can read
58f92157deSopenharmony_cihttps://github.com/google/googletest/blob/master/docs/gmock_cook_book.md
59f92157deSopenharmony_cifor more
60f92157deSopenharmony_ciinformation.
61f92157deSopenharmony_ci"""
62f92157deSopenharmony_ci
63f92157deSopenharmony_cifrom __future__ import print_function
64f92157deSopenharmony_ci
65f92157deSopenharmony_ciimport os
66f92157deSopenharmony_ciimport re
67f92157deSopenharmony_ciimport sys
68f92157deSopenharmony_ci
69f92157deSopenharmony_ci__author__ = 'wan@google.com (Zhanyong Wan)'
70f92157deSopenharmony_ci
71f92157deSopenharmony_ci# We assume that this file is in the scripts/ directory in the Google
72f92157deSopenharmony_ci# Mock root directory.
73f92157deSopenharmony_ciDEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
74f92157deSopenharmony_ci
75f92157deSopenharmony_ci# We need to call into googletest/scripts/fuse_gtest_files.py.
76f92157deSopenharmony_cisys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
77f92157deSopenharmony_ciimport fuse_gtest_files as gtest  # pylint:disable=g-import-not-at-top
78f92157deSopenharmony_ci
79f92157deSopenharmony_ci# Regex for matching
80f92157deSopenharmony_ci# '#include "gmock/..."'.
81f92157deSopenharmony_ciINCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
82f92157deSopenharmony_ci
83f92157deSopenharmony_ci# Where to find the source seed files.
84f92157deSopenharmony_ciGMOCK_H_SEED = 'include/gmock/gmock.h'
85f92157deSopenharmony_ciGMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
86f92157deSopenharmony_ci
87f92157deSopenharmony_ci# Where to put the generated files.
88f92157deSopenharmony_ciGTEST_H_OUTPUT = 'gtest/gtest.h'
89f92157deSopenharmony_ciGMOCK_H_OUTPUT = 'gmock/gmock.h'
90f92157deSopenharmony_ciGMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
91f92157deSopenharmony_ci
92f92157deSopenharmony_ci
93f92157deSopenharmony_cidef GetGTestRootDir(gmock_root):
94f92157deSopenharmony_ci  """Returns the root directory of Google Test."""
95f92157deSopenharmony_ci
96f92157deSopenharmony_ci  return os.path.join(gmock_root, '../googletest')
97f92157deSopenharmony_ci
98f92157deSopenharmony_ci
99f92157deSopenharmony_cidef ValidateGMockRootDir(gmock_root):
100f92157deSopenharmony_ci  """Makes sure gmock_root points to a valid gmock root directory.
101f92157deSopenharmony_ci
102f92157deSopenharmony_ci  The function aborts the program on failure.
103f92157deSopenharmony_ci
104f92157deSopenharmony_ci  Args:
105f92157deSopenharmony_ci    gmock_root: A string with the mock root directory.
106f92157deSopenharmony_ci  """
107f92157deSopenharmony_ci
108f92157deSopenharmony_ci  gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
109f92157deSopenharmony_ci  gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
110f92157deSopenharmony_ci  gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
111f92157deSopenharmony_ci
112f92157deSopenharmony_ci
113f92157deSopenharmony_cidef ValidateOutputDir(output_dir):
114f92157deSopenharmony_ci  """Makes sure output_dir points to a valid output directory.
115f92157deSopenharmony_ci
116f92157deSopenharmony_ci  The function aborts the program on failure.
117f92157deSopenharmony_ci
118f92157deSopenharmony_ci  Args:
119f92157deSopenharmony_ci    output_dir: A string representing the output directory.
120f92157deSopenharmony_ci  """
121f92157deSopenharmony_ci
122f92157deSopenharmony_ci  gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
123f92157deSopenharmony_ci  gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
124f92157deSopenharmony_ci  gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
125f92157deSopenharmony_ci
126f92157deSopenharmony_ci
127f92157deSopenharmony_cidef FuseGMockH(gmock_root, output_dir):
128f92157deSopenharmony_ci  """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
129f92157deSopenharmony_ci
130f92157deSopenharmony_ci  output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
131f92157deSopenharmony_ci  processed_files = set()  # Holds all gmock headers we've processed.
132f92157deSopenharmony_ci
133f92157deSopenharmony_ci  def ProcessFile(gmock_header_path):
134f92157deSopenharmony_ci    """Processes the given gmock header file."""
135f92157deSopenharmony_ci
136f92157deSopenharmony_ci    # We don't process the same header twice.
137f92157deSopenharmony_ci    if gmock_header_path in processed_files:
138f92157deSopenharmony_ci      return
139f92157deSopenharmony_ci
140f92157deSopenharmony_ci    processed_files.add(gmock_header_path)
141f92157deSopenharmony_ci
142f92157deSopenharmony_ci    # Reads each line in the given gmock header.
143f92157deSopenharmony_ci
144f92157deSopenharmony_ci    with open(os.path.join(gmock_root, gmock_header_path), 'r') as fh:
145f92157deSopenharmony_ci      for line in fh:
146f92157deSopenharmony_ci        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
147f92157deSopenharmony_ci        if m:
148f92157deSopenharmony_ci          # '#include "gmock/..."'
149f92157deSopenharmony_ci          # - let's process it recursively.
150f92157deSopenharmony_ci          ProcessFile('include/' + m.group(1))
151f92157deSopenharmony_ci        else:
152f92157deSopenharmony_ci          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
153f92157deSopenharmony_ci          if m:
154f92157deSopenharmony_ci            # '#include "gtest/foo.h"'
155f92157deSopenharmony_ci            # We translate it to "gtest/gtest.h", regardless of what foo is,
156f92157deSopenharmony_ci            # since all gtest headers are fused into gtest/gtest.h.
157f92157deSopenharmony_ci
158f92157deSopenharmony_ci            # There is no need to #include gtest.h twice.
159f92157deSopenharmony_ci            if gtest.GTEST_H_SEED not in processed_files:
160f92157deSopenharmony_ci              processed_files.add(gtest.GTEST_H_SEED)
161f92157deSopenharmony_ci              output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
162f92157deSopenharmony_ci          else:
163f92157deSopenharmony_ci            # Otherwise we copy the line unchanged to the output file.
164f92157deSopenharmony_ci            output_file.write(line)
165f92157deSopenharmony_ci
166f92157deSopenharmony_ci  ProcessFile(GMOCK_H_SEED)
167f92157deSopenharmony_ci  output_file.close()
168f92157deSopenharmony_ci
169f92157deSopenharmony_ci
170f92157deSopenharmony_cidef FuseGMockAllCcToFile(gmock_root, output_file):
171f92157deSopenharmony_ci  """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
172f92157deSopenharmony_ci
173f92157deSopenharmony_ci  processed_files = set()
174f92157deSopenharmony_ci
175f92157deSopenharmony_ci  def ProcessFile(gmock_source_file):
176f92157deSopenharmony_ci    """Processes the given gmock source file."""
177f92157deSopenharmony_ci
178f92157deSopenharmony_ci    # We don't process the same #included file twice.
179f92157deSopenharmony_ci    if gmock_source_file in processed_files:
180f92157deSopenharmony_ci      return
181f92157deSopenharmony_ci
182f92157deSopenharmony_ci    processed_files.add(gmock_source_file)
183f92157deSopenharmony_ci
184f92157deSopenharmony_ci    # Reads each line in the given gmock source file.
185f92157deSopenharmony_ci
186f92157deSopenharmony_ci    with open(os.path.join(gmock_root, gmock_source_file), 'r') as fh:
187f92157deSopenharmony_ci      for line in fh:
188f92157deSopenharmony_ci        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
189f92157deSopenharmony_ci        if m:
190f92157deSopenharmony_ci          # '#include "gmock/foo.h"'
191f92157deSopenharmony_ci          # We treat it as '#include  "gmock/gmock.h"', as all other gmock
192f92157deSopenharmony_ci          # headers are being fused into gmock.h and cannot be
193f92157deSopenharmony_ci          # included directly.  No need to
194f92157deSopenharmony_ci          # #include "gmock/gmock.h"
195f92157deSopenharmony_ci          # more than once.
196f92157deSopenharmony_ci
197f92157deSopenharmony_ci          if GMOCK_H_SEED not in processed_files:
198f92157deSopenharmony_ci            processed_files.add(GMOCK_H_SEED)
199f92157deSopenharmony_ci            output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
200f92157deSopenharmony_ci        else:
201f92157deSopenharmony_ci          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
202f92157deSopenharmony_ci          if m:
203f92157deSopenharmony_ci            # '#include "gtest/..."'
204f92157deSopenharmony_ci            # There is no need to #include gtest.h as it has been
205f92157deSopenharmony_ci            # #included by gtest-all.cc.
206f92157deSopenharmony_ci
207f92157deSopenharmony_ci            pass
208f92157deSopenharmony_ci          else:
209f92157deSopenharmony_ci            m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
210f92157deSopenharmony_ci            if m:
211f92157deSopenharmony_ci              # It's '#include "src/foo"' - let's process it recursively.
212f92157deSopenharmony_ci              ProcessFile(m.group(1))
213f92157deSopenharmony_ci            else:
214f92157deSopenharmony_ci              # Otherwise we copy the line unchanged to the output file.
215f92157deSopenharmony_ci              output_file.write(line)
216f92157deSopenharmony_ci
217f92157deSopenharmony_ci  ProcessFile(GMOCK_ALL_CC_SEED)
218f92157deSopenharmony_ci
219f92157deSopenharmony_ci
220f92157deSopenharmony_cidef FuseGMockGTestAllCc(gmock_root, output_dir):
221f92157deSopenharmony_ci  """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
222f92157deSopenharmony_ci
223f92157deSopenharmony_ci  with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
224f92157deSopenharmony_ci            'w') as output_file:
225f92157deSopenharmony_ci    # First, fuse gtest-all.cc into gmock-gtest-all.cc.
226f92157deSopenharmony_ci    gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
227f92157deSopenharmony_ci    # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
228f92157deSopenharmony_ci    FuseGMockAllCcToFile(gmock_root, output_file)
229f92157deSopenharmony_ci
230f92157deSopenharmony_ci
231f92157deSopenharmony_cidef FuseGMock(gmock_root, output_dir):
232f92157deSopenharmony_ci  """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
233f92157deSopenharmony_ci
234f92157deSopenharmony_ci  ValidateGMockRootDir(gmock_root)
235f92157deSopenharmony_ci  ValidateOutputDir(output_dir)
236f92157deSopenharmony_ci
237f92157deSopenharmony_ci  gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
238f92157deSopenharmony_ci  FuseGMockH(gmock_root, output_dir)
239f92157deSopenharmony_ci  FuseGMockGTestAllCc(gmock_root, output_dir)
240f92157deSopenharmony_ci
241f92157deSopenharmony_ci
242f92157deSopenharmony_cidef main():
243f92157deSopenharmony_ci  argc = len(sys.argv)
244f92157deSopenharmony_ci  if argc == 2:
245f92157deSopenharmony_ci    # fuse_gmock_files.py OUTPUT_DIR
246f92157deSopenharmony_ci    FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
247f92157deSopenharmony_ci  elif argc == 3:
248f92157deSopenharmony_ci    # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
249f92157deSopenharmony_ci    FuseGMock(sys.argv[1], sys.argv[2])
250f92157deSopenharmony_ci  else:
251f92157deSopenharmony_ci    print(__doc__)
252f92157deSopenharmony_ci    sys.exit(1)
253f92157deSopenharmony_ci
254f92157deSopenharmony_ci
255f92157deSopenharmony_ciif __name__ == '__main__':
256f92157deSopenharmony_ci  main()
257