[PATCH] D126450: [cmake] make FindLibEdit.cmake more robust

Tobias Ribizel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 26 01:16:49 PDT 2022


upsj created this revision.
upsj added a reviewer: MaskRay.
Herald added subscribers: StephenFan, mgorny.
Herald added a project: All.
upsj requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

FindLibEdit uses pkg-config to find the necessary flags, but this may break with cross-compilation, because the PkgConfig module in CMake doesn't respect the SYSROOT specified in a toolchain file. Instead of taking the parameters from pkg-config for granted, we check whether our compiler can actually include and link against the library.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126450

Files:
  cmake/Modules/FindLibEdit.cmake


Index: cmake/Modules/FindLibEdit.cmake
===================================================================
--- cmake/Modules/FindLibEdit.cmake
+++ cmake/Modules/FindLibEdit.cmake
@@ -13,55 +13,50 @@
 #   LibEdit_LIBRARIES      - libraries to link
 #   LibEdit_VERSION_STRING - version number
 
-if(LibEdit_INCLUDE_DIRS AND LibEdit_LIBRARIES)
-  set(LibEdit_FOUND TRUE)
-else()
-  find_package(PkgConfig QUIET)
-  pkg_check_modules(PC_LIBEDIT QUIET libedit)
+find_package(PkgConfig QUIET)
+pkg_check_modules(PC_LIBEDIT QUIET libedit)
 
-  find_path(LibEdit_INCLUDE_DIRS
-            NAMES
-              histedit.h
-            HINTS
-              ${PC_LIBEDIT_INCLUDEDIR}
-              ${PC_LIBEDIT_INCLUDE_DIRS}
-              "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
-  find_library(LibEdit_LIBRARIES
-               NAMES
-                 edit libedit
-               HINTS
-                 ${PC_LIBEDIT_LIBDIR}
-                 ${PC_LIBEDIT_LIBRARY_DIRS}
-                 "${CMAKE_INSTALL_FULL_LIBDIR}")
+find_path(LibEdit_INCLUDE_DIRS NAMES histedit.h HINTS ${PC_LIBEDIT_INCLUDE_DIRS})
+find_library(LibEdit_LIBRARIES NAMES edit HINTS ${PC_LIBEDIT_LIBRARY_DIRS})
 
-  if(LibEdit_INCLUDE_DIRS AND EXISTS "${LibEdit_INCLUDE_DIRS}/histedit.h")
+include(CheckIncludeFile)
+if(LibEdit_INCLUDE_DIRS AND EXISTS "${LibEdit_INCLUDE_DIRS}/histedit.h")
+  cmake_push_check_state()
+  list(APPEND CMAKE_REQUIRED_INCLUDES ${LibEdit_INCLUDE_DIRS})
+  list(APPEND CMAKE_REQUIRED_LIBRARIES ${LibEdit_LIBRARIES})
+  check_include_file(histedit.h HAVE_HISTEDIT_H)
+  cmake_pop_check_state()
+  if (HAVE_HISTEDIT_H)
     file(STRINGS "${LibEdit_INCLUDE_DIRS}/histedit.h"
-         libedit_major_version_str
-         REGEX "^#define[ \t]+LIBEDIT_MAJOR[ \t]+[0-9]+")
+          libedit_major_version_str
+          REGEX "^#define[ \t]+LIBEDIT_MAJOR[ \t]+[0-9]+")
     string(REGEX REPLACE "^#define[ \t]+LIBEDIT_MAJOR[ \t]+([0-9]+)" "\\1"
-           LIBEDIT_MAJOR_VERSION "${libedit_major_version_str}")
+            libedit_major_version "${libedit_major_version_str}")
 
     file(STRINGS "${LibEdit_INCLUDE_DIRS}/histedit.h"
-         libedit_minor_version_str
-         REGEX "^#define[ \t]+LIBEDIT_MINOR[ \t]+[0-9]+")
+          libedit_minor_version_str
+          REGEX "^#define[ \t]+LIBEDIT_MINOR[ \t]+[0-9]+")
     string(REGEX REPLACE "^#define[ \t]+LIBEDIT_MINOR[ \t]+([0-9]+)" "\\1"
-           LIBEDIT_MINOR_VERSION "${libedit_minor_version_str}")
+            libedit_minor_version "${libedit_minor_version_str}")
 
     set(LibEdit_VERSION_STRING "${libedit_major_version}.${libedit_minor_version}")
+  else()
+    set(LibEdit_INCLUDE_DIRS "")
+    set(LibEdit_LIBRARIES "")
   endif()
-
-  include(FindPackageHandleStandardArgs)
-  find_package_handle_standard_args(LibEdit
-                                    FOUND_VAR
-                                      LibEdit_FOUND
-                                    REQUIRED_VARS
-                                      LibEdit_INCLUDE_DIRS
-                                      LibEdit_LIBRARIES
-                                    VERSION_VAR
-                                      LibEdit_VERSION_STRING)
-  mark_as_advanced(LibEdit_INCLUDE_DIRS LibEdit_LIBRARIES)
 endif()
 
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(LibEdit
+                                  FOUND_VAR
+                                    LibEdit_FOUND
+                                  REQUIRED_VARS
+                                    LibEdit_INCLUDE_DIRS
+                                    LibEdit_LIBRARIES
+                                  VERSION_VAR
+                                    LibEdit_VERSION_STRING)
+mark_as_advanced(LibEdit_INCLUDE_DIRS LibEdit_LIBRARIES)
+
 if (LibEdit_FOUND AND NOT TARGET LibEdit::LibEdit)
   add_library(LibEdit::LibEdit UNKNOWN IMPORTED)
   set_target_properties(LibEdit::LibEdit PROPERTIES


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126450.432210.patch
Type: text/x-patch
Size: 3894 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220526/25e24ae0/attachment.bin>


More information about the llvm-commits mailing list