util: Add a --debug-build option to the m5 util 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
48 # Universal settings.
49 if GetOption('debug_build'):
50 main.Append(CXXFLAGS=[ '-O0', '-g' ])
51 main.Append(CCFLAGS=[ '-O0', '-g' ])
52 else:
53 main.Append(CXXFLAGS=[ '-O2' ])
54 main.Append(CCFLAGS=[ '-O2' ])
55 main.Append(CPPPATH=[ common_include ])
56
57 # Propogate the environment's PATH setting.
58 main['ENV']['PATH'] = os.environ['PATH']
59
60 # Detect some dependencies of some forms of the m5 utility/library.
61 main['HAVE_JAVA'] = all(key in main for key in ('JAVAC', 'JAR'))
62 main['HAVE_PKG_CONFIG'] = main.Detect('pkg-config') is not None
63 main['HAVE_LUA51'] = (main['HAVE_PKG_CONFIG'] and
64 os.system('pkg-config --exists lua51') == 0)
65
66 # Put the sconsign file in the build dir so everything can be deleted at once.
67 main.SConsignFile(os.path.join(abspath(build_dir), 'sconsign'))
68 # Use soft links instead of hard links when setting up a build directory.
69 main.SetOption('duplicate', 'soft-copy')
70
71 def GTest(env, name, *srcs, **kwargs):
72 if 'GTEST_ENV' not in env:
73 gtest_env = env.Clone(OBJSUFFIX='.to', SHOBJSUFFIX='.sto')
74 gtest_env.Append(CPPFLAGS=[ '${GTEST_CPPFLAGS}' ])
75 gtest_env.Append(LIBS=[ '${GTEST_LIBS}' ])
76 env['GTEST_ENV'] = gtest_env
77
78 if not srcs:
79 srcs = [ name + '.cc', name + '.test.cc' ]
80 test_bin = env['GTEST_ENV'].Program('test/bin/%s' % name, srcs, **kwargs)
81
82 # The native environment doesn't need QEMU, and doesn't define HAVE_QEMU.
83 need_qemu_to_run = 'HAVE_QEMU' in env;
84
85 # If we can run this test...
86 if not need_qemu_to_run or env['HAVE_QEMU']:
87 # An XML file which holds the results of the test.
88 xml = Dir('test').Dir('result').File('%s.xml' % name)
89 # The basic command line for the test.
90 cmd = '${SOURCES[0]} --gtest_output=xml:${TARGETS[0]}'
91 if need_qemu_to_run:
92 # A prefix that runs it in QEMU if necessary.
93 cmd = '${QEMU} -L ${QEMU_SYSROOT} -- ' + cmd
94 AlwaysBuild(env.Command(xml, test_bin, cmd))
95
96 main.AddMethod(GTest)
97
98 native = main.Clone()
99 native_dir = build_dir.Dir('native')
100
101 # Bring in the googletest sources.
102 native.SConscript(googletest_dir.File('SConscript'),
103 variant_dir=native_dir.Dir('googletest'), exports={ 'main': native })
104
105 native.SConscript(src_dir.File('SConscript.native'),
106 variant_dir=native_dir, exports={ 'env': native })
107
108 main['CC'] = '${CROSS_COMPILE}gcc'
109 main['CXX'] = '${CROSS_COMPILE}g++'
110 main['AS'] = '${CROSS_COMPILE}as'
111 main['LD'] = '${CROSS_COMPILE}ld'
112 main['AR'] = '${CROSS_COMPILE}ar'
113 main['QEMU'] = 'qemu-${QEMU_ARCH}'
114
115 class CallType(object):
116 def __init__(self, name):
117 self.name = name
118 self.impl_file = None
119 self.enabled = False
120 self.verifier = None
121 self.default = False
122
123 def impl(self, impl, verifier=None, default=False):
124 self.impl_file = impl
125 self.enabled = True
126 self.verifier = verifier
127 self.default = default
128
129 # Being the default can be disabled for testing purposes, so we can tell if
130 # a call type was selected because it was chosen, or because nobody else
131 # was.
132 def setup_env(self, env, allow_default=True):
133 env = env.Clone()
134 is_default = 'true' if self.default and allow_default else 'false'
135 env.Append(CXXFLAGS=[ '-DCALL_TYPE_IS_DEFAULT=%s' % is_default ])
136 return env
137
138 call_types = {
139 # Magic instruction.
140 'inst': CallType('inst'),
141 # Magic address.
142 'addr': CallType('addr'),
143 # Semihosting extension.
144 'semi': CallType('semi'),
145 }
146
147 for root, dirs, files in os.walk(abspath(src_dir)):
148 # Each SConsopts file describes an ABI of the m5 utility.
149 if 'SConsopts' in files:
150 env = main.Clone()
151
152 env['CALL_TYPE'] = copy.deepcopy(call_types)
153
154 # The user may override ABI settings by setting environment
155 # variables of the form ${ABI}.${OPTION}. For instance, to set the
156 # CROSS_COMPILE prefix for abi foo to bar-, the user would set an
157 # environment variable foo.CROSS_COMPILE=bar-.
158 #
159 # This also considers scons command line settings which may look like
160 # environment variables, but are set after "scons" on the command line.
161 def get_abi_opt(name, default):
162 var_name = env.subst('${ABI}.%s' % name)
163 env[name] = os.environ.get(
164 var_name, ARGUMENTS.get(var_name, default))
165
166 # Process the ABI's settings in the SConsopts file, storing them
167 # in a copy of the primary environment.
168 env.SConscript(Dir(root).File('SConsopts'),
169 exports=[ 'env', 'get_abi_opt' ])
170
171 # Check if this version of QEMU is available for running unit tests.
172 env['HAVE_QEMU'] = env.Detect('${QEMU}') is not None
173 if env['HAVE_QEMU'] and env.Detect('${CC}'):
174 sysroot_cmd = env.subst('${CC} -print-sysroot')
175 sysroot = os.popen(sysroot_cmd).read().strip()
176 env['QEMU_SYSROOT'] = sysroot
177
178 # Once all the options have been configured, set up build targets for
179 # this abi.
180 abi_dir = build_dir.Dir(env.subst('${ABI}'))
181 # Bring in the googletest sources.
182 env.SConscript(googletest_dir.File('SConscript'),
183 variant_dir=abi_dir.Dir('googletest'),
184 exports={ 'main': env })
185 env.SConscript(src_dir.File('SConscript'),
186 variant_dir=abi_dir, exports='env')