[CMake] get name of .lib file

Michael Hertling mhertling at online.de
Mon Nov 7 18:59:57 EST 2011


On 11/07/2011 05:55 PM, Tomasz Grobelny wrote:
> Thanks. But it still means that I have to construct the .lib file name
> sort of manually. And possibly put some IF(WIN32)'s in my CMakeLists.txt. I
> would much prefer to have a list of all files that were produced by given
> target.

Getting such a list at configuration time is particularly difficult,
and the LOCATION property is considered obsolete, AFAIK, and might
have subtle side effects, see [1].

If it's sufficient for you to get that list at build time, I could
offer the following approach: Set the concerned target's diverse
*_OUTPUT_DIRECTORY properties to a target-specific directory; the
latter can be accessed via the $<TARGET_FILE_DIR:target> generator
expression within a custom command that passes this directory to a
CMake script which collects all files by FILE(GLOB ...) and writes
the resulting list to a file. Alternatively, you could treat these
files in the custom command immediately as you will. See here:

# CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(FILELIST C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/files")
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(f STATIC f.c)
SET_TARGET_PROPERTIES(f PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/files/f"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/files/f"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/files/f")
ADD_CUSTOM_COMMAND(TARGET f POST_BUILD
    COMMAND ${CMAKE_COMMAND}
        -DDIR="$<TARGET_FILE_DIR:f>"
        -DFILE="${CMAKE_BINARY_DIR}/f.txt"
        -P ${CMAKE_SOURCE_DIR}/filelist.cmake
    COMMENT "Writing file list for f")
FILE(WRITE ${CMAKE_BINARY_DIR}/g.c "void g(void){}\n")
ADD_LIBRARY(g SHARED g.c)
SET_TARGET_PROPERTIES(g PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/files/g"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/files/g"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/files/g")
ADD_CUSTOM_COMMAND(TARGET g POST_BUILD
    COMMAND ${CMAKE_COMMAND}
        -DDIR="$<TARGET_FILE_DIR:g>"
        -DFILE="${CMAKE_BINARY_DIR}/g.txt"
        -P ${CMAKE_SOURCE_DIR}/filelist.cmake
    COMMENT "Writing file list for g")

# filelist.cmake:
FILE(GLOB FILES "${DIR}/*")
FILE(WRITE "${FILE}")
FOREACH(i IN LISTS FILES)
    FILE(APPEND "${FILE}" "${i}\n")
ENDFOREACH()

After configuring and building, there're {f,g}.txt with the file lists.

'hope that helps.

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg33669.html


More information about the CMake mailing list