Skip to content

Commit 4256a6e

Browse files
committed
CMake: treat third party deps IMPORTED
CMake thirdParty libraries should be marked as imported. We do so by creating `openPMD::thirdparty::` targets that are imported only and depend on `find_package` or `add_subdirectory` targets to decouple them. Imported targets are always SYSTEM libraries: https://cmake.org/cmake/help/v3.8/prop_tgt/NO_SYSTEM_FROM_IMPORTED.html That allows us to use stricter warnings in our project than third party headers support.
1 parent 33fcb5b commit 4256a6e

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Other
3838

3939
- Docs: upgrade guide added #385
4040
- CI: GCC 8.1.0 & Python 3.7.0 #376
41+
- CMake: treat third party libraries properly as ``IMPORTED`` #389
4142

4243

4344
0.6.2-alpha

CMakeLists.txt

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ elseif(openPMD_USE_JSON)
125125
else()
126126
set(openPMD_HAVE_JSON FALSE)
127127
endif()
128+
if(openPMD_USE_JSON)
129+
add_library(openPMD::thirdparty::nlohmann_json INTERFACE IMPORTED)
130+
target_link_libraries(openPMD::thirdparty::nlohmann_json
131+
INTERFACE nlohmann_json::nlohmann_json)
132+
endif()
128133

129134
# external library: HDF5 (optional)
130135
if(openPMD_USE_HDF5 STREQUAL AUTO)
@@ -265,6 +270,11 @@ elseif(openPMD_USE_PYTHON)
265270
else()
266271
set(openPMD_HAVE_PYTHON FALSE)
267272
endif()
273+
if(openPMD_HAVE_PYTHON)
274+
add_library(openPMD::thirdparty::pybind11 INTERFACE IMPORTED)
275+
target_link_libraries(openPMD::thirdparty::pybind11
276+
INTERFACE pybind11::pybind11)
277+
endif()
268278

269279

270280
# Targets #####################################################################
@@ -330,26 +340,31 @@ target_include_directories(openPMD PUBLIC
330340
)
331341

332342
# C++11 std::variant (C++17 stdlib preview)
343+
add_library(openPMD::thirdparty::mpark_variant INTERFACE IMPORTED)
333344
if(openPMD_USE_INTERNAL_VARIANT)
334-
target_include_directories(openPMD SYSTEM PUBLIC
345+
target_include_directories(openPMD::thirdparty::mpark_variant SYSTEM INTERFACE
335346
$<BUILD_INTERFACE:${openPMD_SOURCE_DIR}/share/openPMD/thirdParty/variant/include>
336347
)
337348
message(STATUS "MPark.Variant: Using INTERNAL version 1.3.0")
338349
else()
339350
find_package(mpark_variant 1.3.0 REQUIRED)
340-
target_link_libraries(openPMD PUBLIC mpark_variant)
351+
target_link_libraries(openPMD::thirdparty::mpark_variant
352+
INTERFACE mpark_variant)
341353
message(STATUS "MPark.Variant: Found version ${mpark_variant_VERSION}")
342354
endif()
343355

344356
# Catch2 for unit tests
345357
if(BUILD_TESTING)
358+
add_library(openPMD::thirdparty::Catch2 INTERFACE IMPORTED)
346359
if(openPMD_USE_INTERNAL_CATCH)
347-
target_include_directories(openPMD SYSTEM PUBLIC
360+
target_include_directories(openPMD::thirdparty::Catch2 SYSTEM INTERFACE
348361
$<BUILD_INTERFACE:${openPMD_SOURCE_DIR}/share/openPMD/thirdParty/catch2/include>
349362
)
350363
message(STATUS "Catch2: Using INTERNAL version 2.3.0")
351364
else()
352365
find_package(Catch2 2.3.0 CONFIG REQUIRED)
366+
target_link_libraries(openPMD::thirdparty::Catch2
367+
INTERFACE Catch2::Catch2)
353368
message(STATUS "Catch2: Found version ${Catch2_VERSION}")
354369
endif()
355370
endif()
@@ -366,7 +381,7 @@ endif()
366381

367382
# JSON Backend
368383
if(openPMD_HAVE_JSON)
369-
target_link_libraries(openPMD SYSTEM PRIVATE nlohmann_json::nlohmann_json)
384+
target_link_libraries(openPMD PRIVATE openPMD::thirdparty::nlohmann_json)
370385
target_compile_definitions(openPMD PUBLIC "-DopenPMD_HAVE_JSON=1")
371386
else()
372387
target_compile_definitions(openPMD PUBLIC "-DopenPMD_HAVE_JSON=0")
@@ -395,15 +410,8 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
395410
target_compile_options(openPMD.ADIOS1.Serial PUBLIC "/bigobj")
396411
target_compile_options(openPMD.ADIOS1.Parallel PUBLIC "/bigobj")
397412
endif()
398-
if(openPMD_USE_INTERNAL_VARIANT)
399-
target_include_directories(openPMD.ADIOS1.Serial SYSTEM PUBLIC
400-
$<BUILD_INTERFACE:${openPMD_SOURCE_DIR}/share/openPMD/thirdParty/variant/include>)
401-
target_include_directories(openPMD.ADIOS1.Parallel SYSTEM PUBLIC
402-
$<BUILD_INTERFACE:${openPMD_SOURCE_DIR}/share/openPMD/thirdParty/variant/include>)
403-
else()
404-
target_link_libraries(openPMD.ADIOS1.Serial PUBLIC mpark_variant)
405-
target_link_libraries(openPMD.ADIOS1.Parallel PUBLIC mpark_variant)
406-
endif()
413+
target_link_libraries(openPMD.ADIOS1.Serial PUBLIC openPMD::thirdparty::mpark_variant)
414+
target_link_libraries(openPMD.ADIOS1.Parallel PUBLIC openPMD::thirdparty::mpark_variant)
407415

408416
target_include_directories(openPMD.ADIOS1.Serial SYSTEM PRIVATE
409417
${openPMD_SOURCE_DIR}/include)
@@ -613,9 +621,7 @@ if(BUILD_TESTING)
613621
target_compile_definitions(${testname}Tests PUBLIC "-DopenPMD_HAVE_ADIOS2=1")
614622
endif()
615623
target_link_libraries(${testname}Tests PRIVATE openPMD)
616-
if(TARGET Catch2::Catch2)
617-
target_link_libraries(${testname}Tests PRIVATE Catch2::Catch2)
618-
endif()
624+
target_link_libraries(${testname}Tests PRIVATE openPMD::thirdparty::Catch2)
619625
endforeach()
620626
endif()
621627

0 commit comments

Comments
 (0)