fuse_gmock_files.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/docs/gmock_cook_book.md
  53. for more
  54. information.
  55. """
  56. from __future__ import print_function
  57. import os
  58. import re
  59. import sys
  60. __author__ = 'wan@google.com (Zhanyong Wan)'
  61. # We assume that this file is in the scripts/ directory in the Google
  62. # Mock root directory.
  63. DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
  64. # We need to call into googletest/scripts/fuse_gtest_files.py.
  65. sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
  66. import fuse_gtest_files as gtest # pylint:disable=g-import-not-at-top
  67. # Regex for matching
  68. # '#include "gmock/..."'.
  69. INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
  70. # Where to find the source seed files.
  71. GMOCK_H_SEED = 'include/gmock/gmock.h'
  72. GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
  73. # Where to put the generated files.
  74. GTEST_H_OUTPUT = 'gtest/gtest.h'
  75. GMOCK_H_OUTPUT = 'gmock/gmock.h'
  76. GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
  77. def GetGTestRootDir(gmock_root):
  78. """Returns the root directory of Google Test."""
  79. return os.path.join(gmock_root, '../googletest')
  80. def ValidateGMockRootDir(gmock_root):
  81. """Makes sure gmock_root points to a valid gmock root directory.
  82. The function aborts the program on failure.
  83. Args:
  84. gmock_root: A string with the mock root directory.
  85. """
  86. gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
  87. gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
  88. gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
  89. def ValidateOutputDir(output_dir):
  90. """Makes sure output_dir points to a valid output directory.
  91. The function aborts the program on failure.
  92. Args:
  93. output_dir: A string representing the output directory.
  94. """
  95. gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
  96. gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
  97. gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
  98. def FuseGMockH(gmock_root, output_dir):
  99. """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
  100. output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
  101. processed_files = set() # Holds all gmock headers we've processed.
  102. def ProcessFile(gmock_header_path):
  103. """Processes the given gmock header file."""
  104. # We don't process the same header twice.
  105. if gmock_header_path in processed_files:
  106. return
  107. processed_files.add(gmock_header_path)
  108. # Reads each line in the given gmock header.
  109. with open(os.path.join(gmock_root, gmock_header_path), 'r') as fh:
  110. for line in fh:
  111. m = INCLUDE_GMOCK_FILE_REGEX.match(line)
  112. if m:
  113. # '#include "gmock/..."'
  114. # - let's process it recursively.
  115. ProcessFile('include/' + m.group(1))
  116. else:
  117. m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
  118. if m:
  119. # '#include "gtest/foo.h"'
  120. # We translate it to "gtest/gtest.h", regardless of what foo is,
  121. # since all gtest headers are fused into gtest/gtest.h.
  122. # There is no need to #include gtest.h twice.
  123. if gtest.GTEST_H_SEED not in processed_files:
  124. processed_files.add(gtest.GTEST_H_SEED)
  125. output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
  126. else:
  127. # Otherwise we copy the line unchanged to the output file.
  128. output_file.write(line)
  129. ProcessFile(GMOCK_H_SEED)
  130. output_file.close()
  131. def FuseGMockAllCcToFile(gmock_root, output_file):
  132. """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
  133. processed_files = set()
  134. def ProcessFile(gmock_source_file):
  135. """Processes the given gmock source file."""
  136. # We don't process the same #included file twice.
  137. if gmock_source_file in processed_files:
  138. return
  139. processed_files.add(gmock_source_file)
  140. # Reads each line in the given gmock source file.
  141. with open(os.path.join(gmock_root, gmock_source_file), 'r') as fh:
  142. for line in fh:
  143. m = INCLUDE_GMOCK_FILE_REGEX.match(line)
  144. if m:
  145. # '#include "gmock/foo.h"'
  146. # We treat it as '#include "gmock/gmock.h"', as all other gmock
  147. # headers are being fused into gmock.h and cannot be
  148. # included directly. No need to
  149. # #include "gmock/gmock.h"
  150. # more than once.
  151. if GMOCK_H_SEED not in processed_files:
  152. processed_files.add(GMOCK_H_SEED)
  153. output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
  154. else:
  155. m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
  156. if m:
  157. # '#include "gtest/..."'
  158. # There is no need to #include gtest.h as it has been
  159. # #included by gtest-all.cc.
  160. pass
  161. else:
  162. m = gtest.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. # Otherwise we copy the line unchanged to the output file.
  168. output_file.write(line)
  169. ProcessFile(GMOCK_ALL_CC_SEED)
  170. def FuseGMockGTestAllCc(gmock_root, output_dir):
  171. """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
  172. with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
  173. 'w') as output_file:
  174. # First, fuse gtest-all.cc into gmock-gtest-all.cc.
  175. gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
  176. # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
  177. FuseGMockAllCcToFile(gmock_root, output_file)
  178. def FuseGMock(gmock_root, output_dir):
  179. """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
  180. ValidateGMockRootDir(gmock_root)
  181. ValidateOutputDir(output_dir)
  182. gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
  183. FuseGMockH(gmock_root, output_dir)
  184. FuseGMockGTestAllCc(gmock_root, output_dir)
  185. def main():
  186. argc = len(sys.argv)
  187. if argc == 2:
  188. # fuse_gmock_files.py OUTPUT_DIR
  189. FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
  190. elif argc == 3:
  191. # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
  192. FuseGMock(sys.argv[1], sys.argv[2])
  193. else:
  194. print(__doc__)
  195. sys.exit(1)
  196. if __name__ == '__main__':
  197. main()