view site_scons/site_tools/cpython/__init__.py @ 16:927f2cf75357

Changed build system from SCons to WAF. * WAF is an old fork of SCons that is now for all intents and purposes a different build system. * Unlike SCons which requires a system install of the scons library to work, the entire WAF library is self-contained in a single 'waf' Python script provided with PARPG. * Build instructions are a little different from SCons - execute the local 'waf' script with the arguments 'configure install'. * To make a local install for testing, add the '--destdir=<directory>' option to make all files install under <directory> as a fake root (e.g. '--destdir=dev_install' would make WAF install all files under the 'dev_install' directory in the PARPG source). * Added a waf_paths.py WAF tool to set GNU-compatible installation path variables (i.e. PREFIX, EXEC_PREFIX, LIBDIR, etc.). These variables should be initialized to sane defaults on Windows, where GNU standards don't usually apply.
author M. George Hansen <technopolitica@gmail.com>
date Thu, 09 Jun 2011 21:35:19 -1000
parents 4706e0194af3
children
line wrap: on
line source

"""SCons.Tool.cpython

Tool-specific initialization for Python binary builder.

There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.

"""

#
# Copyright (c) 2001-7,2010 The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "src/engine/SCons/Tool/python.py 3263 2008/07/31 13:50:51 MatiGruca"

import os
try:
    import py_compile
except ImportError:
    raise SCons.Errors.InternalError, "Couldn't import py_compile module"
try:
    import compileall
except ImportError:
    raise SCons.Errors.InternalError, "Couldn't import compileall module"
import glob

import SCons.Action
import SCons.Builder
import SCons.Errors
import SCons.Node
from SCons.Tool.install import installFunc, add_targets_to_INSTALLED_FILES
from SCons.Tool.install import copyFunc

##########################################################################
#  Create Python builder
def createPythonBuilder(env):
    """
    This is a utility function that creates the InstallPython
    Builder in an Environment if it's not there already.

    If it's there already, we return the existing one.

    This builder is based on Install/InstallAs methods. It makes use
    of those builder's functions: installFunc(), and
    add_targets_to_INSTALLED_FILES()).
    """

    try:
        InstallPythonBuilder = env['BUILDERS']['InstallPython']
    except KeyError:
        installpython_action = SCons.Action.Action(installFunc, '$PYTHON_PYCOMSTR')
        InstallPythonBuilder = SCons.Builder.Builder(
            action=installpython_action,
            src_suffix='$PYTHON_SUFFIX',
            target_factory=env.fs.Entry,
            source_factory=env.fs.Entry,
            multi=1,
            emitter=[add_targets_to_INSTALLED_FILES],
            name='InstallPythonBuilder'
        )

    return InstallPythonBuilder

def InstallPython(env, target=None, source=None):
    """
    InstallPython creates .pyc or .pyo files for .py source files
    and adds them to the list of targets along with the source files.
    They are later copied to the destination (target) directory.

    InstallPython takes a target (destination) directory as its first
    argument and a list of source files/directories as a second argument.

    InstallPython returns the list of target files to copy to the
    target directory.
    """
    try:
        target_nodes = env.arg2nodes(target, env.fs.Dir)
    except TypeError:
        raise SCons.Errors.UserError, "Target `%s' of Install() is a file, but should be a directory.  Perhaps you have the InstallPython() arguments backwards?" % str(dir)
    
    source_nodes = env.arg2nodes(source, env.fs.Entry)
    targets = []
    
    # import `compileall` module only if there is a dir in sources list
    dir_in_sources = any(entry.isdir() for entry in sources)
    
    compile_py = env['PYTHON_COMPILE']
    optimize = env['PYTHON_OPTIMIZE']
    if optimize:
        PYTHON_TARGETSUFFIX = 'o'
    else:
        PYTHON_TARGETSUFFIX = 'c'
    
    for target_node in target_nodes:
        for source_node in source_nodes:
            # add *.py and *.pyc files from a directory to tgt list
            if source_node.isdir() and compile_py:
                compileall.compile_dir(str(source_node), maxlevels=0, quiet=True)
                glob_path = os.path.join(source_node.path, '*.py')
                py_and_pycs = glob.glob(glob_path) + glob.glob(glob_path + 'c')
                for file_name in py_and_pycs:
                    target = env.Entry('.'+os.sep+filename, dnode)
                    targets.extend(apply(PythonInstallBuilder, (env, target, filename), kw))
            # add *.py and *.pyo files from a directory to tgt list
            elif isinstance(src, SCons.Node.FS.Dir):
                to_compile = []
                py_files = glob.glob(src.path + os.sep + '*.py')
                for py_file in py_files:
                    to_compile.append(py_file)
                    target_path = '.' + os.sep + py_file

                    # add '.py' file to tgt list
                    py_src = env.fs.Entry(py_file)
                    py_tgt = env.fs.Entry(target_path, dnode)
                    tgt.extend(apply(PIB, (env, py_tgt, py_src), kw))

                    # add '.pyo' file to tgt list
                    pyo_src = env.fs.Entry(py_file + CPYTHON_TARGETSUFFIX)
                    pyo_tgt = env.fs.Entry(target_path + CPYTHON_TARGETSUFFIX, dnode)
                    tgt.extend(apply(PIB, (env, pyo_tgt, pyo_src), kw))
                act = SCons.Action.CommandAction('@$CPYTHON_PYCOM %s' % (' '.join(to_compile)))
                act([], [], env)
            # add single '.py' and '.pyc' or '.pyo' file to tgt list
            else:
                # add '.py' file to tgt list
                target = env.fs.Entry('.'+os.sep+src.path, dnode)
                tgt.extend(apply(PIB, (env, target, src), kw))

                # .pyc or .pyo source and target files
                pyco_src = env.fs.Entry(src.path + CPYTHON_TARGETSUFFIX)
                pyco_tgt = env.fs.Entry(target.path + CPYTHON_TARGETSUFFIX)

                if compile_pyc:
                    py_compile.compile(src.path)
                else:
                    act = SCons.Action.CommandAction('@$CPYTHON_PYCOM %s' % (src.path))
                    act([], [], env)

                # add '.pyc' or '.pyo' file to tgt list
                tgt.extend(apply(PIB, (env, pyco_tgt, pyco_src), kw))

    return tgt

def generate(env):
    try:
        env['INSTALL']
    except KeyError:
        env['INSTALL'] = copyFunc

    global InstallPythonBuilder
    InstallPythonBuilder = createPythonBuilder(env)

    env['PYTHON_OPTIMIZE'] = False # generate '.pyc' files by default
    env['PYTHON_OPTIMIZE_FLAGS'] = '-O'
    env['PYTHON_COMPILE_CMD'] = "-m py_compile $SOURCES"
    env['PYTHON_PYCOM'] = '$PYTHON ${PYTHON_OPTIMIZE_FLAGS if ' \
                          'PYTHON_OPTIMIZE} $CPYTHON_COMPILE_CMD'
    env['PYTHON_PYCOMSTR'] = 'Install file: "$SOURCE" as "$TARGET"'
    env['PYTHON_SUFFIX'] = '.py' # extension for Python source files

    try:
        env.Append(BUILDERS={'InstallPython': InstallPythonBuilder})
    except AttributeError:
        # Looks like we use a pre-0.98 version of SCons...
        from SCons.Script.SConscript import SConsEnvironment
        SConsEnvironment.InstallPython = InstallPythonBuilder

def exists(env):
    return 1