[CMake] Libraries with custom build command

Michael Hertling mhertling at online.de
Tue Nov 15 10:11:47 EST 2011


On 11/14/2011 08:11 PM, Mathias Gaunard wrote:
> What is the recommended way to define libraries with custom build commands?
> 
> This trick seems to be the only way:
> 
> if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp)
>    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp)
> endif()
> add_library(foo dummy.cpp)
> add_custom_command(TARGET foo POST_BUILD
>                     COMMAND my command to replace the dummy libfoo
>                    )
> 
> Is it normal that kind of thing is not possible with IMPORTED libraries?

IMO, no: ;-)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(CUSTOMLIBRARIES C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/f.c "void f(void){}\n")
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_BINARY_DIR}/libf.so
    COMMAND gcc -fPIC -c f.c
    COMMAND gcc -fPIC -shared -o libf.so f.o
    DEPENDS ${CMAKE_BINARY_DIR}/f.c)
ADD_CUSTOM_TARGET(libf DEPENDS ${CMAKE_BINARY_DIR}/libf.so)
ADD_LIBRARY(f SHARED IMPORTED)
SET_TARGET_PROPERTIES(f PROPERTIES
    IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/libf.so)
ADD_DEPENDENCIES(f libf)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main f)

Does this suit your needs?

Regards,

Michael


More information about the CMake mailing list