| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
"""fuse_gmock_files.py v0.1.0 |
| 33 |
Fuses Google Mock and Google Test source code into two .h files and a .cc file. |
| 34 |
|
| 35 |
SYNOPSIS |
| 36 |
fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR |
| 37 |
|
| 38 |
Scans GMOCK_ROOT_DIR for Google Mock and Google Test source |
| 39 |
code, assuming Google Test is in the GMOCK_ROOT_DIR/gtest |
| 40 |
sub-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. |
| 47 |
|
| 48 |
GMOCK_ROOT_DIR can be omitted and defaults to the parent |
| 49 |
directory of the directory holding this script. |
| 50 |
|
| 51 |
EXAMPLES |
| 52 |
./fuse_gmock_files.py fused_gmock |
| 53 |
./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock |
| 54 |
|
| 55 |
This tool is experimental. In particular, it assumes that there is no |
| 56 |
conditional inclusion of Google Mock or Google Test headers. Please |
| 57 |
report any problems to [email protected]. You can read |
| 58 |
http://code.google.com/p/googlemock/wiki/CookBook for more |
| 59 |
information. |
| 60 |
""" |
| 61 |
|
| 62 |
__author__ = '[email protected] (Zhanyong Wan)' |
| 63 |
|
| 64 |
import os |
| 65 |
import re |
| 66 |
import sets |
| 67 |
import sys |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..') |
| 72 |
|
| 73 |
|
| 74 |
sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, 'gtest/scripts')) |
| 75 |
import fuse_gtest_files |
| 76 |
gtest = fuse_gtest_files |
| 77 |
|
| 78 |
|
| 79 |
INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"') |
| 80 |
|
| 81 |
|
| 82 |
GMOCK_H_SEED = 'include/gmock/gmock.h' |
| 83 |
GMOCK_ALL_CC_SEED = 'src/gmock-all.cc' |
| 84 |
|
| 85 |
|
| 86 |
GTEST_H_OUTPUT = 'gtest/gtest.h' |
| 87 |
GMOCK_H_OUTPUT = 'gmock/gmock.h' |
| 88 |
GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc' |
| 89 |
|
| 90 |
|
| 91 |
def GetGTestRootDir(gmock_root): |
| 92 |
"""Returns the root directory of Google Test.""" |
| 93 |
|
| 94 |
return os.path.join(gmock_root, 'gtest') |
| 95 |
|
| 96 |
|
| 97 |
def ValidateGMockRootDir(gmock_root): |
| 98 |
"""Makes sure gmock_root points to a valid gmock root directory. |
| 99 |
|
| 100 |
The function aborts the program on failure. |
| 101 |
""" |
| 102 |
|
| 103 |
gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root)) |
| 104 |
gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED) |
| 105 |
gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED) |
| 106 |
|
| 107 |
|
| 108 |
def ValidateOutputDir(output_dir): |
| 109 |
"""Makes sure output_dir points to a valid output directory. |
| 110 |
|
| 111 |
The function aborts the program on failure. |
| 112 |
""" |
| 113 |
|
| 114 |
gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT) |
| 115 |
gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT) |
| 116 |
gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT) |
| 117 |
|
| 118 |
|
| 119 |
def FuseGMockH(gmock_root, output_dir): |
| 120 |
"""Scans folder gmock_root to generate gmock/gmock.h in output_dir.""" |
| 121 |
|
| 122 |
output_file = file(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w') |
| 123 |
processed_files = sets.Set() |
| 124 |
|
| 125 |
def ProcessFile(gmock_header_path): |
| 126 |
"""Processes the given gmock header file.""" |
| 127 |
|
| 128 |
|
| 129 |
if gmock_header_path in processed_files: |
| 130 |
return |
| 131 |
|
| 132 |
processed_files.add(gmock_header_path) |
| 133 |
|
| 134 |
|
| 135 |
for line in file(os.path.join(gmock_root, gmock_header_path), 'r'): |
| 136 |
m = INCLUDE_GMOCK_FILE_REGEX.match(line) |
| 137 |
if m: |
| 138 |
|
| 139 |
ProcessFile('include/' + m.group(1)) |
| 140 |
else: |
| 141 |
m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line) |
| 142 |
if m: |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if not gtest.GTEST_H_SEED in processed_files: |
| 149 |
processed_files.add(gtest.GTEST_H_SEED) |
| 150 |
output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,)) |
| 151 |
else: |
| 152 |
|
| 153 |
output_file.write(line) |
| 154 |
|
| 155 |
ProcessFile(GMOCK_H_SEED) |
| 156 |
output_file.close() |
| 157 |
|
| 158 |
|
| 159 |
def FuseGMockAllCcToFile(gmock_root, output_file): |
| 160 |
"""Scans folder gmock_root to fuse gmock-all.cc into output_file.""" |
| 161 |
|
| 162 |
processed_files = sets.Set() |
| 163 |
|
| 164 |
def ProcessFile(gmock_source_file): |
| 165 |
"""Processes the given gmock source file.""" |
| 166 |
|
| 167 |
|
| 168 |
if gmock_source_file in processed_files: |
| 169 |
return |
| 170 |
|
| 171 |
processed_files.add(gmock_source_file) |
| 172 |
|
| 173 |
|
| 174 |
for line in file(os.path.join(gmock_root, gmock_source_file), 'r'): |
| 175 |
m = INCLUDE_GMOCK_FILE_REGEX.match(line) |
| 176 |
if m: |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if not GMOCK_H_SEED in processed_files: |
| 183 |
processed_files.add(GMOCK_H_SEED) |
| 184 |
output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,)) |
| 185 |
else: |
| 186 |
m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line) |
| 187 |
if m: |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
pass |
| 192 |
else: |
| 193 |
m = gtest.INCLUDE_SRC_FILE_REGEX.match(line) |
| 194 |
if m: |
| 195 |
|
| 196 |
ProcessFile(m.group(1)) |
| 197 |
else: |
| 198 |
|
| 199 |
output_file.write(line) |
| 200 |
|
| 201 |
ProcessFile(GMOCK_ALL_CC_SEED) |
| 202 |
|
| 203 |
|
| 204 |
def FuseGMockGTestAllCc(gmock_root, output_dir): |
| 205 |
"""Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir.""" |
| 206 |
|
| 207 |
output_file = file(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT), 'w') |
| 208 |
|
| 209 |
gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file) |
| 210 |
|
| 211 |
FuseGMockAllCcToFile(gmock_root, output_file) |
| 212 |
output_file.close() |
| 213 |
|
| 214 |
|
| 215 |
def FuseGMock(gmock_root, output_dir): |
| 216 |
"""Fuses gtest.h, gmock.h, and gmock-gtest-all.h.""" |
| 217 |
|
| 218 |
ValidateGMockRootDir(gmock_root) |
| 219 |
ValidateOutputDir(output_dir) |
| 220 |
|
| 221 |
gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir) |
| 222 |
FuseGMockH(gmock_root, output_dir) |
| 223 |
FuseGMockGTestAllCc(gmock_root, output_dir) |
| 224 |
|
| 225 |
|
| 226 |
def main(): |
| 227 |
argc = len(sys.argv) |
| 228 |
if argc == 2: |
| 229 |
|
| 230 |
FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1]) |
| 231 |
elif argc == 3: |
| 232 |
|
| 233 |
FuseGMock(sys.argv[1], sys.argv[2]) |
| 234 |
else: |
| 235 |
print __doc__ |
| 236 |
sys.exit(1) |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == '__main__': |
| 240 |
main() |