SConscript 11.7 KB
Newer Older
shiqian's avatar
shiqian 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python2.4
#
# Copyright 2008, 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.


"""Builds the Google Test (gtest) lib; this is for Windows projects
using SCons and can probably be easily extended for cross-platform
SCons builds. The compilation settings from your project will be used,
with some specific flags required for gtest added.

You should be able to call this file from more or less any SConscript
file.

You can optionally set a variable on the construction environment to
have the unit test executables copied to your output directory.  The
variable should be env['EXE_OUTPUT'].

Another optional variable is env['LIB_OUTPUT'].  If set, the generated
libraries are copied to the folder indicated by the variable.

If you place the gtest sources within your own project's source
directory, you should be able to call this SConscript file simply as
follows:

# -- cut here --
# Build gtest library; first tell it where to copy executables.
env['EXE_OUTPUT'] = '#/mybuilddir/mybuildmode'  # example, optional
env['LIB_OUTPUT'] = '#/mybuilddir/mybuildmode/lib'
env.SConscript('whateverpath/gtest/scons/SConscript')
# -- cut here --

If on the other hand you place the gtest sources in a directory
outside of your project's source tree, you would use a snippet similar
to the following:

# -- cut here --

# The following assumes that $BUILD_DIR refers to the root of the
# directory for your current build mode, e.g. "#/mybuilddir/mybuildmode"

# Build gtest library; as it is outside of our source root, we need to
# tell SCons that the directory it will refer to as
70
# e.g. $BUILD_DIR/gtest is actually on disk in original form as
shiqian's avatar
shiqian committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# ../../gtest (relative to your project root directory).  Recall that
# SCons by default copies all source files into the build directory
# before building.
gtest_dir = env.Dir('$BUILD_DIR/gtest')

# Modify this part to point to gtest relative to the current
# SConscript or SConstruct file's directory.  The ../.. path would
# be different per project, to locate the base directory for gtest.
gtest_dir.addRepository(env.Dir('../../gtest'))

# Tell the gtest SCons file where to copy executables.
env['EXE_OUTPUT'] = '$BUILD_DIR'  # example, optional

# Call the gtest SConscript to build gtest.lib and unit tests.  The
# location of the library should end up as
# '$BUILD_DIR/gtest/scons/gtest.lib'
env.SConscript(env.File('scons/SConscript', gtest_dir))

# -- cut here --
"""


__author__ = 'joi@google.com (Joi Sigurdsson)'


Import('env')
env = env.Clone()

99
100
101
# Include paths to gtest headers are relative to either the gtest
# directory or the 'include' subdirectory of it, and this SConscript
# file is one directory deeper than the gtest directory.
102
103
env.Prepend(CPPPATH = ['#/..',
                       '#/../include'])
shiqian's avatar
shiqian committed
104

105
106
107
# Sources used by base library and library that includes main.
gtest_source = '../src/gtest-all.cc'
gtest_main_source = '../src/gtest_main.cc'
shiqian's avatar
shiqian committed
108
109
110
111

# gtest.lib to be used by most apps (if you have your own main
# function)
gtest = env.StaticLibrary(target='gtest',
112
                          source=[gtest_source])
shiqian's avatar
shiqian committed
113
114
115
116

# gtest_main.lib can be used if you just want a basic main function;
# it is also used by the tests for Google Test itself.
gtest_main = env.StaticLibrary(target='gtest_main',
117
118
119
                               source=[gtest_source, gtest_main_source])

env_with_exceptions = env.Clone()
120
if env_with_exceptions['PLATFORM'] == 'win32':
121
122
  env_with_exceptions.Append(CCFLAGS = ['/EHsc'])
  env_with_exceptions.Append(CPPDEFINES = '_HAS_EXCEPTIONS=1')
123
124
125
126
127
  # Undoes the _TYPEINFO_ hack, which is unnecessary and only creates
  # trouble when exceptions are enabled.
  cppdefines = env_with_exceptions['CPPDEFINES']
  if '_TYPEINFO_' in cppdefines:
    cppdefines.remove('_TYPEINFO_')
128
129
130
131
132
133

gtest_ex_obj = env_with_exceptions.Object(target='gtest_ex',
                                          source=gtest_source)
gtest_main_ex_obj = env_with_exceptions.Object(target='gtest_main_ex',
                                               source=gtest_main_source)

134
135
136
137
gtest_ex = env_with_exceptions.StaticLibrary(
    target='gtest_ex',
    source=gtest_ex_obj)

138
139
140
gtest_ex_main = env_with_exceptions.StaticLibrary(
    target='gtest_ex_main',
    source=gtest_ex_obj + gtest_main_ex_obj)
shiqian's avatar
shiqian committed
141
142
143

# Install the libraries if needed.
if 'LIB_OUTPUT' in env.Dictionary():
144
  env.Install('$LIB_OUTPUT', source=[gtest, gtest_main, gtest_ex_main])
shiqian's avatar
shiqian committed
145
146


147
148
def ConstructSourceList(target, dir_prefix, additional_sources=None):
  """Helper to create source file list for gtest binaries.
shiqian's avatar
shiqian committed
149
150

  Args:
151
    target: The basename of the target's main source file.
152
    dir_prefix: The path to prefix the main source file.
shiqian's avatar
shiqian committed
153
    gtest_lib: The gtest lib to use.
154
    additional_sources: A list of additional source files in the target.
shiqian's avatar
shiqian committed
155
  """
156
  source = [env.File('%s.cc' % target, env.Dir(dir_prefix))]
shiqian's avatar
shiqian committed
157
158
  if additional_sources:
    source += additional_sources
159
160
161
162
163
164
165
166
167
168
169
170
171
  return source

def GtestBinary(env, target, gtest_lib, sources):
  """Helper to create gtest binaries: tests, samples, etc.

  Args:
    env: The SCons construction environment to use to build.
    target: The basename of the target's main source file, also used as target
            name.
    gtest_lib: The gtest lib to use.
    sources: A list of source files in the target.
  """
  unit_test = env.Program(target=target, source=sources, LIBS=[gtest_lib])
shiqian's avatar
shiqian committed
172
173
174
  if 'EXE_OUTPUT' in env.Dictionary():
    env.Install('$EXE_OUTPUT', source=[unit_test])

175
176
177
178
179
180
181
def GtestUnitTest(env, target, gtest_lib, additional_sources=None):
  """Helper to create gtest unit tests.

  Args:
    env: The SCons construction environment to use to build.
    target: The basename of the target unit test .cc file.
    gtest_lib: The gtest lib to use.
182
    additional_sources: A list of additional source files in the target.
183
  """
184
185
186
187
188
  GtestBinary(env,
              target,
              gtest_lib,
              ConstructSourceList(target, "../test",
                                  additional_sources=additional_sources))
shiqian's avatar
shiqian committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

GtestUnitTest(env, 'gtest-filepath_test', gtest_main)
GtestUnitTest(env, 'gtest-message_test', gtest_main)
GtestUnitTest(env, 'gtest-options_test', gtest_main)
GtestUnitTest(env, 'gtest_environment_test', gtest)
GtestUnitTest(env, 'gtest_main_unittest', gtest_main)
GtestUnitTest(env, 'gtest_no_test_unittest', gtest)
GtestUnitTest(env, 'gtest_pred_impl_unittest', gtest_main)
GtestUnitTest(env, 'gtest_prod_test', gtest_main,
              additional_sources=['../test/production.cc'])
GtestUnitTest(env, 'gtest_repeat_test', gtest)
GtestUnitTest(env, 'gtest_sole_header_test', gtest_main)
GtestUnitTest(env, 'gtest-test-part_test', gtest_main)
GtestUnitTest(env, 'gtest-typed-test_test', gtest_main,
              additional_sources=['../test/gtest-typed-test2_test.cc'])
204
205
GtestUnitTest(env, 'gtest-param-test_test', gtest,
              additional_sources=['../test/gtest-param-test2_test.cc'])
206
GtestUnitTest(env, 'gtest_unittest', gtest_main)
shiqian's avatar
shiqian committed
207
208
GtestUnitTest(env, 'gtest_output_test_', gtest)
GtestUnitTest(env, 'gtest_color_test_', gtest)
209
210
GtestUnitTest(env, 'gtest-linked_ptr_test', gtest_main)
GtestUnitTest(env, 'gtest-port_test', gtest_main)
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
GtestUnitTest(env, 'gtest_break_on_failure_unittest_', gtest)
GtestUnitTest(env, 'gtest_filter_unittest_', gtest)
GtestUnitTest(env, 'gtest_help_test_', gtest_main)
GtestUnitTest(env, 'gtest_list_tests_unittest_', gtest)
GtestUnitTest(env, 'gtest_throw_on_failure_test_', gtest)
GtestUnitTest(env_with_exceptions, 'gtest_throw_on_failure_ex_test', gtest_ex)
GtestUnitTest(env, 'gtest_xml_outfile1_test_', gtest_main)
GtestUnitTest(env, 'gtest_xml_outfile2_test_', gtest_main)
GtestUnitTest(env, 'gtest_xml_output_unittest_', gtest_main)

# Assuming POSIX-like environment with GCC.
# TODO(vladl@google.com): sniff presence of pthread_atfork instead of
# selecting on a platform.
env_with_threads = env.Clone()
if env_with_threads['PLATFORM'] != 'win32':
  env_with_threads.Append(CCFLAGS = ['-pthread'])
  env_with_threads.Append(LINKFLAGS = ['-pthread'])
GtestUnitTest(env_with_threads, 'gtest-death-test_test', gtest_main)
229
230
231
232
233
234
235
236

gtest_unittest_ex_obj = env_with_exceptions.Object(
    target='gtest_unittest_ex',
    source='../test/gtest_unittest.cc')
GtestBinary(env_with_exceptions,
            'gtest_ex_unittest',
            gtest_ex_main,
            gtest_unittest_ex_obj)
shiqian's avatar
shiqian committed
237

238
239
240
241
242
243
244
245
246
247
248
# We need to disable some optimization flags for some tests on
# Windows; otherwise the redirection of stdout does not work
# (apparently because of a compiler bug).
env_with_less_optimization = env.Clone()
if env_with_less_optimization['PLATFORM'] == 'win32':
  linker_flags = env_with_less_optimization['LINKFLAGS']
  for flag in ["/O1", "/Os", "/Og", "/Oy"]:
    if flag in linker_flags:
      linker_flags.remove(flag)
GtestUnitTest(env_with_less_optimization, 'gtest_env_var_test_', gtest)
GtestUnitTest(env_with_less_optimization, 'gtest_uninitialized_test_', gtest)
249
250
251
252
253
254
255
256

def GtestSample(env, target, gtest_lib, additional_sources=None):
  """Helper to create gtest samples.

  Args:
    env: The SCons construction environment to use to build.
    target: The basename of the target unit test .cc file.
    gtest_lib: The gtest lib to use.
257
    additional_sources: A list of additional source files in the target.
258
  """
259
260
261
262
263
  GtestBinary(env,
              target,
              gtest_lib,
              ConstructSourceList(target, "../samples",
                                  additional_sources=additional_sources))
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278

# Use the GTEST_BUILD_SAMPLES build variable to control building of samples.
# In your SConstruct file, add
#   vars = Variables()
#   vars.Add(BoolVariable('GTEST_BUILD_SAMPLES', 'Build samples', True))
#   my_environment = Environment(variables = vars, ...)
# Then, in the command line use GTEST_BUILD_SAMPLES=true to enable them.
#
if env.get('GTEST_BUILD_SAMPLES', False):
  sample1_obj = env.Object('../samples/sample1.cc')
  GtestSample(env, 'sample1_unittest', gtest_main,
              additional_sources=[sample1_obj])
  GtestSample(env, 'sample2_unittest', gtest_main,
              additional_sources=['../samples/sample2.cc'])
  GtestSample(env, 'sample3_unittest', gtest_main)
279
280
  GtestSample(env, 'sample4_unittest', gtest_main,
              additional_sources=['../samples/sample4.cc'])
281
282
283
284
285
  GtestSample(env, 'sample5_unittest', gtest_main,
              additional_sources=[sample1_obj])
  GtestSample(env, 'sample6_unittest', gtest_main)
  GtestSample(env, 'sample7_unittest', gtest_main)
  GtestSample(env, 'sample8_unittest', gtest_main)