fuse_gmock_files.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_gmock_files.py v0.1.0
  32. Fuses Google Mock and Google Test source code into two .h files and a .cc file.
  33. SYNOPSIS
  34. fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
  35. Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
  36. code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
  37. directory, and generates three files:
  38. OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
  39. OUTPUT_DIR/gmock-gtest-all.cc. Then you can build your tests
  40. by adding OUTPUT_DIR to the include search path and linking
  41. with OUTPUT_DIR/gmock-gtest-all.cc. These three files contain
  42. everything you need to use Google Mock. Hence you can
  43. "install" Google Mock by copying them to wherever you want.
  44. GMOCK_ROOT_DIR can be omitted and defaults to the parent
  45. directory of the directory holding this script.
  46. EXAMPLES
  47. ./fuse_gmock_files.py fused_gmock
  48. ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
  49. This tool is experimental. In particular, it assumes that there is no
  50. conditional inclusion of Google Mock or Google Test headers. Please
  51. report any problems to googlemock@googlegroups.com. You can read
  52. https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md for more
  53. information.
  54. """
  55. __author__ = 'wan@google.com (Zhanyong Wan)'
  56. import os
  57. import re
  58. import sets
  59. import sys
  60. # We assume that this file is in the scripts/ directory in the Google
  61. # Mock root directory.
  62. DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
  63. # We need to call into googletest/scripts/fuse_gtest_files.py.
  64. sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
  65. import fuse_gtest_files
  66. gtest = fuse_gtest_files
  67. # Regex for matching '#include "gmock/..."'.
  68. INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
  69. # Where to find the source seed files.
  70. GMOCK_H_SEED = 'include/gmock/gmock.h'
  71. GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
  72. # Where to put the generated files.
  73. GTEST_H_OUTPUT = 'gtest/gtest.h'
  74. GMOCK_H_OUTPUT = 'gmock/gmock.h'
  75. GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
  76. def GetGTestRootDir(gmock_root):
  77. """Returns the root directory of Google Test."""
  78. return os.path.join(gmock_root, '../googletest')
  79. def ValidateGMockRootDir(gmock_root):
  80. """Makes sure gmock_root points to a valid gmock root directory.
  81. The function aborts the program on failure.
  82. """
  83. gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
  84. gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
  85. gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
  86. def ValidateOutputDir(output_dir):
  87. """Makes sure output_dir points to a valid output directory.
  88. The function aborts the program on failure.
  89. """
  90. gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
  91. gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
  92. gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
  93. def FuseGMockH(gmock_root, output_dir):
  94. """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
  95. output_file = file(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
  96. processed_files = sets.Set() # Holds all gmock headers we've processed.
  97. def ProcessFile(gmock_header_path):
  98. """Processes the given gmock header file."""
  99. # We don't process the same header twice.
  100. if gmock_header_path in processed_files:
  101. return
  102. processed_files.add(gmock_header_path)
  103. # Reads each line in the given gmock header.
  104. for line in file(os.path.join(gmock_root, gmock_header_path), 'r'):
  105. m = INCLUDE_GMOCK_FILE_REGEX.match(line)
  106. if m:
  107. # It's '#include "gmock/..."' - let's process it recursively.
  108. ProcessFile('include/' + m.group(1))
  109. else:
  110. m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
  111. if m:
  112. # It's '#include "gtest/foo.h"'. We translate it to
  113. # "gtest/gtest.h", regardless of what foo is, since all
  114. # gtest headers are fused into gtest/gtest.h.
  115. # There is no need to #include gtest.h twice.
  116. if not gtest.GTEST_H_SEED in processed_files:
  117. processed_files.add(gtest.GTEST_H_SEED)
  118. output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
  119. else:
  120. # Otherwise we copy the line unchanged to the output file.
  121. output_file.write(line)
  122. ProcessFile(GMOCK_H_SEED)
  123. output_file.close()
  124. def FuseGMockAllCcToFile(gmock_root, output_file):
  125. """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
  126. processed_files = sets.Set()
  127. def ProcessFile(gmock_source_file):
  128. """Processes the given gmock source file."""
  129. # We don't process the same #included file twice.
  130. if gmock_source_file in processed_files:
  131. return
  132. processed_files.add(gmock_source_file)
  133. # Reads each line in the given gmock source file.
  134. for line in file(os.path.join(gmock_root, gmock_source_file), 'r'):
  135. m = INCLUDE_GMOCK_FILE_REGEX.match(line)
  136. if m:
  137. # It's '#include "gmock/foo.h"'. We treat it as '#include
  138. # "gmock/gmock.h"', as all other gmock headers are being fused
  139. # into gmock.h and cannot be #included directly.
  140. # There is no need to #include "gmock/gmock.h" more than once.
  141. if not GMOCK_H_SEED in processed_files:
  142. processed_files.add(GMOCK_H_SEED)
  143. output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
  144. else:
  145. m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
  146. if m:
  147. # It's '#include "gtest/..."'.
  148. # There is no need to #include gtest.h as it has been
  149. # #included by gtest-all.cc.
  150. pass
  151. else:
  152. m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
  153. if m:
  154. # It's '#include "src/foo"' - let's process it recursively.
  155. ProcessFile(m.group(1))
  156. else:
  157. # Otherwise we copy the line unchanged to the output file.
  158. output_file.write(line)
  159. ProcessFile(GMOCK_ALL_CC_SEED)
  160. def FuseGMockGTestAllCc(gmock_root, output_dir):
  161. """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
  162. output_file = file(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT), 'w')
  163. # First, fuse gtest-all.cc into gmock-gtest-all.cc.
  164. gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
  165. # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
  166. FuseGMockAllCcToFile(gmock_root, output_file)
  167. output_file.close()
  168. def FuseGMock(gmock_root, output_dir):
  169. """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
  170. ValidateGMockRootDir(gmock_root)
  171. ValidateOutputDir(output_dir)
  172. gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
  173. FuseGMockH(gmock_root, output_dir)
  174. FuseGMockGTestAllCc(gmock_root, output_dir)
  175. def main():
  176. argc = len(sys.argv)
  177. if argc == 2:
  178. # fuse_gmock_files.py OUTPUT_DIR
  179. FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
  180. elif argc == 3:
  181. # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
  182. FuseGMock(sys.argv[1], sys.argv[2])
  183. else:
  184. print __doc__
  185. sys.exit(1)
  186. if __name__ == '__main__':
  187. main()