123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- """fuse_gmock_files.py v0.1.0.
- Fuses Google Mock and Google Test source code into two .h files and a .cc file.
- SYNOPSIS
- fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
- Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
- code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
- directory, and generates three files:
- OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
- OUTPUT_DIR/gmock-gtest-all.cc. Then you can build your tests
- by adding OUTPUT_DIR to the include search path and linking
- with OUTPUT_DIR/gmock-gtest-all.cc. These three files contain
- everything you need to use Google Mock. Hence you can
- "install" Google Mock by copying them to wherever you want.
- GMOCK_ROOT_DIR can be omitted and defaults to the parent
- directory of the directory holding this script.
- EXAMPLES
- ./fuse_gmock_files.py fused_gmock
- ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
- This tool is experimental. In particular, it assumes that there is no
- conditional inclusion of Google Mock or Google Test headers. Please
- report any problems to googlemock@googlegroups.com. You can read
- https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md
- for more
- information.
- """
- from __future__ import print_function
- import os
- import re
- import sys
- __author__ = 'wan@google.com (Zhanyong Wan)'
- DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
- sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
- import fuse_gtest_files as gtest
- INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
- GMOCK_H_SEED = 'include/gmock/gmock.h'
- GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
- GTEST_H_OUTPUT = 'gtest/gtest.h'
- GMOCK_H_OUTPUT = 'gmock/gmock.h'
- GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
- def GetGTestRootDir(gmock_root):
- """Returns the root directory of Google Test."""
- return os.path.join(gmock_root, '../googletest')
- def ValidateGMockRootDir(gmock_root):
- """Makes sure gmock_root points to a valid gmock root directory.
- The function aborts the program on failure.
- Args:
- gmock_root: A string with the mock root directory.
- """
- gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
- gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
- gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
- def ValidateOutputDir(output_dir):
- """Makes sure output_dir points to a valid output directory.
- The function aborts the program on failure.
- Args:
- output_dir: A string representing the output directory.
- """
- gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
- gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
- gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
- def FuseGMockH(gmock_root, output_dir):
- """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
- output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
- processed_files = set()
- def ProcessFile(gmock_header_path):
- """Processes the given gmock header file."""
-
- if gmock_header_path in processed_files:
- return
- processed_files.add(gmock_header_path)
-
- with open(os.path.join(gmock_root, gmock_header_path), 'r') as fh:
- for line in fh:
- m = INCLUDE_GMOCK_FILE_REGEX.match(line)
- if m:
-
-
- ProcessFile('include/' + m.group(1))
- else:
- m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
- if m:
-
-
-
-
- if gtest.GTEST_H_SEED not in processed_files:
- processed_files.add(gtest.GTEST_H_SEED)
- output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
- else:
-
- output_file.write(line)
- ProcessFile(GMOCK_H_SEED)
- output_file.close()
- def FuseGMockAllCcToFile(gmock_root, output_file):
- """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
- processed_files = set()
- def ProcessFile(gmock_source_file):
- """Processes the given gmock source file."""
-
- if gmock_source_file in processed_files:
- return
- processed_files.add(gmock_source_file)
-
- with open(os.path.join(gmock_root, gmock_source_file), 'r') as fh:
- for line in fh:
- m = INCLUDE_GMOCK_FILE_REGEX.match(line)
- if m:
-
-
-
-
-
-
- if GMOCK_H_SEED not in processed_files:
- processed_files.add(GMOCK_H_SEED)
- output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
- else:
- m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
- if m:
-
-
-
- pass
- else:
- m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
- if m:
-
- ProcessFile(m.group(1))
- else:
-
- output_file.write(line)
- ProcessFile(GMOCK_ALL_CC_SEED)
- def FuseGMockGTestAllCc(gmock_root, output_dir):
- """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
- with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
- 'w') as output_file:
-
- gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
-
- FuseGMockAllCcToFile(gmock_root, output_file)
- def FuseGMock(gmock_root, output_dir):
- """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
- ValidateGMockRootDir(gmock_root)
- ValidateOutputDir(output_dir)
- gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
- FuseGMockH(gmock_root, output_dir)
- FuseGMockGTestAllCc(gmock_root, output_dir)
- def main():
- argc = len(sys.argv)
- if argc == 2:
-
- FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
- elif argc == 3:
-
- FuseGMock(sys.argv[1], sys.argv[2])
- else:
- print(__doc__)
- sys.exit(1)
- if __name__ == '__main__':
- main()
|