Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """fuse_gmock_files.py v0.1.0
00033 Fuses Google Mock and Google Test source code into two .h files and a .cc file.
00034
00035 SYNOPSIS
00036 fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
00037
00038 Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
00039 code, assuming Google Test is in the GMOCK_ROOT_DIR/gtest
00040 sub-directory, and generates three files:
00041 OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
00042 OUTPUT_DIR/gmock-gtest-all.cc. Then you can build your tests
00043 by adding OUTPUT_DIR to the include search path and linking
00044 with OUTPUT_DIR/gmock-gtest-all.cc. These three files contain
00045 everything you need to use Google Mock. Hence you can
00046 "install" Google Mock by copying them to wherever you want.
00047
00048 GMOCK_ROOT_DIR can be omitted and defaults to the parent
00049 directory of the directory holding this script.
00050
00051 EXAMPLES
00052 ./fuse_gmock_files.py fused_gmock
00053 ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
00054
00055 This tool is experimental. In particular, it assumes that there is no
00056 conditional inclusion of Google Mock or Google Test headers. Please
00057 report any problems to googlemock@googlegroups.com. You can read
00058 http://code.google.com/p/googlemock/wiki/CookBook for more
00059 information.
00060 """
00061
00062 __author__ = 'wan@google.com (Zhanyong Wan)'
00063
00064 import os
00065 import re
00066 import sets
00067 import sys
00068
00069
00070
00071 DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
00072
00073
00074 sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, 'gtest/scripts'))
00075 import fuse_gtest_files
00076 gtest = fuse_gtest_files
00077
00078
00079 INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
00080
00081
00082 GMOCK_H_SEED = 'include/gmock/gmock.h'
00083 GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
00084
00085
00086 GTEST_H_OUTPUT = 'gtest/gtest.h'
00087 GMOCK_H_OUTPUT = 'gmock/gmock.h'
00088 GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
00089
00090
00091 def GetGTestRootDir(gmock_root):
00092 """Returns the root directory of Google Test."""
00093
00094 return os.path.join(gmock_root, 'gtest')
00095
00096
00097 def ValidateGMockRootDir(gmock_root):
00098 """Makes sure gmock_root points to a valid gmock root directory.
00099
00100 The function aborts the program on failure.
00101 """
00102
00103 gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
00104 gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
00105 gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
00106
00107
00108 def ValidateOutputDir(output_dir):
00109 """Makes sure output_dir points to a valid output directory.
00110
00111 The function aborts the program on failure.
00112 """
00113
00114 gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
00115 gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
00116 gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
00117
00118
00119 def FuseGMockH(gmock_root, output_dir):
00120 """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
00121
00122 output_file = file(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
00123 processed_files = sets.Set()
00124
00125 def ProcessFile(gmock_header_path):
00126 """Processes the given gmock header file."""
00127
00128
00129 if gmock_header_path in processed_files:
00130 return
00131
00132 processed_files.add(gmock_header_path)
00133
00134
00135 for line in file(os.path.join(gmock_root, gmock_header_path), 'r'):
00136 m = INCLUDE_GMOCK_FILE_REGEX.match(line)
00137 if m:
00138
00139 ProcessFile('include/' + m.group(1))
00140 else:
00141 m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
00142 if m:
00143
00144
00145
00146
00147
00148 if not gtest.GTEST_H_SEED in processed_files:
00149 processed_files.add(gtest.GTEST_H_SEED)
00150 output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
00151 else:
00152
00153 output_file.write(line)
00154
00155 ProcessFile(GMOCK_H_SEED)
00156 output_file.close()
00157
00158
00159 def FuseGMockAllCcToFile(gmock_root, output_file):
00160 """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
00161
00162 processed_files = sets.Set()
00163
00164 def ProcessFile(gmock_source_file):
00165 """Processes the given gmock source file."""
00166
00167
00168 if gmock_source_file in processed_files:
00169 return
00170
00171 processed_files.add(gmock_source_file)
00172
00173
00174 for line in file(os.path.join(gmock_root, gmock_source_file), 'r'):
00175 m = INCLUDE_GMOCK_FILE_REGEX.match(line)
00176 if m:
00177
00178
00179
00180
00181
00182 if not GMOCK_H_SEED in processed_files:
00183 processed_files.add(GMOCK_H_SEED)
00184 output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
00185 else:
00186 m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
00187 if m:
00188
00189
00190
00191 pass
00192 else:
00193 m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
00194 if m:
00195
00196 ProcessFile(m.group(1))
00197 else:
00198
00199 output_file.write(line)
00200
00201 ProcessFile(GMOCK_ALL_CC_SEED)
00202
00203
00204 def FuseGMockGTestAllCc(gmock_root, output_dir):
00205 """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
00206
00207 output_file = file(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT), 'w')
00208
00209 gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
00210
00211 FuseGMockAllCcToFile(gmock_root, output_file)
00212 output_file.close()
00213
00214
00215 def FuseGMock(gmock_root, output_dir):
00216 """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
00217
00218 ValidateGMockRootDir(gmock_root)
00219 ValidateOutputDir(output_dir)
00220
00221 gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
00222 FuseGMockH(gmock_root, output_dir)
00223 FuseGMockGTestAllCc(gmock_root, output_dir)
00224
00225
00226 def main():
00227 argc = len(sys.argv)
00228 if argc == 2:
00229
00230 FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
00231 elif argc == 3:
00232
00233 FuseGMock(sys.argv[1], sys.argv[2])
00234 else:
00235 print __doc__
00236 sys.exit(1)
00237
00238
00239 if __name__ == '__main__':
00240 main()