Glob files in post build custom command in CMake

1.1k Views Asked by At

I am generating C# bindings from C++ code using CMake and SWIG. 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:

...

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

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. The SWIG part is just an example that is specific to my case but the question is more general in nature.

1

There are 1 best solutions below

0
On

I was able to use SWIG_SUPPORT_FILES successfully for this. 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}
)