CMake globbing 生成的文件 [英] CMake globbing generated files

查看:17
本文介绍了CMake globbing 生成的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 asn1c 是为了从一个或多个 .asn1 文件中生成一系列 .h.c 文件到给定的文件夹中.

I'm using asn1c in order to produce from one or more .asn1 files a series of .h and .c files into a given folder.

这些 C 文件在名称上与原始 asn1 文件没有对应关系.

These C files have no correspondence in names with the original asn1 files.

这些文件必须与我的链接在一起才能获得工作的可执行文件.我希望能够:

These files must be linked together with mine in order to obtain a working executable. I'd love to be able to:

  • 自动生成build目录下的文件,避免污染项目的其余部分(可能用add_custom_target完成)
  • 指定我的可执行文件对这些文件的依赖关系,以便在文件丢失或 .asn1 文件之一被更新时自动运行 asn1c 可执行文件.
  • 自动将所有生成的文件添加到我的可执行文件的编译中.
  • Automatically generate the files in the build directory to avoid polluting the rest of the project (probably done with add_custom_target)
  • Specify the dependency of my executable on those files, so that the asn1c executable is automatically run if the files are missing or if one of the .asn1 files is updated.
  • Automatically add ALL generated files to the compilation of my executable.

由于事先不知道生成的文件,因此可以对 asn1c 命令的输出目录的任何内容进行 glob - 只要该目录不为空,我很高兴.

Since the generated files are not known in advance it's ok to just glob whatever the contents of the output directory of the asn1c command - as long as the directory is not empty I'm happy.

推荐答案

CMake 期望将完整列表 源传递给 add_executable().也就是说,您不能在 构建阶段 生成 glob 文件 - 为时已晚.

CMake expects complete list of sources to be passed to add_executable(). That is, you cannot glob files generated at build stage - it would be too late.

您有几种方法可以在不事先知道源文件名称的情况下处理生成源文件:

You have several ways for handle generating source files without knowing their names in advance:

  1. 配置阶段使用execute_process生成文件.之后,您可以使用 file(GLOB) 收集源名称并将它们传递给 add_executable():

  1. Generate files at configuration stage with execute_process. After that you may use file(GLOB) for collect source names and pass them to add_executable():

execute_process(COMMAND asn1c <list of .asn1 files>)
file(GLOB generated_sources "${CMAKE_CURRENT_BINARY_DIR}/*.c")
add_executable(my_exe <list of normal sources> ${generated_sources})

如果将来不打算更改用于生成的输入文件(在您的情况下为 .asn1),这是最简单的方法.

If input files for generation (.asn1 in your case) are not intended to be changed in the future, this is the most simple approach which just works.

如果您打算更改输入文件并希望 CMake 检测到这些更改并重新生成源,则应采取更多措施.例如,您可以先使用 configure_file(COPY_ONLY) 将输入文件复制到构建目录.在这种情况下,将跟踪输入文件,如果更改,CMake 将重新运行:

If you intend to change input files and expect CMake to detect these changings and regenerate source, some more action should be taken. E.g., you may first copy input files into build directory with configure_file(COPY_ONLY). In that case input files will be tracked, and CMake will rerun if they are changed:

set(build_input_files) # Will be list of files copied into build tree
foreach(input_file <list of .asn1 files>)
    # Extract name of the file for generate path of the file in the build tree
    get_filename_component(input_file_name ${input_file} NAME)
    # Path to the file created by copy
    set(build_input_file ${CMAKE_CURRENT_BINARY_DIR}/${input_file_name})
    # Copy file
    configure_file(${input_file} ${build_input_file} COPY_ONLY)
    # Add name of created file into the list
    list(APPEND build_input_files ${build_input_file})
endforeach()

execute_process(COMMAND asn1c ${build_input_files})
file(GLOB generated_sources "${CMAKE_CURRENT_BINARY_DIR}/*.c")
add_executable(my_exe <list of normal sources> ${generated_sources})

  • 解析输入文件以确定哪些文件将从它们中创建.不确定它是否适用于 .asn1,但对于某些格式,它适用:

  • Parse input files for determine, which files would be created from them. Not sure whether it works with .asn1, but for some formats this works:

    set(input_files <list of .asn1 files>)
    execute_process(COMMAND <determine_output_files> ${input_files}
        OUTPUT_VARIABLE generated_sources)
    add_executable(my_exe <list of normal sources> ${generated_sources})
    add_custom_command(OUTPUT ${generated_sources}
        COMMAND asn1c ${input_files}
        DEPENDS ${input_files})
    

    在这种情况下,CMake 会检测输入文件的变化(但如果修改了生成的源文件的 list,您需要重新运行 cmake 手动).

    In that case CMake will detect changes in input files (but in case of modification of list of generated source files you need to rerun cmake manually).

    这篇关于CMake globbing 生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆