获取scons根目录

Jas*_*n S 9 scons

我需要按顺序运行两个程序作为自定义构建器的一部分.

其中一个是我坚持的程序,无法处理绝对/相对路径,因此我必须使用chdir=1Builder 的选项,以便其操作在与目标相同的目录中运行.

第二个是位于tools项目子目录中的脚本; SConstruct文件位于项目的根目录中.我需要创建一个操作来运行这个脚本,并且遇到了麻烦,因为我既没有项目的绝对路径,也没有从目标所在的目录返回到tools脚本所在子目录的相对路径.如果我能以某种方式获得我项目的根目录的绝对路径,我将全部设置,我可以连接`tools/myscript.bar'并完成它.

这就是我所拥有的,或多或少:

env['BUILDERS']['FooBar'] = Builder(action = [
    'c:/bin/foo.exe ${SOURCE.filebase}',
    'c:/bin/bar-interpreter.exe myscript.bar ${SOURCE.filebase}',
    ], chdir=1);
Run Code Online (Sandbox Code Playgroud)

问题是我需要更改有问题的操作,以便找到"myscript.bar",例如:

env['BUILDERS']['FooBar'] = Builder(action = [
    'c:/bin/foo.exe ${SOURCE.filebase}',
    'c:/bin/bar-interpreter.exe $PATHTOHERE/tools/myscript.bar ${SOURCE.filebase}',
    ], chdir=1);
Run Code Online (Sandbox Code Playgroud)

这似乎很简单,但我无法弄清楚如何.

ric*_*chq 19

您应该使用"#"表示源目录的顶部.

print Dir('#').abspath
Run Code Online (Sandbox Code Playgroud)

如果您也使用变体目录,则此版本有效.例如在SConstruct中:

SConscript('main.scons', variant_dir="build")
Run Code Online (Sandbox Code Playgroud)

然后在main.scons中:

print Dir('.').abspath
print Dir('#').abspath
Run Code Online (Sandbox Code Playgroud)

第一个将打印/path/to/project/build,而第二个将显示正确/path/to/project.


Jas*_*n S 2

咕噜。很简单; 这似乎有效。

env['BUILD_ROOT'] = Dir('.');
Builder(action = ['somecmd ${BUILD_ROOT.abspath}/tools/myscript.bar ...']);
Run Code Online (Sandbox Code Playgroud)