使用SCons的VariantDir和存储库通过自定义生成器进行构建

时间:2019-04-05 06:33:47

标签: python scons

我有一个工作正常的SConstruct文件。我目前不使用任何SConscript文件,并且希望在我的源代码存储库(git,而不是SCons)中不需要任何文件。

快速总结-更改某些参数,然后返回到先前的参数时,会出现我的问题,

我运行scons -f Builder_repo/SConstruct 'NameOfTargetLibrary.b'来建立一个库,NameOfTargetLibrary.b来自NameOfTargetLibrary.src

<lib>.b应该放置在取决于各种标志(调试/释放,32/64位,平台(来自列表))的位置,如下所示:

topdir
|\-- Builder_repo (containing SConstruct, site_scons\...)
|\-- Lib1 (contains lib1.src, bunch of source files)
|\-- Lib2 (lib2.src, lib2 sources)
\--- BuiltLibs
     |\-- Windows
     |    |\-- Release_32
     |    |    |\-- lib1.b
     |    |    |\-- lib2.b
     |    |    \--- libn.b
     |    |\-- Debug_64
     |    |    |\-- lib1.b
     |    |    |\-- lib2.b
     |    |    \--- libn.b
     |    \--- (Debug_32, Release_64)
     \--- (etc, other targets, currently just one)

命令行类似于(为了便于阅读,分成多行,但SCons / cmdLine中只有一行)

"abspath to exe" "static path and args" --x64 -- 
    "abspath(lib1.src)" "abspath(BuiltLibs)"
    "abspath(BuiltLibs/Windows/Release_64)" 
    "flags for debug/release, target, bitness"

“正在运行”的SConstruct使用带有generate(env)的工具,例如:

  • 构建目标目录(例如BuiltLibs\Windows\Release_32)存储在env中。
  • 搜索.src个文件
  • 获取包含目录(使用os.path.dirname
  • 添加到env.Repositories(dirname(lib.src))
  • tgt = env.BLDR(<TARGETDIR>/lib.b, lib.src)
  • env.Alias(lib.b, tgt)

然后,生成器使用发射器将source所依赖的任何<TARGETDIR>/libx.b文件(从源文件读取)添加到lib.src列表中。如果可以的话,可以将它们添加为libx.b

生成器解析输入目标和源列表以形成命令行,并返回该命令行。在当前配置下,目标和源都是相对路径,因此可能Repository调用是不必要的。

我跑步时

scons -f Builder_repo\SConstruct 'lib2.b' DEBUG=0 BITNESS=32 TRGT=Windows

(由于发射器的原因,lib2.src依赖于lib1.b),正确的lib1.blib2.b会被构建并放置在BuiltLibs\Windows\Release_32\lib{1,2}.b中。

如果我重复执行该命令,则不会构建任何内容,并且'lib2.b是最新的'。

然后,我尝试scons -f <..> 'lib2.b' DEBUG=1 <other args same>。这两个库均已按预期构建并放置在BuiltLibs\Windows\Debug_32\lib{1,2}.b中。

当我再次尝试第一个命令(DEBUG=0)时,我期望什么都不会构建(lib1.b,lib2.b仍是最新的-没有更改源,并且先前构建的文件仍在Release_32),而是将它们重建。

我尝试通过在for_signature为true时返回精简的命令行来解决此问题,使得在这种情况下返回的值更像是:

"abspath to exe" "static path and args" --
     "abspath(lib1.src)" "abspath(BuiltLibs)" "version string" 

其中“版本字符串”不受调试/发行版32/64平台标志的影响(但会随源代码而改变)。这似乎没有什么区别。

我尝试使用env.VariantDir(<TARGETDIR>, '.', duplicate=0),然后使用tgt = env.BLDR(lib.b, Lib1/lib.src)env.Alias(<TARGETDIR>/lib.b, tgt)或类似方法对此进行了一些修改,但是我没有设法改善任何地方(某些配置总是使其得以重新构造,而其他配置做到了这一点,因此找不到依赖项并且SCons错误。

我应该怎么做?


SConstruct:

import os

Decider('make')
Default(None)

# Add command line arguments with default values.
# These can be specified as, for example, LV_TARGET=cRIO
cmdVars = Variables(None, ARGUMENTS)
cmdVars.AddVariables(
    EnumVariable('LV_TARGET', 'Choose the target for LabVIEW packages', 
        'Windows', allowed_values=('Windows', 'cRIO')),
    BoolVariable('DEBUG', 'Set to 1 to build a debug-enabled package', 0),
    EnumVariable('BITNESS', 'Choose the bitness for LabVIEW packages', 
        '32', allowed_values=('32', '64')),
    EnumVariable('LV_VER', 'Choose the version of LabVIEW to use', 
        '2017', allowed_values=('2017',))
)

# Define a list of source extensions
src_exts = ['.vi', '.ctl', '.lvlib', '.vim', '.vit']

env = Environment(variables = cmdVars, ENV = os.environ, tools=['PPL'], PPLDIR='PPLs', SRC_EXTS=' '.join(src_exts))

init.py 用于PPL工具:

""" SCons.Tool.PPL
Tool-specific initialization for compilation of lvlibp files from lvlib files,
using the Wiresmith 'labview-cli.exe' and the LabVIEW code stored in the 
PPL_Builder GitHub repository.
This module should not usually be imported directly.
It can be imported using a line in SConstruct or SConscript like
env = Environment(tools=['PPL'])
"""

# A reference for this code can be found at
# https://github.com/SCons/scons/wiki/ToolsForFools
# which describes the creation of a Tool for JALv2 compilation.

import SCons.Builder
from SCons.Script import GetOption
import SCons.Node
import SCons.Util
import os.path
import textwrap
import re

import contextlib
import subprocess

# Add warning classes
class ToolPPLWarning(SCons.Warnings.Warning):
    pass

class LabVIEW_CLI_ExeNotFound(ToolPPLWarning):
    pass

SCons.Warnings.enableWarningClass(ToolPPLWarning)

__verbose = False

class LV_BuildPaths:
    """ A simple class to contain the build paths 
        and configuration flags for PPL compilation

        Contains the attributes:
        hwTarget{,Dir}, debug{Opt,Flag,String}, bitness{,Flag}, lv_ver,
        {ppl,storage,copy,topData,data}Dir
    """
    def __init__(self, env):
        # Set the target parameters
        self.hwTarget = env.get('LV_TARGET')
        copyDirExtra = ""
        if self.hwTarget == "cRIO":
            self.hwTargetDir = "cRIO-9045"
            copyDirExtra = os.path.join('home','lvuser','natinst','bin')
        else:
            self.hwTargetDir = self.hwTarget
        # Set the debug parameters
        self.debugOpt = env.get('DEBUG')
        self.debugFlag = int(self.debugOpt)
        self.debugString = "Debug" if self.debugOpt else "Release"
        # Set the bitness parameters
        self.bitness = env.get('BITNESS')
        self.bitnessFlag = ''
        if self.bitness == '64':
            self.bitnessFlag = '--x64'
        # Get the LabVIEW year
        self.lv_ver = env.get('LV_VER')

        # Get important build directory paths.
        # PPL files should be searched for in storageDir
        self.pplDir = os.path.normpath(env.get('PPLDIR', 'PPLs'))
        self.storageDir = os.path.join(self.pplDir, self.hwTargetDir, f'{self.debugString}_{self.bitness}', copyDirExtra)
        self.copyDir = os.path.join(self.pplDir, self.hwTargetDir, copyDirExtra)
        self.topDataDir = os.path.join(self.pplDir, 'Data')
        self.dataDir = os.path.join(self.copyDir, 'Data')

    def __str__(self):
        return (textwrap.dedent(f"""\
        The directories are as follows...
            PPL Dir:      {self.pplDir}
            Storage Dir:  {self.storageDir}
            Copy Dir:     {self.copyDir}""")
        ) 


def _print_info(message):
    """ Disable the output of messages if '--quiet', '-s' or '--silent'
        are passed to the command line """
    if not GetOption('silent'):
        print(message)

def _detectCLI(env):
    """ Search for the labview-cli.exe installed by Wiresmith's VIPackage """
    try:
        # If defined in the environment, use this
        _print_info(f"labview-cli defined in the environment at {env['LV_CLI']}")
        return env['LV_CLI']
    except KeyError:
        pass

    cli = env.WhereIs('labview-cli')
    if cli:
        _print_info(f"Found labview-cli at {cli}")
        return cli

    raise SCons.Errors.StopError(
        LabVIEW_CLI_ExeNotFound,
        "Could not detect the labview-cli executable")
    return None

@contextlib.contextmanager
def pushd(new_dir):
    previous_dir = os.getcwd()
    os.chdir(new_dir)
    yield
    os.chdir(previous_dir)

def _getHash(env, dir):
    if env['GIT_EXE']:
        with pushd(dir):
            #cmdLine = env['git_showref']
            cmdLine = env['git_describe']
            return subprocess.run(cmdLine, shell=True, capture_output=True, text=True).stdout
    return ''

def _detectGit(env):
    """ Search for a git executable. This is not required for usage """
    git = None
    try:
        # If defined in the environment, use this
        _print_info(f"git executable defined in the environment at {env['GIT_EXE']}")
        git = env['GIT_EXE']
    except KeyError:
        pass

    cli = env.WhereIs('git')
    if cli:
        _print_info(f"Found git at {cli}")
        git = cli

    if git:
        hash_len = 12
        env['GIT_EXE'] = f"'{git}'" # I edited this line compared to the version in the repository, but I don't think it's relevant.
        env['git_describe'] = f'"{git}" describe --dirty="*" --long --tags --always --abbrev={hash_len}'
        env['git_showref'] = f'"{git}" show-ref --hash={hash_len} --head head'
    return None

#
# Builder, Generator and Emitter
#
def _ppl_generator(source, target, env, for_signature):
    """ This function generates the command line to build the PPL.
        It should expect to receive a target as a relative path
        ['<SD>/A.lvlibp'], and source will be either None, or
        ['<src>/A.lvlib'].

        When for_signature == 0, the PPL will actually be built.
    """

    # Get these parameters properly
    run_vi = os.path.abspath(os.path.join('.','PPL_Builder','Call_Builder_Wiresmith.vi'))
    cliOpts = ''
    package_ver = "0.0.0.0#sconsTest"

    # These are extracted from the environment
    cli = env['LV_CLI']
    bp = env['LV_Dirs']
    ver = bp.lv_ver
    pplDir = f'{os.path.abspath(bp.pplDir)}'
    storageDir = f'{os.path.abspath(bp.storageDir)}'
    # Dependencies are parsed for the command line. They are already dependencies of the target.
    pplSrcs = source[1:]
    depsString = ""
    if pplSrcs:
        if __verbose:
            _print_info("Adding PPL dependencies: %s" % [ str(ppl) for ppl in pplSrcs ])
        depsString = " ".join([f'"{os.path.basename(ppl.get_string(for_signature))}"' for ppl in pplSrcs])

    cmdLine = f'"{cli}" --lv-ver {ver} {bp.bitnessFlag} {run_vi} {cliOpts} -- '
    lvlib_relpath = str(source[0])
    lvlib_abspath = os.path.abspath(lvlib_relpath)
    git_ver = _getHash(env, os.path.dirname(lvlib_abspath))
    print("git version is " + str(git_ver).strip())

    argsLine = f'"{lvlib_abspath}" "{pplDir}" "{storageDir}" {bp.debugFlag} {bp.hwTarget} "{package_ver}" {depsString}'

    if not for_signature:
        _print_info(f"Making {lvlib_abspath}")

    return cmdLine + argsLine
    #return cmdLine + argsLine

def _ppl_emitter(target, source, env):
    """ Appends any dependencies found in the .mk file to the list of sources.
        The target should be like [<SD>/A.lvlibp], 
        and the source should be like [<src>/A.lvlib]
    """
    if not source:
        return target, source
    exts_tuple = tuple(env['SRC_EXTS'].split(' '))
    src_files = _get_other_deps(source, exts_tuple)
    if __verbose:
        _print_info("Adding " + str(src_files) + " as dependencies")
    env.Depends(target, src_files)
    depsList, nodeDepsList = _get_ppl_deps(str(source[0]), env)
    if nodeDepsList:
        source += [os.path.normpath(os.path.join(env['LV_Dirs'].storageDir, str(pplNode))) for pplNode in nodeDepsList]
    return target, source

_ppl_builder = SCons.Builder.Builder(generator = _ppl_generator, emitter = _ppl_emitter)

def lvlibpCreator(env, target, source=None, *args, **kw):
    """ A pseudo-Builder for the labview-cli executable
        to build .lvlibp files from .lvlib sources, with
        accompanying dependency checks on appropriate source files

        Anticipate this being called via env.PPL('<SD>/A.lvlibp'),
        where target is a string giving a relative path, or
        env.PPL('<SD>/A.lvlibp', '<src>/A.lvlib')
    """
    bPaths = env['LV_Dirs']

    # Ensure that if source exists, it is a list
    if source and not SCons.Util.is_List(source):
        source = [source]

    if __verbose:
        _print_info(f"Target = {target}")
        if source:
            _print_info("Sources = %s" % [ str(s) for s in source])
    if __verbose:
        _print_info("args: %s" % [ str(s) for s in args ])
        _print_info("kw: %s" % str(kw.items()))

    tgt = _ppl_builder.__call__(env, target, source, **kw)
    return tgt

def _scanForLvlibs(env, topdir=None):
    # Maybe check this...
    if not topdir:
        topdir = '.'

    bPaths = env['LV_Dirs']
    lvlibList = []
    for root, dirs, files in os.walk(topdir):
        # if any of files ends with .lvlib, add to the list
        lvlibList += map(lambda selected: os.path.join(root, selected), filter(lambda x: x[-6:] == '.lvlib', files))
    for lib in lvlibList:
        # Set up the possibility of building the lvlib
        (srcDir, libnameWithExt) = os.path.split(lib)
        # Add the source repository
        if __verbose:
            _print_info("Adding repository at: " + srcDir)
        env.Repository(srcDir)
        # Add the build instruction
        lvlibpName = libnameWithExt + 'p'
        tgt = env.PPL(os.path.normpath(os.path.join(bPaths.storageDir, lvlibpName)),lib)
        if __verbose:
            _print_info(f"Adding alias from {libnameWithExt+'p'} to {str(tgt)}")
        env.Alias(lvlibpName, tgt)

def _get_ppl_deps(lvlib, env):
    lvlib_s = str(lvlib)
    lvlib_name = os.path.basename(lvlib_s)
    mkFile = lvlib_s.replace('.lvlib','.mk')
    if os.path.isfile(mkFile):
        # load dependencies from file
        depVarName = lvlib_name.replace(' ',r'\+').replace('.lvlib','_Deps')
        f = open(mkFile, "r")
        content = f.readlines() # Read all lines (not just first)
        depsList = []
        for line in content:
            matchedDeps = re.match(depVarName+r'[ ]?:=[ ]?(.*)$', line)
            if matchedDeps:
                listDeps = matchedDeps.group(1).replace(r'\ ','+').split(' ')
                depsList = ['"' + elem.replace('+', ' ') + '"' for elem in listDeps]
                nodeList = [ env.File(elem.replace('+', ' ')) for elem in listDeps]
                return (depsList, nodeList)
        raise RuntimeError("Found a .mk file ({mkFile}) but could not parse it to get dependencies.")
    #print(f"No .mk file for {lvlib_name}")
    return ('', None)

def _get_other_deps(source, exts):
    parent_dir = os.path.dirname(str(source[0]))
    if __verbose:
        _print_info(f"Searching {parent_dir} for source files...")
        _print_info(f"Acceptable extensions are {exts}")
    src_files = []
    for root, dirs, files in os.walk(parent_dir):
        src_files += [os.path.join(root, file) for file in files if file.endswith(exts)]
    return src_files

def generate(env):
    '''Add builders and construction variables to the Environment.'''
    env['LV_CLI'] = _detectCLI(env)
    env.AddMethod(lvlibpCreator, "PPL")
    _detectGit(env)

    bp = LV_BuildPaths(env)
    _print_info(bp)
    env['LV_Dirs'] = bp

    # Search for all lvlib files
    _scanForLvlibs(env)

def exists(env):
    return _detectCLI(env)

1 个答案:

答案 0 :(得分:0)

如评论中所述,重建的原因是使用Decider('make')(即按时间戳检查),并且有效地捕获了源文件并捕获了自动生成的文件。

在运行scons --debug=explain时很容易看出来,这是bdbaddog在问题注释中建议的。

尽管有些脆弱,但最简单的解决方案是修改发射器,保留以下内容(请参见--->标记):

def _ppl_emitter(target, source, env):
    """ Appends any dependencies found in the .mk file to the list of sources.
        The target should be like [<SD>/A.lvlibp], 
        and the source should be like [<src>/A.lvlib]
    """
    if not source:
        return target, source
    exts_tuple = tuple(env['SRC_EXTS'].split(' '))
    src_files = _get_other_deps(source, exts_tuple)
--->filtered_files = list(filter(lambda x: "Get PPL Version.vi" not in x, src_files))
    if __verbose:
        _print_info("Adding " + str(filtered_files) + " as dependencies")
    env.Depends(target, filtered_files)
    depsList, nodeDepsList = _get_ppl_deps(str(source[0]), env)
    if nodeDepsList:
        source += [os.path.normpath(os.path.join(env['LV_Dirs'].storageDir, str(pplNode))) for pplNode in nodeDepsList]
    return target, source

通过删除此文件,目标不再对生成的文件有明确的依赖性(这与Decider调用无关)。

从SConstruct文件中另外删除Decider('make')行可以删除并重新下载整个源存储库,而不会触发重建。

作为旁注,还删除了特定于Git的代码,并将其放置在Builder调用的代码内-这样,仅在需要重建时才调用(减少代码收益)比每次SCons都运行)。