Skip to content

Commit 522cfe6

Browse files
committed
refactor(readline): better FindReadline module
The readline library is now searched with pkg-config first. If pkg-config is not installed, the search is manual. In both cases, the Readline::readline target is created, embedding transitive requirements. FindReadline module was written with the help of Le Chat AI (https://chat.mistral.ai/chat)
1 parent aca3819 commit 522cfe6

2 files changed

Lines changed: 71 additions & 32 deletions

File tree

cmake/FindReadline.cmake

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,88 @@
1-
# Code copied from sethhall@github
1+
#.rst:
2+
# FindReadline
3+
# ------------
24
#
3-
# - Try to find readline include dirs and libraries
5+
# Find the readline library.
46
#
57
# Usage of this module as follows:
68
#
7-
# find_package(Readline)
9+
# find_package(Readline [REQUIRED] [QUIET])
10+
# target_link_libraries(<target> PRIVATE Readline::readline)
811
#
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:
1113
#
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.
1516
#
16-
# Variables defined by this module:
17+
# Imported Targets
18+
# ^^^^^^^^^^^^^^^
1719
#
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.
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.
2131

22-
find_path(Readline_ROOT_DIR
23-
NAMES include/readline/readline.h
24-
)
32+
# --- Step 1: Try pkg-config first ---
33+
find_package(PkgConfig)
34+
if(PkgConfig_FOUND)
35+
pkg_check_modules(Readline IMPORTED_TARGET readline)
36+
if(Readline_FOUND)
37+
# pkg-config found readline
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()
2549

2650
find_path(Readline_INCLUDE_DIR
27-
NAMES readline/readline.h
28-
HINTS ${Readline_ROOT_DIR}/include
51+
NAMES readline/readline.h
52+
PATHS ${_Readline_SEARCH_PATHS}
53+
PATH_SUFFIXES include
54+
DOC "readline include directory"
2955
)
3056

3157
find_library(Readline_LIBRARY
32-
NAMES readline
33-
HINTS ${Readline_ROOT_DIR}/lib
58+
NAMES readline
59+
PATHS ${_Readline_SEARCH_PATHS}
60+
PATH_SUFFIXES lib lib64
61+
DOC "readline library"
62+
)
63+
64+
# Managing results with FindPackageHandleStandardArgs
65+
include(FindPackageHandleStandardArgs)
66+
find_package_handle_standard_args(Readline
67+
DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY
3468
)
3569

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)
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+
endif()
81+
endif()
4482

83+
# Hide internal variables
4584
mark_as_advanced(
46-
Readline_ROOT_DIR
47-
Readline_INCLUDE_DIR
48-
Readline_LIBRARY
85+
Readline_INCLUDE_DIR
86+
Readline_LIBRARY
87+
Readline_ROOT
4988
)

src/cli/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ target_link_libraries(text_stream PUBLIC Qt6::Core)
2121
add_library(line_reader STATIC LineReader.cpp)
2222
target_link_libraries(line_reader PUBLIC Qt6::Core)
2323
find_package(Readline MODULE)
24-
if(READLINE_FOUND)
24+
if(Readline_FOUND)
2525
target_compile_definitions(line_reader PRIVATE "USE_READLINE")
26-
target_link_libraries(line_reader PRIVATE ${Readline_LIBRARY})
26+
target_link_libraries(line_reader PRIVATE Readline::readline)
2727
else()
2828
target_link_libraries(line_reader PRIVATE text_stream)
2929
endif()

0 commit comments

Comments
 (0)