xref: /libuv/CMakeLists.txt (revision 2d8371a0)
1cmake_minimum_required(VERSION 3.10)
2
3if(POLICY CMP0091)
4  cmake_policy(SET CMP0091 NEW) # Enable MSVC_RUNTIME_LIBRARY setting
5endif()
6if(POLICY CMP0092)
7  cmake_policy(SET CMP0092 NEW) # disable /W3 warning, if possible
8endif()
9
10project(libuv LANGUAGES C)
11
12list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
13
14include(CMakePackageConfigHelpers)
15include(CMakeDependentOption)
16include(CheckCCompilerFlag)
17include(GNUInstallDirs)
18include(CTest)
19
20set(CMAKE_C_VISIBILITY_PRESET hidden)
21set(CMAKE_C_STANDARD_REQUIRED ON)
22set(CMAKE_C_EXTENSIONS ON)
23set(CMAKE_C_STANDARD 90)
24
25set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
26
27option(LIBUV_BUILD_SHARED "Build shared lib" ON)
28
29cmake_dependent_option(LIBUV_BUILD_TESTS
30  "Build the unit tests when BUILD_TESTING is enabled and we are the root project" ON
31  "BUILD_TESTING;LIBUV_BUILD_SHARED;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
32cmake_dependent_option(LIBUV_BUILD_BENCH
33  "Build the benchmarks when building unit tests and we are the root project" ON
34  "LIBUV_BUILD_TESTS" OFF)
35
36# Qemu Build
37option(QEMU "build for qemu" OFF)
38if(QEMU)
39  list(APPEND uv_defines __QEMU__=1)
40endif()
41
42# Note: these are mutually exclusive.
43option(ASAN "Enable AddressSanitizer (ASan)" OFF)
44option(MSAN "Enable MemorySanitizer (MSan)" OFF)
45option(TSAN "Enable ThreadSanitizer (TSan)" OFF)
46option(UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
47
48if(MSAN AND NOT CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
49  message(SEND_ERROR "MemorySanitizer requires clang. Try again with -DCMAKE_C_COMPILER=clang")
50endif()
51
52if(ASAN)
53  list(APPEND uv_defines __ASAN__=1)
54  if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|GNU|Clang")
55    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
56    set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
57    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
58  elseif(MSVC)
59    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /fsanitize=address")
60  else()
61    message(SEND_ERROR "AddressSanitizer support requires clang, gcc, or msvc. Try again with -DCMAKE_C_COMPILER.")
62  endif()
63endif()
64
65if(MSAN)
66  list(APPEND uv_defines __MSAN__=1)
67  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=memory")
68  set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=memory")
69  set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=memory")
70endif()
71
72if(TSAN)
73  list(APPEND uv_defines __TSAN__=1)
74  if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|GNU|Clang")
75    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
76    set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
77    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
78  else()
79    message(SEND_ERROR "ThreadSanitizer support requires clang or gcc. Try again with -DCMAKE_C_COMPILER.")
80  endif()
81endif()
82
83if(UBSAN)
84  cmake_minimum_required(VERSION 3.13)
85  list(APPEND uv_defines __UBSAN__=1)
86  if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|GNU|Clang")
87    add_compile_options("-fsanitize=undefined" "-fno-sanitize-recover=undefined")
88    if (NOT WIN32)
89      add_link_options("-fsanitize=undefined")
90    endif()
91    if(MSVC)
92      add_compile_options("/Oy-")
93    else()
94      add_compile_options("-fno-omit-frame-pointer")
95    endif()
96  else()
97    message(SEND_ERROR "UndefinedBehaviorSanitizer support requires clang or gcc. Try again with -DCMAKE_C_COMPILER.")
98  endif()
99endif()
100
101# Compiler check
102string(CONCAT is-msvc $<OR:
103  $<C_COMPILER_ID:MSVC>,
104  $<STREQUAL:${CMAKE_C_COMPILER_FRONTEND_VARIANT},MSVC>
105>)
106
107check_c_compiler_flag(/W4 UV_LINT_W4)
108check_c_compiler_flag(/wd4100 UV_LINT_NO_UNUSED_PARAMETER_MSVC)
109check_c_compiler_flag(/wd4127 UV_LINT_NO_CONDITIONAL_CONSTANT_MSVC)
110check_c_compiler_flag(/wd4201 UV_LINT_NO_NONSTANDARD_MSVC)
111check_c_compiler_flag(/wd4206 UV_LINT_NO_NONSTANDARD_EMPTY_TU_MSVC)
112check_c_compiler_flag(/wd4210 UV_LINT_NO_NONSTANDARD_FILE_SCOPE_MSVC)
113check_c_compiler_flag(/wd4232 UV_LINT_NO_NONSTANDARD_NONSTATIC_DLIMPORT_MSVC)
114check_c_compiler_flag(/wd4456 UV_LINT_NO_HIDES_LOCAL)
115check_c_compiler_flag(/wd4457 UV_LINT_NO_HIDES_PARAM)
116check_c_compiler_flag(/wd4459 UV_LINT_NO_HIDES_GLOBAL)
117check_c_compiler_flag(/wd4706 UV_LINT_NO_CONDITIONAL_ASSIGNMENT_MSVC)
118check_c_compiler_flag(/wd4996 UV_LINT_NO_UNSAFE_MSVC)
119
120check_c_compiler_flag(-Wall UV_LINT_WALL) # DO NOT use this under MSVC
121
122# TODO: Place these into its own function
123check_c_compiler_flag(-Wno-unused-parameter UV_LINT_NO_UNUSED_PARAMETER)
124check_c_compiler_flag(-Wstrict-prototypes UV_LINT_STRICT_PROTOTYPES)
125check_c_compiler_flag(-Wextra UV_LINT_EXTRA)
126
127check_c_compiler_flag(/utf-8 UV_LINT_UTF8_MSVC)
128
129set(lint-no-unused-parameter $<$<BOOL:${UV_LINT_NO_UNUSED_PARAMETER}>:-Wno-unused-parameter>)
130set(lint-strict-prototypes $<$<BOOL:${UV_LINT_STRICT_PROTOTYPES}>:-Wstrict-prototypes>)
131set(lint-extra $<$<BOOL:${UV_LINT_EXTRA}>:-Wextra>)
132set(lint-w4 $<$<BOOL:${UV_LINT_W4}>:/W4>)
133set(lint-no-unused-parameter-msvc $<$<BOOL:${UV_LINT_NO_UNUSED_PARAMETER_MSVC}>:/wd4100>)
134set(lint-no-conditional-constant-msvc $<$<BOOL:${UV_LINT_NO_CONDITIONAL_CONSTANT_MSVC}>:/wd4127>)
135set(lint-no-nonstandard-msvc $<$<BOOL:${UV_LINT_NO_NONSTANDARD_MSVC}>:/wd4201>)
136set(lint-no-nonstandard-empty-tu-msvc $<$<BOOL:${UV_LINT_NO_NONSTANDARD_EMPTY_TU_MSVC}>:/wd4206>)
137set(lint-no-nonstandard-file-scope-msvc $<$<BOOL:${UV_LINT_NO_NONSTANDARD_FILE_SCOPE_MSVC}>:/wd4210>)
138set(lint-no-nonstandard-nonstatic-dlimport-msvc $<$<BOOL:${UV_LINT_NO_NONSTANDARD_NONSTATIC_DLIMPORT_MSVC}>:/wd4232>)
139set(lint-no-hides-local-msvc $<$<BOOL:${UV_LINT_NO_HIDES_LOCAL}>:/wd4456>)
140set(lint-no-hides-param-msvc $<$<BOOL:${UV_LINT_NO_HIDES_PARAM}>:/wd4457>)
141set(lint-no-hides-global-msvc $<$<BOOL:${UV_LINT_NO_HIDES_GLOBAL}>:/wd4459>)
142set(lint-no-conditional-assignment-msvc $<$<BOOL:${UV_LINT_NO_CONDITIONAL_ASSIGNMENT_MSVC}>:/wd4706>)
143set(lint-no-unsafe-msvc $<$<BOOL:${UV_LINT_NO_UNSAFE_MSVC}>:/wd4996>)
144# Unfortunately, this one is complicated because MSVC and clang-cl support -Wall
145# but using it is like calling -Weverything
146string(CONCAT lint-default $<
147  $<AND:$<BOOL:${UV_LINT_WALL}>,$<NOT:${is-msvc}>>:-Wall
148>)
149set(lint-utf8-msvc $<$<BOOL:${UV_LINT_UTF8_MSVC}>:/utf-8>)
150
151list(APPEND uv_cflags ${lint-strict-prototypes} ${lint-extra} ${lint-default} ${lint-w4})
152list(APPEND uv_cflags ${lint-no-unused-parameter})
153list(APPEND uv_cflags ${lint-no-unused-parameter-msvc})
154list(APPEND uv_cflags ${lint-no-conditional-constant-msvc})
155list(APPEND uv_cflags ${lint-no-nonstandard-msvc})
156list(APPEND uv_cflags ${lint-no-nonstandard-empty-tu-msvc})
157list(APPEND uv_cflags ${lint-no-nonstandard-file-scope-msvc})
158list(APPEND uv_cflags ${lint-no-nonstandard-nonstatic-dlimport-msvc})
159list(APPEND uv_cflags ${lint-no-hides-local-msvc})
160list(APPEND uv_cflags ${lint-no-hides-param-msvc})
161list(APPEND uv_cflags ${lint-no-hides-global-msvc})
162list(APPEND uv_cflags ${lint-no-conditional-assignment-msvc})
163list(APPEND uv_cflags ${lint-no-unsafe-msvc})
164list(APPEND uv_cflags ${lint-utf8-msvc} )
165
166check_c_compiler_flag(-fno-strict-aliasing UV_F_STRICT_ALIASING)
167list(APPEND uv_cflags $<$<BOOL:${UV_F_STRICT_ALIASING}>:-fno-strict-aliasing>)
168
169if (MSVC)
170  # Error on calling undeclared functions.
171  list(APPEND uv_cflags "/we4013")
172endif()
173
174set(uv_sources
175    src/fs-poll.c
176    src/idna.c
177    src/inet.c
178    src/random.c
179    src/strscpy.c
180    src/strtok.c
181    src/thread-common.c
182    src/threadpool.c
183    src/timer.c
184    src/uv-common.c
185    src/uv-data-getter-setters.c
186    src/version.c)
187
188if(WIN32)
189  list(APPEND uv_defines WIN32_LEAN_AND_MEAN _WIN32_WINNT=0x0602 _CRT_DECLARE_NONSTDC_NAMES=0)
190  list(APPEND uv_libraries
191       psapi
192       user32
193       advapi32
194       iphlpapi
195       userenv
196       ws2_32
197       dbghelp
198       ole32
199       shell32)
200  list(APPEND uv_sources
201       src/win/async.c
202       src/win/core.c
203       src/win/detect-wakeup.c
204       src/win/dl.c
205       src/win/error.c
206       src/win/fs.c
207       src/win/fs-event.c
208       src/win/getaddrinfo.c
209       src/win/getnameinfo.c
210       src/win/handle.c
211       src/win/loop-watcher.c
212       src/win/pipe.c
213       src/win/thread.c
214       src/win/poll.c
215       src/win/process.c
216       src/win/process-stdio.c
217       src/win/signal.c
218       src/win/snprintf.c
219       src/win/stream.c
220       src/win/tcp.c
221       src/win/tty.c
222       src/win/udp.c
223       src/win/util.c
224       src/win/winapi.c
225       src/win/winsock.c)
226  list(APPEND uv_test_libraries ws2_32)
227  list(APPEND uv_test_sources src/win/snprintf.c test/runner-win.c)
228else()
229  list(APPEND uv_defines _FILE_OFFSET_BITS=64 _LARGEFILE_SOURCE)
230  if(NOT CMAKE_SYSTEM_NAME MATCHES "Android|OS390|QNX")
231    # TODO: This should be replaced with find_package(Threads) if possible
232    # Android has pthread as part of its c library, not as a separate
233    # libpthread.so.
234    list(APPEND uv_libraries pthread)
235  endif()
236  list(APPEND uv_sources
237       src/unix/async.c
238       src/unix/core.c
239       src/unix/dl.c
240       src/unix/fs.c
241       src/unix/getaddrinfo.c
242       src/unix/getnameinfo.c
243       src/unix/loop-watcher.c
244       src/unix/loop.c
245       src/unix/pipe.c
246       src/unix/poll.c
247       src/unix/process.c
248       src/unix/random-devurandom.c
249       src/unix/signal.c
250       src/unix/stream.c
251       src/unix/tcp.c
252       src/unix/thread.c
253       src/unix/tty.c
254       src/unix/udp.c)
255  list(APPEND uv_test_sources test/runner-unix.c)
256endif()
257
258if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
259  list(APPEND uv_defines
260       _ALL_SOURCE
261       _LINUX_SOURCE_COMPAT
262       _THREAD_SAFE
263       _XOPEN_SOURCE=500
264       HAVE_SYS_AHAFS_EVPRODS_H)
265  list(APPEND uv_libraries perfstat)
266  list(APPEND uv_sources
267       src/unix/aix.c
268       src/unix/aix-common.c)
269endif()
270
271if(CMAKE_SYSTEM_NAME STREQUAL "Android")
272  list(APPEND uv_defines _GNU_SOURCE)
273  list(APPEND uv_libraries dl)
274  list(APPEND uv_sources
275       src/unix/linux.c
276       src/unix/procfs-exepath.c
277       src/unix/random-getentropy.c
278       src/unix/random-getrandom.c
279       src/unix/random-sysctl-linux.c)
280endif()
281
282if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "Android|Linux")
283  list(APPEND uv_sources src/unix/proctitle.c)
284endif()
285
286if(CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD")
287  list(APPEND uv_sources src/unix/freebsd.c)
288endif()
289
290if(CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD|NetBSD|OpenBSD")
291  list(APPEND uv_sources src/unix/posix-hrtime.c src/unix/bsd-proctitle.c)
292endif()
293
294if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD|NetBSD|OpenBSD")
295  list(APPEND uv_sources src/unix/bsd-ifaddrs.c src/unix/kqueue.c)
296endif()
297
298if(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
299  list(APPEND uv_sources src/unix/random-getrandom.c)
300endif()
301
302if(APPLE OR CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
303  list(APPEND uv_sources src/unix/random-getentropy.c)
304endif()
305
306if(APPLE)
307  list(APPEND uv_defines _DARWIN_UNLIMITED_SELECT=1 _DARWIN_USE_64_BIT_INODE=1)
308  list(APPEND uv_sources
309       src/unix/darwin-proctitle.c
310       src/unix/darwin.c
311       src/unix/fsevents.c)
312endif()
313
314if(CMAKE_SYSTEM_NAME STREQUAL "GNU")
315  list(APPEND uv_defines _GNU_SOURCE _POSIX_C_SOURCE=200112 _XOPEN_SOURCE=500)
316  list(APPEND uv_libraries dl)
317  list(APPEND uv_sources
318       src/unix/bsd-ifaddrs.c
319       src/unix/no-fsevents.c
320       src/unix/no-proctitle.c
321       src/unix/posix-hrtime.c
322       src/unix/posix-poll.c
323       src/unix/hurd.c)
324endif()
325
326if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
327  list(APPEND uv_defines _GNU_SOURCE _POSIX_C_SOURCE=200112)
328  list(APPEND uv_libraries dl rt)
329  list(APPEND uv_sources
330       src/unix/linux.c
331       src/unix/procfs-exepath.c
332       src/unix/random-getrandom.c
333       src/unix/random-sysctl-linux.c)
334endif()
335
336if(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
337  list(APPEND uv_sources src/unix/netbsd.c)
338  list(APPEND uv_libraries kvm)
339endif()
340
341if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
342  list(APPEND uv_sources src/unix/openbsd.c)
343endif()
344
345if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
346  enable_language(CXX)
347  list(APPEND uv_defines PATH_MAX=1024)
348  list(APPEND uv_defines _AE_BIMODAL)
349  list(APPEND uv_defines _ALL_SOURCE)
350  list(APPEND uv_defines _ENHANCED_ASCII_EXT=0xFFFFFFFF)
351  list(APPEND uv_defines _ISOC99_SOURCE)
352  list(APPEND uv_defines _LARGE_TIME_API)
353  list(APPEND uv_defines _OPEN_MSGQ_EXT)
354  list(APPEND uv_defines _OPEN_SYS_FILE_EXT)
355  list(APPEND uv_defines _OPEN_SYS_IF_EXT)
356  list(APPEND uv_defines _OPEN_SYS_SOCK_EXT3)
357  list(APPEND uv_defines _OPEN_SYS_SOCK_IPV6)
358  list(APPEND uv_defines _UNIX03_SOURCE)
359  list(APPEND uv_defines _UNIX03_THREADS)
360  list(APPEND uv_defines _UNIX03_WITHDRAWN)
361  list(APPEND uv_defines _XOPEN_SOURCE=600)
362  list(APPEND uv_defines _XOPEN_SOURCE_EXTENDED)
363  list(APPEND uv_sources
364       src/unix/os390.c
365       src/unix/os390-syscalls.c
366       src/unix/os390-proctitle.c)
367  list(APPEND uv_cflags
368       -q64
369       -qascii
370       -qexportall
371       -qgonumber
372       -qlongname
373       -qlibansi
374       -qfloat=IEEE
375       -qtune=10
376       -qarch=10
377       -qasm
378       -qasmlib=sys1.maclib:sys1.modgen)
379  find_library(ZOSLIB
380    NAMES zoslib
381    PATHS ${ZOSLIB_DIR}
382    PATH_SUFFIXES lib
383  )
384  list(APPEND uv_libraries ${ZOSLIB})
385endif()
386
387if(CMAKE_SYSTEM_NAME STREQUAL "OS400")
388  list(APPEND uv_defines
389       _ALL_SOURCE
390       _LINUX_SOURCE_COMPAT
391       _THREAD_SAFE
392       _XOPEN_SOURCE=500)
393  list(APPEND uv_sources
394    src/unix/aix-common.c
395    src/unix/ibmi.c
396    src/unix/no-fsevents.c
397    src/unix/posix-poll.c)
398endif()
399
400if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
401  if(CMAKE_SYSTEM_VERSION STREQUAL "5.10")
402    list(APPEND uv_defines SUNOS_NO_IFADDRS)
403    list(APPEND uv_libraries rt)
404  endif()
405  list(APPEND uv_defines __EXTENSIONS__ _XOPEN_SOURCE=500 _REENTRANT)
406  list(APPEND uv_libraries kstat nsl sendfile socket)
407  list(APPEND uv_sources
408       src/unix/no-proctitle.c
409       src/unix/sunos.c)
410endif()
411
412if(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
413  list(APPEND uv_defines _BSD_SOURCE)
414  list(APPEND uv_libraries bsd network)
415  list(APPEND uv_sources
416	  src/unix/haiku.c
417	  src/unix/bsd-ifaddrs.c
418	  src/unix/no-fsevents.c
419	  src/unix/no-proctitle.c
420	  src/unix/posix-hrtime.c
421	  src/unix/posix-poll.c)
422endif()
423
424if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
425  list(APPEND uv_sources
426    src/unix/posix-hrtime.c
427    src/unix/posix-poll.c
428    src/unix/qnx.c
429    src/unix/bsd-ifaddrs.c
430    src/unix/no-proctitle.c
431    src/unix/no-fsevents.c)
432  list(APPEND uv_libraries socket)
433endif()
434
435if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD|Linux|NetBSD|OpenBSD")
436  list(APPEND uv_test_libraries util)
437endif()
438
439if(CYGWIN OR MSYS)
440  list(APPEND uv_defines _GNU_SOURCE)
441  list(APPEND uv_sources
442       src/unix/cygwin.c
443       src/unix/bsd-ifaddrs.c
444       src/unix/no-fsevents.c
445       src/unix/no-proctitle.c
446       src/unix/posix-hrtime.c
447       src/unix/posix-poll.c
448       src/unix/procfs-exepath.c
449       src/unix/sysinfo-loadavg.c
450       src/unix/sysinfo-memory.c)
451endif()
452
453if(LIBUV_BUILD_SHARED)
454  add_library(uv SHARED ${uv_sources})
455  target_compile_definitions(uv
456    INTERFACE
457      USING_UV_SHARED=1
458    PRIVATE
459      BUILDING_UV_SHARED=1
460      ${uv_defines})
461  target_compile_options(uv PRIVATE ${uv_cflags})
462  target_include_directories(uv
463    PUBLIC
464      $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
465      $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
466    PRIVATE
467      $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
468  if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
469    target_include_directories(uv PUBLIC $<BUILD_INTERFACE:${ZOSLIB_DIR}/include>)
470    set_target_properties(uv PROPERTIES LINKER_LANGUAGE CXX)
471  endif()
472  target_link_libraries(uv ${uv_libraries})
473  set_target_properties(uv PROPERTIES OUTPUT_NAME "uv")
474endif()
475
476add_library(uv_a STATIC ${uv_sources})
477target_compile_definitions(uv_a PRIVATE ${uv_defines})
478target_compile_options(uv_a PRIVATE ${uv_cflags})
479target_include_directories(uv_a
480  PUBLIC
481    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
482    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
483  PRIVATE
484    $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
485if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
486  target_include_directories(uv_a PUBLIC $<BUILD_INTERFACE:${ZOSLIB_DIR}/include>)
487  set_target_properties(uv_a PROPERTIES LINKER_LANGUAGE CXX)
488endif()
489target_link_libraries(uv_a ${uv_libraries})
490set_target_properties(uv_a PROPERTIES OUTPUT_NAME "uv")
491if(WIN32)
492  set_target_properties(uv_a PROPERTIES PREFIX "lib")
493endif()
494
495if(LIBUV_BUILD_TESTS)
496  # Small hack: use ${uv_test_sources} now to get the runner skeleton,
497  # before the actual tests are added.
498  add_executable(
499    uv_run_benchmarks_a
500    ${uv_test_sources}
501    test/benchmark-async-pummel.c
502    test/benchmark-async.c
503    test/benchmark-fs-stat.c
504    test/benchmark-getaddrinfo.c
505    test/benchmark-loop-count.c
506    test/benchmark-queue-work.c
507    test/benchmark-million-async.c
508    test/benchmark-million-timers.c
509    test/benchmark-multi-accept.c
510    test/benchmark-ping-pongs.c
511    test/benchmark-ping-udp.c
512    test/benchmark-pound.c
513    test/benchmark-pump.c
514    test/benchmark-sizes.c
515    test/benchmark-spawn.c
516    test/benchmark-tcp-write-batch.c
517    test/benchmark-thread.c
518    test/benchmark-udp-pummel.c
519    test/blackhole-server.c
520    test/echo-server.c
521    test/run-benchmarks.c
522    test/runner.c)
523  target_compile_definitions(uv_run_benchmarks_a PRIVATE ${uv_defines})
524  target_compile_options(uv_run_benchmarks_a PRIVATE ${uv_cflags})
525  target_link_libraries(uv_run_benchmarks_a uv_a ${uv_test_libraries})
526
527  list(APPEND uv_test_sources
528       test/blackhole-server.c
529       test/echo-server.c
530       test/run-tests.c
531       test/runner.c
532       test/test-active.c
533       test/test-async-null-cb.c
534       test/test-async.c
535       test/test-barrier.c
536       test/test-callback-stack.c
537       test/test-close-fd.c
538       test/test-close-order.c
539       test/test-condvar.c
540       test/test-connect-unspecified.c
541       test/test-connection-fail.c
542       test/test-cwd-and-chdir.c
543       test/test-default-loop-close.c
544       test/test-delayed-accept.c
545       test/test-dlerror.c
546       test/test-eintr-handling.c
547       test/test-embed.c
548       test/test-emfile.c
549       test/test-env-vars.c
550       test/test-error.c
551       test/test-fail-always.c
552       test/test-fork.c
553       test/test-fs-copyfile.c
554       test/test-fs-event.c
555       test/test-fs-poll.c
556       test/test-fs.c
557       test/test-fs-readdir.c
558       test/test-fs-fd-hash.c
559       test/test-fs-open-flags.c
560       test/test-get-currentexe.c
561       test/test-get-loadavg.c
562       test/test-get-memory.c
563       test/test-get-passwd.c
564       test/test-getaddrinfo.c
565       test/test-gethostname.c
566       test/test-getnameinfo.c
567       test/test-getsockname.c
568       test/test-getters-setters.c
569       test/test-gettimeofday.c
570       test/test-handle-fileno.c
571       test/test-homedir.c
572       test/test-hrtime.c
573       test/test-idle.c
574       test/test-idna.c
575       test/test-iouring-pollhup.c
576       test/test-ip4-addr.c
577       test/test-ip6-addr.c
578       test/test-ip-name.c
579       test/test-ipc-heavy-traffic-deadlock-bug.c
580       test/test-ipc-send-recv.c
581       test/test-ipc.c
582       test/test-loop-alive.c
583       test/test-loop-close.c
584       test/test-loop-configure.c
585       test/test-loop-handles.c
586       test/test-loop-stop.c
587       test/test-loop-time.c
588       test/test-metrics.c
589       test/test-multiple-listen.c
590       test/test-mutexes.c
591       test/test-not-readable-nor-writable-on-read-error.c
592       test/test-not-writable-after-shutdown.c
593       test/test-osx-select.c
594       test/test-pass-always.c
595       test/test-ping-pong.c
596       test/test-pipe-bind-error.c
597       test/test-pipe-close-stdout-read-stdin.c
598       test/test-pipe-connect-error.c
599       test/test-pipe-connect-multiple.c
600       test/test-pipe-connect-prepare.c
601       test/test-pipe-getsockname.c
602       test/test-pipe-pending-instances.c
603       test/test-pipe-sendmsg.c
604       test/test-pipe-server-close.c
605       test/test-pipe-set-fchmod.c
606       test/test-pipe-set-non-blocking.c
607       test/test-platform-output.c
608       test/test-poll-close-doesnt-corrupt-stack.c
609       test/test-poll-close.c
610       test/test-poll-closesocket.c
611       test/test-poll-multiple-handles.c
612       test/test-poll-oob.c
613       test/test-poll.c
614       test/test-process-priority.c
615       test/test-process-title-threadsafe.c
616       test/test-process-title.c
617       test/test-queue-foreach-delete.c
618       test/test-random.c
619       test/test-readable-on-eof.c
620       test/test-ref.c
621       test/test-run-nowait.c
622       test/test-run-once.c
623       test/test-semaphore.c
624       test/test-shutdown-close.c
625       test/test-shutdown-eof.c
626       test/test-shutdown-simultaneous.c
627       test/test-shutdown-twice.c
628       test/test-signal-multiple-loops.c
629       test/test-signal-pending-on-close.c
630       test/test-signal.c
631       test/test-socket-buffer-size.c
632       test/test-spawn.c
633       test/test-stdio-over-pipes.c
634       test/test-strscpy.c
635       test/test-strtok.c
636       test/test-tcp-alloc-cb-fail.c
637       test/test-tcp-bind-error.c
638       test/test-tcp-bind6-error.c
639       test/test-tcp-close-accept.c
640       test/test-tcp-close-after-read-timeout.c
641       test/test-tcp-close-while-connecting.c
642       test/test-tcp-close.c
643       test/test-tcp-close-reset.c
644       test/test-tcp-connect-error-after-write.c
645       test/test-tcp-connect-error.c
646       test/test-tcp-connect-timeout.c
647       test/test-tcp-connect6-error.c
648       test/test-tcp-create-socket-early.c
649       test/test-tcp-flags.c
650       test/test-tcp-oob.c
651       test/test-tcp-open.c
652       test/test-tcp-read-stop.c
653       test/test-tcp-reuseport.c
654       test/test-tcp-read-stop-start.c
655       test/test-tcp-rst.c
656       test/test-tcp-shutdown-after-write.c
657       test/test-tcp-try-write.c
658       test/test-tcp-write-in-a-row.c
659       test/test-tcp-try-write-error.c
660       test/test-tcp-unexpected-read.c
661       test/test-tcp-write-after-connect.c
662       test/test-tcp-write-fail.c
663       test/test-tcp-write-queue-order.c
664       test/test-tcp-write-to-half-open-connection.c
665       test/test-tcp-writealot.c
666       test/test-test-macros.c
667       test/test-thread-affinity.c
668       test/test-thread-equal.c
669       test/test-thread.c
670       test/test-thread-priority.c
671       test/test-threadpool-cancel.c
672       test/test-threadpool.c
673       test/test-timer-again.c
674       test/test-timer-from-check.c
675       test/test-timer.c
676       test/test-tmpdir.c
677       test/test-tty-duplicate-key.c
678       test/test-tty-escape-sequence-processing.c
679       test/test-tty.c
680       test/test-udp-alloc-cb-fail.c
681       test/test-udp-bind.c
682       test/test-udp-connect.c
683       test/test-udp-connect6.c
684       test/test-udp-create-socket-early.c
685       test/test-udp-dgram-too-big.c
686       test/test-udp-ipv6.c
687       test/test-udp-mmsg.c
688       test/test-udp-multicast-interface.c
689       test/test-udp-multicast-interface6.c
690       test/test-udp-multicast-join.c
691       test/test-udp-multicast-join6.c
692       test/test-udp-multicast-ttl.c
693       test/test-udp-open.c
694       test/test-udp-options.c
695       test/test-udp-send-and-recv.c
696       test/test-udp-send-hang-loop.c
697       test/test-udp-send-immediate.c
698       test/test-udp-sendmmsg-error.c
699       test/test-udp-send-unreachable.c
700       test/test-udp-try-send.c
701       test/test-udp-recv-in-a-row.c
702       test/test-udp-reuseport.c
703       test/test-uname.c
704       test/test-walk-handles.c
705       test/test-watcher-cross-stop.c)
706
707  add_executable(uv_run_tests ${uv_test_sources} uv_win_longpath.manifest)
708  target_compile_definitions(uv_run_tests
709                             PRIVATE ${uv_defines} USING_UV_SHARED=1)
710  target_compile_options(uv_run_tests PRIVATE ${uv_cflags})
711  target_link_libraries(uv_run_tests uv ${uv_test_libraries})
712  add_test(NAME uv_test
713           COMMAND uv_run_tests
714           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
715  if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
716    set_tests_properties(uv_test PROPERTIES ENVIRONMENT
717                         "LIBPATH=${CMAKE_BINARY_DIR}:$ENV{LIBPATH}")
718  endif()
719  if(WIN32)
720    add_custom_command(TARGET uv_run_tests POST_BUILD
721                       COMMAND "${CMAKE_COMMAND}" -E copy
722                       "$<TARGET_FILE:uv_run_tests>"
723                       "$<TARGET_FILE_DIR:uv_run_tests>/uv_run_tests_no_ext")
724  endif()
725  add_executable(uv_run_tests_a ${uv_test_sources} uv_win_longpath.manifest)
726  target_compile_definitions(uv_run_tests_a PRIVATE ${uv_defines})
727  target_compile_options(uv_run_tests_a PRIVATE ${uv_cflags})
728  if(QEMU)
729    target_link_libraries(uv_run_tests_a uv_a ${uv_test_libraries} -static)
730  else()
731    target_link_libraries(uv_run_tests_a uv_a ${uv_test_libraries})
732  endif()
733  add_test(NAME uv_test_a
734           COMMAND uv_run_tests_a
735           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
736  if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
737    set_target_properties(uv_run_benchmarks_a PROPERTIES LINKER_LANGUAGE CXX)
738    set_target_properties(uv_run_tests PROPERTIES LINKER_LANGUAGE CXX)
739    set_target_properties(uv_run_tests_a PROPERTIES LINKER_LANGUAGE CXX)
740  endif()
741  if(WIN32)
742    add_custom_command(TARGET uv_run_tests_a POST_BUILD
743                       COMMAND "${CMAKE_COMMAND}" -E copy
744                       "$<TARGET_FILE:uv_run_tests_a>"
745                       "$<TARGET_FILE_DIR:uv_run_tests_a>/uv_run_tests_a_no_ext")
746  endif()
747endif()
748
749# Now for some gibbering horrors from beyond the stars...
750foreach(lib IN LISTS uv_libraries)
751  list(APPEND LIBS "-l${lib}")
752endforeach()
753string(REPLACE ";" " " LIBS "${LIBS}")
754# Consider setting project version via project() call?
755file(STRINGS configure.ac configure_ac REGEX ^AC_INIT)
756string(REGEX MATCH "([0-9]+)[.][0-9]+[.][0-9]+" PACKAGE_VERSION "${configure_ac}")
757set(UV_VERSION_MAJOR "${CMAKE_MATCH_1}")
758
759set(includedir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
760set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
761set(prefix ${CMAKE_INSTALL_PREFIX})
762configure_file(libuv-static.pc.in libuv-static.pc @ONLY)
763
764install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
765install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
766install(FILES LICENSE-extra DESTINATION ${CMAKE_INSTALL_DOCDIR})
767install(FILES ${PROJECT_BINARY_DIR}/libuv-static.pc
768        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
769install(TARGETS uv_a EXPORT libuvConfig
770        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
771install(EXPORT libuvConfig
772	DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libuv
773	NAMESPACE libuv::)
774
775if(LIBUV_BUILD_SHARED)
776  # The version in the filename is mirroring the behaviour of autotools.
777  set_target_properties(uv PROPERTIES
778          VERSION ${UV_VERSION_MAJOR}.0.0
779          SOVERSION ${UV_VERSION_MAJOR})
780  configure_file(libuv.pc.in libuv.pc @ONLY)
781  install(FILES ${PROJECT_BINARY_DIR}/libuv.pc
782          DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
783  install(TARGETS uv EXPORT libuvConfig
784          RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
785          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
786          ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
787endif()
788
789if(MSVC)
790  set(CMAKE_DEBUG_POSTFIX d)
791  get_filename_component(CMAKE_C_COMPILER_DIR ${CMAKE_C_COMPILER} DIRECTORY)
792  if(ASAN)
793    file(INSTALL "${CMAKE_C_COMPILER_DIR}/llvm-symbolizer.exe" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
794    file(INSTALL "${CMAKE_C_COMPILER_DIR}/clang_rt.asan_dynamic-x86_64.dll" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
795    file(INSTALL "${CMAKE_C_COMPILER_DIR}/clang_rt.asan_dbg_dynamic-x86_64.dll" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
796  endif()
797endif()
798
799if(BUILD_SHARED_LIBS)
800  set(LIB_SELECTED uv)
801else()
802  set(LIB_SELECTED uv_a)
803endif()
804
805add_library(libuv::libuv ALIAS ${LIB_SELECTED})
806
807message(STATUS "summary of build options:
808    Install prefix:  ${CMAKE_INSTALL_PREFIX}
809    Target system:   ${CMAKE_SYSTEM_NAME}
810    Compiler:
811      C compiler:    ${CMAKE_C_COMPILER} (${CMAKE_C_COMPILER_ID})
812      CFLAGS:        ${CMAKE_C_FLAGS_${_build_type}} ${CMAKE_C_FLAGS}
813")
814