From 1e975f64f777ffd726d9d5c72952b66dcc4c0a5c Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sun, 14 Feb 2016 15:55:54 +0100 Subject: [PATCH] Improve error messages when libraries aren't found --- src/cpp-utils/CMakeLists.txt | 2 +- src/fspp/CMakeLists.txt | 5 +++-- utils.cmake | 30 ++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/cpp-utils/CMakeLists.txt b/src/cpp-utils/CMakeLists.txt index f4abfd41..9519158c 100644 --- a/src/cpp-utils/CMakeLists.txt +++ b/src/cpp-utils/CMakeLists.txt @@ -43,7 +43,7 @@ endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") target_link_libraries(${PROJECT_NAME} PRIVATE pthread curl) # TODO From Crypto++ 5.7 on, it should support cmake with find_package() instead of find_library(). -find_library(CryptoPP cryptopp $ENV{CRYPTOPP_LIB_PATH}) +find_library_with_path(CryptoPP cryptopp CRYPTOPP_LIB_PATH) target_link_libraries(${PROJECT_NAME} PUBLIC ${CryptoPP} scrypt spdlog) target_add_boost(${PROJECT_NAME} filesystem system thread) diff --git a/src/fspp/CMakeLists.txt b/src/fspp/CMakeLists.txt index 5ea55702..5df5b4c2 100644 --- a/src/fspp/CMakeLists.txt +++ b/src/fspp/CMakeLists.txt @@ -22,9 +22,10 @@ target_activate_cpp14(${PROJECT_NAME}) if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") set(CMAKE_FIND_FRAMEWORK LAST) - find_library(FUSE osxfuse $ENV{FUSE_LIB_PATH}) + set(FUSE_LIB_NAME "osxfuse") else(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - find_library(FUSE fuse $ENV{FUSE_LIB_PATH}) + set(FUSE_LIB_NAME "fuse") endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") +find_library_with_path(FUSE ${FUSE_LIB_NAME} FUSE_LIB_PATH) target_link_libraries(${PROJECT_NAME} PUBLIC ${FUSE}) diff --git a/utils.cmake b/utils.cmake index cb7ae82c..4c2ead5b 100644 --- a/utils.cmake +++ b/utils.cmake @@ -63,7 +63,6 @@ function(require_gcc_version VERSION) endif (CMAKE_COMPILER_IS_GNUCXX) endfunction(require_gcc_version) - ################################################## # Specify that a specific minimal version of clang is required # @@ -76,4 +75,31 @@ function(require_clang_version VERSION) message(FATAL_ERROR "Needs at least clang version ${VERSION}, found clang ${CMAKE_CXX_COMPILER_VERSION}") endif (CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${VERSION}) endif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") -endfunction(require_clang_version) \ No newline at end of file +endfunction(require_clang_version) + +################################################## +# Find the location of a library and return its full path in OUTPUT_VARIABLE. +# If PATH_VARIABLE points to a defined variable, then the library will only be searched in this path. +# If PATH_VARIABLE points to a undefined variable, default system locations will be searched. +# +# Uses (the following will search for fuse in system locations by default, and if the user passes -DFUSE_LIB_PATH to cmake, it will only search in this path. +# find_library_with_path(MYLIBRARY fuse FUSE_LIB_PATH) +# target_link_library(target ${MYLIBRARY}) +################################################## +function(find_library_with_path OUTPUT_VARIABLE LIBRARY_NAME PATH_VARIABLE) + if(${PATH_VARIABLE}) + find_library(${OUTPUT_VARIABLE} ${LIBRARY_NAME} PATHS ${${PATH_VARIABLE}} NO_DEFAULT_PATH) + if (${OUTPUT_VARIABLE} MATCHES NOTFOUND) + message(FATAL_ERROR "Didn't find ${LIBRARY_NAME} in path specified by the ${PATH_VARIABLE} parameter (${${PATH_VARIABLE}}). Pass in the correct path or remove the parameter to try common system locations.") + else(${OUTPUT_VARIABLE} MATCHES NOTFOUND) + message(STATUS "Found ${LIBRARY_NAME} in user-defined path ${${PATH_VARIABLE}}") + endif(${OUTPUT_VARIABLE} MATCHES NOTFOUND) + else(${PATH_VARIABLE}) + find_library(${OUTPUT_VARIABLE} ${LIBRARY_NAME}) + if (${OUTPUT_VARIABLE} MATCHES NOTFOUND) + message(FATAL_ERROR "Didn't find ${LIBRARY_NAME} library. If ${LIBRARY_NAME} is installed, try passing in the library location with -D${PATH_VARIABLE}=/path/to/${LIBRARY_NAME}/lib.") + else(${OUTPUT_VARIABLE} MATCHES NOTFOUND) + message(STATUS "Found ${LIBRARY_NAME} in system location") + endif(${OUTPUT_VARIABLE} MATCHES NOTFOUND) + endif(${PATH_VARIABLE}) +endfunction(find_library_with_path) \ No newline at end of file