32 """fuse_gtest_files.py v0.2.0
33 Fuses Google Test source code into a .h file and a .cc file.
36 fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR
38 Scans GTEST_ROOT_DIR for Google Test source code, and generates
39 two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc.
40 Then you can build your tests by adding OUTPUT_DIR to the include
41 search path and linking with OUTPUT_DIR/gtest/gtest-all.cc. These
42 two files contain everything you need to use Google Test. Hence
43 you can "install" Google Test by copying them to wherever you want.
45 GTEST_ROOT_DIR can be omitted and defaults to the parent
46 directory of the directory holding this script.
49 ./fuse_gtest_files.py fused_gtest
50 ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest
52 This tool is experimental. In particular, it assumes that there is no
53 conditional inclusion of Google Test headers. Please report any
54 problems to googletestframework@googlegroups.com. You can read
55 http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for
59 __author__ =
'wan@google.com (Zhanyong Wan)'
64 from sets
import Set
as set
71 DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__),
'..')
74 INCLUDE_GTEST_FILE_REGEX = re.compile(
r'^\s*#\s*include\s*"(gtest/.+)"')
77 INCLUDE_SRC_FILE_REGEX = re.compile(
r'^\s*#\s*include\s*"(src/.+)"')
80 GTEST_H_SEED =
'include/gtest/gtest.h'
81 GTEST_SPI_H_SEED =
'include/gtest/gtest-spi.h'
82 GTEST_ALL_CC_SEED =
'src/gtest-all.cc'
85 GTEST_H_OUTPUT =
'gtest/gtest.h'
86 GTEST_ALL_CC_OUTPUT =
'gtest/gtest-all.cc'
90 """Verifies that the given file exists; aborts on failure.
92 relative_path is the file path relative to the given directory.
95 if not os.path.isfile(os.path.join(directory, relative_path)):
96 print(
'ERROR: Cannot find %s in directory %s.' % (relative_path,
98 print(
'Please either specify a valid project root directory '
99 'or omit it on the command line.')
104 """Makes sure gtest_root points to a valid gtest root directory.
106 The function aborts the program on failure.
114 """Verifies that the given output file path is valid.
116 relative_path is relative to the output_dir directory.
120 output_file = os.path.join(output_dir, relative_path)
121 if os.path.exists(output_file):
125 print(
'%s already exists in directory %s - overwrite it? (y/N) ' %
126 (relative_path, output_dir))
127 answer = sys.stdin.readline().strip()
128 if answer
not in [
'y',
'Y']:
134 parent_directory = os.path.dirname(output_file)
135 if not os.path.isdir(parent_directory):
136 os.makedirs(parent_directory)
140 """Makes sure output_dir points to a valid output directory.
142 The function aborts the program on failure.
150 """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
152 output_file =
open(os.path.join(output_dir, GTEST_H_OUTPUT),
'w')
153 processed_files =
set()
155 def ProcessFile(gtest_header_path):
156 """Processes the given gtest header file."""
159 if gtest_header_path
in processed_files:
162 processed_files.add(gtest_header_path)
165 for line
in open(os.path.join(gtest_root, gtest_header_path),
'r'):
166 m = INCLUDE_GTEST_FILE_REGEX.match(line)
169 ProcessFile(
'include/' + m.group(1))
172 output_file.write(line)
174 ProcessFile(GTEST_H_SEED)
179 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
181 processed_files =
set()
183 def ProcessFile(gtest_source_file):
184 """Processes the given gtest source file."""
187 if gtest_source_file
in processed_files:
190 processed_files.add(gtest_source_file)
193 for line
in open(os.path.join(gtest_root, gtest_source_file),
'r'):
194 m = INCLUDE_GTEST_FILE_REGEX.match(line)
196 if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
199 ProcessFile(GTEST_SPI_H_SEED)
207 if not GTEST_H_SEED
in processed_files:
208 processed_files.add(GTEST_H_SEED)
209 output_file.write(
'#include "%s"\n' % (GTEST_H_OUTPUT,))
211 m = INCLUDE_SRC_FILE_REGEX.match(line)
214 ProcessFile(m.group(1))
216 output_file.write(line)
218 ProcessFile(GTEST_ALL_CC_SEED)
222 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
224 output_file =
open(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT),
'w')
230 """Fuses gtest.h and gtest-all.cc."""
243 FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
252 if __name__ ==
'__main__':