util: Add a --no-test-xml option to the m5 util's scons.
[gem5.git] / util / m5 / SConstruct
1 # Copyright 2020 Google, Inc.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met: redistributions of source code must retain the above copyright
6 # notice, this list of conditions and the following disclaimer;
7 # redistributions in binary form must reproduce the above copyright
8 # notice, this list of conditions and the following disclaimer in the
9 # documentation and/or other materials provided with the distribution;
10 # neither the name of the copyright holders nor the names of its
11 # contributors may be used to endorse or promote products derived from
12 # this software without specific prior written permission.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 import copy
27 import os
28
29 main = Environment()
30
31 gem5_root = Dir('..').Dir('..')
32
33 # Includes which are shared with gem5 itself.
34 common_include = gem5_root.Dir('include')
35
36 ext_dir = gem5_root.Dir('ext')
37 googletest_dir = ext_dir.Dir('googletest')
38
39 src_dir = Dir('src')
40 build_dir = Dir('build')
41
42 def abspath(d):
43 return os.path.abspath(str(d))
44
45 AddOption('--debug-build', dest='debug_build', action='store_true',
46 help='Build with debug info, and disable optimizations.')
47 AddOption('--no-test-xml', dest='no_tests', action='store_true',
48 help='Omit test output xml files from the build.')
49
50 # Universal settings.
51 if GetOption('debug_build'):
52 main.Append(CXXFLAGS=[ '-O0', '-g' ])
53 main.Append(CCFLAGS=[ '-O0', '-g' ])
54 else:
55 main.Append(CXXFLAGS=[ '-O2' ])
56 main.Append(CCFLAGS=[ '-O2' ])
57 main.Append(CPPPATH=[ common_include ])
58
59 # Propogate the environment's PATH setting.
60 main['ENV']['PATH'] = os.environ['PATH']
61 # Pass through terminal information to, for instance, enable color output.
62 main['ENV']['TERM'] = os.environ['TERM']
63
64 # Detect some dependencies of some forms of the m5 utility/library.
65 main['HAVE_JAVA'] = all(key in main for key in ('JAVAC', 'JAR'))
66 main['HAVE_PKG_CONFIG'] = main.Detect('pkg-config') is not None
67 main['HAVE_LUA51'] = (main['HAVE_PKG_CONFIG'] and
68 os.system('pkg-config --exists lua51') == 0)
69
70 # Put the sconsign file in the build dir so everything can be deleted at once.
71 main.SConsignFile(os.path.join(abspath(build_dir), 'sconsign'))
72 # Use soft links instead of hard links when setting up a build directory.
73 main.SetOption('duplicate', 'soft-copy')
74
75 def GTest(env, name, *srcs, **kwargs):
76 if 'GTEST_ENV' not in env:
77 gtest_env = env.Clone(OBJSUFFIX='.to', SHOBJSUFFIX='.sto')
78 gtest_env.Append(CPPFLAGS=[ '${GTEST_CPPFLAGS}' ])
79 gtest_env.Append(LIBS=[ '${GTEST_LIBS}' ])
80 env['GTEST_ENV'] = gtest_env
81
82 if not srcs:
83 srcs = [ name + '.cc', name + '.test.cc' ]
84 test_bin = env['GTEST_ENV'].Program('test/bin/%s' % name, srcs, **kwargs)
85
86 # The native environment doesn't need QEMU, and doesn't define HAVE_QEMU.
87 need_qemu_to_run = 'HAVE_QEMU' in env;
88
89 # If we can run this test...
90 if (not need_qemu_to_run or env['HAVE_QEMU']) \
91 and not GetOption('no_tests'):
92 # An XML file which holds the results of the test.
93 xml = Dir('test').Dir('result').File('%s.xml' % name)
94 # The basic command line for the test.
95 cmd = '${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}'
96 if need_qemu_to_run:
97 # A prefix that runs it in QEMU if necessary.
98 cmd = '${QEMU} -L ${QEMU_SYSROOT} -- ' + cmd
99 AlwaysBuild(env.Command(xml, test_bin, cmd))
100
101 main.AddMethod(GTest)
102
103 native = main.Clone()
104 native_dir = build_dir.Dir('native')
105
106 # Bring in the googletest sources.
107 native.SConscript(googletest_dir.File('SConscript'),
108 variant_dir=native_dir.Dir('googletest'), exports={ 'main': native })
109
110 native.SConscript(src_dir.File('SConscript.native'),
111 variant_dir=native_dir, exports={ 'env': native })
112
113 main['CC'] = '${CROSS_COMPILE}gcc'
114 main['CXX'] = '${CROSS_COMPILE}g++'
115 main['AS'] = '${CROSS_COMPILE}as'
116 main['LD'] = '${CROSS_COMPILE}ld'
117 main['AR'] = '${CROSS_COMPILE}ar'
118 main['QEMU'] = 'qemu-${QEMU_ARCH}'
119
120 class CallType(object):
121 def __init__(self, name):
122 self.name = name
123 self.impl_file = None
124 self.enabled = False
125 self.verifier = None
126 self.default = False
127
128 def impl(self, impl, verifier=None, default=False):
129 self.impl_file = impl
130 self.enabled = True
131 self.verifier = verifier
132 self.default = default
133
134 # Being the default can be disabled for testing purposes, so we can tell if
135 # a call type was selected because it was chosen, or because nobody else
136 # was.
137 def setup_env(self, env, allow_default=True):
138 env = env.Clone()
139 is_default = 'true' if self.default and allow_default else 'false'
140 env.Append(CXXFLAGS=[ '-DCALL_TYPE_IS_DEFAULT=%s' % is_default ])
141 return env
142
143 call_types = {
144 # Magic instruction.
145 'inst': CallType('inst'),
146 # Magic address.
147 'addr': CallType('addr'),
148 # Semihosting extension.
149 'semi': CallType('semi'),
150 }
151
152 for root, dirs, files in os.walk(abspath(src_dir)):
153 # Each SConsopts file describes an ABI of the m5 utility.
154 if 'SConsopts' in files:
155 env = main.Clone()
156
157 env['CALL_TYPE'] = copy.deepcopy(call_types)
158
159 # The user may override ABI settings by setting environment
160 # variables of the form ${ABI}.${OPTION}. For instance, to set the
161 # CROSS_COMPILE prefix for abi foo to bar-, the user would set an
162 # environment variable foo.CROSS_COMPILE=bar-.
163 #
164 # This also considers scons command line settings which may look like
165 # environment variables, but are set after "scons" on the command line.
166 def get_abi_opt(name, default):
167 var_name = env.subst('${ABI}.%s' % name)
168 env[name] = os.environ.get(
169 var_name, ARGUMENTS.get(var_name, default))
170
171 # Process the ABI's settings in the SConsopts file, storing them
172 # in a copy of the primary environment.
173 env.SConscript(Dir(root).File('SConsopts'),
174 exports=[ 'env', 'get_abi_opt' ])
175
176 # Check if this version of QEMU is available for running unit tests.
177 env['HAVE_QEMU'] = env.Detect('${QEMU}') is not None
178 if env['HAVE_QEMU'] and env.Detect('${CC}'):
179 sysroot_cmd = env.subst('${CC} -print-sysroot')
180 sysroot = os.popen(sysroot_cmd).read().strip()
181 env['QEMU_SYSROOT'] = sysroot
182
183 # Once all the options have been configured, set up build targets for
184 # this abi.
185 abi_dir = build_dir.Dir(env.subst('${ABI}'))
186 # Bring in the googletest sources.
187 env.SConscript(googletest_dir.File('SConscript'),
188 variant_dir=abi_dir.Dir('googletest'),
189 exports={ 'main': env })
190 env.SConscript(src_dir.File('SConscript'),
191 variant_dir=abi_dir, exports='env')