2017-05-19 82 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