Skip to content

Commit

Permalink
add find_dependency for cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
koparasy committed Jan 3, 2025
1 parent 4d854ba commit d0f4411
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 61 deletions.
20 changes: 10 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ option(WITH_AMS_DEBUG "Enable verbose messages" OFF)
option(WITH_PERFFLOWASPECT "Use PerfFlowAspect for Profiling" OFF)
option(WITH_WORKFLOW "Install python drivers used by the outer workflow" OFF)
option(WITH_AMS_LIB "Install C++ library to support scientific applications" ON)
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

# ------------------------------------------------------------------------------
find_package(nlohmann_json REQUIRED)
Expand All @@ -68,6 +67,7 @@ endif()
# ------------------------------------------------------------------------------
if (WITH_CALIPER)
find_package(caliper REQUIRED)
message(STATUS "Caliper Directory is ${caliper_DIR}")
list(APPEND AMS_APP_DEFINES "__AMS_ENABLE_CALIPER__")
endif()

Expand All @@ -76,16 +76,16 @@ if (WITH_AMS_DEBUG)
endif()

if (WITH_HDF5)
set(HDF5_ROOT ${AMS_HDF5_DIR})
if (HDF5_USE_STATIC_LIBRARIES)
find_package(HDF5 COMPONENTS C static NO_DEFAULT_PATH PATHS ${AMS_HDF5_DIR} ${AMS_HDF5_DIR}/share/cmake)
set(AMS_HDF5_TARGET hdf5-static)
message(STATUS "HDF5 uses Static Library : ${AMS_HDF5_TARGET}")
else()
find_package(HDF5 COMPONENTS C shared NO_DEFAULT_PATH PATHS ${AMS_HDF5_DIR} ${AMS_HDF5_DIR}/share/cmake)
set(AMS_HDF5_TARGET hdf5-shared)
message(STATUS "HDF5 uses Shared Library : ${AMS_HDF5_TARGET}")
endif()
find_package(HDF5 COMPONENTS C static NO_DEFAULT_PATH PATHS ${AMS_HDF5_DIR} ${AMS_HDF5_DIR}/share/cmake)
set(AMS_HDF5_TARGET hdf5-static)
set(AMS_HDF5_LIB_TYPE "static")
else()
find_package(HDF5 COMPONENTS C shared NO_DEFAULT_PATH PATHS ${AMS_HDF5_DIR} ${AMS_HDF5_DIR}/share/cmake)
set(AMS_HDF5_LIB_TYPE "shared")
set(AMS_HDF5_TARGET hdf5-shared)
endif()
message(STATUS "HDF5 Dir is ${HDF5_DIR}")
list(APPEND AMS_APP_DEFINES "__ENABLE_HDF5__")
endif() # WITH_HDF5

Expand Down
57 changes: 43 additions & 14 deletions cmake/AMSConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,55 @@ set(AMS_WITH_MPI @WITH_MPI@)
set(AMS_WITH_CUDA @WITH_CUDA@)
set(AMS_WITH_CALIPER @WITH_CALIPER@)
set(AMS_WITH_HDF5 @WITH_HDF5@)
include(CMakeFindDependencyMacro)

# Check if the AMS target is static
get_target_property(AMS_TYPE AMS::AMS TYPE)
if (NOT TARGET Torch)
set(AMS_TORCH_DIR @Torch_DIR@)
if (NOT Torch_DIR)
set(Torch_DIR ${AMS_TORCH_DIR})
endif()
find_dependency(Torch REQUIRED HINTS ${Torch_DIR})
endif()

if(AMS_TYPE STREQUAL "STATIC_LIBRARY")
message(STATUS "AMS is a static library. Finding required dependencies...")
# Add usage hints for downstream projects
if(AMS_WITH_MPI)
find_package(MPI REQUIRED)
#Add usage hints for downstream projects
if(AMS_WITH_MPI)
if (NOT TARGET MPI)
find_dependency(MPI REQUIRED)
endif()
if(AMS_WITH_CUDA)
find_package(CUDAToolkit REQUIRED)
endif()


if(AMS_WITH_CUDA)
if (NOT TARGET CUDA::cudart)
find_dependency(CUDAToolkit REQUIRED)
endif()
if (AMS_WITH_HDF5)
if (@HDF5_USE_STATIC_LIBRARIES@)
find_package(HDF5 COMPONENTS C static HINTS @AMS_HDF5_DIR@ @AMS_HDF5_DIR@/share/cmake)
endif()

if (AMS_WITH_HDF5)
set(AMS_HDF5_DIR @AMS_HDF5_DIR@)
set(AMS_HDF5_LIB_TYPE @AMS_HDF5_LIB_TYPE@)
if (NOT TARGET hdf5-shared OR NOT TARGET hdf5-static)
if (NOT HDF5_DIR)
set(HDF5_DIR ${AMS_HDF5_DIR})
endif()

if (${AMS_HDF5_LIB_TYPE} STREQUAL "static")
find_dependency(HDF5 COMPONENTS C static HINTS @AMS_HDF5_DIR@ @AMS_HDF5_DIR@/share/cmake)
else()
find_package(HDF5 COMPONENTS C shared HINTS @AMS_HDF5_DIR@ @AMS_HDF5_DIR@/share/cmake)
find_dependency(HDF5 COMPONENTS C shared HINTS @AMS_HDF5_DIR@ @AMS_HDF5_DIR@/share/cmake)
endif()
endif()
endif()

if (AMS_WITH_CALIPER)
if (NOT TARGET caliper)
set(CALIPER_AMS_DIR @caliper_DIR@)
# check if we specify another caliper directory
if (NOT caliper_DIR)
set(caliper_DIR ${CALIPER_AMS_DIR})
endif()
find_dependency(caliper HINTS ${caliper_DIR})
endif()
find_package(Torch REQUIRED HINTS @Torch_DIR@)
endif()

check_required_components("@PROJECT_NAME@")
6 changes: 3 additions & 3 deletions examples/ideal_gas/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021-2023 Lawrence Livermore National Security, LLC and other
# Copyright 2021-2023 Lawrence Livermore National Security, LLC and otherMS
# AMSLib Project Developers
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand Down Expand Up @@ -41,14 +41,14 @@ if (WITH_MPI)
find_package(MPI REQUIRED)
endif()

find_package(AMS REQUIRED)

function(ADDExec binary_name use_ams)
target_link_libraries(${binary_name} PRIVATE ${MFEM_LIBRARIES})
target_include_directories(${binary_name} PRIVATE ${MFEM_INCLUDE_DIRS})
# we always use ams to avoid if def conflicts.
# but we only enable it for the use_ams case
find_package(AMS REQUIRED)
target_link_libraries(${binary_name} PUBLIC AMS::AMS)
target_link_libraries(${binary_name} PRIVATE AMS::AMS)
if (${use_ams})
target_compile_definitions(${binary_name} PRIVATE USE_AMS)
endif()
Expand Down
46 changes: 12 additions & 34 deletions src/AMSlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,46 +30,24 @@ target_include_directories(AMS PUBLIC
)


if (AMS_TYPE STREQUAL "STATIC_LIBRARY")
if (WITH_CUDA)
target_link_libraries(AMS PRIVATE CUDA::cudart)
endif()

if (WITH_HDF5)
target_link_libraries(AMS PRIVATE ${AMS_HDF5_TARGET})
endif()


if (WITH_CALIPER)
target_link_libraries(AMS PRIVATE caliper)
endif()

if (WITH_MPI)
target_link_libraries(AMS PRIVATE MPI::MPI_CXX)
endif()

target_link_libraries(AMS PRIVATE stdc++fs torch)
else()
if (WITH_CUDA)
target_link_libraries(AMS PRIVATE CUDA::cudart)
endif()

if (WITH_HDF5)
target_link_libraries(AMS PRIVATE ${AMS_HDF5_TARGET})
endif()
if (WITH_CUDA)
target_link_libraries(AMS PRIVATE CUDA::cudart)
endif()

if (WITH_HDF5)
target_link_libraries(AMS PRIVATE ${AMS_HDF5_TARGET})
endif()

if (WITH_CALIPER)
target_link_libraries(AMS PRIVATE caliper)
endif()

if (WITH_MPI)
target_link_libraries(AMS PRIVATE MPI::MPI_CXX)
endif()
if (WITH_CALIPER)
target_link_libraries(AMS PRIVATE caliper)
endif()

target_link_libraries(AMS PRIVATE stdc++fs torch)
if (WITH_MPI)
target_link_libraries(AMS PRIVATE MPI::MPI_CXX)
endif()

target_link_libraries(AMS PRIVATE stdc++fs torch)


configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/include/AMS-config.h.in" "${PROJECT_BINARY_DIR}/include/AMS-config.h")
Expand Down

0 comments on commit d0f4411

Please sign in to comment.