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

How to copy a file in Meson to a subdirectory

我的应用程序使用 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')
    )

缺点是用户需要安装应用程序。如果用户不想在他们的系统上安装它,我希望用户能够使用 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
    )

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

有没有办法 运行 ninja 并让它在构建文件夹中创建目录 myapp/resources,然后将 Glade 和 JSON 文件复制到那里用作资源?比如让用户 运行 的应用不用做 ninja install?

你可以通过制作脚本并从 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)

然后在您的 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')
)