测试目标是否存在

发布于 2025-01-01 09:27:40 字数 953 浏览 0 评论 0原文

我有一个 SCons 文件,它调用其他人构建程序的不同变体。 遗憾的是,其中一些变体(又名较低的 SConscript)需要通过调用外部程序来构建库。

# /SConstruct
<...>
subdirs = ['variant1', 'variant2', 'variant3']
for subdir in subdirs:
    SConscript(dirs=subdir, src_dir=subdir)

# /variant1/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

# /variant2/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

这给出了警告:

scons: warning: Two different environments were specified for target ../libs/generated.so
                but they appear to have the same action: someexternalscript

这当然是正确的,但我不知道如何检查变体 SConscripts 中是否存在目标。 由于其他人稍后可能会调用 SConscripts,因此我不能依赖全局变量的存在。

I have a SCons file which calls others to build different variants of a program.
Sadly a few of these variants (AKA lower SConscripts) need to build a library via invoking an external program.

# /SConstruct
<...>
subdirs = ['variant1', 'variant2', 'variant3']
for subdir in subdirs:
    SConscript(dirs=subdir, src_dir=subdir)

# /variant1/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

# /variant2/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

This gives the warnings:

scons: warning: Two different environments were specified for target ../libs/generated.so
                but they appear to have the same action: someexternalscript

Which is of course correct, but I don't know how I can check for a target's existence in the variant SConscripts.
And since others may call the SConscripts later on I cannot rely on global variables being present.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生共白头 2025-01-08 09:27:40

很难知道您是否真的认为 ../libs/ generated.so 是构建的一部分。

如果这是一个您希望存在于构建之外的库,但您想确定一下。我建议您使用“配置”来确保该库存在,如果不存在,您可能会失败或运行某些外部脚本来纠正这种情况。

config = Configure(env)
if not config.CheckLib("generated.so") make_generated_so()

实际上如何 make_ generated_so() 取决于您想要如何执行它,您可以将其作为构建的一部分,或者只是在此处的配置阶段运行 shell 脚本。

另一方面,如果您将 ../libs/ generated.so 视为构建的一部分,则必须创建此目标一次,并使依赖目标依赖于此 .so 文件,您可以通过包含 generated.so 来做到这一点节点作为 env.Program("someprogram",target) 中目标的一部分,或者只是执行 env.Depends(sometarget," generated.so")

It is hard to know if you really regard the ../libs/generated.so is a part of the build or not.

If this is a library you expect to exist outside of the build, but you want to make sure. I suggest you use the Configure to make sure that the library exist, and if not you might fail or run the someexternalscript to remedy the situation.

config = Configure(env)
if not config.CheckLib("generated.so") make_generated_so()

How you actually make_generated_so() depends on how you want to do it, you might make it a part of your build or just run the shell script during the config phase here.

If you on the other hand regard the ../libs/generated.so as a part of the build you must create this target once, and make the depending targets depend on this .so file you might do this by including the generated.so node as a part of targets in the env.Program("someprogram",target) or just do env.Depends(sometarget,"generated.so")

好听的两个字的网名 2025-01-08 09:27:40

因此,SConscript1 将构建:../libs/ generated.so,而 SConscript2 将构建相同的 ../libs/ generated.so - 相同的目标文件。

您不能使用 2 个相同的目标(SConscript1 和 SConscript2 中的 ../libs/ generated.so )。您必须为 SConscripts 中的命令之一指定不同的目标。例如,对于 SConscript2 命令(../libs/ generated2.so.

但我不知道如何检查目标是否存在于
变体 SConscripts

为什么需要检查它?如果 scons 检测到目标需要构建,则会构建它。如果任何源文件发生更改,您还可以为运行命令设置额外的依赖项来生成 .so。

genLibCmd = localEnv.Command('../libs/generated.so', '', 'someexternalscript')
Depends(genLibCmd, [ listOfSrcThatSomeExternalScriptUse ])

为了测试现有目标,您可以使用 scons 类文件:

File('#libs/generated.so').exists()

但是,我确信 - 如果您两次构建相同的目标,您会得到错误的 Sconscripts。

So, SConscript1 will build : ../libs/generated.so, and SConscript2 will build same ../libs/generated.so - same target file.

You can't use 2 same targets (../libs/generated.so in SConscript1 and SConscript2). You must specify different target for one of Command in SConscripts. For example for SConscript2 Command(../libs/generated2.so.

but I don't know how I can check for a target's existence in the
variant SConscripts

Why you need to check it? If scons detects target need to build it would be build. You also can set additional dependecy for run Command to generate .so if any of source files would be changed.

genLibCmd = localEnv.Command('../libs/generated.so', '', 'someexternalscript')
Depends(genLibCmd, [ listOfSrcThatSomeExternalScriptUse ])

For test existing target you may use scons class File:

File('#libs/generated.so').exists()

But, i'm sure - you have wrong Sconscripts if you build same target twice.

千纸鹤带着心事 2025-01-08 09:27:40

在这种情况下,我不会克隆该特定进程的环境。我认为您不需要在您的示例中这样做,因为该命令不会更改 env 中的任何内容。之后您可以克隆。
如果这个目标最终被不同的 SConscript 调用,并且它们的环境已经克隆,那么有一个可用于此目的的单一环境。

In this situation, I do not clone the environment for this particular process. And I don't think you need to in your example, as that Command does not change anything in env. You can clone afterwards.
If this target ends up being called from different SConscripts, with their env already cloned, have a single environment accessible for this purpose.

猫七 2025-01-08 09:27:40

如果这确实是代码,我会看到两种删除警告的方法:

# /SConstruct
<...>
subdirs = ['variant1', 'variant2', 'variant3']
for subdir in subdirs:
    SConscript(dirs=subdir, src_dir=subdir)

# /variant1/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

# /variant2/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

另请注意,实际上没有两个 SConscript(一个在variant1中,一个在variant2中,它是相同的SConscript。

第一个:在克隆之前调用一些外部脚本:

# /variant*/SConscript
env.Command('../libs/generated.so', '', 'someexternalscript')
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
<...>

其次:添加一些条件逻辑并通过 SConscript 调用传递一个标志:

# /SConstruct
<...>
subdirs = ['variant1', 'variant2', 'variant3']
for subdir in subdirs:
    build_generated = (subdir == 'variant1')
    SConscript(dirs=subdir, src_dir=subdir, exports=['env','build_generated'])

# /variant*/SConscript
Import('build_generated','env')
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
if build_generated:
    localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

最后:如果您总是希望调用此脚本,则可以将调用放在在S构造中另外

,为什么将目标的源列为“”?

该目标永远不会改变,至少源应该是某个外部脚本。

If this is literally the code, I see two ways to remove the warning:

# /SConstruct
<...>
subdirs = ['variant1', 'variant2', 'variant3']
for subdir in subdirs:
    SConscript(dirs=subdir, src_dir=subdir)

# /variant1/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

# /variant2/SConscript
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

Also note, there really isn't two SConscripts (one in variant1, and one in variant2, its the same SConscript.

First: Call some externalscript before the clone:

# /variant*/SConscript
env.Command('../libs/generated.so', '', 'someexternalscript')
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
<...>

Secondly: add some conditional logic and pass a flag via SConscript call:

# /SConstruct
<...>
subdirs = ['variant1', 'variant2', 'variant3']
for subdir in subdirs:
    build_generated = (subdir == 'variant1')
    SConscript(dirs=subdir, src_dir=subdir, exports=['env','build_generated'])

# /variant*/SConscript
Import('build_generated','env')
localEnv = env.Clone() # Duplicates the global Environment so we may modify it without problems
if build_generated:
    localEnv.Command('../libs/generated.so', '', 'someexternalscript')
<...>

Lastly: if you always want this script called, you can put the call in SConstruct itself.

Additionally, why list the source for the target as ''?

That target will never changed, at the very least the source should be someexternalscript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文