11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#!/usr/bin/env python
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Copyright 2009, Google Inc.
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# All rights reserved.
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Redistribution and use in source and binary forms, with or without
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# modification, are permitted provided that the following conditions are
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# met:
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions of source code must retain the above copyright
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# notice, this list of conditions and the following disclaimer.
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions in binary form must reproduce the above
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# copyright notice, this list of conditions and the following disclaimer
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# in the documentation and/or other materials provided with the
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# distribution.
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Neither the name of Google Inc. nor the names of its
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# contributors may be used to endorse or promote products derived from
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# this software without specific prior written permission.
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot"""fuse_gtest_files.py v0.2.0
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaFuses Google Test source code into a .h file and a .cc file.
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaSYNOPSIS
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       Scans GTEST_ROOT_DIR for Google Test source code, and generates
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc.
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       Then you can build your tests by adding OUTPUT_DIR to the include
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       search path and linking with OUTPUT_DIR/gtest/gtest-all.cc.  These
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       two files contain everything you need to use Google Test.  Hence
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       you can "install" Google Test by copying them to wherever you want.
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot       GTEST_ROOT_DIR can be omitted and defaults to the parent
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot       directory of the directory holding this script.
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaEXAMPLES
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       ./fuse_gtest_files.py fused_gtest
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania       ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest
511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
521be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaThis tool is experimental.  In particular, it assumes that there is no
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaconditional inclusion of Google Test headers.  Please report any
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaproblems to googletestframework@googlegroups.com.  You can read
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniahttp://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniamore information.
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = 'wan@google.com (Zhanyong Wan)'
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport os
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport re
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport sets
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport sys
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# We assume that this file is in the scripts/ directory in the Google
6741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# Test root directory.
6841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotDEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
6941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
7041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# Regex for matching '#include "gtest/..."'.
7141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotINCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Regex for matching '#include "src/..."'.
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaINCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# Where to find the source seed files.
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_H_SEED = 'include/gtest/gtest.h'
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h'
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_ALL_CC_SEED = 'src/gtest-all.cc'
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Where to put the generated files.
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_H_OUTPUT = 'gtest/gtest.h'
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc'
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotdef VerifyFileExists(directory, relative_path):
8741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Verifies that the given file exists; aborts on failure.
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  relative_path is the file path relative to the given directory.
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
9241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if not os.path.isfile(os.path.join(directory, relative_path)):
9341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    print 'ERROR: Cannot find %s in directory %s.' % (relative_path,
9441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                                      directory)
9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    print ('Please either specify a valid project root directory '
9641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot           'or omit it on the command line.')
9741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    sys.exit(1)
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef ValidateGTestRootDir(gtest_root):
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Makes sure gtest_root points to a valid gtest root directory.
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  The function aborts the program on failure.
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
10641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  VerifyFileExists(gtest_root, GTEST_H_SEED)
10741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED)
10841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
10941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
11041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotdef VerifyOutputFile(output_dir, relative_path):
11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Verifies that the given output file path is valid.
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  relative_path is relative to the output_dir directory.
11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
11641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  # Makes sure the output file either doesn't exist or can be overwritten.
11741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  output_file = os.path.join(output_dir, relative_path)
11841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if os.path.exists(output_file):
11941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    # TODO(wan@google.com): The following user-interaction doesn't
12041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    # work with automated processes.  We should provide a way for the
12141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    # Makefile to force overwriting the files.
12241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    print ('%s already exists in directory %s - overwrite it? (y/N) ' %
12341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot           (relative_path, output_dir))
12441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    answer = sys.stdin.readline().strip()
12541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    if answer not in ['y', 'Y']:
12641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      print 'ABORTED.'
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      sys.exit(1)
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  # Makes sure the directory holding the output file exists; creates
13041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  # it and all its ancestors if necessary.
13141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  parent_directory = os.path.dirname(output_file)
13241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  if not os.path.isdir(parent_directory):
13341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    os.makedirs(parent_directory)
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef ValidateOutputDir(output_dir):
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Makes sure output_dir points to a valid output directory.
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  The function aborts the program on failure.
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
14241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  VerifyOutputFile(output_dir, GTEST_H_OUTPUT)
14341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT)
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef FuseGTestH(gtest_root, output_dir):
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  output_file = file(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  processed_files = sets.Set()  # Holds all gtest headers we've processed.
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def ProcessFile(gtest_header_path):
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Processes the given gtest header file."""
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # We don't process the same header twice.
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if gtest_header_path in processed_files:
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    processed_files.add(gtest_header_path)
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # Reads each line in the given gtest header.
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for line in file(os.path.join(gtest_root, gtest_header_path), 'r'):
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      m = INCLUDE_GTEST_FILE_REGEX.match(line)
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      if m:
16541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        # It's '#include "gtest/..."' - let's process it recursively.
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ProcessFile('include/' + m.group(1))
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      else:
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        # Otherwise we copy the line unchanged to the output file.
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        output_file.write(line)
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ProcessFile(GTEST_H_SEED)
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  output_file.close()
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
17541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotdef FuseGTestAllCcToFile(gtest_root, output_file):
17641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  processed_files = sets.Set()
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def ProcessFile(gtest_source_file):
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Processes the given gtest source file."""
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # We don't process the same #included file twice.
1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if gtest_source_file in processed_files:
1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return
1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    processed_files.add(gtest_source_file)
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # Reads each line in the given gtest source file.
1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    for line in file(os.path.join(gtest_root, gtest_source_file), 'r'):
1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      m = INCLUDE_GTEST_FILE_REGEX.match(line)
1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      if m:
1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
19441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          # It's '#include "gtest/gtest-spi.h"'.  This file is not
19541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          # #included by "gtest/gtest.h", so we need to process it.
1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          ProcessFile(GTEST_SPI_H_SEED)
1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        else:
19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          # It's '#include "gtest/foo.h"' where foo is not gtest-spi.
19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          # We treat it as '#include "gtest/gtest.h"', as all other
2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          # gtest headers are being fused into gtest.h and cannot be
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          # #included directly.
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot          # There is no need to #include "gtest/gtest.h" more than once.
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          if not GTEST_H_SEED in processed_files:
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            processed_files.add(GTEST_H_SEED)
20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot            output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,))
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      else:
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        m = INCLUDE_SRC_FILE_REGEX.match(line)
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        if m:
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          # It's '#include "src/foo"' - let's process it recursively.
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          ProcessFile(m.group(1))
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        else:
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania          output_file.write(line)
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ProcessFile(GTEST_ALL_CC_SEED)
21641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
21741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
21841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotdef FuseGTestAllCc(gtest_root, output_dir):
21941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
22041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
22141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  output_file = file(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
22241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  FuseGTestAllCcToFile(gtest_root, output_file)
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  output_file.close()
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef FuseGTest(gtest_root, output_dir):
22741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  """Fuses gtest.h and gtest-all.cc."""
22841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ValidateGTestRootDir(gtest_root)
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ValidateOutputDir(output_dir)
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FuseGTestH(gtest_root, output_dir)
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  FuseGTestAllCc(gtest_root, output_dir)
2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef main():
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  argc = len(sys.argv)
2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if argc == 2:
2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # fuse_gtest_files.py OUTPUT_DIR
24041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  elif argc == 3:
2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    FuseGTest(sys.argv[1], sys.argv[2])
2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  else:
2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    print __doc__
2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    sys.exit(1)
2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif __name__ == '__main__':
2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  main()
251