-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Config] Add cxxopts cmake find module (#4538)
* Add cxxopts cmake find module * fix case where cmake config file for cxxopts was found
- Loading branch information
1 parent
4fa4bfd
commit ebd18c0
Showing
2 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Find the cxxopts headers | ||
# Behavior is to first look for config files, such as the one installed by some package | ||
# managers who provides their own cmake files. | ||
# Most of them and official sources does not provide cmake finders, so if no config files | ||
# were found, this tries to find the library by looking at headers file. | ||
# | ||
# Defines: | ||
# cxxopts_FOUND : True if cxxopts is found | ||
# cxxopts_VERSION : cxxopts version if found | ||
# | ||
# Provides both target cxxopts::cxxopts. | ||
|
||
find_package(cxxopts NO_MODULE QUIET HINTS ${cxxopts_DIR}) | ||
|
||
macro(_cxxopts_check_version) | ||
if(EXISTS "${cxxopts_INCLUDE_DIR}/cxxopts.hpp") | ||
file(READ "${cxxopts_INCLUDE_DIR}/cxxopts.hpp" _cxxopts_version_header) | ||
endif() | ||
|
||
string(REGEX MATCH "define[ \t]+CXXOPTS__VERSION_MAJOR[ \t]+([0-9]+)" _cxxopts_major_version_match "${_cxxopts_version_header}") | ||
set(cxxopts_VERSION_MAJOR "${CMAKE_MATCH_1}") | ||
string(REGEX MATCH "define[ \t]+CXXOPTS__VERSION_MINOR[ \t]+([0-9]+)" _cxxopts_minor_version_match "${_cxxopts_version_header}") | ||
set(cxxopts_VERSION_MINOR "${CMAKE_MATCH_1}") | ||
string(REGEX MATCH "define[ \t]+CXXOPTS__VERSION_PATCH[ \t]+([0-9]+)" _cxxopts_patch_version_match "${_cxxopts_version_header}") | ||
set(cxxopts_VERSION_PATCH "${CMAKE_MATCH_1}") | ||
|
||
set(cxxopts_VERSION ${cxxopts_VERSION_MAJOR}.${cxxopts_VERSION_MINOR}.${cxxopts_VERSION_PATCH}) | ||
set(cxxopts_VERSION_OK TRUE) | ||
if(${cxxopts_VERSION} VERSION_LESS ${cxxopts_FIND_VERSION}) | ||
set(cxxopts_VERSION_OK FALSE) | ||
message(SEND_ERROR "cxxopts version ${cxxopts_VERSION} found in ${cxxopts_INCLUDE_DIR}/cxxopts.hpp, " | ||
"but at least version ${cxxopts_FIND_VERSION} is required") | ||
endif() | ||
if(${cxxopts_FIND_VERSION_EXACT} AND NOT ${cxxopts_VERSION} VERSION_EQUAL ${cxxopts_FIND_VERSION}) | ||
set(cxxopts_VERSION_OK FALSE) | ||
message(SEND_ERROR "cxxopts version ${cxxopts_VERSION} found in ${cxxopts_INCLUDE_DIR}, " | ||
"but exact version ${cxxopts_FIND_VERSION_EXACT} is required") | ||
endif() | ||
endmacro() | ||
|
||
if(NOT TARGET cxxopts::cxxopts) | ||
|
||
if(NOT cxxopts_INCLUDE_DIR) | ||
find_path(cxxopts_INCLUDE_DIR | ||
NAMES cxxopts.hpp | ||
PATH_SUFFIXES include | ||
) | ||
endif() | ||
|
||
if(cxxopts_INCLUDE_DIR) | ||
_cxxopts_check_version() | ||
set(cxxopts_FOUND ${cxxopts_VERSION_OK}) | ||
endif() | ||
|
||
if(cxxopts_FOUND) | ||
set(cxxopts_INCLUDE_DIRS ${cxxopts_INCLUDE_DIR}) | ||
|
||
if(NOT cxxopts_FIND_QUIETLY) | ||
message(STATUS "Found cxxopts version ${cxxopts_VERSION} from ${cxxopts_INCLUDE_DIR}/cxxopts.hpp)") | ||
endif() | ||
|
||
if(NOT TARGET cxxopts::cxxopts) | ||
add_library(cxxopts::cxxopts INTERFACE IMPORTED) | ||
set_property(TARGET cxxopts::cxxopts PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${cxxopts_INCLUDE_DIR}") | ||
endif() | ||
else() | ||
endif() | ||
|
||
mark_as_advanced(cxxopts_INCLUDE_DIR) | ||
endif() | ||
|
||
if(NOT cxxopts_FOUND AND cxxopts_FIND_REQUIRED) | ||
message(FATAL_ERROR "Cannot find cxxopts") | ||
endif() |