2017-05-19 89 views
1

我正在使用asn1c以便从一个或多个.asn1文件生成一系列.h.c文件到一个给定的文件夹中。CMake globbing生成的文件

这些C文件与原始文件asn1没有对应的名称。

这些文件必须与我的链接在一起,以获得一个可执行的工作。我很想能够:

  • 自动生成生成目录中的文件,以避免污染项目的其余部分(可能与add_custom_target完成)
  • 指定这些文件我的可执行文件的依赖性,以便asn1c可执行文件在文件丢失或.asn1文件中的一个被更新时自动运行。
  • 自动将所有生成的文件添加到我的可执行文件的编译中。

由于生成的文件都事先不知道它的确定只是水珠无论asn1c命令的输出目录的内容 - 只要目录不是空的,我很高兴。

回答

3

CMake预计完整列表来源将被传递到add_executable()。也就是说,你不能在构建阶段上生成glob文件 - 这太迟了。

您有手柄生成源文件几种方式,而不需要事先知道他们的名字:

  1. 配置阶段生成的文件与execute_process。之后,你可以使用file(GLOB)用于收集源名称,并通过他们向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)为代输入文件不打算在未来被改变,这是最简单的方法这只是起作用。

    如果您打算更改输入文件并期望CMake检测这些更改并重新生成源代码,则应采取更多操作。例如,您可以先将输入文件复制到包含configure_file(COPY_ONLY)的构建目录中。在这种情况下,输入文件将被跟踪,如果他们改变的CMake将重新开始:

    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. 用于确定解析输入文件,哪些文件会从他们被创建。不知道是否它与.asn1,但对于某些格式的这个作品:

    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将检测输入文件中的变化(但在生成的源文件的列表修改的情况下,你需要重新运行cmake手动)。

+0

这个真正的问题是,我也可以使用'CMake'实际构建了'asn1c'可执行文件,所以在构建阶段之前,我不能运行它。 – Svalorzen

+0

您可以在*配置阶段使用'execute_process()'建立'asn1c'作为子项目。这看起来像一个很好的决定:不需要推迟在* build阶段*构建所有东西。 – Tsyvarev