Skip to content

Commit 252642d

Browse files
committed
Allow building on wider range of dependencies on Linux
This patch allows building libdispatch with a wider range of dependencies on Linux systems. This also removes references to ‘swift’ in paths if Swift is not enabled.
1 parent 244a5fe commit 252642d

14 files changed

+136
-40
lines changed

CMakeLists.txt

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ if(ENABLE_SWIFT)
6262
PROPERTIES
6363
IMPORTED_LOCATION
6464
${SWIFT_RUNTIME_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}swiftSwiftOnoneSupport${CMAKE_SHARED_LIBRARY_SUFFIX})
65+
66+
set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}/swift/${SWIFT_OS}" CACHE PATH "Path where the libraries will be installed")
67+
set(INSTALL_DISPATCH_HEADERS_DIR "${INSTALL_LIBDIR}/swift/dispatch" CACHE PATH "Path where the headers will be installed for libdispatch")
68+
set(INSTALL_BLOCK_HEADERS_DIR "${INSTALL_LIBDIR}/swift/Block" CACHE PATH "Path where the headers will be installed for the blocks runtime")
69+
set(INSTALL_OS_HEADERS_DIR "${INSTALL_LIBDIR}/swift/os" CACHE PATH "Path where the os/ headers will be installed")
6570
endif()
6671

67-
if(CMAKE_SYSTEM_NAME STREQUAL Android)
68-
set(ENABLE_DTRACE_DEFAULT OFF)
69-
else()
70-
find_program(DTRACEPROG dtrace)
71-
if(DTRACEPROG)
72-
set(ENABLE_DTRACE_DEFAULT ON)
73-
else()
74-
set(ENABLE_DTRACE_DEFAULT OFF)
75-
endif()
72+
if(NOT ENABLE_SWIFT)
73+
set(INSTALL_TARGET_DIR "${INSTALL_LIBDIR}" CACHE PATH "Path where the libraries will be installed")
74+
set(INSTALL_DISPATCH_HEADERS_DIR "include/dispatch" CACHE PATH "Path where the headers will be installed")
75+
set(INSTALL_BLOCK_HEADERS_DIR "include" CACHE PATH "Path where the headers will be installed for the blocks runtime")
76+
set(INSTALL_OS_HEADERS_DIR "include/os" CACHE PATH "Path where the headers will be installed")
7677
endif()
77-
option(ENABLE_DTRACE "enable dtrace support" ${ENABLE_DTRACE_DEFAULT})
78+
79+
option(ENABLE_DTRACE "enable dtrace support" "")
7880

7981
option(BUILD_SHARED_LIBS "build shared libraries" ON)
8082

@@ -114,13 +116,15 @@ else()
114116
endif()
115117
endif()
116118

119+
option(INSTALL_PRIVATE_HEADERS "installs private headers in the same location as the public ones" OFF)
120+
117121
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
118122
CMAKE_SYSTEM_NAME STREQUAL Android OR
119123
CMAKE_SYSTEM_NAME STREQUAL Windows)
120124
add_library(BlocksRuntime
121125
STATIC
122126
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/data.c
123-
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/runtime.c)
127+
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/runtime.c)
124128
set_target_properties(BlocksRuntime
125129
PROPERTIES
126130
POSITION_INDEPENDENT_CODE TRUE)
@@ -130,6 +134,17 @@ if(CMAKE_SYSTEM_NAME STREQUAL Linux OR
130134
INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS})
131135
endif()
132136
set(WITH_BLOCKS_RUNTIME "${CMAKE_SOURCE_DIR}/src/BlocksRuntime" CACHE PATH "Path to blocks runtime" FORCE)
137+
138+
install(FILES
139+
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/Block.h
140+
DESTINATION
141+
"${INSTALL_BLOCK_HEADERS_DIR}")
142+
if(INSTALL_PRIVATE_HEADERS)
143+
install(FILES
144+
${CMAKE_SOURCE_DIR}/src/BlocksRuntime/Block_private.h
145+
DESTINATION
146+
"${INSTALL_BLOCK_HEADERS_DIR}")
147+
endif()
133148
else()
134149
# TODO(compnerd) support system installed BlocksRuntime
135150
# find_package(BlocksRuntime REQUIRED)
@@ -159,6 +174,7 @@ check_function_exists(pthread_main_np HAVE_PTHREAD_MAIN_NP)
159174
check_function_exists(pthread_workqueue_setdispatch_np HAVE_PTHREAD_WORKQUEUE_SETDISPATCH_NP)
160175
check_function_exists(strlcpy HAVE_STRLCPY)
161176
check_function_exists(sysconf HAVE_SYSCONF)
177+
check_function_exists(arc4random HAVE_ARC4RANDOM)
162178

163179
if(NOT HAVE_STRLCPY AND NOT HAVE_GETPROGNAME)
164180
include(FindPkgConfig)
@@ -233,14 +249,29 @@ check_symbol_exists(VQ_NEARLOWDISK "sys/mount.h" HAVE_DECL_VQ_NEARLOWDISK)
233249
check_symbol_exists(VQ_QUOTA "sys/mount.h" HAVE_DECL_VQ_QUOTA)
234250
check_symbol_exists(VQ_UPDATE "sys/mount.h" HAVE_DECL_VQ_UPDATE)
235251
check_symbol_exists(VQ_VERYLOWDISK "sys/mount.h" HAVE_DECL_VQ_VERYLOWDISK)
236-
252+
check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
237253
check_symbol_exists(program_invocation_name "errno.h" HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
254+
if (HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME)
255+
add_definitions(-D_GNU_SOURCE=1)
256+
endif()
257+
check_symbol_exists(__printflike "bsd/sys/cdefs.h" HAVE_PRINTFLIKE)
238258

239259
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
240260
add_definitions(-DTARGET_OS_WIN32)
241261
endif()
242262

243-
if(ENABLE_DTRACE)
263+
if(CMAKE_SYSTEM_NAME STREQUAL Android)
264+
set(ENABLE_DTRACE_DEFAULT OFF)
265+
endif()
266+
267+
if(ENABLE_DTRACE STREQUAL "")
268+
find_program(dtrace_EXECUTABLE dtrace)
269+
if(dtrace_EXECUTABLE)
270+
add_definitions(-DDISPATCH_USE_DTRACE=1)
271+
else()
272+
add_definitions(-DDISPATCH_USE_DTRACE=0)
273+
endif()
274+
elseif(ENABLE_DTRACE)
244275
find_program(dtrace_EXECUTABLE dtrace)
245276
if(NOT dtrace_EXECUTABLE)
246277
message(FATAL_ERROR "dtrace not found but explicitly requested")

dispatch/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ install(FILES
1414
source.h
1515
time.h
1616
DESTINATION
17-
"${INSTALL_LIBDIR}/swift/dispatch")
17+
"${INSTALL_DISPATCH_HEADERS_DIR}")
1818
if(ENABLE_SWIFT)
1919
get_filename_component(MODULE_MAP module.modulemap REALPATH)
2020
install(FILES
2121
${MODULE_MAP}
2222
DESTINATION
23-
"${INSTALL_LIBDIR}/swift/dispatch")
23+
"${INSTALL_DISPATCH_HEADERS_DIR}")
2424
endif()
2525

os/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ install(FILES
66
object.h
77
linux_base.h
88
DESTINATION
9-
"${INSTALL_LIBDIR}/swift/os")
9+
"${INSTALL_OS_HEADERS_DIR}")
1010

private/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,17 @@
33
# io_private.h layout_private.h mach_private.h private.h queue_private.h
44
# source_private.h are included in the source tarball
55

6+
if (INSTALL_PRIVATE_HEADERS)
7+
install(FILES
8+
benchmark.h
9+
data_private.h
10+
introspection_private.h
11+
io_private.h
12+
layout_private.h
13+
mach_private.h
14+
private.h
15+
queue_private.h
16+
source_private.h
17+
DESTINATION
18+
"${INSTALL_DISPATCH_HEADERS_DIR}")
19+
endif()

src/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ add_library(dispatch
1919
time.c
2020
transform.c
2121
voucher.c
22+
shims.c
2223
protocol.defs
2324
provider.d
2425
allocator_internal.h
@@ -208,12 +209,12 @@ add_custom_command(TARGET dispatch POST_BUILD
208209
install(TARGETS
209210
dispatch
210211
DESTINATION
211-
"${INSTALL_LIBDIR}/swift/${SWIFT_OS}")
212+
"${INSTALL_TARGET_DIR}")
212213
if(ENABLE_SWIFT)
213214
install(FILES
214215
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftmodule
215216
${CMAKE_CURRENT_BINARY_DIR}/swift/Dispatch.swiftdoc
216217
DESTINATION
217-
"${INSTALL_LIBDIR}/swift/${SWIFT_OS}/${CMAKE_SYSTEM_PROCESSOR}")
218+
"${INSTALL_TARGET_DIR}/${CMAKE_SYSTEM_PROCESSOR}")
218219
endif()
219220

src/internal.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,11 @@ DISPATCH_EXPORT DISPATCH_NOTHROW void dispatch_atfork_child(void);
373373
#define _TAILQ_MARK_NOT_ENQUEUED(elm, field) \
374374
do { (elm)->field.tqe_prev = NULL; } while (0)
375375

376-
#if DISPATCH_DEBUG
377376
// sys/queue.h debugging
378-
#if defined(__linux__)
379-
#define QUEUE_MACRO_DEBUG 1
380-
#else
381-
#undef TRASHIT
377+
#ifndef TRASHIT
382378
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
383379
#endif
384-
#endif // DISPATCH_DEBUG
380+
385381
#define _TAILQ_TRASH_ENTRY(elm, field) do { \
386382
TRASHIT((elm)->field.tqe_next); \
387383
TRASHIT((elm)->field.tqe_prev); \

src/shims.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2013-2016 Apple Inc. All rights reserved.
3+
*
4+
* @APPLE_APACHE_LICENSE_HEADER_START@
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @APPLE_APACHE_LICENSE_HEADER_END@
19+
*/
20+
21+
#include "internal.h"
22+
#include "shims.h"
23+
24+
#if !HAVE_STRLCPY
25+
size_t strlcpy(char *dst, const char *src, size_t size) {
26+
size_t res = strlen(dst) + strlen(src) + 1;
27+
if (size > 0) {
28+
size_t n = size - 1;
29+
strncpy(dst, src, n);
30+
dst[n] = 0;
31+
}
32+
return res;
33+
}
34+
#endif

src/shims.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,18 @@
6666
#define FD_COPY(f, t) (void)(*(t) = *(f))
6767
#endif
6868

69+
#if HAVE_STRLCPY
70+
#include <string.h>
71+
#else // that is, if !HAVE_STRLCPY
72+
73+
size_t strlcpy(char *dst, const char *src, size_t size);
74+
75+
#endif // HAVE_STRLCPY
76+
77+
6978
#if TARGET_OS_WIN32
7079
#define bzero(ptr,len) memset((ptr), 0, (len))
7180
#define snprintf _snprintf
72-
73-
inline size_t strlcpy(char *dst, const char *src, size_t size) {
74-
int res = strlen(dst) + strlen(src) + 1;
75-
if (size > 0) {
76-
size_t n = size - 1;
77-
strncpy(dst, src, n);
78-
dst[n] = 0;
79-
}
80-
return res;
81-
}
8281
#endif // TARGET_OS_WIN32
8382

8483
#if PTHREAD_WORKQUEUE_SPI_VERSION < 20140716

src/shims/getprogname.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* @APPLE_APACHE_LICENSE_HEADER_END@
2020
*/
2121

22+
#include <errno.h>
23+
2224
#ifndef __DISPATCH_SHIMS_GETPROGNAME__
2325
#define __DISPATCH_SHIMS_GETPROGNAME__
2426

src/shims/linux_stubs.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
(var) && ((temp) = TAILQ_NEXT((var), field), 1); (var) = (temp))
2323
#endif
2424

25-
#if DISPATCH_DEBUG
26-
#ifndef TRASHIT
27-
#define TRASHIT(x) do { (x) = (void *)-1; } while (0)
28-
#endif
29-
#endif
30-
3125
/*
3226
* Stub out defines for some mach types and related macros
3327
*/

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "${CMAKE_SOURCE_DIR}/private" "${CMAKE_CURRENT_BINARY_DIR}/dispatch")
33
execute_process(COMMAND "${CMAKE_COMMAND}" -E create_symlink "${CMAKE_CURRENT_SOURCE_DIR}/leaks-wrapper.sh" "${CMAKE_CURRENT_BINARY_DIR}/leaks-wrapper")
44

5+
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
6+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lrt")
7+
endif()
8+
59
add_library(bsdtests
610
STATIC
711
bsdtests.c

tests/bsdtests.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
#ifndef __BSD_TEST_H__
2222
#define __BSD_TEST_H__
2323

24+
25+
#if !HAVE_PRINTFLIKE
26+
#ifndef __printflike
27+
#if __has_attribute(format)
28+
#define __printflike(a,b) __attribute__((format(printf, a, b)))
29+
#else
30+
#define __printflike(a,b)
31+
#endif // __has_attribute(format)
32+
#endif // !defined(__printflike)
33+
#endif // !HAVE_PRINTFLIKE
34+
35+
2436
#include <errno.h>
2537
#ifdef __APPLE__
2638
#include <mach/error.h>
@@ -30,6 +42,7 @@
3042
#endif
3143

3244
#include <string.h>
45+
#include <stdint.h>
3346

3447
static inline const char*
3548
__BASENAME__(const char *_str_)

tests/dispatch_group.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ test_group_notify2(long cycle, dispatch_group_t tested)
126126

127127
// n=4 works great for a 4CPU Mac Pro, this might work for a wider range of
128128
// systems.
129+
#if HAVE_ARC4RANDOM
129130
const int n = 1 + arc4random() % 8;
131+
#else
132+
const int n = 1 + random() % 8;
133+
#endif
130134
dispatch_group_t group = dispatch_group_create();
131135
dispatch_queue_t qa[n];
132136

tests/dispatch_queue_finalizer.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ main(void)
5050
{
5151
dispatch_test_start("Dispatch Queue Finalizer");
5252

53+
#if HAS_ARC4RANDOM
5354
#if defined(__LP64__) || defined(_WIN64)
5455
ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random());
5556
#else
5657
ctxt_magic = (void*)arc4random();
5758
#endif
59+
#else // that is, if !HAS_ARC4RANDOM
60+
ctxt_magic = (void *)random();
61+
#endif
5862

5963
// we need a non-NULL value for the tests to work properly
6064
if (ctxt_magic == NULL) {

0 commit comments

Comments
 (0)