Files
keepassxc/cmake/FindReadline.cmake
Adrien OllierandJonathan White caa7d14761 Cleanup line reader code for CLI (#13330)
* refactor(LineReader): Make use of the LineReader class in a much simpler way

The LineReader is made a concrete class whose declaration is independent of the readline library. Only its implementation depends on the presence or absence of the readline library. The LineReader code is embedded in a new library : line_reader, to make the cli library independant of the USE_READLINE definition.

* 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)

---------

Co-authored-by: Jonathan White <[email protected]>
2026-07-23 06:41:11 -04:00

89 lines
2.4 KiB
CMake

#.rst:
# FindReadline
# ------------
#
# Find the readline library.
#
# Usage of this module as follows:
#
# find_package(Readline [REQUIRED] [QUIET])
# target_link_libraries(<target> PRIVATE Readline::readline)
#
# Variables used by this module:
#
# Readline_ROOT - Root directory to search for readline (e.g., /usr/local or the vcpkg installation path).
# If not set, the module will search in standard system paths.
#
# Imported Targets
# ^^^^^^^^^^^^^^^
#
# This module defines the following :prop_tgt:`IMPORTED` target:
#
# ``Readline::readline``
# The readline library, if found.
#
# Result Variables
# ^^^^^^^^^^^^^^
#
# ``Readline_FOUND`` - True if readline is found.
# ``Readline_INCLUDE_DIRS`` - The readline include directories.
# ``Readline_LIBRARIES`` - The readline libraries.
# --- Step 1: Try pkg-config first ---
find_package(PkgConfig)
if(PkgConfig_FOUND)
pkg_check_modules(Readline IMPORTED_TARGET readline)
if(Readline_FOUND)
# pkg-config found readline
add_library(Readline::readline ALIAS PkgConfig::Readline)
return()
endif()
endif()
# --- Step 2: Manual search if pkg-config fails ---
# Uses Readline_ROOT if defined (CMake convention: <PackageName>_ROOT)
set(_Readline_SEARCH_PATHS)
if(Readline_ROOT)
list(APPEND _Readline_SEARCH_PATHS "${Readline_ROOT}")
endif()
find_path(Readline_INCLUDE_DIR
NAMES readline/readline.h
PATHS ${_Readline_SEARCH_PATHS}
PATH_SUFFIXES include
DOC "readline include directory"
)
find_library(Readline_LIBRARY
NAMES readline
PATHS ${_Readline_SEARCH_PATHS}
PATH_SUFFIXES lib lib64
DOC "readline library"
)
# Managing results with FindPackageHandleStandardArgs
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Readline
DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY
)
if(Readline_FOUND)
set(Readline_INCLUDE_DIRS ${Readline_INCLUDE_DIR})
set(Readline_LIBRARIES ${Readline_LIBRARY})
if(NOT TARGET Readline::readline)
add_library(Readline::readline UNKNOWN IMPORTED)
set_target_properties(Readline::readline PROPERTIES
IMPORTED_LOCATION "${Readline_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${Readline_INCLUDE_DIRS}"
)
endif()
endif()
# Hide internal variables
mark_as_advanced(
Readline_INCLUDE_DIR
Readline_LIBRARY
Readline_ROOT
)