install(...) ignores OPTIONAL keyword under certain conditions
Hi,
I came across a rather puzzling issue today. In our project we declare the installation of our libraries in a manner similar to this example:
# Set up the project.
cmake_minimum_required( VERSION 3.6 )
project( OptionalLibrary VERSION 1.0.0 LANGUAGES CXX )
# Set up the build of a dummy library.
add_library( DummyLibrary SHARED dummy.cxx )
# Set up the installation of this library.
install( TARGETS DummyLibrary
ARCHIVE DESTINATION "lib"
LIBRARY DESTINATION "lib"
OBJECTS DESTINATION "obj"
OPTIONAL )
(It's a long story why we want compiled libraries to be optional during the installation...)
Technically I do such an elaborate call because I didn't want to introduce logic into the code that sets up the installation of the individual libraries, of what sort of library was passed to it.
Now, the problem is that this particular demonstrator project generates the following code for the build directory's cmake_install.cmake
file:
if("x${CMAKE_INSTALL_COMPONENT}x" STREQUAL "xUnspecifiedx" OR NOT CMAKE_INSTALL_COMPONENT)
if(EXISTS "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so" AND
NOT IS_SYMLINK "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so")
file(RPATH_CHECK
FILE "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so"
RPATH "")
endif()
file(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" TYPE SHARED_LIBRARY FILES "/home/krasznaa/Development/cmake/build/libDummyLibrary.so")
if(EXISTS "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so" AND
NOT IS_SYMLINK "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so")
if(CMAKE_INSTALL_DO_STRIP)
execute_process(COMMAND "/usr/bin/strip" "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so")
endif()
endif()
endif()
Notice the missing OPTIONAL
keyword from the file(...)
command!
The issue seems to be tied to the usage of the OBJECTS DESTINATION
specification. If I remove that line completely, I get the expected payload in cmake_install.cmake
. I.e.
if("x${CMAKE_INSTALL_COMPONENT}x" STREQUAL "xUnspecifiedx" OR NOT CMAKE_INSTALL_COMPONENT)
if(EXISTS "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so" AND
NOT IS_SYMLINK "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so")
file(RPATH_CHECK
FILE "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so"
RPATH "")
endif()
file(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" TYPE SHARED_LIBRARY OPTIONAL FILES "/home/krasznaa/Development/cmake/build/libDummyLibrary.so")
if(EXISTS "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so" AND
NOT IS_SYMLINK "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so")
if(CMAKE_INSTALL_DO_STRIP)
execute_process(COMMAND "/usr/bin/strip" "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/lib/libDummyLibrary.so")
endif()
endif()
endif()
What's even more interesting is that if I move the OPTIONAL
keyword "earlier" in my own code, that also results in the correct output. I mean, if I do this:
install( TARGETS DummyLibrary
OPTIONAL
ARCHIVE DESTINATION "lib"
LIBRARY DESTINATION "lib"
OBJECTS DESTINATION "obj" )
So at this point I'm 99.99% sure that I found some bug in CMake's logic.
Cheers, Attila