Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:53

0001 #!/usr/bin/env python
0002 #
0003 # Copyright 2009, Google Inc.
0004 # All rights reserved.
0005 #
0006 # Redistribution and use in source and binary forms, with or without
0007 # modification, are permitted provided that the following conditions are
0008 # met:
0009 #
0010 #     * Redistributions of source code must retain the above copyright
0011 # notice, this list of conditions and the following disclaimer.
0012 #     * Redistributions in binary form must reproduce the above
0013 # copyright notice, this list of conditions and the following disclaimer
0014 # in the documentation and/or other materials provided with the
0015 # distribution.
0016 #     * Neither the name of Google Inc. nor the names of its
0017 # contributors may be used to endorse or promote products derived from
0018 # this software without specific prior written permission.
0019 #
0020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0023 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0024 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0025 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0026 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0027 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0028 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0029 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0031 
0032 """fuse_gmock_files.py v0.1.0
0033 Fuses Google Mock and Google Test source code into two .h files and a .cc file.
0034 
0035 SYNOPSIS
0036        fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
0037 
0038        Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
0039        code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
0040        directory, and generates three files:
0041        OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
0042        OUTPUT_DIR/gmock-gtest-all.cc.  Then you can build your tests
0043        by adding OUTPUT_DIR to the include search path and linking
0044        with OUTPUT_DIR/gmock-gtest-all.cc.  These three files contain
0045        everything you need to use Google Mock.  Hence you can
0046        "install" Google Mock by copying them to wherever you want.
0047 
0048        GMOCK_ROOT_DIR can be omitted and defaults to the parent
0049        directory of the directory holding this script.
0050 
0051 EXAMPLES
0052        ./fuse_gmock_files.py fused_gmock
0053        ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
0054 
0055 This tool is experimental.  In particular, it assumes that there is no
0056 conditional inclusion of Google Mock or Google Test headers.  Please
0057 report any problems to googlemock@googlegroups.com.  You can read
0058 http://code.google.com/p/googlemock/wiki/CookBook for more
0059 information.
0060 """
0061 
0062 __author__ = 'wan@google.com (Zhanyong Wan)'
0063 
0064 import os
0065 import re
0066 import sets
0067 import sys
0068 
0069 # We assume that this file is in the scripts/ directory in the Google
0070 # Mock root directory.
0071 DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
0072 
0073 # We need to call into googletest/scripts/fuse_gtest_files.py.
0074 sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
0075 import fuse_gtest_files
0076 gtest = fuse_gtest_files
0077 
0078 # Regex for matching '#include "gmock/..."'.
0079 INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
0080 
0081 # Where to find the source seed files.
0082 GMOCK_H_SEED = 'include/gmock/gmock.h'
0083 GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
0084 
0085 # Where to put the generated files.
0086 GTEST_H_OUTPUT = 'gtest/gtest.h'
0087 GMOCK_H_OUTPUT = 'gmock/gmock.h'
0088 GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
0089 
0090 
0091 def GetGTestRootDir(gmock_root):
0092   """Returns the root directory of Google Test."""
0093 
0094   return os.path.join(gmock_root, '../googletest')
0095 
0096 
0097 def ValidateGMockRootDir(gmock_root):
0098   """Makes sure gmock_root points to a valid gmock root directory.
0099 
0100   The function aborts the program on failure.
0101   """
0102 
0103   gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
0104   gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
0105   gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
0106 
0107 
0108 def ValidateOutputDir(output_dir):
0109   """Makes sure output_dir points to a valid output directory.
0110 
0111   The function aborts the program on failure.
0112   """
0113 
0114   gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
0115   gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
0116   gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
0117 
0118 
0119 def FuseGMockH(gmock_root, output_dir):
0120   """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
0121 
0122   output_file = file(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
0123   processed_files = sets.Set()  # Holds all gmock headers we've processed.
0124 
0125   def ProcessFile(gmock_header_path):
0126     """Processes the given gmock header file."""
0127 
0128     # We don't process the same header twice.
0129     if gmock_header_path in processed_files:
0130       return
0131 
0132     processed_files.add(gmock_header_path)
0133 
0134     # Reads each line in the given gmock header.
0135     for line in file(os.path.join(gmock_root, gmock_header_path), 'r'):
0136       m = INCLUDE_GMOCK_FILE_REGEX.match(line)
0137       if m:
0138         # It's '#include "gmock/..."' - let's process it recursively.
0139         ProcessFile('include/' + m.group(1))
0140       else:
0141         m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
0142         if m:
0143           # It's '#include "gtest/foo.h"'.  We translate it to
0144           # "gtest/gtest.h", regardless of what foo is, since all
0145           # gtest headers are fused into gtest/gtest.h.
0146 
0147           # There is no need to #include gtest.h twice.
0148           if not gtest.GTEST_H_SEED in processed_files:
0149             processed_files.add(gtest.GTEST_H_SEED)
0150             output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
0151         else:
0152           # Otherwise we copy the line unchanged to the output file.
0153           output_file.write(line)
0154 
0155   ProcessFile(GMOCK_H_SEED)
0156   output_file.close()
0157 
0158 
0159 def FuseGMockAllCcToFile(gmock_root, output_file):
0160   """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
0161 
0162   processed_files = sets.Set()
0163 
0164   def ProcessFile(gmock_source_file):
0165     """Processes the given gmock source file."""
0166 
0167     # We don't process the same #included file twice.
0168     if gmock_source_file in processed_files:
0169       return
0170 
0171     processed_files.add(gmock_source_file)
0172 
0173     # Reads each line in the given gmock source file.
0174     for line in file(os.path.join(gmock_root, gmock_source_file), 'r'):
0175       m = INCLUDE_GMOCK_FILE_REGEX.match(line)
0176       if m:
0177         # It's '#include "gmock/foo.h"'.  We treat it as '#include
0178         # "gmock/gmock.h"', as all other gmock headers are being fused
0179         # into gmock.h and cannot be #included directly.
0180 
0181         # There is no need to #include "gmock/gmock.h" more than once.
0182         if not GMOCK_H_SEED in processed_files:
0183           processed_files.add(GMOCK_H_SEED)
0184           output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
0185       else:
0186         m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
0187         if m:
0188           # It's '#include "gtest/..."'.
0189           # There is no need to #include gtest.h as it has been
0190           # #included by gtest-all.cc.
0191           pass
0192         else:
0193           m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
0194           if m:
0195             # It's '#include "src/foo"' - let's process it recursively.
0196             ProcessFile(m.group(1))
0197           else:
0198             # Otherwise we copy the line unchanged to the output file.
0199             output_file.write(line)
0200 
0201   ProcessFile(GMOCK_ALL_CC_SEED)
0202 
0203 
0204 def FuseGMockGTestAllCc(gmock_root, output_dir):
0205   """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
0206 
0207   output_file = file(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT), 'w')
0208   # First, fuse gtest-all.cc into gmock-gtest-all.cc.
0209   gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
0210   # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
0211   FuseGMockAllCcToFile(gmock_root, output_file)
0212   output_file.close()
0213 
0214 
0215 def FuseGMock(gmock_root, output_dir):
0216   """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
0217 
0218   ValidateGMockRootDir(gmock_root)
0219   ValidateOutputDir(output_dir)
0220 
0221   gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
0222   FuseGMockH(gmock_root, output_dir)
0223   FuseGMockGTestAllCc(gmock_root, output_dir)
0224 
0225 
0226 def main():
0227   argc = len(sys.argv)
0228   if argc == 2:
0229     # fuse_gmock_files.py OUTPUT_DIR
0230     FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
0231   elif argc == 3:
0232     # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
0233     FuseGMock(sys.argv[1], sys.argv[2])
0234   else:
0235     print __doc__
0236     sys.exit(1)
0237 
0238 
0239 if __name__ == '__main__':
0240   main()