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