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

问题描述 投票:3回答:1

我有一个几乎工作的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"

“工作”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)

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

Generator解析输入目标和源列表以形成它返回的命令行。对于当前配置,目标和源都是相对路径,因此可能不需要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))

用于PPL工具的init.py:

""" 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)
python scons
1个回答
0
投票

正如在评论中简要描述的那样,重建的原因是使用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特定的代码也被删除并放置在Builder调用的代码中 - 这样,它还需要(为了减少代码的好处)只在需要重建时调用(而不是每次都调用) SCons运行)。

© www.soinside.com 2019 - 2024. All rights reserved.