fuse_gtest_files.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for
  50. more information.
  51. """
  52. __author__ = 'wan@google.com (Zhanyong Wan)'
  53. import os
  54. import re
  55. import sets
  56. import sys
  57. # We assume that this file is in the scripts/ directory in the Google
  58. # Test root directory.
  59. DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
  60. # Regex for matching '#include "gtest/..."'.
  61. INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
  62. # Regex for matching '#include "src/..."'.
  63. INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
  64. # Where to find the source seed files.
  65. GTEST_H_SEED = 'include/gtest/gtest.h'
  66. GTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h'
  67. GTEST_ALL_CC_SEED = 'src/gtest-all.cc'
  68. # Where to put the generated files.
  69. GTEST_H_OUTPUT = 'gtest/gtest.h'
  70. GTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc'
  71. def VerifyFileExists(directory, relative_path):
  72. """Verifies that the given file exists; aborts on failure.
  73. relative_path is the file path relative to the given directory.
  74. """
  75. if not os.path.isfile(os.path.join(directory, relative_path)):
  76. print 'ERROR: Cannot find %s in directory %s.' % (relative_path,
  77. directory)
  78. print ('Please either specify a valid project root directory '
  79. 'or omit it on the command line.')
  80. sys.exit(1)
  81. def ValidateGTestRootDir(gtest_root):
  82. """Makes sure gtest_root points to a valid gtest root directory.
  83. The function aborts the program on failure.
  84. """
  85. VerifyFileExists(gtest_root, GTEST_H_SEED)
  86. VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED)
  87. def VerifyOutputFile(output_dir, relative_path):
  88. """Verifies that the given output file path is valid.
  89. relative_path is relative to the output_dir directory.
  90. """
  91. # Makes sure the output file either doesn't exist or can be overwritten.
  92. output_file = os.path.join(output_dir, relative_path)
  93. if os.path.exists(output_file):
  94. # TODO(wan@google.com): The following user-interaction doesn't
  95. # work with automated processes. We should provide a way for the
  96. # Makefile to force overwriting the files.
  97. print ('%s already exists in directory %s - overwrite it? (y/N) ' %
  98. (relative_path, output_dir))
  99. answer = sys.stdin.readline().strip()
  100. if answer not in ['y', 'Y']:
  101. print 'ABORTED.'
  102. sys.exit(1)
  103. # Makes sure the directory holding the output file exists; creates
  104. # it and all its ancestors if necessary.
  105. parent_directory = os.path.dirname(output_file)
  106. if not os.path.isdir(parent_directory):
  107. os.makedirs(parent_directory)
  108. def ValidateOutputDir(output_dir):
  109. """Makes sure output_dir points to a valid output directory.
  110. The function aborts the program on failure.
  111. """
  112. VerifyOutputFile(output_dir, GTEST_H_OUTPUT)
  113. VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT)
  114. def FuseGTestH(gtest_root, output_dir):
  115. """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
  116. output_file = file(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
  117. processed_files = sets.Set() # Holds all gtest headers we've processed.
  118. def ProcessFile(gtest_header_path):
  119. """Processes the given gtest header file."""
  120. # We don't process the same header twice.
  121. if gtest_header_path in processed_files:
  122. return
  123. processed_files.add(gtest_header_path)
  124. # Reads each line in the given gtest header.
  125. for line in file(os.path.join(gtest_root, gtest_header_path), 'r'):
  126. m = INCLUDE_GTEST_FILE_REGEX.match(line)
  127. if m:
  128. # It's '#include "gtest/..."' - let's process it recursively.
  129. ProcessFile('include/' + m.group(1))
  130. else:
  131. # Otherwise we copy the line unchanged to the output file.
  132. output_file.write(line)
  133. ProcessFile(GTEST_H_SEED)
  134. output_file.close()
  135. def FuseGTestAllCcToFile(gtest_root, output_file):
  136. """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
  137. processed_files = sets.Set()
  138. def ProcessFile(gtest_source_file):
  139. """Processes the given gtest source file."""
  140. # We don't process the same #included file twice.
  141. if gtest_source_file in processed_files:
  142. return
  143. processed_files.add(gtest_source_file)
  144. # Reads each line in the given gtest source file.
  145. for line in file(os.path.join(gtest_root, gtest_source_file), 'r'):
  146. m = INCLUDE_GTEST_FILE_REGEX.match(line)
  147. if m:
  148. if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
  149. # It's '#include "gtest/gtest-spi.h"'. This file is not
  150. # #included by "gtest/gtest.h", so we need to process it.
  151. ProcessFile(GTEST_SPI_H_SEED)
  152. else:
  153. # It's '#include "gtest/foo.h"' where foo is not gtest-spi.
  154. # We treat it as '#include "gtest/gtest.h"', as all other
  155. # gtest headers are being fused into gtest.h and cannot be
  156. # #included directly.
  157. # There is no need to #include "gtest/gtest.h" more than once.
  158. if not GTEST_H_SEED in processed_files:
  159. processed_files.add(GTEST_H_SEED)
  160. output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,))
  161. else:
  162. m = INCLUDE_SRC_FILE_REGEX.match(line)
  163. if m:
  164. # It's '#include "src/foo"' - let's process it recursively.
  165. ProcessFile(m.group(1))
  166. else:
  167. output_file.write(line)
  168. ProcessFile(GTEST_ALL_CC_SEED)
  169. def FuseGTestAllCc(gtest_root, output_dir):
  170. """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
  171. output_file = file(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
  172. FuseGTestAllCcToFile(gtest_root, output_file)
  173. output_file.close()
  174. def FuseGTest(gtest_root, output_dir):
  175. """Fuses gtest.h and gtest-all.cc."""
  176. ValidateGTestRootDir(gtest_root)
  177. ValidateOutputDir(output_dir)
  178. FuseGTestH(gtest_root, output_dir)
  179. FuseGTestAllCc(gtest_root, output_dir)
  180. def main():
  181. argc = len(sys.argv)
  182. if argc == 2:
  183. # fuse_gtest_files.py OUTPUT_DIR
  184. FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
  185. elif argc == 3:
  186. # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
  187. FuseGTest(sys.argv[1], sys.argv[2])
  188. else:
  189. print __doc__
  190. sys.exit(1)
  191. if __name__ == '__main__':
  192. main()