1 # Copyright 2020 Google, Inc.
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.
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.
31 gem5_root
= Dir('..').Dir('..')
33 # Includes which are shared with gem5 itself.
34 common_include
= gem5_root
.Dir('include')
36 ext_dir
= gem5_root
.Dir('ext')
37 googletest_dir
= ext_dir
.Dir('googletest')
40 build_dir
= Dir('build')
43 return os
.path
.abspath(str(d
))
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.')
51 if GetOption('debug_build'):
52 main
.Append(CXXFLAGS
=[ '-O0', '-g' ])
53 main
.Append(CCFLAGS
=[ '-O0', '-g' ])
55 main
.Append(CXXFLAGS
=[ '-O2' ])
56 main
.Append(CCFLAGS
=[ '-O2' ])
57 main
.Append(CPPPATH
=[ common_include
])
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']
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)
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')
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
83 srcs
= [ name
+ '.cc', name
+ '.test.cc' ]
84 test_bin
= env
['GTEST_ENV'].Program('test/bin/%s' % name
, srcs
, **kwargs
)
86 # The native environment doesn't need QEMU, and doesn't define HAVE_QEMU.
87 need_qemu_to_run
= 'HAVE_QEMU' in env
;
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]}'
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
))
101 main
.AddMethod(GTest
)
103 native
= main
.Clone()
104 native_dir
= build_dir
.Dir('native')
106 # Bring in the googletest sources.
107 native
.SConscript(googletest_dir
.File('SConscript'),
108 variant_dir
=native_dir
.Dir('googletest'), exports
={ 'main': native
})
110 native
.SConscript(src_dir
.File('SConscript.native'),
111 variant_dir
=native_dir
, exports
={ 'env': native
})
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}'
120 class CallType(object):
121 def __init__(self
, name
):
123 self
.impl_file
= None
128 def impl(self
, impl
, verifier
=None, default
=False):
129 self
.impl_file
= impl
131 self
.verifier
= verifier
132 self
.default
= default
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
137 def setup_env(self
, env
, allow_default
=True):
139 is_default
= 'true' if self
.default
and allow_default
else 'false'
140 env
.Append(CXXFLAGS
=[ '-DCALL_TYPE_IS_DEFAULT=%s' % is_default
])
145 'inst': CallType('inst'),
147 'addr': CallType('addr'),
148 # Semihosting extension.
149 'semi': CallType('semi'),
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
:
157 env
['CALL_TYPE'] = copy
.deepcopy(call_types
)
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-.
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
))
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' ])
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
183 # Once all the options have been configured, set up build targets for
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')