raptor
raptor

Reputation: 829

CMake policy CMP0046, find_package(), and add_subdirectory()

We have a complicated CMake project that suffers from CMP0046 since upgrading to CMake 3. Here is the basic setup in the root CMakeLists.txt:

find_package(Sqlite)  # Finds the system library at /usr/lib64/libsqlite3.so
...
add_subdirectory(master)

Then in the master/ sub-directory, the CMakeLists.txt:

set(MASTER_DEPS sqlite)
...
add_library(master_lib OBJECT EXCLUDE_FROM_ALL ${MASTER_SOURCES})
add_dependencies(master_lib ${MASTER_DEPS})

The policy warning for CMP0046 is thrown for the sqlite dependency, yet it is found in the main CMakeLists.txt before the add_subdirectory is done.

I've attempted to rewrite our FindSqlite.cmake module several times. Also, the warning goes away if I add sqlite source myself instead of using the find_package call like so:

add_library(sqlite
    ${CMAKE_CURRENT_SOURCE_DIR}/sqlite3.c
    ${CMAKE_CURRENT_SOURCE_DIR}/sqlite3.h
)

For reference, here is our current FindSqlite.cmake module.

How do I make it not warn when finding the system library?

I do not want to suppress the warning.

Upvotes: 0

Views: 1012

Answers (1)

sakra
sakra

Reputation: 65901

The command add_dependencies should only be used for CMake targets introduced with an add_executable, add_library or add_custom_target command.

Libraries found with find_library (as your FindSqlite.cmake does) should be linked with target_link_libraries instead. In your master/CMakeLists.txt use:

target_link_libraries(master_lib ${SQLITE3_LIBRARIES})

The CMP0046 warning should then go away.

Upvotes: 3

Related Questions