31"""fuse_gmock_files.py v0.1.0.
33Fuses Google Mock and Google Test source code into two .h files and a .cc file.
36 fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
38 Scans GMOCK_ROOT_DIR for Google Mock
and Google Test source
39 code, assuming Google Test
is in the GMOCK_ROOT_DIR/../googletest
40 directory,
and generates three files:
41 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.
48 GMOCK_ROOT_DIR can be omitted
and defaults to the parent
49 directory of the directory holding this script.
52 ./fuse_gmock_files.py fused_gmock
53 ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
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
58https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md
63from __future__ import print_function
69__author__ = 'wan@google.com (Zhanyong Wan)'
71# We assume that this file is in the scripts/ directory in the Google
73DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
75# We need to call into googletest/scripts/fuse_gtest_files.py.
76sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
77import fuse_gtest_files as gtest
81INCLUDE_GMOCK_FILE_REGEX = re.compile(
r'^\s*#\s*include\s*"(gmock/.+)"')
84GMOCK_H_SEED =
'include/gmock/gmock.h'
85GMOCK_ALL_CC_SEED =
'src/gmock-all.cc'
88GTEST_H_OUTPUT =
'gtest/gtest.h'
89GMOCK_H_OUTPUT =
'gmock/gmock.h'
90GMOCK_GTEST_ALL_CC_OUTPUT =
'gmock-gtest-all.cc'
94 """Returns the root directory of Google Test."""
96 return os.path.join(gmock_root,
'../googletest')
100 """Makes sure gmock_root points to a valid gmock root directory.
102 The function aborts the program on failure.
105 gmock_root: A string with the mock root directory.
109 gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
110 gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
114 """Makes sure output_dir points to a valid output directory.
116 The function aborts the program on failure.
119 output_dir: A string representing the output directory.
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)
128 """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
130 output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT),
'w')
131 processed_files =
set()
133 def ProcessFile(gmock_header_path):
134 """Processes the given gmock header file."""
137 if gmock_header_path
in processed_files:
140 processed_files.add(gmock_header_path)
144 with open(os.path.join(gmock_root, gmock_header_path),
'r')
as fh:
146 m = INCLUDE_GMOCK_FILE_REGEX.match(line)
150 ProcessFile(
'include/' + m.group(1))
152 m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
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,))
164 output_file.write(line)
166 ProcessFile(GMOCK_H_SEED)
171 """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
173 processed_files =
set()
175 def ProcessFile(gmock_source_file):
176 """Processes the given gmock source file."""
179 if gmock_source_file
in processed_files:
182 processed_files.add(gmock_source_file)
186 with open(os.path.join(gmock_root, gmock_source_file),
'r')
as fh:
188 m = INCLUDE_GMOCK_FILE_REGEX.match(line)
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,))
201 m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
209 m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
212 ProcessFile(m.group(1))
215 output_file.write(line)
217 ProcessFile(GMOCK_ALL_CC_SEED)
221 """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
223 with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
232 """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
246 FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
255if __name__ ==
'__main__':
def FuseGMockGTestAllCc(gmock_root, output_dir)
def ValidateOutputDir(output_dir)
def GetGTestRootDir(gmock_root)
def ValidateGMockRootDir(gmock_root)
def FuseGMockH(gmock_root, output_dir)
def FuseGMock(gmock_root, output_dir)
def FuseGMockAllCcToFile(gmock_root, output_file)