From fa631bc9badc56dbdfda9286586d982c4c570e4c Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Wed, 4 Dec 2024 06:46:14 -0700 Subject: [PATCH 1/2] remove custom netcdf find function --- CMakeLists.txt | 8 +- cmake/FindNetCDF.cmake | 96 ------------ cmake/FindNetCDF.cmake~ | 143 ----------------- cmake/LibCheck.cmake | 104 ------------- cmake/LibFind.cmake | 330 ---------------------------------------- 5 files changed, 6 insertions(+), 675 deletions(-) delete mode 100644 cmake/FindNetCDF.cmake delete mode 100644 cmake/FindNetCDF.cmake~ delete mode 100644 cmake/LibCheck.cmake delete mode 100644 cmake/LibFind.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index bb88c81..330c0fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ endif() project (cprnc Fortran C) include (CheckFunctionExists) include (ExternalProject) +find_package(PkgConfig REQUIRED) #===== Local modules ===== list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) @@ -74,8 +75,11 @@ foreach (SRC_FILE IN LISTS CPRNC_GenF90_SRCS) DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FILE}.in genf90) endforeach () +#===== NetCDF ===== +pkg_check_modules(NetCDF REQUIRED netcdf) + #===== NetCDF-Fortran ===== -INCLUDE(FindNetCDF) +pkg_check_modules(NetCDF_Fortran REQUIRED netcdf-fortran) add_executable (cprnc ${CPRNC_Fortran_SRCS} ${CPRNC_GenF90_SRCS}) target_include_directories(cprnc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${NetCDF_Fortran_INCLUDE_DIRS}) @@ -84,7 +88,7 @@ add_dependencies (cprnc genf90) # Always use -fPIC set_property(TARGET cprnc PROPERTY POSITION_INDEPENDENT_CODE ON) target_link_libraries (cprnc - PUBLIC ${NetCDF_Fortran_LIBRARIES} ${NetCDF_C_LIBRARIES} ${NetCDF_LIBRARIES}) + PUBLIC ${NetCDF_Fortran_LIBRARIES} ${NetCDF_LIBRARIES}) # We do not want cprnc injecting ctests into parent projects if (CPRNC_STANDALONE) diff --git a/cmake/FindNetCDF.cmake b/cmake/FindNetCDF.cmake deleted file mode 100644 index e1ddc7e..0000000 --- a/cmake/FindNetCDF.cmake +++ /dev/null @@ -1,96 +0,0 @@ -# First try to locate nf-config. -find_program(NetCDF_Fortran_CONFIG_EXECUTABLE - NAMES nf-config - HINTS ENV NetCDF_ROOT ENV NetCDF_Fortran_ROOT - PATH_SUFFIXES bin Bin - DOC "NetCDF config program. Used to detect NetCDF Fortran include directory and linker flags." ) -mark_as_advanced(NetCDF_Fortran_CONFIG_EXECUTABLE) -find_program(NetCDF_C_CONFIG_EXECUTABLE - NAMES nc-config - HINTS ENV NetCDF_ROOT ENV NetCDF_C_ROOT - PATH_SUFFIXES bin Bin - DOC "NetCDF config program. Used to detect NetCDF C include directory and linker flags." ) -mark_as_advanced(NetCDF_C_CONFIG_EXECUTABLE) - - -if(NetCDF_Fortran_CONFIG_EXECUTABLE) - # Found nf-config - use it to retrieve include directory and linking flags. - # Note that if the process fails (as e.g. on Windows), the output variables - # will remain empty - execute_process(COMMAND ${NetCDF_Fortran_CONFIG_EXECUTABLE} --includedir - OUTPUT_VARIABLE DEFAULT_Fortran_INCLUDE_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE) - execute_process(COMMAND ${NetCDF_Fortran_CONFIG_EXECUTABLE} --flibs - OUTPUT_VARIABLE flibs - OUTPUT_STRIP_TRAILING_WHITESPACE) - if (flibs) - set(NetCDF_Fortran_LIBRARIES ${flibs} CACHE STRING "NetCDF libraries (or linking flags)") - set(AUTODETECTED_NetCDF_Fortran_LIBRARIES ON) - endif() - - execute_process(COMMAND ${NetCDF_Fortran_CONFIG_EXECUTABLE} --prefix - OUTPUT_VARIABLE root - OUTPUT_STRIP_TRAILING_WHITESPACE) - if (root) - set(NetCDF_Fortran_ROOT ${root} CACHE STRING "NetCDF Fortran Root") - set(AUTODETECTED_NetCDF_Fortran_ROOT ON) - endif() - -endif() - -if(NetCDF_C_CONFIG_EXECUTABLE) - # Found nc-config - use it to retrieve include directory and linking flags. - # Note that if the process fails (as e.g. on Windows), the output variables - # will remain empty - execute_process(COMMAND ${NetCDF_C_CONFIG_EXECUTABLE} --includedir - OUTPUT_VARIABLE DEFAULT_C_INCLUDE_DIR - OUTPUT_STRIP_TRAILING_WHITESPACE) - execute_process(COMMAND ${NetCDF_C_CONFIG_EXECUTABLE} --libs - OUTPUT_VARIABLE clibs - OUTPUT_STRIP_TRAILING_WHITESPACE) - if (clibs) - set(NetCDF_C_LIBRARIES ${clibs} CACHE STRING "NetCDF libraries (or linking flags)") - set(AUTODETECTED_NetCDF_C ON) - endif() - -endif() - -# Determine default name of NetCDf library -# If nf-config succeeded, its result takes priority as it has already been -# used to set NetCDF_LIBRARIES -if(DEFINED ENV{NetCDFLIBNAME}) - set(DEFAULT_LIBRARY_NAME "$ENV{NETCDFLIBNAME}") -else() - set(DEFAULT_LIBRARY_NAME netcdff) -endif() - -find_path(NetCDF_Fortran_INCLUDE_DIRS netcdf.mod - HINTS "${DEFAULT_Fortran_INCLUDE_DIR}" "$ENV{NetCDF_Fortran_INCLUDE_DIRS}" "$ENV{CONDA_PREFIX}/Library/include" - DOC "NetCDF Fortran include directories") - -find_library(NetCDF_Fortran_LIBRARY NAMES ${DEFAULT_LIBRARY_NAME} - HINTS "${DEFAULT_LIBRARY_DIR}" "$ENV{NetCDF_Fortran_LIBRARY}" "$ENV{CONDA_PREFIX}/Library/lib" - DOC "NetCDF libraries (or linking flags)") - -find_path(NetCDF_C_INCLUDE_DIRS netcdf.h - HINTS "${DEFAULT_C_INCLUDE_DIR}" "$ENV{NetCDF_C_INCLUDE_DIRS}" "$ENV{CONDA_PREFIX}/Library/include" - DOC "NetCDF C include directories") - -find_library(NetCDF_C_LIBRARY NAMES ${DEFAULT_LIBRARY_NAME} - HINTS "${DEFAULT_LIBRARY_DIR}" "$ENV{NetCDF_C_LIBRARY}" "$ENV{CONDA_PREFIX}/Library/lib" - DOC "NetCDF C libraries (or linking flags)") - -if(AUTODETECTED_NetCDF) - mark_as_advanced(NetCDF_Fortran_INCLUDE_DIRS NetCDF_Fortran_LIBRARIES) - mark_as_advanced(NetCDF_C_INCLUDE_DIRS NetCDF_C_LIBRARIES) - mark_as_advanced(NetCDF_Fortran_ROOT) -endif() - -# Process default arguments (QUIET, REQUIRED) -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args (NetCDF DEFAULT_MSG NetCDF_Fortran_LIBRARIES NetCDF_Fortran_INCLUDE_DIRS) -find_package_handle_standard_args (NetCDF DEFAULT_MSG NetCDF_C_LIBRARIES NetCDF_C_INCLUDE_DIRS) - -add_library(netcdf INTERFACE IMPORTED GLOBAL) -set_property(TARGET netcdf APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${NetCDF_C_INCLUDE_DIRS} ${NetCDF_Fortran_INCLUDE_DIRS}") -set_property(TARGET netcdf APPEND PROPERTY INTERFACE_LINK_LIBRARIES "${NetCDF_Fortran_LIBRARIES} ${NetCDF_C_LIBRARIES}") diff --git a/cmake/FindNetCDF.cmake~ b/cmake/FindNetCDF.cmake~ deleted file mode 100644 index 344714b..0000000 --- a/cmake/FindNetCDF.cmake~ +++ /dev/null @@ -1,143 +0,0 @@ -# - Try to find NetCDF -# -# This can be controlled by setting the NetCDF_PATH (or, equivalently, the -# NETCDF environment variable), or NetCDF__PATH CMake variables, where -# is the COMPONENT language one needs. -# -# Once done, this will define: -# -# NetCDF__FOUND (BOOL) - system has NetCDF -# NetCDF__IS_SHARED (BOOL) - whether library is shared/dynamic -# NetCDF__INCLUDE_DIR (PATH) - Location of the C header file -# NetCDF__INCLUDE_DIRS (LIST) - the NetCDF include directories -# NetCDF__LIBRARY (FILE) - Path to the C library file -# NetCDF__LIBRARIES (LIST) - link these to use NetCDF -# -# The available COMPONENTS are: C Fortran -# If no components are specified, it assumes only C -include (LibFind) -include (LibCheck) - -# Define NetCDF C Component -define_package_component (NetCDF DEFAULT - COMPONENT C - INCLUDE_NAMES netcdf.h - LIBRARY_NAMES netcdf) - -# Define NetCDF Fortran Component -define_package_component (NetCDF - COMPONENT Fortran - INCLUDE_NAMES netcdf.mod netcdf.inc - LIBRARY_NAMES netcdff) - -# Search for list of valid components requested -find_valid_components (NetCDF) - -#============================================================================== -# SEARCH FOR VALIDATED COMPONENTS -foreach (NCDFcomp IN LISTS NetCDF_FIND_VALID_COMPONENTS) - - # If not found already, search... - if (NOT NetCDF_${NCDFcomp}_FOUND) - - # Manually add the MPI include and library dirs to search paths - # and search for the package component - if (MPI_${NCDFcomp}_FOUND) - initialize_paths (NetCDF_${NCDFcomp}_PATHS - INCLUDE_DIRECTORIES ${MPI_${NCDFcomp}_INCLUDE_PATH} - LIBRARIES ${MPI_${NCDFcomp}_LIBRARIES}) - find_package_component(NetCDF COMPONENT ${NCDFcomp} - PATHS ${NetCDF_${NCDFcomp}_PATHS}) - else () - find_package_component(NetCDF COMPONENT ${NCDFcomp}) - endif () - - # Continue only if component found - if (NetCDF_${NCDFcomp}_FOUND) - - # Checks - if (NCDFcomp STREQUAL C) - - # Check version - check_version (NetCDF - NAME "netcdf_meta.h" - HINTS ${NetCDF_C_INCLUDE_DIRS} - MACRO_REGEX "NC_VERSION_") - - # Check for parallel support - check_macro (NetCDF_C_HAS_PARALLEL - NAME TryNetCDF_PARALLEL.c - HINTS ${CMAKE_MODULE_PATH} - DEFINITIONS -I${NetCDF_C_INCLUDE_DIR} - COMMENT "whether NetCDF has parallel support") - - # Check if logging enabled - set(CMAKE_REQUIRED_INCLUDES ${NetCDF_C_INCLUDE_DIR}) - set(CMAKE_REQUIRED_LIBRARIES ${NetCDF_C_LIBRARIES}) - CHECK_FUNCTION_EXISTS(nc_set_log_level NetCDF_C_LOGGING_ENABLED) - - endif () - - # Dependencies - if (NCDFcomp STREQUAL C AND NOT NetCDF_C_IS_SHARED) - - # DEPENDENCY: PnetCDF (if PnetCDF enabled) - check_macro (NetCDF_C_HAS_PNETCDF - NAME TryNetCDF_PNETCDF.c - HINTS ${CMAKE_MODULE_PATH} - DEFINITIONS -I${NetCDF_C_INCLUDE_DIR} - COMMENT "whether NetCDF has PnetCDF support") - if (NetCDF_C_HAS_PNETCDF) - find_package (PnetCDF COMPONENTS C) - if (CURL_FOUND) - list (APPEND NetCDF_C_INCLUDE_DIRS ${PnetCDF_C_INCLUDE_DIRS}) - list (APPEND NetCDF_C_LIBRARIES ${PnetCDF_C_LIBRARIES}) - endif () - endif () - - # DEPENDENCY: CURL (If DAP enabled) - check_macro (NetCDF_C_HAS_DAP - NAME TryNetCDF_DAP.c - HINTS ${CMAKE_MODULE_PATH} - DEFINITIONS -I${NetCDF_C_INCLUDE_DIR} - COMMENT "whether NetCDF has DAP support") - if (NetCDF_C_HAS_DAP) - find_package (CURL) - if (CURL_FOUND) - list (APPEND NetCDF_C_INCLUDE_DIRS ${CURL_INCLUDE_DIRS}) - list (APPEND NetCDF_C_LIBRARIES ${CURL_LIBRARIES}) - endif () - endif () - - # DEPENDENCY: HDF5 - find_package (HDF5 COMPONENTS HL C) - if (HDF5_C_FOUND) - list (APPEND NetCDF_C_INCLUDE_DIRS ${HDF5_C_INCLUDE_DIRS} - ${HDF5_HL_INCLUDE_DIRS}) - list (APPEND NetCDF_C_LIBRARIES ${HDF5_C_LIBRARIES} - ${HDF5_HL_LIBRARIES}) - endif () - - # DEPENDENCY: LIBDL Math - list (APPEND NetCDF_C_LIBRARIES -ldl -lm) - - elseif (NCDFcomp STREQUAL Fortran AND NOT NetCDF_Fortran_IS_SHARED) - - # DEPENDENCY: NetCDF - set (orig_comp ${NCDFcomp}) - set (orig_comps ${NetCDF_FIND_VALID_COMPONENTS}) - find_package (NetCDF COMPONENTS C) - set (NetCDF_FIND_VALID_COMPONENTS ${orig_comps}) - set (NCDFcomp ${orig_comp}) - if (NetCDF_C_FOUND) - list (APPEND NetCDF_Fortran_INCLUDE_DIRS ${NetCDF_C_INCLUDE_DIRS}) - list (APPEND NetCDF_Fortran_LIBRARIES ${NetCDF_C_LIBRARIES}) - endif () - - endif () - - endif () - - endif () - -endforeach () diff --git a/cmake/LibCheck.cmake b/cmake/LibCheck.cmake deleted file mode 100644 index 2e8cadc..0000000 --- a/cmake/LibCheck.cmake +++ /dev/null @@ -1,104 +0,0 @@ -include (CMakeParseArguments) -include (CheckFunctionExists) -#============================================================================== -# -# FUNCTIONS TO HELP WITH Check* MODULES -# -#============================================================================== - -#______________________________________________________________________________ -# - Basic function to check a property of a package using a try_compile step -# -# SYNTAX: check_macro ( -# NAME -# HINTS ... -# DEFINITIONS ... -# COMMENT ) -# -function (check_macro VARIABLE) - - # Parse the input arguments - set (oneValueArgs COMMENT NAME) - set (multiValueArgs HINTS DEFINITIONS) - cmake_parse_arguments (${VARIABLE} "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - - # If the return variable is defined, already, don't continue - if (NOT DEFINED ${VARIABLE}) - - message (STATUS "Checking ${${VARIABLE}_COMMENT}") - find_file (${VARIABLE}_TRY_FILE - NAMES ${${VARIABLE}_NAME} - HINTS ${${VARIABLE}_HINTS}) - if (${VARIABLE}_TRY_FILE) - try_compile (COMPILE_RESULT - ${CMAKE_CURRENT_BINARY_DIR}/try${VARIABLE} - SOURCES ${${VARIABLE}_TRY_FILE} - COMPILE_DEFINITIONS ${${VARIABLE}_DEFINITIONS} - OUTPUT_VARIABLE TryOUT) - if (COMPILE_RESULT) - message (STATUS "Checking ${${VARIABLE}_COMMENT} - yes") - else () - message (STATUS "Checking ${${VARIABLE}_COMMENT} - no") - endif () - - set (${VARIABLE} ${COMPILE_RESULT} - CACHE BOOL "${${VARIABLE}_COMMENT}") - - else () - message (STATUS "Checking ${${VARIABLE}_COMMENT} - failed") - endif () - - unset (${VARIABLE}_TRY_FILE CACHE) - endif () - -endfunction () - -#______________________________________________________________________________ -# - Basic function to check the version of a package using a try_run step -# -# SYNTAX: check_version ( -# NAME -# HINTS ... -# DEFINITIONS ...) -# -function (check_version PKG) - - # Parse the input arguments - set (oneValueArgs NAME MACRO_REGEX) - set (multiValueArgs HINTS) - cmake_parse_arguments (${PKG} "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - - # If the return variable is defined, already, don't continue - if (NOT DEFINED ${PKG}_VERSION) - - message (STATUS "Checking ${PKG} version") - find_file (${PKG}_VERSION_HEADER - NAMES ${${PKG}_NAME} - HINTS ${${PKG}_HINTS}) - if (${PKG}_VERSION_HEADER) - set (def) - file (STRINGS ${${PKG}_VERSION_HEADER} deflines - REGEX "^#define[ \\t]+${${PKG}_MACRO_REGEX}") - foreach (defline IN LISTS deflines) - string (REPLACE "\"" "" defline "${defline}") - string (REPLACE "." "" defline "${defline}") - string (REGEX REPLACE "[ \\t]+" ";" deflist "${defline}") - list (GET deflist 2 arg) - list (APPEND def ${arg}) - endforeach () - string (REPLACE ";" "." vers "${def}") - message (STATUS "Checking ${PKG} version - ${vers}") - set (${PKG}_VERSION ${vers} - CACHE STRING "${PKG} version string") - if (${PKG}_VERSION VERSION_LESS ${PKG}_FIND_VERSION}) - message (FATAL_ERROR "${PKG} version insufficient") - endif () - else () - message (STATUS "Checking ${PKG} version - failed") - endif () - - unset (${PKG}_VERSION_HEADER CACHE) - - endif () - -endfunction () diff --git a/cmake/LibFind.cmake b/cmake/LibFind.cmake deleted file mode 100644 index ac13748..0000000 --- a/cmake/LibFind.cmake +++ /dev/null @@ -1,330 +0,0 @@ -include (CMakeParseArguments) -include(FindPackageHandleStandardArgs) - -#============================================================================== -# -# FUNCTIONS TO HELP WITH Find* MODULES -# -#============================================================================== - -#______________________________________________________________________________ -# - Wrapper for finding static libraries ONLY -# -macro (find_static_library) - set (_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) - set (CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX}) - find_library(${ARGN}) - set (CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES}) - unset (_CMAKE_FIND_LIBRARY_SUFFIXES) -endmacro () - - -#______________________________________________________________________________ -# - Wrapper for finding shared/dynamic libraries ONLY -# -macro (find_shared_library) - set (_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) - set (CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX}) - find_library(${ARGN}) - set (CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES}) - unset (_CMAKE_FIND_LIBRARY_SUFFIXES) -endmacro () - - -#______________________________________________________________________________ -# - Function to determine type (SHARED or STATIC) of library -# -# Input: -# LIB (FILE) -# -# Returns: -# RETURN_VAR (BOOL) -# -function (is_shared_library RETURN_VAR LIB) - get_filename_component(libext ${LIB} EXT) - if (libext MATCHES ${CMAKE_SHARED_LIBRARY_SUFFIX}) - set (${RETURN_VAR} TRUE PARENT_SCOPE) - else () - set (${RETURN_VAR} FALSE PARENT_SCOPE) - endif () -endfunction () - - -#______________________________________________________________________________ -# - Function to define a valid package component -# -# Input: -# ${PKG}_DEFAULT (BOOL) -# ${PKG}_COMPONENT (STRING) -# ${PKG}_INCLUDE_NAMES (LIST) -# ${PKG}_LIBRARY_NAMES (LIST) -# -# Returns: -# ${PKG}_DEFAULT_COMPONENT (STRING) -# ${PKG}_VALID_COMPONENTS (LIST) -# ${PKG}_${COMPONENT}_INCLUDE_NAMES (LIST) -# ${PKG}_${COMPONENT}_LIBRARY_NAMES (LIST) -# -function (define_package_component PKG) - - # Parse the input arguments - set (options DEFAULT) - set (oneValueArgs COMPONENT) - set (multiValueArgs INCLUDE_NAMES LIBRARY_NAMES) - cmake_parse_arguments (${PKG} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - if (${PKG}_COMPONENT) - set (PKGCOMP ${PKG}_${${PKG}_COMPONENT}) - else () - set (PKGCOMP ${PKG}) - endif () - - # Set return values - if (${PKG}_COMPONENT) - if (${PKG}_DEFAULT) - set (${PKG}_DEFAULT_COMPONENT ${${PKG}_COMPONENT} PARENT_SCOPE) - endif () - set (VALID_COMPONENTS ${${PKG}_VALID_COMPONENTS}) - list (APPEND VALID_COMPONENTS ${${PKG}_COMPONENT}) - set (${PKG}_VALID_COMPONENTS ${VALID_COMPONENTS} PARENT_SCOPE) - endif () - set (${PKGCOMP}_INCLUDE_NAMES ${${PKG}_INCLUDE_NAMES} PARENT_SCOPE) - set (${PKGCOMP}_LIBRARY_NAMES ${${PKG}_LIBRARY_NAMES} PARENT_SCOPE) - -endfunction () - - -#______________________________________________________________________________ -# - Function to find valid package components -# -# Assumes pre-defined variables: -# ${PKG}_FIND_COMPONENTS (LIST) -# ${PKG}_DEFAULT_COMPONENT (STRING) -# ${PKG}_VALID_COMPONENTS (LIST) -# -# Returns: -# ${PKG}_FIND_VALID_COMPONENTS (LIST) -# -function (find_valid_components PKG) - - if (NOT ${PKG}_FIND_COMPONENTS) - set (${PKG}_FIND_COMPONENTS ${${PKG}_DEFAULT_COMPONENT}) - endif () - - set (FIND_VALID_COMPONENTS) - foreach (comp IN LISTS ${PKG}_FIND_COMPONENTS) - if (";${${PKG}_VALID_COMPONENTS};" MATCHES ";${comp};") - list (APPEND FIND_VALID_COMPONENTS ${comp}) - endif () - endforeach () - - set (${PKG}_FIND_VALID_COMPONENTS ${FIND_VALID_COMPONENTS} PARENT_SCOPE) - -endfunction () - - -#______________________________________________________________________________ -# - Initialize a list of paths from a list of includes and libraries -# -# Input: -# INCLUDE_DIRECTORIES -# LIBRARIES -# -# Ouput: -# ${PATHLIST} -# -function (initialize_paths PATHLIST) - - # Parse the input arguments - set (multiValueArgs INCLUDE_DIRECTORIES LIBRARIES) - cmake_parse_arguments (INIT "" "" "${multiValueArgs}" ${ARGN}) - - set (paths) - foreach (inc IN LISTS INIT_INCLUDE_DIRECTORIES) - list (APPEND paths ${inc}) - get_filename_component (dname ${inc} NAME) - if (dname MATCHES "include") - get_filename_component (prefx ${inc} PATH) - list (APPEND paths ${prefx}) - endif () - endforeach () - foreach (lib IN LISTS INIT_LIBRARIES) - get_filename_component (libdir ${lib} PATH) - list (APPEND paths ${libdir}) - get_filename_component (dname ${libdir} PATH) - if (dname MATCHES "lib") - get_filename_component (prefx ${libdir} PATH) - list (APPEND paths ${prefx}) - endif () - endforeach () - - set (${PATHLIST} ${paths} PARENT_SCOPE) - -endfunction () - - -#______________________________________________________________________________ -# - Basic find package macro for a specific component -# -# Assumes pre-defined variables: -# ${PKG}_${COMP}_INCLUDE_NAMES or ${PKG}_INCLUDE_NAMES -# ${PKG}_${COMP}_LIBRARY_NAMES or ${PKG}_LIBRARY_NAMES -# -# Input: -# ${PKG}_COMPONENT -# ${PKG}_HINTS -# ${PKG}_PATHS -# -function (find_package_component PKG) - - # Parse the input arguments - set (options) - set (oneValueArgs COMPONENT) - set (multiValueArgs HINTS PATHS) - cmake_parse_arguments (${PKG} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - set (COMP ${${PKG}_COMPONENT}) - if (COMP) - set (PKGCOMP ${PKG}_${COMP}) - else () - set (PKGCOMP ${PKG}) - endif () - string (TOUPPER ${PKG} PKGUP) - string (TOUPPER ${PKGCOMP} PKGCOMPUP) - - # Only continue if package not found already - if (NOT ${PKGCOMP}_FOUND) - - # Handle QUIET and REQUIRED arguments - if (${${PKG}_FIND_QUIETLY}) - set (${PKGCOMP}_FIND_QUIETLY TRUE) - endif () - if (${${PKG}_FIND_REQUIRED}) - set (${PKGCOMP}_FIND_REQUIRED TRUE) - endif () - - # Determine search order - set (SEARCH_DIRS) - if (${PKG}_HINTS) - list (APPEND SEARCH_DIRS ${${PKG}_HINTS}) - endif () - if (${PKGCOMP}_PATH) - list (APPEND SEARCH_DIRS ${${PKGCOMP}_PATH}) - endif () - if (${PKG}_PATH) - list (APPEND SEARCH_DIRS ${${PKG}_PATH}) - endif () - if (DEFINED ENV{${PKGCOMPUP}}) - list (APPEND SEARCH_DIRS $ENV{${PKGCOMPUP}}) - endif () - if (DEFINED ENV{${PKGUP}}) - list (APPEND SEARCH_DIRS $ENV{${PKGUP}}) - endif () - if (CMAKE_SYSTEM_PREFIX_PATH) - list (APPEND SEARCH_DIRS ${CMAKE_SYSTEM_PREFIX_PATH}) - endif () - if (${PKG}_PATHS) - list (APPEND SEARCH_DIRS ${${PKG}_PATHS}) - endif () - - # Start the search for the include file and library file. Only overload - # if the variable is not defined. - foreach (suffix PREFIX LIBRARY INCLUDE_DIR) - if (NOT DEFINED ${PKGCOMP}_${suffix}) - set (${PKGCOMP}_${suffix} ${PKGCOMP}_${suffix}-NOTFOUND) - endif () - endforeach () - - foreach (dir IN LISTS SEARCH_DIRS) - - # Search for include file names in current dirrectory - foreach (iname IN LISTS ${PKGCOMP}_INCLUDE_NAMES) - if (EXISTS ${dir}/${iname}) - set (${PKGCOMP}_PREFIX ${dir}) - set (${PKGCOMP}_INCLUDE_DIR ${dir}) - break () - endif () - if (EXISTS ${dir}/include/${iname}) - set (${PKGCOMP}_PREFIX ${dir}) - set (${PKGCOMP}_INCLUDE_DIR ${dir}/include) - break () - endif () - endforeach () - - # Search for library file names in the found prefix only! - if (${PKGCOMP}_PREFIX) - find_library (${PKGCOMP}_LIBRARY - NAMES ${${PKGCOMP}_LIBRARY_NAMES} - PATHS ${${PKGCOMP}_PREFIX} - PATH_SUFFIXES lib - NO_DEFAULT_PATH) - - # If found, check if library is static or dynamic - if (${PKGCOMP}_LIBRARY) - is_shared_library (${PKGCOMP}_IS_SHARED ${${PKGCOMP}_LIBRARY}) - - # If we want only shared libraries, and it isn't shared... - if (PREFER_SHARED AND NOT ${PKGCOMP}_IS_SHARED) - find_shared_library (${PKGCOMP}_SHARED_LIBRARY - NAMES ${${PKGCOMP}_LIBRARY_NAMES} - PATHS ${${PKGCOMP}_PREFIX} - PATH_SUFFIXES lib - NO_DEFAULT_PATH) - if (${PKGCOMP}_SHARED_LIBRARY) - set (${PKGCOMP}_LIBRARY ${${PKGCOMP}_SHARED_LIBRARY}) - set (${PKGCOMP}_IS_SHARED TRUE) - endif () - - # If we want only static libraries, and it is shared... - elseif (PREFER_STATIC AND ${PKGCOMP}_IS_SHARED) - find_static_library (${PKGCOMP}_STATIC_LIBRARY - NAMES ${${PKGCOMP}_LIBRARY_NAMES} - PATHS ${${PKGCOMP}_PREFIX} - PATH_SUFFIXES lib - NO_DEFAULT_PATH) - if (${PKGCOMP}_STATIC_LIBRARY) - set (${PKGCOMP}_LIBRARY ${${PKGCOMP}_STATIC_LIBRARY}) - set (${PKGCOMP}_IS_SHARED FALSE) - endif () - endif () - endif () - - # If include dir and library both found, then we're done - if (${PKGCOMP}_INCLUDE_DIR AND ${PKGCOMP}_LIBRARY) - break () - - # Otherwise, reset the search variables and continue - else () - set (${PKGCOMP}_PREFIX ${PKGCOMP}_PREFIX-NOTFOUND) - set (${PKGCOMP}_INCLUDE_DIR ${PKGCOMP}_INCLUDE_DIR-NOTFOUND) - set (${PKGCOMP}_LIBRARY ${PKGCOMP}_LIBRARY-NOTFOUND) - endif () - endif () - - endforeach () - - # handle the QUIETLY and REQUIRED arguments and - # set NetCDF_C_FOUND to TRUE if all listed variables are TRUE - find_package_handle_standard_args (${PKGCOMP} DEFAULT_MSG - ${PKGCOMP}_LIBRARY - ${PKGCOMP}_INCLUDE_DIR) - mark_as_advanced (${PKGCOMP}_INCLUDE_DIR ${PKGCOMP}_LIBRARY) - - # HACK For bug in CMake v3.0: - set (${PKGCOMP}_FOUND ${${PKGCOMPUP}_FOUND}) - - # Set return variables - if (${PKGCOMP}_FOUND) - set (${PKGCOMP}_INCLUDE_DIRS ${${PKGCOMP}_INCLUDE_DIR}) - set (${PKGCOMP}_LIBRARIES ${${PKGCOMP}_LIBRARY}) - endif () - - # Set variables in parent scope - set (${PKGCOMP}_FOUND ${${PKGCOMP}_FOUND} PARENT_SCOPE) - set (${PKGCOMP}_INCLUDE_DIR ${${PKGCOMP}_INCLUDE_DIR} PARENT_SCOPE) - set (${PKGCOMP}_INCLUDE_DIRS ${${PKGCOMP}_INCLUDE_DIRS} PARENT_SCOPE) - set (${PKGCOMP}_LIBRARY ${${PKGCOMP}_LIBRARY} PARENT_SCOPE) - set (${PKGCOMP}_LIBRARIES ${${PKGCOMP}_LIBRARIES} PARENT_SCOPE) - set (${PKGCOMP}_IS_SHARED ${${PKGCOMP}_IS_SHARED} PARENT_SCOPE) - - endif () - -endfunction () From 0784ad18d5a48dbb497baa603ad1e4409b9979cc Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Wed, 4 Dec 2024 09:44:54 -0700 Subject: [PATCH 2/2] more changes suggested by hammpie --- CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 330c0fb..0404289 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,19 +76,19 @@ foreach (SRC_FILE IN LISTS CPRNC_GenF90_SRCS) endforeach () #===== NetCDF ===== -pkg_check_modules(NetCDF REQUIRED netcdf) +pkg_check_modules(NetCDF REQUIRED IMPORTED_TARGET netcdf) #===== NetCDF-Fortran ===== -pkg_check_modules(NetCDF_Fortran REQUIRED netcdf-fortran) +pkg_check_modules(NetCDF_Fortran REQUIRED IMPORTED_TARGET netcdf-fortran) add_executable (cprnc ${CPRNC_Fortran_SRCS} ${CPRNC_GenF90_SRCS}) -target_include_directories(cprnc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${NetCDF_Fortran_INCLUDE_DIRS}) +target_include_directories(cprnc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) add_dependencies (cprnc genf90) # Always use -fPIC set_property(TARGET cprnc PROPERTY POSITION_INDEPENDENT_CODE ON) target_link_libraries (cprnc - PUBLIC ${NetCDF_Fortran_LIBRARIES} ${NetCDF_LIBRARIES}) + PUBLIC PkgConfig::NetCDF_Fortran PkgConfig::NetCDF) # We do not want cprnc injecting ctests into parent projects if (CPRNC_STANDALONE)