简体   繁体   English

CMake 中后期构建自定义命令中的 Glob 文件

[英]Glob files in post build custom command in CMake

I am generating C# bindings from C++ code using CMake and SWIG.我正在使用 CMake 和 SWIG 从 C++ 代码生成 C# 绑定。 Here is the part of the CMakeLists.txt that calls SWIG and also does what I want to do but in a non-automatic way:这是CMakeLists.txt中调用 SWIG 的部分,它也以非自动方式执行我想做的事情:

...

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

set(CMAKE_SWIG_FLAGS "")

set_source_files_properties(matrixsharp.i PROPERTIES CPLUSPLUS ON)

include_directories("../include/")

set(SOURCE_FILES
    lmfont.cpp
    lmsize.cpp
    lmpoint.cpp
    lmpanel.cpp)

swig_add_module(matrixsharp csharp matrixsharp.i ${SOURCE_FILES})

swig_link_libraries(matrixsharp matrix
                    ...) # Library dependencies

# TODO Don't add the cs files manually but instead try to glob these during the post-build stage and then pass onto the mcs command
set(CSHARP_TEST_APP_SRC
    ${CMAKE_CURRENT_SOURCE_DIR}/TestApp.cs
    ${CMAKE_CURRENT_BINARY_DIR}/LmFont.cs
    ${CMAKE_CURRENT_BINARY_DIR}/LmSize.cs
    ${CMAKE_CURRENT_BINARY_DIR}/LmPoint.cs
    ${CMAKE_CURRENT_BINARY_DIR}/LmMatrixProtocol.cs
    ${CMAKE_CURRENT_BINARY_DIR}/LmPanelType.cs
    ${CMAKE_CURRENT_BINARY_DIR}/LmPanel.cs
    ${CMAKE_CURRENT_BINARY_DIR}/SWIGTYPE_p_unsigned_char.cs
    ${CMAKE_CURRENT_BINARY_DIR}/MatrixSharpPINVOKE.cs
)

add_custom_command(TARGET matrixsharp
    POST_BUILD
    COMMAND mcs ${CSHARP_TEST_APP_SRC}
                -out:Test.exe
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Building C# Test.exe"
)

During the build process all the required *.cs files are generated and put into the binary directory.在构建过程中,所有必需的*.cs文件都会生成并放入二进制目录中。 Since the Test.exe requires all the C# files it would be much easier if I can somehow invoke glob during the post-build stage (but before the custom command above), store the output in a variable and then pass it to the custom command that invokes mcs (Mono compiler) to produce the final executable Test.exe .由于Test.exe需要所有 C# 文件,如果我可以在构建后阶段(但在上面的自定义命令之前)以某种方式调用glob ,将输出存储在变量中,然后将其传递给自定义命令,则会容易得多调用mcs (单声道编译器)来生成最终的可执行文件Test.exe

Is there a way to do that?有没有办法做到这一点? I was thinking of adding another custom command before this one that uses ls and grep to get all the *.cs files from the output binary directory but the problem is that I don't know how to store the returned string in a variable.我正在考虑在此之前添加另一个自定义命令,该命令使用lsgrep从输出二进制目录中获取所有*.cs文件,但问题是我不知道如何将返回的字符串存储在变量中。 The SWIG part is just an example that is specific to my case but the question is more general in nature. SWIG 部分只是一个特定于我的案例的例子,但问题本质上更普遍。

I was able to use SWIG_SUPPORT_FILES successfully for this.为此,我能够成功使用SWIG_SUPPORT_FILES Edit: It does not contain all the files.编辑:它不包含所有文件。

swig_add_library(foo
  TYPE SHARED
    LANGUAGE java
    SOURCES java.i
)
...
get_property(swigGeneratedFiles TARGET foo PROPERTY SWIG_SUPPORT_FILES)
add_jar(foobar
  ${swigGeneratedFiles}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM