From: Andreas Sandberg Date: Wed, 21 Oct 2020 16:41:56 +0000 (+0100) Subject: scons: Test if binaries can embed the Python interpreter X-Git-Tag: develop-gem5-snapshot~575 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bc2e12321a21f026b10e29e2e8a254c335970398;p=gem5.git scons: Test if binaries can embed the Python interpreter Add some more stringent Python tests that ensure that we can link with and run applications that embed Python. This is implemented by running building a small c++ program that embeds Python using PyBind11. The program is run by the build system and prints the version of the Python interpreter. The version information is then used by the build system to ensure that the installed version is supported. Change-Id: I727e0832f171362f5506247c022bea365068a0f6 Signed-off-by: Andreas Sandberg Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/36383 Reviewed-by: Gabe Black Reviewed-by: Daniel Carvalho Maintainer: Gabe Black Tested-by: kokoro --- diff --git a/SConstruct b/SConstruct index 8e7ec3481..a057da308 100755 --- a/SConstruct +++ b/SConstruct @@ -575,6 +575,27 @@ int main(){ context.Result(ret) return ret +def CheckPythonLib(context): + context.Message('Checking Python version... ') + ret = context.TryRun(r""" +#include + +int +main(int argc, char **argv) { + pybind11::scoped_interpreter guard{}; + pybind11::exec( + "import sys\n" + "vi = sys.version_info\n" + "sys.stdout.write('%i.%i.%i' % (vi.major, vi.minor, vi.micro));\n"); + return 0; +} + """, extension=".cc") + context.Result(ret[1] if ret[0] == 1 else 0) + if ret[0] == 0: + return None + else: + return tuple(map(int, ret[1].split("."))) + # Platform-specific configuration. Note again that we assume that all # builds under a given build root run on the same host platform. conf = Configure(main, @@ -582,6 +603,7 @@ conf = Configure(main, log_file = joinpath(build_root, 'scons_config.log'), custom_tests = { 'CheckMember' : CheckMember, + 'CheckPythonLib' : CheckPythonLib, }) # Check if we should compile a 64 bit binary on Mac OS X/Darwin @@ -637,9 +659,6 @@ if main['USE_PYTHON']: main['PYTHON_CONFIG']) print("Info: Using Python config: %s" % (python_config, )) - if python_config != 'python3-config': - warning('python3-config could not be found.\n' - 'Future releases of gem5 will drop support for python2.') py_includes = readCommand([python_config, '--includes'], exception='').split() @@ -697,6 +716,17 @@ main.Prepend(CPPPATH=Dir('ext/pybind11/include/')) marshal_env = main.Clone() marshal_env.Append(CCFLAGS='$MARSHAL_CCFLAGS_EXTRA') marshal_env.Append(LINKFLAGS='$MARSHAL_LDFLAGS_EXTRA') +py_version = conf.CheckPythonLib() +if not py_version: + error("Can't find a working Python installation") + +# Found a working Python installation. Check if it meets minimum +# requirements. +if py_version[0] < 3 or \ + (py_version[0] == 3 and py_version[1] < 6): + error('Python version too old. Version 3.6 or newer is required.') +elif py_version[0] > 3: + warning('Python version too new. Python 3 expected.') # On Solaris you need to use libsocket for socket ops if not conf.CheckLibWithHeader(None, 'sys/socket.h', 'C++', 'accept(0,0,0);'):