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