Skip to content

Commit 1b850a2

Browse files
committed
build: improve libedit handling for builds
Use the FindLibEdit.cmake module from LLDB to properly control where the libedit libraries are searched for and linked from as well as where the headers come from. This uses the standard mechanisms which allows users to control where libedit is pulled from (which is important for cross-compilation). This second version is more aggressive about pruning the libedit handling. The Ubuntu 14.04 version of libedit does not have `histedit.h`, and the intent is to rely on that to determine if we have unicode support or not.
1 parent 746b58e commit 1b850a2

File tree

9 files changed

+108
-58
lines changed

9 files changed

+108
-58
lines changed

CMakeLists.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -924,14 +924,11 @@ else()
924924
find_package(LibXml2)
925925
endif()
926926

927-
# You need libedit linked in order to check if you have el_wgets.
928-
cmake_push_check_state()
929-
list(APPEND CMAKE_REQUIRED_LIBRARIES "edit")
930-
check_symbol_exists(el_wgets "histedit.h" HAVE_EL_WGETS)
931-
if(HAVE_EL_WGETS)
932-
set(HAVE_UNICODE_LIBEDIT 1)
927+
if(LLVM_ENABLE_LIBEDIT)
928+
find_package(LibEdit REQUIRED)
929+
else()
930+
find_package(LibEdit)
933931
endif()
934-
cmake_pop_check_state()
935932

936933
check_symbol_exists(wait4 "sys/wait.h" HAVE_WAIT4)
937934

cmake/modules/FindLibEdit.cmake

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#.rst:
2+
# FindLibEdit
3+
# -----------
4+
#
5+
# Find libedit library and headers
6+
#
7+
# The module defines the following variables:
8+
#
9+
# ::
10+
#
11+
# libedit_FOUND - true if libedit was found
12+
# libedit_INCLUDE_DIRS - include search path
13+
# libedit_LIBRARIES - libraries to link
14+
# libedit_VERSION - version number
15+
16+
if(libedit_INCLUDE_DIRS AND libedit_LIBRARIES)
17+
set(libedit_FOUND TRUE)
18+
else()
19+
find_package(PkgConfig QUIET)
20+
pkg_check_modules(PC_LIBEDIT QUIET libedit)
21+
22+
find_path(libedit_INCLUDE_DIRS
23+
NAMES
24+
histedit.h
25+
HINTS
26+
${PC_LIBEDIT_INCLUDEDIR}
27+
${PC_LIBEDIT_INCLUDE_DIRS}
28+
${CMAKE_INSTALL_FULL_INCLUDEDIR})
29+
find_library(libedit_LIBRARIES
30+
NAMES
31+
edit libedit
32+
HINTS
33+
${PC_LIBEDIT_LIBDIR}
34+
${PC_LIBEDIT_LIBRARY_DIRS}
35+
${CMAKE_INSTALL_FULL_LIBDIR})
36+
37+
if(libedit_INCLUDE_DIRS AND EXISTS "${libedit_INCLUDE_DIRS}/histedit.h")
38+
file(STRINGS "${libedit_INCLUDE_DIRS}/histedit.h"
39+
libedit_major_version_str
40+
REGEX "^#define[ \t]+LIBEDIT_MAJOR[ \t]+[0-9]+")
41+
string(REGEX REPLACE "^#define[ \t]+LIBEDIT_MAJOR[ \t]+([0-9]+)" "\\1"
42+
LIBEDIT_MAJOR_VERSION "${libedit_major_version_str}")
43+
44+
file(STRINGS "${libedit_INCLUDE_DIRS}/histedit.h"
45+
libedit_minor_version_str
46+
REGEX "^#define[ \t]+LIBEDIT_MINOR[ \t]+[0-9]+")
47+
string(REGEX REPLACE "^#define[ \t]+LIBEDIT_MINOR[ \t]+([0-9]+)" "\\1"
48+
LIBEDIT_MINOR_VERSION "${libedit_minor_version_str}")
49+
50+
set(libedit_VERSION_STRING "${libedit_major_version}.${libedit_minor_version}")
51+
endif()
52+
53+
include(FindPackageHandleStandardArgs)
54+
find_package_handle_standard_args(libedit
55+
REQUIRED_VARS
56+
libedit_INCLUDE_DIRS
57+
libedit_LIBRARIES
58+
VERSION_VAR
59+
libedit_VERSION_STRING)
60+
mark_as_advanced(libedit_INCLUDE_DIRS libedit_LIBRARIES)
61+
endif()

include/swift/Config.h.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#cmakedefine SWIFT_HAVE_WORKING_STD_REGEX 1
88

9-
#cmakedefine HAVE_UNICODE_LIBEDIT 1
10-
119
#cmakedefine HAVE_WAIT4 1
1210

1311
#cmakedefine HAVE_PROC_PID_RUSAGE 1

lib/Immediate/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ target_link_libraries(swiftImmediate PRIVATE
1212
swiftIRGen
1313
swiftSILGen
1414
swiftSILOptimizer)
15-
16-
if(HAVE_UNICODE_LIBEDIT)
17-
target_link_libraries(swiftImmediate PRIVATE edit)
15+
if(libedit_FOUND)
16+
target_compile_definitions(swiftImmediate PRIVATE
17+
HAVE_LIBEDIT)
18+
target_link_libraries(swiftImmediate PRIVATE
19+
${libedit_LIBRARIES})
1820
endif()

lib/Immediate/REPL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "llvm/Support/PrettyStackTrace.h"
3838
#include "llvm/Support/Process.h"
3939

40-
#if HAVE_UNICODE_LIBEDIT
40+
#if HAVE_LIBEDIT
4141
#include <histedit.h>
4242
#include <wchar.h>
4343
#endif
@@ -120,8 +120,8 @@ class ConvertForWcharSize<4> {
120120
};
121121

122122
using Convert = ConvertForWcharSize<sizeof(wchar_t)>;
123-
124-
#if HAVE_UNICODE_LIBEDIT
123+
124+
#if HAVE_LIBEDIT
125125
static void convertFromUTF8(llvm::StringRef utf8,
126126
llvm::SmallVectorImpl<wchar_t> &out) {
127127
size_t reserve = out.size() + utf8.size();
@@ -135,7 +135,7 @@ static void convertFromUTF8(llvm::StringRef utf8,
135135
(void)res;
136136
out.set_size(wide_begin - out.begin());
137137
}
138-
138+
139139
static void convertToUTF8(llvm::ArrayRef<wchar_t> wide,
140140
llvm::SmallVectorImpl<char> &out) {
141141
size_t reserve = out.size() + wide.size()*4;
@@ -153,7 +153,7 @@ static void convertToUTF8(llvm::ArrayRef<wchar_t> wide,
153153

154154
} // end anonymous namespace
155155

156-
#if HAVE_UNICODE_LIBEDIT
156+
#if HAVE_LIBEDIT
157157

158158
static ModuleDecl *
159159
typeCheckREPLInput(ModuleDecl *MostRecentModule, StringRef Name,

tools/SourceKit/tools/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include_directories(
66

77
add_swift_lib_subdirectory(sourcekitd)
88
add_swift_tool_subdirectory(sourcekitd-test)
9-
if(HAVE_UNICODE_LIBEDIT)
9+
if(libedit_FOUND)
1010
add_swift_tool_subdirectory(sourcekitd-repl)
1111
endif()
1212
add_swift_tool_subdirectory(complete-test)
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} edit)
2-
check_symbol_exists(el_wgets "histedit.h" HAVE_UNICODE_LIBEDIT)
3-
4-
if(HAVE_UNICODE_LIBEDIT)
5-
add_sourcekit_executable(sourcekitd-repl
6-
sourcekitd-repl.cpp
7-
LLVM_LINK_COMPONENTS coverage lto
8-
)
9-
target_link_libraries(sourcekitd-repl PRIVATE edit)
10-
if(SWIFT_SOURCEKIT_USE_INPROC_LIBRARY)
11-
target_link_libraries(sourcekitd-repl PRIVATE sourcekitdInProc)
12-
else()
13-
target_link_libraries(sourcekitd-repl PRIVATE sourcekitd)
14-
endif()
15-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
16-
target_link_libraries(sourcekitd-repl PRIVATE
17-
dispatch
18-
BlocksRuntime)
19-
endif()
20-
21-
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
22-
set_target_properties(sourcekitd-repl
23-
PROPERTIES
24-
LINK_FLAGS "-Wl,-rpath -Wl,@executable_path/../lib")
25-
endif()
26-
if(SWIFT_ANALYZE_CODE_COVERAGE)
27-
set_property(TARGET sourcekitd-repl APPEND_STRING PROPERTY
28-
LINK_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
29-
endif()
1+
add_sourcekit_executable(sourcekitd-repl
2+
sourcekitd-repl.cpp
3+
LLVM_LINK_COMPONENTS coverage lto
4+
)
5+
if(SWIFT_SOURCEKIT_USE_INPROC_LIBRARY)
6+
target_link_libraries(sourcekitd-repl PRIVATE sourcekitdInProc)
7+
else()
8+
target_link_libraries(sourcekitd-repl PRIVATE sourcekitd)
9+
endif()
10+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
11+
target_link_libraries(sourcekitd-repl PRIVATE
12+
dispatch
13+
BlocksRuntime)
14+
endif()
15+
target_include_directories(sourcekitd-repl PRIVATE
16+
${libedit_INCLUDE_DIRS})
17+
target_link_libraries(sourcekitd-repl PRIVATE
18+
${libedit_LIBRARIES})
3019

31-
add_dependencies(tools sourcekitd-repl)
32-
swift_install_in_component(TARGETS sourcekitd-repl
33-
RUNTIME
34-
DESTINATION bin
35-
COMPONENT tools)
20+
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
21+
set_target_properties(sourcekitd-repl
22+
PROPERTIES
23+
LINK_FLAGS "-Wl,-rpath -Wl,@executable_path/../lib")
24+
endif()
25+
if(SWIFT_ANALYZE_CODE_COVERAGE)
26+
set_property(TARGET sourcekitd-repl APPEND_STRING PROPERTY
27+
LINK_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
3628
endif()
29+
30+
add_dependencies(tools sourcekitd-repl)
31+
swift_install_in_component(TARGETS sourcekitd-repl
32+
RUNTIME
33+
DESTINATION bin
34+
COMPONENT tools)

tools/driver/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ target_link_libraries(swift
99
PRIVATE
1010
swiftDriver
1111
swiftFrontendTool)
12-
if(HAVE_UNICODE_LIBEDIT)
13-
target_link_libraries(swift PRIVATE edit)
14-
endif()
1512

1613
swift_create_post_build_symlink(swift
1714
SOURCE "swift${CMAKE_EXECUTABLE_SUFFIX}"

tools/swift-remoteast-test/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ target_link_libraries(swift-remoteast-test
77
swiftFrontendTool
88
swiftRemoteAST)
99
set_target_properties(swift-remoteast-test PROPERTIES ENABLE_EXPORTS 1)
10-
if(HAVE_UNICODE_LIBEDIT)
11-
target_link_libraries(swift-remoteast-test PRIVATE edit)
12-
endif()
1310

1411
# If building as part of clang, make sure the headers are installed.
1512
if(NOT SWIFT_BUILT_STANDALONE)

0 commit comments

Comments
 (0)