blt icon indicating copy to clipboard operation
blt copied to clipboard

add a blt_find_package() macro?

Open rrsettgast opened this issue 5 years ago • 1 comments

I recently had some good clean fun getting blas and lapack packages properly linked into our code project. As part of this, I eventually just make FindBlas.cmake and FindLAPACK.cmake files that are essentially identical aside from the name. It seems that cmake has a somewhat inconsistent interface with its find_package() macro as the various implementation of the FindXYZ (i.e. FindMPI, FindBLAS, FindLAPACK) do not have a similar interface. By this I mean they use different input variables to find what you are looking for, and they set different output variables. Anyways, the files that I made seem like they might be useful for making a blt_find_package() macro. Here is the BLAS one:

if( NOT BLAS_DIR )

    find_package(BLAS REQUIRED)
    
    list( GET BLAS_LIBRARIES 0 BLAS_LIB0 )
    get_filename_component( BLAS_LIBRARY_DIR ${BLAS_LIB0} DIRECTORY CACHE )
    get_filename_component( BLAS_DIR ${BLAS_LIBRARY_DIR} DIRECTORY CACHE )
    set( BLAS_INCLUDE_DIR ${BLAS_DIR}/include CACHE PATH "" )
    
else()
    find_path( BLAS_INCLUDE_DIR 
               NAMES  blas.h cblas.h mkl_blas.h mkl_cblas.h
               PATHS  ${BLAS_DIR}/include
               NO_DEFAULT_PATH
               NO_CMAKE_ENVIRONMENT_PATH
               NO_CMAKE_PATH
               NO_SYSTEM_ENVIRONMENT_PATH
               NO_CMAKE_SYSTEM_PATH)
     
    if( NOT DEFINED BLAS_LIBRARY_NAMES )
        set( BLAS_LIBRARY_NAMES "blas;cblas;libmkl_rt.so" CACHE STRING "")
    endif()
    
    find_library( BLAS_LIBRARIES 
                  NAMES ${BLAS_LIBRARY_NAMES}
                  PATHS ${BLAS_DIR}/lib ${BLAS_DIR}/lib64
                  NO_DEFAULT_PATH
                  NO_CMAKE_ENVIRONMENT_PATH
                  NO_CMAKE_PATH
                  NO_SYSTEM_ENVIRONMENT_PATH
                  NO_CMAKE_SYSTEM_PATH)
              
    list( GET BLAS_LIBRARIES 0 BLAS_LIB0 )
    get_filename_component( BLAS_LIBRARY_DIR ${BLAS_LIB0} DIRECTORY CACHE )

endif()

find_package_handle_standard_args( BLAS 
                                   DEFAULT_MSG
                                   BLAS_DIR
                                   BLAS_INCLUDE_DIR
                                   BLAS_LIBRARY_DIR
                                   BLAS_LIBRARIES
                                   )

set(BLAS_LIBRARY_NAMES "" CACHE STRING "" FORCE)
foreach( lib ${BLAS_LIBRARIES} )
    get_filename_component( libname ${lib} NAME )
    list( APPEND BLAS_LIBRARY_NAMES ${libname} )
endforeach()

message( "    BLAS_FOUND         = ${BLAS_FOUND}" )
message( "    BLAS_DIR           = ${BLAS_DIR}" )
message( "    BLAS_INCLUDE_DIR   = ${BLAS_INCLUDE_DIR}" )
message( "    BLAS_LIBRARY_DIR   = ${BLAS_LIBRARY_DIR}" )
message( "    BLAS_LIBRARIES     = ${BLAS_LIBRARIES}" )
message( "    BLAS_LIBRARY_NAMES = ${BLAS_LIBRARY_NAMES}" )
message( "    BLAS_LINKER_FLAGS  = ${BLAS_LINKER_FLAGS}" )

possible inputs are:

PACKAGE_NAME
PACKAGE_DIR
INCLUDE_PATHS
INCLUDE_NAMES
LIBRARY_PATHS
LIBRARY_NAMES

outputs are all listed in the message at the end.

So let me know if you think this is useful and within scope of blt. @cyrush @white238 @kennyweiss

rrsettgast avatar Jun 24 '19 17:06 rrsettgast

Thanks @rrsettgast. Assuming this is general enough to cover a reasonable set of common libraries, something like this could be nice.

One issue with the above code is that it only finds includes and libraries, but not, e..g. build flags or other metadata used to generate them (e.g. versions), which one could get from importing a cmake file generated during a build process. So, obviously, it would not apply in those cases.

See also: @white238's PR for a related set of macros/functions to set up common TPLs: https://github.com/LLNL/blt/pull/230

kennyweiss avatar Jun 24 '19 22:06 kennyweiss