代码之家  ›  专栏  ›  技术社区  ›  Wang

如何在CMake的execute\u进程中传递list变量?

  •  0
  • Wang  · 技术社区  · 8 年前

    execute_process ,并将一些缓存变量作为 -D 选项。

    typical method 在命令行中传递列表似乎不起作用。

    我尝试了答案中列出的所有组合。我甚至想加入 mylist 具有 "\\;" 或 "\\\\;" 执行\u进程 '-DVal2=a\\;b\\;c\\;' 或 '-DVal2=a;b;c' 到 -Dval2=a b c .

    我怎样才能防止这种情况?仅限 -DVal2=a\\;b\\;c 很管用,但很烦人。

    set(
        mylist
        a
        b
        c
    )
    
    set(
        cmake_args
        "-DVal1=abc"
        "'-DVal2=${mylist}'" #does not work, the execute_process will unpack it into seperated args
    )
    
    execute_process(
            COMMAND ${CMAKE_COMMAND} ${cmake_args} ${CMAKE_SOURCE_DIR}/subproject
            OUTPUT_FILE ${CMAKE_BINARY_DIR}/config.log
            ERROR_FILE ${CMAKE_BINARY_DIR}/config.log
            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/subproject
            RESULT_VARIABLE config_result
    )
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   compor    8 年前

    我认为你需要逃离 ; 字符,这是CMake中列表的默认分隔符,但不清楚如何执行,因此它不适用于您。

    所以,试试这个

    set(mylist_str "")
    
    foreach(item ${mylist})
      string(APPEND mylist_str ${item} "\;")
    endforeach()
    
    # this is for debugging
    message(STATUS "List as string: ${mylist_str}")
    
    set(cmake_args 
        "-DVal1=abc"
        "-DVal2=${mylist_str}"
        "-DVal3=\"${mylist_str}\"" # this has quotes around it
    )
    
    # this is for debugging
    foreach(item ${cmake_args})
      message(STATUS "A list item: ${item}")
    endforeach()
    
        2
  •  3
  •   Carlo Wood    6 年前

    在传递列表之前,请在其上运行以下行:

    string(REPLACE ";" "\\;" escaped_list "${my_list}")
    

    然后通过你的名单。在另一端,它会 与我的\u列表完全相同的值。

    set(my_list "a\;b" "c" "d")
    string(REPLACE ";" "\\;" escaped_list "${my_list}")
    execute_process(COMMAND ${CMAKE_COMMAND} -Dmy_list=${escaped_list} -P test.cmake)
    

    (使用cmake 3.17测试)。

    cmake_args 把它传过去。

    测试1.cmake

    # Construction.
    set(my_list "a\;b" "c" "d")
    set(other_list "e" "f\;g" "h")
    
    # For debugging purposes.
    message("my_list = \"${my_list}\".")
    foreach(arg ${my_list})
      message("-> ${arg}")
    endforeach()
    message("other_list = \"${other_list}\".")
    foreach(arg ${other_list})
      message("-> ${arg}")
    endforeach()
    
    # Encoding.
    string(REPLACE ";" "\\;" escaped_list "${my_list}")
    message("escaped_list = \"${escaped_list}\".")
    
    string(REPLACE ";" "\\;" other_escaped_list "${other_list}")
    message("other_escaped_list = \"${other_escaped_list}\".")
    
    set(cmake_args "-Dother_list=${other_escaped_list}" "-Dmy_list=${escaped_list}")
    
    execute_process(
      COMMAND
      ${CMAKE_COMMAND} ${cmake_args} -P test2.cmake
    )
    

    # For debugging purpose.
    message("my_list = \"${my_list}\".")
    foreach(arg ${my_list})
      message("-> ${arg}")
    endforeach()
    
    message("other_list = \"${other_list}\".")
    foreach(arg ${other_list})
      message("-> ${arg}")
    endforeach()
    

    运行输出 cmake -P test1.cmake :

    my_list = "a\;b;c;d".
    -> a;b
    -> c
    -> d
    other_list = "e;f\;g;h".
    -> e
    -> f;g
    -> h
    escaped_list = "a\\;b\;c\;d".
    other_escaped_list = "e\;f\\;g\;h".
    my_list = "a\;b;c;d".
    -> a;b
    -> c
    -> d
    other_list = "e;f\;g;h".
    -> e
    -> f;g
    -> h
    

    推荐文章