cmake globbing生成的文件

发布于 2025-02-10 21:03:15 字数 499 浏览 1 评论 0原文

我正在使用asn1c是为了从一个或多个.asn1文件中产生一系列.h.c 。代码>文件到给定文件夹。

这些C文件与原始ASN1文件没有任何对应关系。

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

  • 自动生成构建目录中的文件,以避免污染项目的其余部分(可能使用add_custom_target
  • 指定我的可执行文件对这些文件的依赖性,以便ASN1C如果文件丢失,或者如果更新了.asn1文件之一,则会自动运行。
  • 自动将所有生成的文件添加到我的可执行文件的汇编中。

由于未知生成的文件不知道,只要目录不是空的,我很高兴,只要asn1c命令的输出目录的内容都可以。

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.

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:

  • 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.

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.

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

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

发布评论

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

评论(1

一身软味 2025-02-17 21:03:15

CMAKE期望完整的列表将传递给add_executable()。也就是说,您不能在 build stage 上生成的网络文件 - 为时已晚。

您有几种方法可以处理生成源文件,而无需提前知道其名称:

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

      execute_process(命令ASN1C< .asn1文件的列表>)
    file(glob oferated_sources“ $ {cmake_current_binary_dir}/*。c”)
    add_executable(my_exe<普通来源的列表> $ {generated_sources})
     

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

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

      set(build_input_files)#将被复制到构建树中的文件列表
    foreach(input_file< .asn1文件的列表>)
        #在构建树中的文件的生成路径的文件的提取名称
        get_filename_component(input_file_name $ {input_file}名称)
        #通过复制创建的文件的路径
        set(build_input_file $ {cmake_current_binary_dir}/$ {input_file_name})
        #复制文件
        configure_file($ {input_file} $ {build_input_file} copy_only)
        #将创建文件的名称添加到列表中
        list(append build_input_files $ {build_input_file})
    endforeach()
    
    execute_process(命令asn1c $ {build_input_files})
    file(glob oferated_sources“ $ {cmake_current_binary_dir}/*。c”)
    add_executable(my_exe<普通资源的列表> $ {generated_sources})
     
  2. 解析输入文件以确定,将从它们创建哪些文件。不确定它是否适用于.asn1,但对于某些格式,这起作用:

      set(input_files< .asn1文件的列表>)
    execute_process(command< dester_output_files> $ {input_files}
        output_variable oferated_sources)
    add_executable(my_exe<普通资源的列表> $ {generated_sources})
    add_custom_command(输出$ {generated_sources}
        命令asn1c $ {input_files}
        取决于$ {input_files})
     

    在这种情况下,cmake将检测输入文件中的更改(但如果修改列表生成的源文件,您需要重新运行cmake 手动 em>)。

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. 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})
    

    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.

    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})
    
  2. 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})
    

    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).

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