使用 SCons 的 VariantDir 和 Repository 来构建使用自定义生成器

Using SCons' VariantDir and Repository to build using custom Generator

我有一个几乎可以工作的 SConstruct 文件。我目前没有使用任何 SConscript 文件,并且不想在我的源存储库中使用任何文件(git,而不是 SCons)。

快速总结 -- 我的问题发生在更改一些参数时,然后返回到以前的参数,重建相同的文件。

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

<lib>.b 应放置在取决于各种标志(Debug/Release、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"

'working' SConstruct 使用带有 generate(env) 的工具,例如:

Builder 然后使用发射器将 lib.src 所依赖的任何 <TARGETDIR>/libx.b 文件添加到 source 列表(从源文件读取)。如果更愿意,可以将这些添加为 libx.b?

生成器解析输入的目标和源列表以形成命令行,returns。使用当前配置,目标和源都是相对路径,因此可能不需要 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 is up to date'。

然后,我试试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 为真时返回简化的命令行来解决这个问题,这样在这种情况下返回的值更像是:

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

其中 "version string" 不受 debug/release、32/64 平台标志的影响(但会随源代码发生变化)。这似乎没有什么区别。

我尝试使用 env.VariantDir(<TARGETDIR>, '.', duplicate=0)tgt = env.BLDR(lib.b, Lib1/lib.src)env.Alias(<TARGETDIR>/lib.b, tgt) 或类似方法对此进行一些变体,但我没有设法改进任何东西(一些配置只是让它总是重建,其他人做到了,所以找不到依赖项并且 SCons 出错。

我应该怎么做?


S构造:

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)

正如评论中简要描述的那样,重建的原因是使用 Decider('make')(即按时间戳检查),有效地匹配源文件以捕获自动生成的文件。

正如 bdbaddog 在对问题的评论中所建议的 运行 scons --debug=explain 时很容易看出这一点。

虽然有点脆,但最简单的解决方案是修改发射器,留下以下内容(见--->标记):

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-特定代码也被删除并放置在构建器调用的代码中 - 这样,另外(为了减少代码的好处)仅在以下情况下调用需要重建(而不是每次 SCons 运行时)。