util: Rework some checks in the m5 util scons to use Configure().
[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('--run-tests', dest='run_tests', action='store_true',
48 help='Enable test output xml files as build targets.')
49 AddOption('--verbose', dest='verbose', action='store_true')
50
51 # Universal settings.
52 if GetOption('debug_build'):
53 main.Append(CXXFLAGS=[ '-O0', '-g' ])
54 main.Append(CCFLAGS=[ '-O0', '-g' ])
55 else:
56 main.Append(CXXFLAGS=[ '-O2' ])
57 main.Append(CCFLAGS=[ '-O2' ])
58 main.Append(CPPPATH=[ common_include ])
59
60 if not GetOption('verbose'):
61 # A functor which returns a shorter summary string to replace the normal
62 # scons output when running a command.
63 class ComStr(object):
64 def __init__(self, cmd):
65 self.cmd = cmd
66
67 def __call__(self, target, source, env, for_signature=None):
68 tgts = list([str(t).strip() for t in target])
69 return self.cmd + ' ' + ', '.join(tgts)
70 main['CXXCOMSTR'] = ComStr('CXX')
71 main['SHCXXCOMSTR'] = ComStr('SHCXX')
72 main['CCCOMSTR'] = ComStr('CC')
73 main['SHCCCOMSTR'] = ComStr('SHCC')
74 main['LINKCOMSTR'] = ComStr('LINK')
75 main['SHLINKCOMSTR'] = ComStr('SHLINK')
76 main['ASCOMSTR'] = ComStr('AS')
77 main['ASPPCOMSTR'] = ComStr('ASPP')
78 main['ARCOMSTR'] = ComStr('AR')
79 main['RANLIBCOMSTR'] = ComStr('RANLIB')
80
81 def MakeAction(action, string, *args, **kwargs):
82 def func(target, source, env, executor):
83 tgts = list([str(t).strip() for t in target])
84 return string + ' ' + ', '.join(tgts)
85 return Action(action, func, *args, **kwargs)
86 else:
87 def MakeAction(action, string, *args, **kwargs):
88 return Action(action, *args, **kwargs)
89
90 # Propogate the environment's PATH setting.
91 main['ENV']['PATH'] = os.environ['PATH']
92 # Pass through terminal information to, for instance, enable color output.
93 main['ENV']['TERM'] = os.environ['TERM']
94 # Pass through the java CLASSPATH (if it exists) so we can find libraries.
95 main['ENV']['CLASSPATH'] = os.environ.get('CLASSPATH', '')
96
97 # Detect some dependencies of some forms of the m5 utility/library.
98 def CheckForJavaPkg(context, pkg_name):
99 context.Message('Checking for java package %s...' % pkg_name)
100 result = main['HAVE_JAVA'] and \
101 context.TryAction('${JAVAC} ${JAVACFLAGS} ${SOURCES}',
102 'import %s.*;' % pkg_name, '.java')[0]
103 context.Result(result)
104 return result
105
106 def CheckForPkgConfigPackage(context, package):
107 context.Message('Checking for pkg-config package %s...' % package)
108 result = main['HAVE_PKG_CONFIG'] and \
109 os.system('pkg-config --exists %s' % package) == 0
110 context.Result(result)
111 return result;
112
113 conf = Configure(main, conf_dir=build_dir.Dir('.scons_config'),
114 log_file=build_dir.File('scons_config.log'), custom_tests={
115 'CheckForJavaPkg' : CheckForJavaPkg,
116 'CheckForPkgConfigPackage' : CheckForPkgConfigPackage
117 })
118 main['HAVE_JAVA'] = all(key in main for key in ('JAVAC', 'JAR'))
119 if not main['HAVE_JAVA']:
120 print('javac and/or jar not detected, not building java wrapper.')
121
122 main['HAVE_JUNIT'] = conf.CheckForJavaPkg('org.junit')
123 if main['HAVE_JAVA'] and not main['HAVE_JUNIT']:
124 print('junit test framework not found, not build java wrapper test')
125
126 main['HAVE_PKG_CONFIG'] = conf.CheckProg('pkg-config')
127 if not main['HAVE_PKG_CONFIG']:
128 print("pkg-config not detected, can't check for lua51.")
129
130 main['HAVE_LUA51'] = conf.CheckForPkgConfigPackage('lua51')
131 if not main['HAVE_LUA51']:
132 print('lua 5.1 not detected, not building lua wrapper.')
133
134 conf.Finish()
135
136 # Put the sconsign file in the build dir so everything can be deleted at once.
137 main.SConsignFile(os.path.join(abspath(build_dir), 'sconsign'))
138 # Use soft links instead of hard links when setting up a build directory.
139 main.SetOption('duplicate', 'soft-copy')
140
141 def GTest(env, name, *srcs, **kwargs):
142 if 'GTEST_ENV' not in env:
143 gtest_env = env.Clone(OBJSUFFIX='.to', SHOBJSUFFIX='.sto')
144 gtest_env.Append(CPPFLAGS=[ '${GTEST_CPPFLAGS}' ])
145 gtest_env.Append(LIBS=[ '${GTEST_LIBS}' ])
146 env['GTEST_ENV'] = gtest_env
147
148 if not srcs:
149 srcs = [ name + '.cc', name + '.test.cc' ]
150 test_bin = env['GTEST_ENV'].Program('test/bin/%s' % name, srcs, **kwargs)
151
152 # The native environment doesn't need QEMU, and doesn't define HAVE_QEMU.
153 need_qemu_to_run = 'HAVE_QEMU' in env;
154
155 # If we can run this test...
156 if GetOption('run_tests') and (not need_qemu_to_run or env['HAVE_QEMU']):
157 # An XML file which holds the results of the test.
158 xml = Dir('test').Dir('result').File('%s.xml' % name)
159 # The basic command line for the test.
160 cmd = '${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}'
161 cmd_str = 'TEST'
162 if need_qemu_to_run:
163 # A prefix that runs it in QEMU if necessary.
164 cmd = '${QEMU} -L ${QEMU_SYSROOT} -- ' + cmd
165 cmd_str = 'QEMU_TEST'
166 AlwaysBuild(env.Command(xml, test_bin, MakeAction(cmd, cmd_str)))
167
168 Export('MakeAction')
169
170 main.AddMethod(GTest)
171
172 native = main.Clone()
173 native_dir = build_dir.Dir('native')
174
175 # Bring in the googletest sources.
176 native.SConscript(googletest_dir.File('SConscript'),
177 variant_dir=native_dir.Dir('googletest'), exports={ 'main': native })
178
179 native.SConscript(src_dir.File('SConscript.native'),
180 variant_dir=native_dir, exports={ 'env': native })
181
182 main['CC'] = '${CROSS_COMPILE}gcc'
183 main['CXX'] = '${CROSS_COMPILE}g++'
184 main['AS'] = '${CROSS_COMPILE}as'
185 main['LD'] = '${CROSS_COMPILE}ld'
186 main['AR'] = '${CROSS_COMPILE}ar'
187 main['QEMU'] = 'qemu-${QEMU_ARCH}'
188
189 class CallType(object):
190 def __init__(self, name):
191 self.name = name
192 self.impl_file = None
193 self.enabled = False
194 self.verifier = None
195 self.default = False
196
197 def impl(self, impl, verifier=None, default=False):
198 self.impl_file = impl
199 self.enabled = True
200 self.verifier = verifier
201 self.default = default
202
203 # Being the default can be disabled for testing purposes, so we can tell if
204 # a call type was selected because it was chosen, or because nobody else
205 # was.
206 def setup_env(self, env, allow_default=True):
207 env = env.Clone()
208 is_default = 'true' if self.default and allow_default else 'false'
209 env.Append(CXXFLAGS=[ '-DCALL_TYPE_IS_DEFAULT=%s' % is_default ])
210 return env
211
212 call_types = {
213 # Magic instruction.
214 'inst': CallType('inst'),
215 # Magic address.
216 'addr': CallType('addr'),
217 # Semihosting extension.
218 'semi': CallType('semi'),
219 }
220
221 for root, dirs, files in os.walk(abspath(src_dir)):
222 # Each SConsopts file describes an ABI of the m5 utility.
223 if 'SConsopts' in files:
224 env = main.Clone()
225
226 env['CALL_TYPE'] = copy.deepcopy(call_types)
227
228 # The user may override ABI settings by setting environment
229 # variables of the form ${ABI}.${OPTION}. For instance, to set the
230 # CROSS_COMPILE prefix for abi foo to bar-, the user would set an
231 # environment variable foo.CROSS_COMPILE=bar-.
232 #
233 # This also considers scons command line settings which may look like
234 # environment variables, but are set after "scons" on the command line.
235 def get_abi_opt(name, default):
236 var_name = env.subst('${ABI}.%s' % name)
237 env[name] = os.environ.get(
238 var_name, ARGUMENTS.get(var_name, default))
239
240 # Process the ABI's settings in the SConsopts file, storing them
241 # in a copy of the primary environment.
242 env.SConscript(Dir(root).File('SConsopts'),
243 exports=[ 'env', 'get_abi_opt' ])
244
245 # Check if this version of QEMU is available for running unit tests.
246 env['HAVE_QEMU'] = env.Detect('${QEMU}') is not None
247 if env['HAVE_QEMU'] and env.Detect('${CC}'):
248 sysroot_cmd = env.subst('${CC} -print-sysroot')
249 sysroot = os.popen(sysroot_cmd).read().strip()
250 env['QEMU_SYSROOT'] = sysroot
251
252 # Once all the options have been configured, set up build targets for
253 # this abi.
254 abi_dir = build_dir.Dir(env.subst('${ABI}'))
255 # Bring in the googletest sources.
256 env.SConscript(googletest_dir.File('SConscript'),
257 variant_dir=abi_dir.Dir('googletest'),
258 exports={ 'main': env })
259 env.SConscript(src_dir.File('SConscript'),
260 variant_dir=abi_dir, exports='env')