fuse_gtest_files.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009, Google Inc.
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. #
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following disclaimer
  14. # in the documentation and/or other materials provided with the
  15. # distribution.
  16. # * Neither the name of Google Inc. nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """fuse_gtest_files.py v0.2.0
  32. Fuses Google Test source code into a .h file and a .cc file.
  33. SYNOPSIS
  34. fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR
  35. Scans GTEST_ROOT_DIR for Google Test source code, and generates
  36. two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc.
  37. Then you can build your tests by adding OUTPUT_DIR to the include
  38. search path and linking with OUTPUT_DIR/gtest/gtest-all.cc. These
  39. two files contain everything you need to use Google Test. Hence
  40. you can "install" Google Test by copying them to wherever you want.
  41. GTEST_ROOT_DIR can be omitted and defaults to the parent
  42. directory of the directory holding this script.
  43. EXAMPLES
  44. ./fuse_gtest_files.py fused_gtest
  45. ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest
  46. This tool is experimental. In particular, it assumes that there is no
  47. conditional inclusion of Google Test headers. Please report any
  48. problems to googletestframework@googlegroups.com. You can read
  49. https://github.com/google/googletest/blob/master/googletest/docs/advanced.md for
  50. more information.
  51. """
  52. __author__ = 'wan@google.com (Zhanyong Wan)'
  53. import os
  54. import re
  55. try:
  56. from sets import Set as set # For Python 2.3 compatibility
  57. except ImportError:
  58. pass
  59. import sys
  60. # We assume that this file is in the scripts/ directory in the Google
  61. # Test root directory.
  62. DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
  63. # Regex for matching '#include "gtest/..."'.
  64. INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
  65. # Regex for matching '#include "src/..."'.
  66. INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
  67. # Where to find the source seed files.
  68. GTEST_H_SEED = 'include/gtest/gtest.h'
  69. GTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h'
  70. GTEST_ALL_CC_SEED = 'src/gtest-all.cc'
  71. # Where to put the generated files.
  72. GTEST_H_OUTPUT = 'gtest/gtest.h'
  73. GTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc'
  74. def VerifyFileExists(directory, relative_path):
  75. """Verifies that the given file exists; aborts on failure.
  76. relative_path is the file path relative to the given directory.
  77. """
  78. if not os.path.isfile(os.path.join(directory, relative_path)):
  79. print('ERROR: Cannot find %s in directory %s.' % (relative_path,
  80. directory))
  81. print('Please either specify a valid project root directory '
  82. 'or omit it on the command line.')
  83. sys.exit(1)
  84. def ValidateGTestRootDir(gtest_root):
  85. """Makes sure gtest_root points to a valid gtest root directory.
  86. The function aborts the program on failure.
  87. """
  88. VerifyFileExists(gtest_root, GTEST_H_SEED)
  89. VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED)
  90. def VerifyOutputFile(output_dir, relative_path):
  91. """Verifies that the given output file path is valid.
  92. relative_path is relative to the output_dir directory.
  93. """
  94. # Makes sure the output file either doesn't exist or can be overwritten.
  95. output_file = os.path.join(output_dir, relative_path)
  96. if os.path.exists(output_file):
  97. # TODO(wan@google.com): The following user-interaction doesn't
  98. # work with automated processes. We should provide a way for the
  99. # Makefile to force overwriting the files.
  100. print('%s already exists in directory %s - overwrite it? (y/N) ' %
  101. (relative_path, output_dir))
  102. answer = sys.stdin.readline().strip()
  103. if answer not in ['y', 'Y']:
  104. print('ABORTED.')
  105. sys.exit(1)
  106. # Makes sure the directory holding the output file exists; creates
  107. # it and all its ancestors if necessary.
  108. parent_directory = os.path.dirname(output_file)
  109. if not os.path.isdir(parent_directory):
  110. os.makedirs(parent_directory)
  111. def ValidateOutputDir(output_dir):
  112. """Makes sure output_dir points to a valid output directory.
  113. The function aborts the program on failure.
  114. """
  115. VerifyOutputFile(output_dir, GTEST_H_OUTPUT)
  116. VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT)
  117. def FuseGTestH(gtest_root, output_dir):
  118. """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
  119. output_file = open(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
  120. processed_files = set() # Holds all gtest headers we've processed.
  121. def ProcessFile(gtest_header_path):
  122. """Processes the given gtest header file."""
  123. # We don't process the same header twice.
  124. if gtest_header_path in processed_files:
  125. return
  126. processed_files.add(gtest_header_path)
  127. # Reads each line in the given gtest header.
  128. for line in open(os.path.join(gtest_root, gtest_header_path), 'r'):
  129. m = INCLUDE_GTEST_FILE_REGEX.match(line)
  130. if m:
  131. # It's '#include "gtest/..."' - let's process it recursively.
  132. ProcessFile('include/' + m.group(1))
  133. else:
  134. # Otherwise we copy the line unchanged to the output file.
  135. output_file.write(line)
  136. ProcessFile(GTEST_H_SEED)
  137. output_file.close()
  138. def FuseGTestAllCcToFile(gtest_root, output_file):
  139. """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
  140. processed_files = set()
  141. def ProcessFile(gtest_source_file):
  142. """Processes the given gtest source file."""
  143. # We don't process the same #included file twice.
  144. if gtest_source_file in processed_files:
  145. return
  146. processed_files.add(gtest_source_file)
  147. # Reads each line in the given gtest source file.
  148. for line in open(os.path.join(gtest_root, gtest_source_file), 'r'):
  149. m = INCLUDE_GTEST_FILE_REGEX.match(line)
  150. if m:
  151. if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
  152. # It's '#include "gtest/gtest-spi.h"'. This file is not
  153. # #included by "gtest/gtest.h", so we need to process it.
  154. ProcessFile(GTEST_SPI_H_SEED)
  155. else:
  156. # It's '#include "gtest/foo.h"' where foo is not gtest-spi.
  157. # We treat it as '#include "gtest/gtest.h"', as all other
  158. # gtest headers are being fused into gtest.h and cannot be
  159. # #included directly.
  160. # There is no need to #include "gtest/gtest.h" more than once.
  161. if not GTEST_H_SEED in processed_files:
  162. processed_files.add(GTEST_H_SEED)
  163. output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,))
  164. else:
  165. m = INCLUDE_SRC_FILE_REGEX.match(line)
  166. if m:
  167. # It's '#include "src/foo"' - let's process it recursively.
  168. ProcessFile(m.group(1))
  169. else:
  170. output_file.write(line)
  171. ProcessFile(GTEST_ALL_CC_SEED)
  172. def FuseGTestAllCc(gtest_root, output_dir):
  173. """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
  174. output_file = open(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
  175. FuseGTestAllCcToFile(gtest_root, output_file)
  176. output_file.close()
  177. def FuseGTest(gtest_root, output_dir):
  178. """Fuses gtest.h and gtest-all.cc."""
  179. ValidateGTestRootDir(gtest_root)
  180. ValidateOutputDir(output_dir)
  181. FuseGTestH(gtest_root, output_dir)
  182. FuseGTestAllCc(gtest_root, output_dir)
  183. def main():
  184. argc = len(sys.argv)
  185. if argc == 2:
  186. # fuse_gtest_files.py OUTPUT_DIR
  187. FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
  188. elif argc == 3:
  189. # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
  190. FuseGTest(sys.argv[1], sys.argv[2])
  191. else:
  192. print(__doc__)
  193. sys.exit(1)
  194. if __name__ == '__main__':
  195. main()