michael@0: #!/usr/bin/env python michael@0: # michael@0: # Copyright 2009, Google Inc. michael@0: # All rights reserved. michael@0: # michael@0: # Redistribution and use in source and binary forms, with or without michael@0: # modification, are permitted provided that the following conditions are michael@0: # met: michael@0: # michael@0: # * Redistributions of source code must retain the above copyright michael@0: # notice, this list of conditions and the following disclaimer. michael@0: # * Redistributions in binary form must reproduce the above michael@0: # copyright notice, this list of conditions and the following disclaimer michael@0: # in the documentation and/or other materials provided with the michael@0: # distribution. michael@0: # * Neither the name of Google Inc. nor the names of its michael@0: # contributors may be used to endorse or promote products derived from michael@0: # this software without specific prior written permission. michael@0: # michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: """fuse_gtest_files.py v0.2.0 michael@0: Fuses Google Test source code into a .h file and a .cc file. michael@0: michael@0: SYNOPSIS michael@0: fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR michael@0: michael@0: Scans GTEST_ROOT_DIR for Google Test source code, and generates michael@0: two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc. michael@0: Then you can build your tests by adding OUTPUT_DIR to the include michael@0: search path and linking with OUTPUT_DIR/gtest/gtest-all.cc. These michael@0: two files contain everything you need to use Google Test. Hence michael@0: you can "install" Google Test by copying them to wherever you want. michael@0: michael@0: GTEST_ROOT_DIR can be omitted and defaults to the parent michael@0: directory of the directory holding this script. michael@0: michael@0: EXAMPLES michael@0: ./fuse_gtest_files.py fused_gtest michael@0: ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest michael@0: michael@0: This tool is experimental. In particular, it assumes that there is no michael@0: conditional inclusion of Google Test headers. Please report any michael@0: problems to googletestframework@googlegroups.com. You can read michael@0: http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for michael@0: more information. michael@0: """ michael@0: michael@0: __author__ = 'wan@google.com (Zhanyong Wan)' michael@0: michael@0: import os michael@0: import re michael@0: import sets michael@0: import sys michael@0: michael@0: # We assume that this file is in the scripts/ directory in the Google michael@0: # Test root directory. michael@0: DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..') michael@0: michael@0: # Regex for matching '#include "gtest/..."'. michael@0: INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"') michael@0: michael@0: # Regex for matching '#include "src/..."'. michael@0: INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"') michael@0: michael@0: # Where to find the source seed files. michael@0: GTEST_H_SEED = 'include/gtest/gtest.h' michael@0: GTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h' michael@0: GTEST_ALL_CC_SEED = 'src/gtest-all.cc' michael@0: michael@0: # Where to put the generated files. michael@0: GTEST_H_OUTPUT = 'gtest/gtest.h' michael@0: GTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc' michael@0: michael@0: michael@0: def VerifyFileExists(directory, relative_path): michael@0: """Verifies that the given file exists; aborts on failure. michael@0: michael@0: relative_path is the file path relative to the given directory. michael@0: """ michael@0: michael@0: if not os.path.isfile(os.path.join(directory, relative_path)): michael@0: print 'ERROR: Cannot find %s in directory %s.' % (relative_path, michael@0: directory) michael@0: print ('Please either specify a valid project root directory ' michael@0: 'or omit it on the command line.') michael@0: sys.exit(1) michael@0: michael@0: michael@0: def ValidateGTestRootDir(gtest_root): michael@0: """Makes sure gtest_root points to a valid gtest root directory. michael@0: michael@0: The function aborts the program on failure. michael@0: """ michael@0: michael@0: VerifyFileExists(gtest_root, GTEST_H_SEED) michael@0: VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED) michael@0: michael@0: michael@0: def VerifyOutputFile(output_dir, relative_path): michael@0: """Verifies that the given output file path is valid. michael@0: michael@0: relative_path is relative to the output_dir directory. michael@0: """ michael@0: michael@0: # Makes sure the output file either doesn't exist or can be overwritten. michael@0: output_file = os.path.join(output_dir, relative_path) michael@0: if os.path.exists(output_file): michael@0: # TODO(wan@google.com): The following user-interaction doesn't michael@0: # work with automated processes. We should provide a way for the michael@0: # Makefile to force overwriting the files. michael@0: print ('%s already exists in directory %s - overwrite it? (y/N) ' % michael@0: (relative_path, output_dir)) michael@0: answer = sys.stdin.readline().strip() michael@0: if answer not in ['y', 'Y']: michael@0: print 'ABORTED.' michael@0: sys.exit(1) michael@0: michael@0: # Makes sure the directory holding the output file exists; creates michael@0: # it and all its ancestors if necessary. michael@0: parent_directory = os.path.dirname(output_file) michael@0: if not os.path.isdir(parent_directory): michael@0: os.makedirs(parent_directory) michael@0: michael@0: michael@0: def ValidateOutputDir(output_dir): michael@0: """Makes sure output_dir points to a valid output directory. michael@0: michael@0: The function aborts the program on failure. michael@0: """ michael@0: michael@0: VerifyOutputFile(output_dir, GTEST_H_OUTPUT) michael@0: VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT) michael@0: michael@0: michael@0: def FuseGTestH(gtest_root, output_dir): michael@0: """Scans folder gtest_root to generate gtest/gtest.h in output_dir.""" michael@0: michael@0: output_file = file(os.path.join(output_dir, GTEST_H_OUTPUT), 'w') michael@0: processed_files = sets.Set() # Holds all gtest headers we've processed. michael@0: michael@0: def ProcessFile(gtest_header_path): michael@0: """Processes the given gtest header file.""" michael@0: michael@0: # We don't process the same header twice. michael@0: if gtest_header_path in processed_files: michael@0: return michael@0: michael@0: processed_files.add(gtest_header_path) michael@0: michael@0: # Reads each line in the given gtest header. michael@0: for line in file(os.path.join(gtest_root, gtest_header_path), 'r'): michael@0: m = INCLUDE_GTEST_FILE_REGEX.match(line) michael@0: if m: michael@0: # It's '#include "gtest/..."' - let's process it recursively. michael@0: ProcessFile('include/' + m.group(1)) michael@0: else: michael@0: # Otherwise we copy the line unchanged to the output file. michael@0: output_file.write(line) michael@0: michael@0: ProcessFile(GTEST_H_SEED) michael@0: output_file.close() michael@0: michael@0: michael@0: def FuseGTestAllCcToFile(gtest_root, output_file): michael@0: """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file.""" michael@0: michael@0: processed_files = sets.Set() michael@0: michael@0: def ProcessFile(gtest_source_file): michael@0: """Processes the given gtest source file.""" michael@0: michael@0: # We don't process the same #included file twice. michael@0: if gtest_source_file in processed_files: michael@0: return michael@0: michael@0: processed_files.add(gtest_source_file) michael@0: michael@0: # Reads each line in the given gtest source file. michael@0: for line in file(os.path.join(gtest_root, gtest_source_file), 'r'): michael@0: m = INCLUDE_GTEST_FILE_REGEX.match(line) michael@0: if m: michael@0: if 'include/' + m.group(1) == GTEST_SPI_H_SEED: michael@0: # It's '#include "gtest/gtest-spi.h"'. This file is not michael@0: # #included by "gtest/gtest.h", so we need to process it. michael@0: ProcessFile(GTEST_SPI_H_SEED) michael@0: else: michael@0: # It's '#include "gtest/foo.h"' where foo is not gtest-spi. michael@0: # We treat it as '#include "gtest/gtest.h"', as all other michael@0: # gtest headers are being fused into gtest.h and cannot be michael@0: # #included directly. michael@0: michael@0: # There is no need to #include "gtest/gtest.h" more than once. michael@0: if not GTEST_H_SEED in processed_files: michael@0: processed_files.add(GTEST_H_SEED) michael@0: output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,)) michael@0: else: michael@0: m = INCLUDE_SRC_FILE_REGEX.match(line) michael@0: if m: michael@0: # It's '#include "src/foo"' - let's process it recursively. michael@0: ProcessFile(m.group(1)) michael@0: else: michael@0: output_file.write(line) michael@0: michael@0: ProcessFile(GTEST_ALL_CC_SEED) michael@0: michael@0: michael@0: def FuseGTestAllCc(gtest_root, output_dir): michael@0: """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir.""" michael@0: michael@0: output_file = file(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w') michael@0: FuseGTestAllCcToFile(gtest_root, output_file) michael@0: output_file.close() michael@0: michael@0: michael@0: def FuseGTest(gtest_root, output_dir): michael@0: """Fuses gtest.h and gtest-all.cc.""" michael@0: michael@0: ValidateGTestRootDir(gtest_root) michael@0: ValidateOutputDir(output_dir) michael@0: michael@0: FuseGTestH(gtest_root, output_dir) michael@0: FuseGTestAllCc(gtest_root, output_dir) michael@0: michael@0: michael@0: def main(): michael@0: argc = len(sys.argv) michael@0: if argc == 2: michael@0: # fuse_gtest_files.py OUTPUT_DIR michael@0: FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1]) michael@0: elif argc == 3: michael@0: # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR michael@0: FuseGTest(sys.argv[1], sys.argv[2]) michael@0: else: michael@0: print __doc__ michael@0: sys.exit(1) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: main()