Q: Is it possible to create CMake targets and file globs after compile time? I am using CMake to compile an external project through ExternalProject_Add. The project compiles perfectly but I need to create additional targets based on the binaries built by the project.
Basically, the external binaries are dependencies for the targets I want to create.
This means that I cannot create a target for a non-existing file (yet) so CMake would complain. For this exact reason, I am unable to create a set of files (file(GLOB files $external_build/bin)) or use any other CMake rule since they are evaluated at configuration time.
Is there a way to evaluate CMake rules after compile time?
Use case
I am working with LLVM and I have modified llc integrating a custom pass to it. I need to test my modified llc executable on real world projects and, for example, here is how I compile the GNU's binutils directly from my source tree:
set(BINUTILS_URL "https://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.xz")
set(BINUTILS_SHA256 "0ab6c55dd86a92ed561972ba15b9b70a8b9f75557f896446c82e8b36e473ee04")
set(BINUTILS_PREFIX ${CMAKE_BINARY_DIR}/binutils)
set(BINUTILS_BINARY_DIR ${BINUTILS_PREFIX}/bin)

find_program(MAKE_BIN NAMES gmake nmake make)
ExternalProject_Add(binutils
        URL ${BINUTILS_URL}
        URL_HASH SHA256=${BINUTILS_SHA256}

        BUILD_IN_SOURCE On
        GIT_PROGRESS On
        CONFIGURE_COMMAND ${WLLVM_CONFIGURE_FLAGS} ./configure --prefix ${BINUTILS_PREFIX}
        BUILD_COMMAND ${WLLVM_COMPILE_FLAGS} ${MAKE_BIN} -j${JOBS}
        )

ExternalProject_Add_Step(binutils
        extract-bitcode
        COMMENT "Extracting binutils' bitcode..."
        DEPENDEES install
        WORKING_DIRECTORY ${BINUTILS_BINARY_DIR}
        COMMAND ${EXTRACT_BC_EXE} ${BINUTILS_BINARY_DIR}
        )

In order to test llc, I need to work on LLVM's bitcode so I'm using wllvm to compile and embed LLVM's bitcode directly into binutils' compiled binaries.
From this point on, I would need the list of binaries (which is pretty simple once you set the output binary directory) to create CMake's targets through add_executable.
I already use add_custom_command() to compile .c files with my custom toolchain: this case would be no different but this time I do not have the bitcode files (conversely to .c files) available since they are created by the extract-bitcode step.
EDIT:
I have followed the suggestions in the comments and I came up with this CMakeLists.txt. I have manually inserted the list of built binaries, which I would like to replace with something automated, and then I've added the directives to create new executables out of the .bc files generated by one of the steps of ExternalProject_Add.
Unfortunately, CMake complains about circular dependencies. I think I have to find a way to make CMake aware of the .bc files.
Here's my CMakeLists.txt:
set(BINUTILS_URL "https://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.xz")
set(BINUTILS_SHA256 "0ab6c55dd86a92ed561972ba15b9b70a8b9f75557f896446c82e8b36e473ee04")
set(BINUTILS_PREFIX ${CMAKE_BINARY_DIR}/binutils)
set(BINUTILS_BINARY_DIR ${BINUTILS_PREFIX}/bin)
set(BINUTILS_BINARY_NAMES
        addr2line
        ar
        as
        c++filt
        elfedit
        gprof
        ld
        ld.bfd
        nm
        objcopy
        objdump
        ranlib
        readelf
        size
        strings
        strip
        )

find_program(MAKE_BIN NAMES gmake nmake make)

# This step will compile GNU's binutils
ExternalProject_Add(binutils
        URL ${BINUTILS_URL}
        URL_HASH SHA256=${BINUTILS_SHA256}

        BUILD_IN_SOURCE On
        GIT_PROGRESS On
        CONFIGURE_COMMAND ${WLLVM_CONFIGURE_FLAGS} ./configure --prefix ${BINUTILS_PREFIX}
        BUILD_COMMAND ${WLLVM_COMPILE_FLAGS} ${MAKE_BIN} -j${JOBS}
        )

# This step will create the ${binutil-exec}.bc files needed in the next step
ExternalProject_Add_Step(binutils
        extract-bitcode
        COMMENT "Extracting binutils' bitcode..."
        DEPENDEES install
        WORKING_DIRECTORY ${BINUTILS_BINARY_DIR}
        COMMAND ${EXTRACT_BC_EXE} ${BINUTILS_BINARY_DIR}
        )

# use llc on the ${binutils-exec}.bc files
foreach (exec_name ${BINUTILS_BINARY_NAMES})
    set(llc_exec_name "${exec_name}-llc")

    add_dependencies(binutils ${llc_exec_name})

    add_executable(${llc_exec_name} ${llc_exec_name}.s)
    target_link_libraries(${llc_exec_name} c)

    add_custom_command(
            OUTPUT
            ${llc_exec_name}.s
            DEPENDS
            binutils llc
            COMMAND
            $<TARGET_FILE:llc>
            ARGS
            ${BINUTILS_BINARY_DIR}/${llc_exec_name}.bc -o ${llc_exec_name}.s
    )
endforeach ()



This is the CMake error when running cmake:
CMake Error: The inter-target dependency graph contains the following strongly connected component (cycle):
  "objcopy-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "elfedit-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "addr2line-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "strip-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "nm-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "as-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "c++filt-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "binutils" of type UTILITY
    depends on "objcopy-llc" (strong)
    depends on "elfedit-llc" (strong)
    depends on "addr2line-llc" (strong)
    depends on "strip-llc" (strong)
    depends on "nm-llc" (strong)
    depends on "as-llc" (strong)
    depends on "c++filt-llc" (strong)
    depends on "ar-llc" (strong)
    depends on "ld.bfd-llc" (strong)
    depends on "objdump-llc" (strong)
    depends on "ld-llc" (strong)
    depends on "gprof-llc" (strong)
    depends on "readelf-llc" (strong)
    depends on "size-llc" (strong)
    depends on "ranlib-llc" (strong)
    depends on "strings-llc" (strong)
  "ar-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "ld.bfd-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "objdump-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "ld-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "gprof-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "readelf-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "size-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "ranlib-llc" of type EXECUTABLE
    depends on "binutils" (strong)
  "strings-llc" of type EXECUTABLE
    depends on "binutils" (strong)
At least one of these targets is not a STATIC_LIBRARY.  Cyclic dependencies are allowed only among static libraries.


A: This can be done with the DEPENDS keyword
ADD_CUSTOM_COMMAND(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/eLut.h
  COMMAND $<TARGET_FILE:eLut> ARGS > ${CMAKE_CURRENT_BINARY_DIR}/eLut.h
  DEPENDS eLut)

See for instance here.
Also an example is given here.
