|
1 | | -# Code copied from sethhall@github |
| 1 | +#.rst: |
| 2 | +# FindReadline |
| 3 | +# ------------ |
2 | 4 | # |
3 | | -# - Try to find readline include dirs and libraries |
| 5 | +# Find the readline library and its dependency (ncurses). |
4 | 6 | # |
5 | 7 | # Usage of this module as follows: |
6 | 8 | # |
7 | | -# find_package(Readline) |
| 9 | +# find_package(Readline [REQUIRED] [QUIET]) |
| 10 | +# target_link_libraries(<target> PRIVATE Readline::readline) |
8 | 11 | # |
9 | | -# Variables used by this module, they can change the default behaviour and need |
10 | | -# to be set before calling find_package: |
| 12 | +# Variables used by this module: |
11 | 13 | # |
12 | | -# Readline_ROOT_DIR Set this variable to the root installation of |
13 | | -# readline if the module has problems finding the |
14 | | -# proper installation path. |
| 14 | +# Readline_ROOT - Root directory to search for readline (e.g., /usr/local or the vcpkg installation path). |
| 15 | +# If not set, the module will search in standard system paths. |
15 | 16 | # |
16 | | -# Variables defined by this module: |
| 17 | +# Imported Targets |
| 18 | +# ^^^^^^^^^^^^^^^ |
17 | 19 | # |
18 | | -# READLINE_FOUND System has readline, include and lib dirs found |
19 | | -# Readline_INCLUDE_DIR The readline include directories. |
20 | | -# Readline_LIBRARY The readline library. |
| 20 | +# This module defines the following :prop_tgt:`IMPORTED` target: |
| 21 | +# |
| 22 | +# ``Readline::readline`` |
| 23 | +# The readline library, if found, with ncurses as a dependency. |
| 24 | +# |
| 25 | +# Result Variables |
| 26 | +# ^^^^^^^^^^^^^^ |
| 27 | +# |
| 28 | +# ``Readline_FOUND`` - True if readline is found. |
| 29 | +# ``Readline_INCLUDE_DIRS`` - The readline include directories. |
| 30 | +# ``Readline_LIBRARIES`` - The readline libraries (including ncurses if found). |
21 | 31 |
|
22 | | -find_path(Readline_ROOT_DIR |
23 | | - NAMES include/readline/readline.h |
24 | | -) |
| 32 | +# --- Step 1: Try pkg-config first --- |
| 33 | +find_package(PkgConfig QUIET) |
| 34 | +if(PkgConfig_FOUND) |
| 35 | + pkg_check_modules(Readline QUIET IMPORTED_TARGET readline) |
| 36 | + if(Readline_FOUND) |
| 37 | + # pkg-config found readline (and handles ncurses automatically) |
| 38 | + add_library(Readline::readline ALIAS PkgConfig::Readline) |
| 39 | + return() |
| 40 | + endif() |
| 41 | +endif() |
| 42 | + |
| 43 | +# --- Step 2: Manual search if pkg-config fails --- |
| 44 | +# Uses Readline_ROOT if defined (CMake convention: <PackageName>_ROOT) |
| 45 | +set(_Readline_SEARCH_PATHS) |
| 46 | +if(Readline_ROOT) |
| 47 | + list(APPEND _Readline_SEARCH_PATHS "${Readline_ROOT}") |
| 48 | +endif() |
25 | 49 |
|
26 | 50 | find_path(Readline_INCLUDE_DIR |
27 | | - NAMES readline/readline.h |
28 | | - HINTS ${Readline_ROOT_DIR}/include |
| 51 | + NAMES readline/readline.h |
| 52 | + HINTS ${_Readline_SEARCH_PATHS} |
| 53 | + PATH_SUFFIXES include |
| 54 | + DOC "readline include directory" |
29 | 55 | ) |
30 | 56 |
|
31 | 57 | find_library(Readline_LIBRARY |
32 | | - NAMES readline |
33 | | - HINTS ${Readline_ROOT_DIR}/lib |
| 58 | + NAMES readline |
| 59 | + HINTS ${_Readline_SEARCH_PATHS} |
| 60 | + PATH_SUFFIXES lib lib64 |
| 61 | + DOC "readline library" |
34 | 62 | ) |
35 | 63 |
|
36 | | -if(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) |
37 | | - set(READLINE_FOUND TRUE) |
38 | | -else(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) |
39 | | - find_library(Readline_LIBRARY NAMES readline) |
40 | | - include(FindPackageHandleStandardArgs) |
41 | | - find_package_handle_standard_args(Readline DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY ) |
42 | | - mark_as_advanced(Readline_INCLUDE_DIR Readline_LIBRARY) |
43 | | -endif(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) |
| 64 | +# Managing results with FindPackageHandleStandardArgs |
| 65 | +include(FindPackageHandleStandardArgs) |
| 66 | +find_package_handle_standard_args(Readline |
| 67 | + DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY |
| 68 | +) |
| 69 | + |
| 70 | +if(Readline_FOUND) |
| 71 | + set(Readline_INCLUDE_DIRS ${Readline_INCLUDE_DIR}) |
| 72 | + set(Readline_LIBRARIES ${Readline_LIBRARY}) |
| 73 | + |
| 74 | + if(NOT TARGET Readline::readline) |
| 75 | + add_library(Readline::readline UNKNOWN IMPORTED) |
| 76 | + set_target_properties(Readline::readline PROPERTIES |
| 77 | + IMPORTED_LOCATION "${Readline_LIBRARY}" |
| 78 | + INTERFACE_INCLUDE_DIRECTORIES "${Readline_INCLUDE_DIRS}" |
| 79 | + ) |
| 80 | + |
| 81 | + # --- ncurses management (Unix only) --- |
| 82 | + if(UNIX) |
| 83 | + find_package(Ncurses MODULE REQUIRED) |
| 84 | + set_property(TARGET Readline::readline PROPERTY |
| 85 | + INTERFACE_LINK_LIBRARIES Ncurses::ncurses |
| 86 | + ) |
| 87 | + endif(UNIX) |
| 88 | + endif() |
| 89 | +endif() |
44 | 90 |
|
| 91 | +# Hide internal variables |
45 | 92 | mark_as_advanced( |
46 | | - Readline_ROOT_DIR |
47 | | - Readline_INCLUDE_DIR |
48 | | - Readline_LIBRARY |
| 93 | + Readline_INCLUDE_DIR |
| 94 | + Readline_LIBRARY |
| 95 | + Readline_ROOT |
49 | 96 | ) |
0 commit comments