Page MenuHomec4science

site_init.py
No OneTemporary

File Metadata

Created
Fri, Apr 26, 08:14

site_init.py

# -*- coding: utf-8 -*-
# @file
# @section LICENSE
#
# Copyright (©) 2016-19 EPFL (École Polytechnique Fédérale de Lausanne),
# Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import subprocess
import SCons
from SCons.Script import Dir, Configure
# ------------------------------------------------------------------------------
def pybind11(env):
"""A tool to configure pybind11"""
if env.GetOption('clean'):
return
# Run code below to get versions from user preference and not scons
versions_script = """
from __future__ import print_function
import distutils.sysconfig as dsys
from numpy.distutils.misc_util import get_numpy_include_dirs as numpy_dirs
print(dsys.get_python_inc())
for d in numpy_dirs():
print(d)"""
includes = subprocess.check_output([env['py_exec'], "-c", versions_script],
universal_newlines=True).split('\n')
includes += [Dir(env['PYBIND11_ROOT'])]
# Extension of shared library for python
extension = subprocess.check_output([env['py_exec'] + '-config',
"--extension-suffix"],
universal_newlines=True).split('\n')[0]
env['SHLIBSUFFIX'] = extension
env.AppendUnique(CPPPATH=includes)
conf = Configure(env)
if not conf.CheckCXXHeader('pybind11/pybind11.h'):
raise SCons.Errors.StopError(
'Failed to find pybind11 header\n' +
"Run 'git submodule update --init --recursive " +
"third-party/pybind11'")
env = conf.Finish()
# ------------------------------------------------------------------------------
def gen_print(action_string, color_string, env):
"""Generic function for creating pretty compile output"""
if env['verbose']:
return None
def print_fun(command, target, source, env):
colors = env['COLOR_DICT']
print("{}[{}] {}{}".format(colors[color_string],
action_string,
colors['end'],
target[0]))
return print_fun
# ------------------------------------------------------------------------------
def CheckPythonModule(context, module):
"""Checks the existence of a python module"""
context.Message('Checking python module {}... '.format(module))
env = context.sconf.env
command = [env.subst('${py_exec}'), '-c', 'import {}'.format(module)]
context.Log('Executing external command: {}\n'.format(command))
try:
subprocess.check_output(command, stderr=subprocess.STDOUT)
result = True
except subprocess.CalledProcessError as e:
result = False
context.Log(e.output + '\n')
context.Result(result)
return result
# ------------------------------------------------------------------------------
def dummy_command(env, command, error_msg):
"""Creates a dummy scons command"""
def print_error(*args, **kwargs):
print(error_msg)
def print_cmd(*args, **kwargs):
pass
comm = env.Command('#.phony_{}'.format(command),
'', print_error, PRINT_CMD_LINE_FUNC=print_cmd)
env.Alias(command, comm)
# ------------------------------------------------------------------------------
def get_python_version(env):
versions_script = """
from __future__ import print_function
from sysconfig import get_python_version
print(get_python_version())"""
version = subprocess.check_output([env['py_exec'], "-c", versions_script],
universal_newlines=True).replace('\n',
'')
print(version)
return version

Event Timeline