File indexing completed on 2025-08-06 08:19:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 """fuse_gmock_files.py v0.1.0
0033 Fuses Google Mock and Google Test source code into two .h files and a .cc file.
0034
0035 SYNOPSIS
0036 fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
0037
0038 Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
0039 code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
0040 directory, and generates three files:
0041 OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
0042 OUTPUT_DIR/gmock-gtest-all.cc. Then you can build your tests
0043 by adding OUTPUT_DIR to the include search path and linking
0044 with OUTPUT_DIR/gmock-gtest-all.cc. These three files contain
0045 everything you need to use Google Mock. Hence you can
0046 "install" Google Mock by copying them to wherever you want.
0047
0048 GMOCK_ROOT_DIR can be omitted and defaults to the parent
0049 directory of the directory holding this script.
0050
0051 EXAMPLES
0052 ./fuse_gmock_files.py fused_gmock
0053 ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
0054
0055 This tool is experimental. In particular, it assumes that there is no
0056 conditional inclusion of Google Mock or Google Test headers. Please
0057 report any problems to googlemock@googlegroups.com. You can read
0058 http://code.google.com/p/googlemock/wiki/CookBook for more
0059 information.
0060 """
0061
0062 __author__ = 'wan@google.com (Zhanyong Wan)'
0063
0064 import os
0065 import re
0066 import sets
0067 import sys
0068
0069
0070
0071 DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
0072
0073
0074 sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
0075 import fuse_gtest_files
0076 gtest = fuse_gtest_files
0077
0078
0079 INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
0080
0081
0082 GMOCK_H_SEED = 'include/gmock/gmock.h'
0083 GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
0084
0085
0086 GTEST_H_OUTPUT = 'gtest/gtest.h'
0087 GMOCK_H_OUTPUT = 'gmock/gmock.h'
0088 GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
0089
0090
0091 def GetGTestRootDir(gmock_root):
0092 """Returns the root directory of Google Test."""
0093
0094 return os.path.join(gmock_root, '../googletest')
0095
0096
0097 def ValidateGMockRootDir(gmock_root):
0098 """Makes sure gmock_root points to a valid gmock root directory.
0099
0100 The function aborts the program on failure.
0101 """
0102
0103 gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
0104 gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
0105 gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
0106
0107
0108 def ValidateOutputDir(output_dir):
0109 """Makes sure output_dir points to a valid output directory.
0110
0111 The function aborts the program on failure.
0112 """
0113
0114 gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
0115 gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
0116 gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
0117
0118
0119 def FuseGMockH(gmock_root, output_dir):
0120 """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
0121
0122 output_file = file(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
0123 processed_files = sets.Set()
0124
0125 def ProcessFile(gmock_header_path):
0126 """Processes the given gmock header file."""
0127
0128
0129 if gmock_header_path in processed_files:
0130 return
0131
0132 processed_files.add(gmock_header_path)
0133
0134
0135 for line in file(os.path.join(gmock_root, gmock_header_path), 'r'):
0136 m = INCLUDE_GMOCK_FILE_REGEX.match(line)
0137 if m:
0138
0139 ProcessFile('include/' + m.group(1))
0140 else:
0141 m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
0142 if m:
0143
0144
0145
0146
0147
0148 if not gtest.GTEST_H_SEED in processed_files:
0149 processed_files.add(gtest.GTEST_H_SEED)
0150 output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
0151 else:
0152
0153 output_file.write(line)
0154
0155 ProcessFile(GMOCK_H_SEED)
0156 output_file.close()
0157
0158
0159 def FuseGMockAllCcToFile(gmock_root, output_file):
0160 """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
0161
0162 processed_files = sets.Set()
0163
0164 def ProcessFile(gmock_source_file):
0165 """Processes the given gmock source file."""
0166
0167
0168 if gmock_source_file in processed_files:
0169 return
0170
0171 processed_files.add(gmock_source_file)
0172
0173
0174 for line in file(os.path.join(gmock_root, gmock_source_file), 'r'):
0175 m = INCLUDE_GMOCK_FILE_REGEX.match(line)
0176 if m:
0177
0178
0179
0180
0181
0182 if not GMOCK_H_SEED in processed_files:
0183 processed_files.add(GMOCK_H_SEED)
0184 output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
0185 else:
0186 m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
0187 if m:
0188
0189
0190
0191 pass
0192 else:
0193 m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
0194 if m:
0195
0196 ProcessFile(m.group(1))
0197 else:
0198
0199 output_file.write(line)
0200
0201 ProcessFile(GMOCK_ALL_CC_SEED)
0202
0203
0204 def FuseGMockGTestAllCc(gmock_root, output_dir):
0205 """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
0206
0207 output_file = file(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT), 'w')
0208
0209 gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
0210
0211 FuseGMockAllCcToFile(gmock_root, output_file)
0212 output_file.close()
0213
0214
0215 def FuseGMock(gmock_root, output_dir):
0216 """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
0217
0218 ValidateGMockRootDir(gmock_root)
0219 ValidateOutputDir(output_dir)
0220
0221 gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
0222 FuseGMockH(gmock_root, output_dir)
0223 FuseGMockGTestAllCc(gmock_root, output_dir)
0224
0225
0226 def main():
0227 argc = len(sys.argv)
0228 if argc == 2:
0229
0230 FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
0231 elif argc == 3:
0232
0233 FuseGMock(sys.argv[1], sys.argv[2])
0234 else:
0235 print __doc__
0236 sys.exit(1)
0237
0238
0239 if __name__ == '__main__':
0240 main()