Skip to content

Commit b0b6977

Browse files
committed
[Build] Make CMake locate librt for us.
This is needed when we're statically linking, otherwise we can't pull in Dispatch because we won't have RT::rt as a CMake target. rdar://123381867
1 parent 32e9e2d commit b0b6977

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ endif()
5757

5858
find_package(ICU COMPONENTS uc i18n REQUIRED OPTIONAL_COMPONENTS data)
5959

60+
# This is needed if we're statically linking, otherwise we can't pull in Dispatch
61+
# because we won't have RT::rt as a CMake target.
62+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Android)
63+
find_package(LibRT)
64+
endif()
65+
6066
include(SwiftSupport)
6167
include(GNUInstallDirs)
6268
include(XCTest)

cmake/modules/FindLibRT.cmake

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#.rst:
2+
# FindLibRT
3+
# ---------
4+
#
5+
# Find librt library and headers.
6+
#
7+
# The mdoule defines the following variables:
8+
#
9+
# ::
10+
#
11+
# LibRT_FOUND - true if librt was found
12+
# LibRT_INCLUDE_DIR - include search path
13+
# LibRT_LIBRARIES - libraries to link
14+
15+
if(UNIX)
16+
find_path(LibRT_INCLUDE_DIR
17+
NAMES
18+
time.h)
19+
find_library(LibRT_LIBRARIES rt)
20+
21+
include(FindPackageHandleStandardArgs)
22+
find_package_handle_standard_args(LibRT
23+
REQUIRED_VARS
24+
LibRT_LIBRARIES
25+
LibRT_INCLUDE_DIR)
26+
27+
if(LibRT_FOUND)
28+
if(NOT TARGET RT::rt)
29+
add_library(RT::rt UNKNOWN IMPORTED)
30+
set_target_properties(RT::rt
31+
PROPERTIES
32+
IMPORTED_LOCATION ${LibRT_LIBRARIES}
33+
INTERFACE_INCLUDE_DIRECTORIES ${LibRT_INCLUDE_DIR})
34+
endif()
35+
endif()
36+
37+
mark_as_advanced(LibRT_LIBRARIES LibRT_INCLUDE_DIR)
38+
endif()
39+

0 commit comments

Comments
 (0)