11cmake_minimum_required(VERSION 3.4)
2- project(libuv LANGUAGES C)
32
4- cmake_policy(SET CMP0057 NEW) # Enable IN_LIST operator
5- cmake_policy(SET CMP0064 NEW) # Support if (TEST) operator
3+ if(POLICY CMP0091)
4+ cmake_policy(SET CMP0091 NEW) # Enable MSVC_RUNTIME_LIBRARY setting
5+ endif()
6+ if(POLICY CMP0092)
7+ cmake_policy(SET CMP0092 NEW) # disable /W3 warning, if possible
8+ endif()
9+
10+ project(libuv LANGUAGES C)
611
712list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
813
@@ -17,38 +22,75 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
1722set(CMAKE_C_EXTENSIONS ON)
1823set(CMAKE_C_STANDARD 90)
1924
25+ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
26+
27+ option(LIBUV_BUILD_SHARED "Build shared lib" ON)
28+
2029cmake_dependent_option(LIBUV_BUILD_TESTS
2130 "Build the unit tests when BUILD_TESTING is enabled and we are the root project" ON
22- "BUILD_TESTING;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
31+ "BUILD_TESTING;LIBUV_BUILD_SHARED; CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF)
2332cmake_dependent_option(LIBUV_BUILD_BENCH
2433 "Build the benchmarks when building unit tests and we are the root project" ON
2534 "LIBUV_BUILD_TESTS" OFF)
2635
2736# Qemu Build
2837option(QEMU "build for qemu" OFF)
2938if(QEMU)
30- add_definitions(-D__QEMU__ =1)
39+ list(APPEND uv_defines __QEMU__ =1)
3140endif()
3241
42+ # Note: these are mutually exclusive.
3343option(ASAN "Enable AddressSanitizer (ASan)" OFF)
44+ option(MSAN "Enable MemorySanitizer (MSan)" OFF)
3445option(TSAN "Enable ThreadSanitizer (TSan)" OFF)
46+ option(UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF)
3547
36- if((ASAN OR TSAN) AND NOT ( CMAKE_C_COMPILER_ID MATCHES "AppleClang|GNU| Clang") )
37- message(SEND_ERROR "Sanitizer support requires clang or gcc . Try again with -DCMAKE_C_COMPILER. ")
48+ if(MSAN AND NOT CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang")
49+ message(SEND_ERROR "MemorySanitizer requires clang. Try again with -DCMAKE_C_COMPILER=clang ")
3850endif()
3951
4052if(ASAN)
41- add_definitions(-D__ASAN__=1)
42- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
43- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
44- set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
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()
63+ endif()
64+
65+ if(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")
4570endif()
4671
4772if(TSAN)
48- add_definitions(-D__TSAN__=1)
49- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
50- set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
51- set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
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()
81+ endif()
82+
83+ if(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()
5294endif()
5395
5496# Compiler check
@@ -126,6 +168,7 @@ set(uv_sources
126168 src/random.c
127169 src/strscpy.c
128170 src/strtok.c
171+ src/thread-common.c
129172 src/threadpool.c
130173 src/timer.c
131174 src/uv-common.c
@@ -140,7 +183,10 @@ if(WIN32)
140183 advapi32
141184 iphlpapi
142185 userenv
143- ws2_32)
186+ ws2_32
187+ dbghelp
188+ ole32
189+ uuid)
144190 list(APPEND uv_sources
145191 src/win/async.c
146192 src/win/core.c
@@ -216,15 +262,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Android")
216262 list(APPEND uv_defines _GNU_SOURCE)
217263 list(APPEND uv_libraries dl)
218264 list(APPEND uv_sources
219- src/unix/linux-core.c
220- src/unix/linux-inotify.c
221- src/unix/linux-syscalls.c
265+ src/unix/linux.c
222266 src/unix/procfs-exepath.c
223- src/unix/pthread-fixes.c
224267 src/unix/random-getentropy.c
225268 src/unix/random-getrandom.c
226- src/unix/random-sysctl-linux.c
227- src/unix/epoll.c)
269+ src/unix/random-sysctl-linux.c)
228270endif()
229271
230272if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "Android|Linux")
@@ -270,22 +312,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "GNU")
270312 src/unix/hurd.c)
271313endif()
272314
273- if(CMAKE_SYSTEM_NAME STREQUAL "kFreeBSD")
274- list(APPEND uv_defines _GNU_SOURCE)
275- list(APPEND uv_libraries dl freebsd-glue)
276- endif()
277-
278315if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
279316 list(APPEND uv_defines _GNU_SOURCE _POSIX_C_SOURCE=200112)
280317 list(APPEND uv_libraries dl rt)
281318 list(APPEND uv_sources
282- src/unix/linux-core.c
283- src/unix/linux-inotify.c
284- src/unix/linux-syscalls.c
319+ src/unix/linux.c
285320 src/unix/procfs-exepath.c
286321 src/unix/random-getrandom.c
287- src/unix/random-sysctl-linux.c
288- src/unix/epoll.c)
322+ src/unix/random-sysctl-linux.c)
289323endif()
290324
291325if(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
@@ -316,7 +350,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
316350 list(APPEND uv_defines _XOPEN_SOURCE=600)
317351 list(APPEND uv_defines _XOPEN_SOURCE_EXTENDED)
318352 list(APPEND uv_sources
319- src/unix/pthread-fixes.c
320353 src/unix/os390.c
321354 src/unix/os390-syscalls.c
322355 src/unix/os390-proctitle.c)
@@ -354,6 +387,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "OS400")
354387endif()
355388
356389if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
390+ if(CMAKE_SYSTEM_VERSION STREQUAL "5.10")
391+ list(APPEND uv_defines SUNOS_NO_IFADDRS)
392+ list(APPEND uv_libraries rt)
393+ endif()
357394 list(APPEND uv_defines __EXTENSIONS__ _XOPEN_SOURCE=500 _REENTRANT)
358395 list(APPEND uv_libraries kstat nsl sendfile socket)
359396 list(APPEND uv_sources
@@ -388,25 +425,42 @@ if(APPLE OR CMAKE_SYSTEM_NAME MATCHES "DragonFly|FreeBSD|Linux|NetBSD|OpenBSD")
388425 list(APPEND uv_test_libraries util)
389426endif()
390427
391- add_library(uv SHARED ${uv_sources})
392- target_compile_definitions(uv
393- INTERFACE
394- USING_UV_SHARED=1
395- PRIVATE
396- BUILDING_UV_SHARED=1
397- ${uv_defines})
398- target_compile_options(uv PRIVATE ${uv_cflags})
399- target_include_directories(uv
400- PUBLIC
401- $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
402- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
403- PRIVATE
404- $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
405- if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
406- target_include_directories(uv PUBLIC $<BUILD_INTERFACE:${ZOSLIB_DIR}/include>)
407- set_target_properties(uv PROPERTIES LINKER_LANGUAGE CXX)
428+ if(CYGWIN OR MSYS)
429+ list(APPEND uv_defines _GNU_SOURCE)
430+ list(APPEND uv_sources
431+ src/unix/cygwin.c
432+ src/unix/bsd-ifaddrs.c
433+ src/unix/no-fsevents.c
434+ src/unix/no-proctitle.c
435+ src/unix/posix-hrtime.c
436+ src/unix/posix-poll.c
437+ src/unix/procfs-exepath.c
438+ src/unix/sysinfo-loadavg.c
439+ src/unix/sysinfo-memory.c)
440+ endif()
441+
442+ if(LIBUV_BUILD_SHARED)
443+ add_library(uv SHARED ${uv_sources})
444+ target_compile_definitions(uv
445+ INTERFACE
446+ USING_UV_SHARED=1
447+ PRIVATE
448+ BUILDING_UV_SHARED=1
449+ ${uv_defines})
450+ target_compile_options(uv PRIVATE ${uv_cflags})
451+ target_include_directories(uv
452+ PUBLIC
453+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
454+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
455+ PRIVATE
456+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
457+ if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
458+ target_include_directories(uv PUBLIC $<BUILD_INTERFACE:${ZOSLIB_DIR}/include>)
459+ set_target_properties(uv PROPERTIES LINKER_LANGUAGE CXX)
460+ endif()
461+ target_link_libraries(uv ${uv_libraries})
462+ set_target_properties(uv PROPERTIES OUTPUT_NAME "uv")
408463endif()
409- target_link_libraries(uv ${uv_libraries})
410464
411465add_library(uv_a STATIC ${uv_sources})
412466target_compile_definitions(uv_a PRIVATE ${uv_defines})
@@ -422,6 +476,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "OS390")
422476 set_target_properties(uv_a PROPERTIES LINKER_LANGUAGE CXX)
423477endif()
424478target_link_libraries(uv_a ${uv_libraries})
479+ set_target_properties(uv_a PROPERTIES OUTPUT_NAME "uv")
480+ if(MSVC)
481+ set_target_properties(uv_a PROPERTIES PREFIX "lib")
482+ endif()
425483
426484if(LIBUV_BUILD_TESTS)
427485 # Small hack: use ${uv_test_sources} now to get the runner skeleton,
@@ -584,6 +642,7 @@ if(LIBUV_BUILD_TESTS)
584642 test/test-tcp-rst.c
585643 test/test-tcp-shutdown-after-write.c
586644 test/test-tcp-try-write.c
645+ test/test-tcp-write-in-a-row.c
587646 test/test-tcp-try-write-error.c
588647 test/test-tcp-unexpected-read.c
589648 test/test-tcp-write-after-connect.c
@@ -592,6 +651,7 @@ if(LIBUV_BUILD_TESTS)
592651 test/test-tcp-write-to-half-open-connection.c
593652 test/test-tcp-writealot.c
594653 test/test-test-macros.c
654+ test/test-thread-affinity.c
595655 test/test-thread-equal.c
596656 test/test-thread.c
597657 test/test-threadpool-cancel.c
@@ -624,6 +684,7 @@ if(LIBUV_BUILD_TESTS)
624684 test/test-udp-sendmmsg-error.c
625685 test/test-udp-send-unreachable.c
626686 test/test-udp-try-send.c
687+ test/test-udp-recv-in-a-row.c
627688 test/test-uname.c
628689 test/test-walk-handles.c
629690 test/test-watcher-cross-stop.c)
@@ -667,27 +728,36 @@ string(REPLACE ";" " " LIBS "${LIBS}")
667728file(STRINGS configure.ac configure_ac REGEX ^AC_INIT)
668729string(REGEX MATCH "([0-9]+)[.][0-9]+[.][0-9]+" PACKAGE_VERSION "${configure_ac}")
669730set(UV_VERSION_MAJOR "${CMAKE_MATCH_1}")
670- # The version in the filename is mirroring the behaviour of autotools.
671- set_target_properties(uv PROPERTIES
672- VERSION ${UV_VERSION_MAJOR}.0.0
673- SOVERSION ${UV_VERSION_MAJOR})
731+
674732set(includedir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
675733set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
676734set(prefix ${CMAKE_INSTALL_PREFIX})
677- configure_file(libuv.pc.in libuv.pc @ONLY)
678735configure_file(libuv-static.pc.in libuv-static.pc @ONLY)
679736
680737install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
681738install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
682- install(FILES ${PROJECT_BINARY_DIR}/libuv.pc ${PROJECT_BINARY_DIR}/libuv-static.pc
739+ install(FILES LICENSE-extra DESTINATION ${CMAKE_INSTALL_DOCDIR})
740+ install(FILES ${PROJECT_BINARY_DIR}/libuv-static.pc
683741 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
684- install(TARGETS uv EXPORT libuvConfig
685- RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
686- LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
687- ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
688742install(TARGETS uv_a EXPORT libuvConfig
689743 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
690- install(EXPORT libuvConfig DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libuv)
744+ install(EXPORT libuvConfig
745+ DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/libuv
746+ NAMESPACE libuv::)
747+
748+ if(LIBUV_BUILD_SHARED)
749+ # The version in the filename is mirroring the behaviour of autotools.
750+ set_target_properties(uv PROPERTIES
751+ VERSION ${UV_VERSION_MAJOR}.0.0
752+ SOVERSION ${UV_VERSION_MAJOR})
753+ configure_file(libuv.pc.in libuv.pc @ONLY)
754+ install(FILES ${PROJECT_BINARY_DIR}/libuv.pc
755+ DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
756+ install(TARGETS uv EXPORT libuvConfig
757+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
758+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
759+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
760+ endif()
691761
692762if(MSVC)
693763 set(CMAKE_DEBUG_POSTFIX d)
0 commit comments