fuse_gmock_files.py 8.75 KB
Newer Older
Jesse Beder's avatar
Jesse Beder committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
#
# Copyright 2009, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Akash Patel's avatar
Akash Patel committed
31
"""fuse_gmock_files.py v0.1.0.
Jesse Beder's avatar
Jesse Beder committed
32
33
34
35
36
37
38

Fuses Google Mock and Google Test source code into two .h files and a .cc file.

SYNOPSIS
       fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR

       Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
39
40
       code, assuming Google Test is in the GMOCK_ROOT_DIR/../googletest
       directory, and generates three files:
Jesse Beder's avatar
Jesse Beder committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
       OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
       OUTPUT_DIR/gmock-gtest-all.cc.  Then you can build your tests
       by adding OUTPUT_DIR to the include search path and linking
       with OUTPUT_DIR/gmock-gtest-all.cc.  These three files contain
       everything you need to use Google Mock.  Hence you can
       "install" Google Mock by copying them to wherever you want.

       GMOCK_ROOT_DIR can be omitted and defaults to the parent
       directory of the directory holding this script.

EXAMPLES
       ./fuse_gmock_files.py fused_gmock
       ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock

This tool is experimental.  In particular, it assumes that there is no
conditional inclusion of Google Mock or Google Test headers.  Please
report any problems to googlemock@googlegroups.com.  You can read
Akash Patel's avatar
Akash Patel committed
58
59
https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md
for more
Jesse Beder's avatar
Jesse Beder committed
60
61
62
information.
"""

Akash Patel's avatar
Akash Patel committed
63
from __future__ import print_function
Jesse Beder's avatar
Jesse Beder committed
64
65
66
67
68

import os
import re
import sys

Akash Patel's avatar
Akash Patel committed
69
70
__author__ = 'wan@google.com (Zhanyong Wan)'

Jesse Beder's avatar
Jesse Beder committed
71
72
73
74
# We assume that this file is in the scripts/ directory in the Google
# Mock root directory.
DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')

75
76
# We need to call into googletest/scripts/fuse_gtest_files.py.
sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, '../googletest/scripts'))
Akash Patel's avatar
Akash Patel committed
77
import fuse_gtest_files as gtest  # pylint:disable=g-import-not-at-top
Jesse Beder's avatar
Jesse Beder committed
78

Akash Patel's avatar
Akash Patel committed
79
80
# Regex for matching
# '#include "gmock/..."'.
Jesse Beder's avatar
Jesse Beder committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')

# Where to find the source seed files.
GMOCK_H_SEED = 'include/gmock/gmock.h'
GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'

# Where to put the generated files.
GTEST_H_OUTPUT = 'gtest/gtest.h'
GMOCK_H_OUTPUT = 'gmock/gmock.h'
GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'


def GetGTestRootDir(gmock_root):
  """Returns the root directory of Google Test."""

96
  return os.path.join(gmock_root, '../googletest')
Jesse Beder's avatar
Jesse Beder committed
97
98
99
100
101
102


def ValidateGMockRootDir(gmock_root):
  """Makes sure gmock_root points to a valid gmock root directory.

  The function aborts the program on failure.
Akash Patel's avatar
Akash Patel committed
103
104
105

  Args:
    gmock_root: A string with the mock root directory.
Jesse Beder's avatar
Jesse Beder committed
106
107
108
109
110
111
112
113
114
115
116
  """

  gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
  gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
  gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)


def ValidateOutputDir(output_dir):
  """Makes sure output_dir points to a valid output directory.

  The function aborts the program on failure.
Akash Patel's avatar
Akash Patel committed
117
118
119

  Args:
    output_dir: A string representing the output directory.
Jesse Beder's avatar
Jesse Beder committed
120
121
122
123
124
125
126
127
128
129
  """

  gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
  gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
  gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)


def FuseGMockH(gmock_root, output_dir):
  """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""

Akash Patel's avatar
Akash Patel committed
130
131
  output_file = open(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
  processed_files = set()  # Holds all gmock headers we've processed.
Jesse Beder's avatar
Jesse Beder committed
132
133
134
135
136
137
138
139
140
141
142

  def ProcessFile(gmock_header_path):
    """Processes the given gmock header file."""

    # We don't process the same header twice.
    if gmock_header_path in processed_files:
      return

    processed_files.add(gmock_header_path)

    # Reads each line in the given gmock header.
Akash Patel's avatar
Akash Patel committed
143
144
145
146

    with open(os.path.join(gmock_root, gmock_header_path), 'r') as fh:
      for line in fh:
        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
Jesse Beder's avatar
Jesse Beder committed
147
        if m:
Akash Patel's avatar
Akash Patel committed
148
149
150
          # '#include "gmock/..."'
          # - let's process it recursively.
          ProcessFile('include/' + m.group(1))
Jesse Beder's avatar
Jesse Beder committed
151
        else:
Akash Patel's avatar
Akash Patel committed
152
153
154
155
156
157
158
159
160
161
162
163
164
          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
          if m:
            # '#include "gtest/foo.h"'
            # We translate it to "gtest/gtest.h", regardless of what foo is,
            # since all gtest headers are fused into gtest/gtest.h.

            # There is no need to #include gtest.h twice.
            if gtest.GTEST_H_SEED not in processed_files:
              processed_files.add(gtest.GTEST_H_SEED)
              output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
          else:
            # Otherwise we copy the line unchanged to the output file.
            output_file.write(line)
Jesse Beder's avatar
Jesse Beder committed
165
166
167
168
169
170
171
172

  ProcessFile(GMOCK_H_SEED)
  output_file.close()


def FuseGMockAllCcToFile(gmock_root, output_file):
  """Scans folder gmock_root to fuse gmock-all.cc into output_file."""

Akash Patel's avatar
Akash Patel committed
173
  processed_files = set()
Jesse Beder's avatar
Jesse Beder committed
174
175
176
177
178
179
180
181
182
183
184

  def ProcessFile(gmock_source_file):
    """Processes the given gmock source file."""

    # We don't process the same #included file twice.
    if gmock_source_file in processed_files:
      return

    processed_files.add(gmock_source_file)

    # Reads each line in the given gmock source file.
Akash Patel's avatar
Akash Patel committed
185
186
187
188

    with open(os.path.join(gmock_root, gmock_source_file), 'r') as fh:
      for line in fh:
        m = INCLUDE_GMOCK_FILE_REGEX.match(line)
Jesse Beder's avatar
Jesse Beder committed
189
        if m:
Akash Patel's avatar
Akash Patel committed
190
191
192
193
194
195
196
197
198
199
          # '#include "gmock/foo.h"'
          # We treat it as '#include  "gmock/gmock.h"', as all other gmock
          # headers are being fused into gmock.h and cannot be
          # included directly.  No need to
          # #include "gmock/gmock.h"
          # more than once.

          if GMOCK_H_SEED not in processed_files:
            processed_files.add(GMOCK_H_SEED)
            output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
Jesse Beder's avatar
Jesse Beder committed
200
        else:
Akash Patel's avatar
Akash Patel committed
201
          m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
Jesse Beder's avatar
Jesse Beder committed
202
          if m:
Akash Patel's avatar
Akash Patel committed
203
204
205
206
207
            # '#include "gtest/..."'
            # There is no need to #include gtest.h as it has been
            # #included by gtest-all.cc.

            pass
Jesse Beder's avatar
Jesse Beder committed
208
          else:
Akash Patel's avatar
Akash Patel committed
209
210
211
212
213
214
215
            m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
            if m:
              # It's '#include "src/foo"' - let's process it recursively.
              ProcessFile(m.group(1))
            else:
              # Otherwise we copy the line unchanged to the output file.
              output_file.write(line)
Jesse Beder's avatar
Jesse Beder committed
216
217
218
219
220
221
222

  ProcessFile(GMOCK_ALL_CC_SEED)


def FuseGMockGTestAllCc(gmock_root, output_dir):
  """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""

Akash Patel's avatar
Akash Patel committed
223
224
225
226
227
228
  with open(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT),
            'w') as output_file:
    # First, fuse gtest-all.cc into gmock-gtest-all.cc.
    gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
    # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
    FuseGMockAllCcToFile(gmock_root, output_file)
Jesse Beder's avatar
Jesse Beder committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250


def FuseGMock(gmock_root, output_dir):
  """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""

  ValidateGMockRootDir(gmock_root)
  ValidateOutputDir(output_dir)

  gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
  FuseGMockH(gmock_root, output_dir)
  FuseGMockGTestAllCc(gmock_root, output_dir)


def main():
  argc = len(sys.argv)
  if argc == 2:
    # fuse_gmock_files.py OUTPUT_DIR
    FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
  elif argc == 3:
    # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
    FuseGMock(sys.argv[1], sys.argv[2])
  else:
Akash Patel's avatar
Akash Patel committed
251
    print(__doc__)
Jesse Beder's avatar
Jesse Beder committed
252
253
254
255
256
    sys.exit(1)


if __name__ == '__main__':
  main()