如何将Meson中的文件复制到子目录

g_l*_*g_l 6 meson-build

我的应用程序使用 Glade 文件并将数据缓存在 JSON 文件中。当我执行以下操作时,只要用户使用以下命令安装应用程序,一切都会正常工作ninja install

    #Install cached JSON file
    install_data(
        join_paths('data', 'dataCache.json'),
        install_dir: join_paths('myapp', 'resources')
    )
    #Install the user interface glade file
    install_data(
        join_paths('src', 'MainWindow.glade'),
        install_dir: join_paths('myapp', 'resources')
    )
Run Code Online (Sandbox Code Playgroud)

缺点是用户需要安装应用程序。我希望用户能够仅构建应用程序并ninja运行它,而无需安装它(如果他们不想在系统上安装它)。问题是当我这样做时

    #Copy the cached JSON file to the build output directory
    configure_file(input : join_paths('data', 'dataCache.json'),
        output : join_paths('myapp', 'resources', 'dataCache.json'),
        copy: true
    )

    #Copy the Glade file to the build output directory
    configure_file(input : join_paths('src', 'MainWindow.glade'),
        output : join_paths('myapp', 'resources', 'MainWindow.glade'),
        copy: true
    )
Run Code Online (Sandbox Code Playgroud)

我收到错误:输出文件名不得包含子目录。

有没有办法运行ninja并让它在构建文件夹上创建目录myapp/resources,然后将 Glade 和 JSON 文件复制到那里以用作资源?比如让用户运行应用程序而无需做什么ninja install?

小智 3

您可以通过制作脚本并从 Meson 调用它来做到这一点。

例如,在copy.py采用相对输入和输出路径作为参数的文件中:

#!/usr/bin/env python3 

import os, sys, shutil

# get absolute input and output paths
input_path = os.path.join(
    os.getenv('MESON_SOURCE_ROOT'), 
    os.getenv('MESON_SUBDIR'),
    sys.argv[1])

output_path = os.path.join(
    os.getenv('MESON_BUILD_ROOT'), 
    os.getenv('MESON_SUBDIR'),
    sys.argv[2])

# make sure destination directory exists
os.makedirs(os.path.dirname(output_path), exist_ok=True)

# and finally copy the file
shutil.copyfile(input_path, output_path)
Run Code Online (Sandbox Code Playgroud)

然后在你的meson.build文件中:

copy = find_program('copy.py')

run_command(
    copy,
    join_paths('src', 'dataCache.json'), 
    join_paths('myapp', 'resources', 'dataCache.json')
)
run_command(
    copy, 
    join_paths('src', 'MainWindow.glade'), 
    join_paths('myapp', 'resources', 'MainWindow.glade')
)
Run Code Online (Sandbox Code Playgroud)