Skip to content

Commit

Permalink
cmake: use UVW_ to avoid polluting parent projects
Browse files Browse the repository at this point in the history
  • Loading branch information
skypjack committed Oct 18, 2024
1 parent 4d54eb9 commit fb2f8d9
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 105 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:

strategy:
matrix:
mode: [-DBUILD_UVW_SHARED_LIB=ON, -DBUILD_UVW_LIBS=ON, -DBUILD_UVW_LIBS=OFF]
mode: [-DUVW_BUILD_SHARED_LIB=ON, -DUVW_BUILD_LIBS=ON, -DUVW_BUILD_LIBS=OFF]

steps:
- uses: actions/checkout@v3
- name: Compile tests
working-directory: build
run: |
cmake ${{ matrix.mode }} -DBUILD_TESTING=ON -Dlibuv_buildtests=OFF ..
cmake ${{ matrix.mode }} -DUVW_BUILD_TESTING=ON -Dlibuv_buildtests=OFF ..
make -j2
2 changes: 1 addition & 1 deletion .github/workflows/build-ubuntu-20.04.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- { pkg: clang, exe: 'clang++', version: 9 }
- { pkg: clang, exe: 'clang++', version: 10 }
- { pkg: clang, exe: 'clang++', version: 11 }
mode: [-DBUILD_UVW_SHARED_LIB=ON, -DBUILD_UVW_LIBS=ON, -DBUILD_UVW_LIBS=OFF]
mode: [-DUVW_BUILD_SHARED_LIB=ON, -DUVW_BUILD_LIBS=ON, -DUVW_BUILD_LIBS=OFF]

runs-on: ubuntu-20.04

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-ubuntu-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- { pkg: clang, exe: 'clang++', version: 12 }
- { pkg: clang, exe: 'clang++', version: 13 }
- { pkg: clang, exe: 'clang++', version: 14 }
mode: [-DBUILD_UVW_SHARED_LIB=ON, -DBUILD_UVW_LIBS=ON, -DBUILD_UVW_LIBS=OFF]
mode: [-DUVW_BUILD_SHARED_LIB=ON, -DUVW_BUILD_LIBS=ON, -DUVW_BUILD_LIBS=OFF]

runs-on: ubuntu-latest

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
strategy:
matrix:
generator: [Visual Studio 17 2022]
mode: [-DBUILD_UVW_SHARED_LIB=ON, -DBUILD_UVW_LIBS=ON, -DBUILD_UVW_LIBS=OFF]
mode: [-DUVW_BUILD_SHARED_LIB=ON, -DUVW_BUILD_LIBS=ON, -DUVW_BUILD_LIBS=OFF]

steps:
- uses: actions/checkout@v3
- name: Compile tests
working-directory: build
run: |
cmake -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE ${{ matrix.mode }} -DBUILD_TESTING=ON -Dlibuv_buildtests=OFF -DCMAKE_CXX_FLAGS=/W1 -G"${{ matrix.generator }}" ..
cmake -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE ${{ matrix.mode }} -DUVW_BUILD_TESTING=ON -Dlibuv_buildtests=OFF -DCMAKE_CXX_FLAGS=/W1 -G"${{ matrix.generator }}" ..
cmake --build . -j 2
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
CXXFLAGS: "-O0 --coverage -fno-inline -fno-inline-small-functions -fno-default-inline"
CXX: g++
run: |
cmake -DBUILD_TESTING=ON -Dlibuv_buildtests=OFF ..
cmake -DUVW_BUILD_TESTING=ON -Dlibuv_buildtests=OFF ..
make -j4
- name: Run tests
working-directory: build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sanitizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
env:
CXX: ${{ matrix.compiler }}
run: |
cmake ${{ matrix.mode }} -DBUILD_TESTING=ON -Dlibuv_buildtests=OFF -DUSE_${{ matrix.sanitizer }}=ON ..
cmake ${{ matrix.mode }} -DUVW_BUILD_TESTING=ON -DUVW_USE_${{ matrix.sanitizer }}=ON -Dlibuv_buildtests=OFF ..
make -j2
- name: Run tests
working-directory: build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
working-directory: build
run: |
export PATH=$PATH:${GITHUB_WORKSPACE}/build/include-what-you-use/build/bin
cmake -DBUILD_TESTING=ON \
cmake -DUVW_BUILD_TESTING=ON \
-Dlibuv_buildtests=OFF \
-DCMAKE_C_COMPILER=clang-$LLVM \
-DCMAKE_CXX_COMPILER=clang++-$LLVM \
Expand All @@ -69,7 +69,7 @@ jobs:
env:
CXX: clang++
run: |
cmake -DBUILD_TESTING=ON -Dlibuv_buildtests=OFF -DUSE_CLANG_TIDY=ON ..
cmake -DUVW_BUILD_TESTING=ON -DUVW_USE_CLANG_TIDY=ON -Dlibuv_buildtests=OFF ..
make -j4
- name: Run tests
working-directory: build
Expand Down
94 changes: 48 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

option(USE_LIBCPP "Use libc++ by adding -stdlib=libc++ flag if available." ON)
option(USE_ASAN "Use address sanitizer by adding -fsanitize=address -fno-omit-frame-pointer flags" OFF)
option(USE_UBSAN "Use address sanitizer by adding -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer flags" OFF)
option(USE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF)
option(BUILD_UVW_LIBS "Prepare targets for static library rather than for a header-only library." OFF)
option(BUILD_UVW_SHARED_LIB "Prepare targets for shared library rather than for a header-only library." OFF)
option(FIND_LIBUV "Try finding libuv library development files in the system" OFF)

if(BUILD_UVW_SHARED_LIB)
set(BUILD_UVW_LIBS BOOL:ON)
option(UVW_USE_LIBCPP "Use libc++ by adding -stdlib=libc++ flag if available." ON)
option(UVW_USE_ASAN "Use address sanitizer by adding -fsanitize=address -fno-omit-frame-pointer flags" OFF)
option(UVW_USE_UBSAN "Use address sanitizer by adding -fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer flags" OFF)
option(UVW_USE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF)
option(UVW_BUILD_LIBS "Prepare targets for static library rather than for a header-only library." OFF)
option(UVW_BUILD_SHARED_LIB "Prepare targets for shared library rather than for a header-only library." OFF)
option(UVW_FIND_LIBUV "Try finding libuv library development files in the system" OFF)

if(UVW_BUILD_SHARED_LIB)
set(UVW_BUILD_LIBS BOOL:ON)
endif()

#
# Compiler stuff
#

if(NOT WIN32 AND USE_LIBCPP)
if(NOT WIN32 AND UVW_USE_LIBCPP)
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)

Expand All @@ -58,34 +58,34 @@ if(NOT WIN32 AND USE_LIBCPP)
check_cxx_source_compiles("
#include<type_traits>
int main() { return std::is_same_v<int, char>; }
" HAS_LIBCPP)
" UVW_HAS_LIBCPP)

if(NOT HAS_LIBCPP)
message(WARNING "The option USE_LIBCPP is set (by default) but libc++ is not available. The flag will not be added to the target.")
if(NOT UVW_HAS_LIBCPP)
message(WARNING "The option UVW_USE_LIBCPP is set (by default) but libc++ is not available. The flag will not be added to the target.")
endif()

cmake_pop_check_state()
endif()

if(USE_CLANG_TIDY)
find_program(CLANG_TIDY_EXECUTABLE "clang-tidy")
if(UVW_USE_CLANG_TIDY)
find_program(UVW_CLANG_TIDY_EXECUTABLE "clang-tidy")

if(NOT CLANG_TIDY_EXECUTABLE)
message(VERBOSE "The option USE_CLANG_TIDY is set but clang-tidy executable is not available.")
if(NOT UVW_CLANG_TIDY_EXECUTABLE)
message(VERBOSE "The option UVW_USE_CLANG_TIDY is set but clang-tidy executable is not available.")
endif()
endif()

# Required minimal libuv version
set(LIBUV_VERSION 1.49.0)
set(UVW_LIBUV_VERSION 1.49.0)

function(fetch_libuv)
if (FETCH_LIBUV)
if (UVW_FETCH_LIBUV)
include(FetchContent)

FetchContent_Declare(
libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG "v${LIBUV_VERSION}"
GIT_TAG "v${UVW_LIBUV_VERSION}"
GIT_SHALLOW 1
)

Expand All @@ -96,38 +96,40 @@ function(fetch_libuv)
add_subdirectory(${libuv_SOURCE_DIR} ${libuv_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

if(BUILD_UVW_SHARED_LIB)
if(UVW_BUILD_SHARED_LIB)
add_library(uv::uv-shared ALIAS uv)
set_target_properties(uv PROPERTIES POSITION_INDEPENDENT_CODE 1)
else()
add_library(uv::uv-static ALIAS uv_a)
set_target_properties(uv_a PROPERTIES POSITION_INDEPENDENT_CODE 1)
endif()
endif(FETCH_LIBUV)
endif(UVW_FETCH_LIBUV)
endfunction()

function(use_libuv)
set(FETCH_LIBUV_DEFAULT ON)
if (FIND_LIBUV)
set(UVW_FETCH_LIBUV_DEFAULT ON)

if (UVW_FIND_LIBUV)
find_package(libuv ${LIBUV_VERSION} QUIET)
if (libuv_FOUND)
add_library(uv::uv-shared ALIAS uv)
set(FETCH_LIBUV_DEFAULT OFF)
set(UVW_FETCH_LIBUV_DEFAULT OFF)
message(STATUS "libuv ${libuv_VERSION} found via cmake")
else(libuv_FOUND)
find_package(PkgConfig QUIET)
if (PkgConfig_FOUND)
pkg_check_modules(libuv IMPORTED_TARGET libuv>=${LIBUV_VERSION})
if (libuv_FOUND)
add_library(uv::uv-shared ALIAS PkgConfig::libuv)
set(FETCH_LIBUV_DEFAULT OFF)
set(UVW_FETCH_LIBUV_DEFAULT OFF)
message(STATUS "libuv ${libuv_VERSION} found via pkg-config")
endif(libuv_FOUND)
endif(PkgConfig_FOUND)
endif(libuv_FOUND)
endif(FIND_LIBUV)
endif(UVW_FIND_LIBUV)

option(UVW_FETCH_LIBUV "Fetch the libuv repo using CMake FetchContent facility" ${UVW_FETCH_LIBUV_DEFAULT})

option(FETCH_LIBUV "Fetch the libuv repo using CMake FetchContent facility" ${FETCH_LIBUV_DEFAULT})
fetch_libuv()
endfunction()

Expand All @@ -137,7 +139,7 @@ endfunction()

include(GNUInstallDirs)

if(BUILD_UVW_LIBS)
if(UVW_BUILD_LIBS)
use_libuv()

add_subdirectory(src)
Expand All @@ -155,25 +157,25 @@ else()
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

if(USE_ASAN)
if(UVW_USE_ASAN)
target_compile_options(uvw INTERFACE $<$<CONFIG:Debug>:-fsanitize=address -fno-omit-frame-pointer>)
target_link_libraries(uvw INTERFACE $<$<CONFIG:Debug>:-fsanitize=address>)
endif()

if(USE_UBSAN)
if(UVW_USE_UBSAN)
target_compile_options(uvw INTERFACE $<$<CONFIG:Debug>:-fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer>)
target_link_libraries(uvw INTERFACE $<$<CONFIG:Debug>:-fsanitize=undefined>)
endif()

if(CLANG_TIDY_EXECUTABLE)
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXECUTABLE};--config-file=${uvw_SOURCE_DIR}/.clang-tidy;--header-filter=${uvw_SOURCE_DIR}/src/uvw/.*")
if(UVW_CLANG_TIDY_EXECUTABLE)
set(CMAKE_CXX_CLANG_TIDY "${UVW_CLANG_TIDY_EXECUTABLE};--config-file=${uvw_SOURCE_DIR}/.clang-tidy;--header-filter=${uvw_SOURCE_DIR}/src/uvw/.*")
endif()

if(HAS_LIBCPP)
if(UVW_HAS_LIBCPP)
target_compile_options(uvw BEFORE INTERFACE -stdlib=libc++)
endif()

file(GLOB HEADERS src/uvw/*.h src/uvw/*.hpp src/uvw/*.cpp)
file(GLOB HEADERS src/uvw/*.h src/uvw/*.hpp)
endif()

#
Expand All @@ -198,7 +200,7 @@ install(
# Install targets
#

if (BUILD_UVW_LIBS)
if (UVW_BUILD_LIBS)
set_target_properties(
uvw PROPERTIES
VERSION ${UVW_VERSION_MAJOR}.${UVW_VERSION_MINOR}.${UVW_VERSION_PATCH}
Expand All @@ -220,26 +222,26 @@ install(
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

if(FETCH_LIBUV AND BUILD_UVW_LIBS)
if(UVW_FETCH_LIBUV AND UVW_BUILD_LIBS)
# libuv is only fetched when both above conditions are true
install(DIRECTORY ${libuv_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/uvw/uv/include)
if (BUILD_UVW_SHARED_LIB)
if (UVW_BUILD_SHARED_LIB)
install(TARGETS uv EXPORT uvwConfig LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/uvw)
else()
install(TARGETS uv_a EXPORT uvwConfig ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/uvw)
endif()
endif(FETCH_LIBUV AND BUILD_UVW_LIBS)
endif(UVW_FETCH_LIBUV AND UVW_BUILD_LIBS)

export(EXPORT uvwConfig)

### Testing

option(BUILD_TESTING "Enable testing with ctest." OFF)
option(UVW_BUILD_TESTING "Enable testing with ctest." OFF)

if(BUILD_TESTING)
option(FIND_GTEST_PACKAGE "Enable finding gtest package." OFF)
if(UVW_BUILD_TESTING)
option(UVW_FIND_GTEST_PACKAGE "Enable finding gtest package." OFF)

if (NOT BUILD_UVW_LIBS)
if (NOT UVW_BUILD_LIBS)
use_libuv()
endif()

Expand All @@ -251,9 +253,9 @@ endif()
# Documentation
#

option(BUILD_DOCS "Enable building with documentation." OFF)
option(UVW_BUILD_DOCS "Enable building with documentation." OFF)

if(BUILD_DOCS)
if(UVW_BUILD_DOCS)
find_package(Doxygen 1.10)

if(DOXYGEN_FOUND)
Expand Down
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"description": "Common (non-OS specific) mode for development",
"inherits": "cmake-pedantic",
"cacheVariables": {
"BUILD_TESTING": true,
"UVW_BUILD_TESTING": true,
"libuv_buildtests": false
}
},
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ When used through `CMake`, the `uvw::uvw` target is exported for convenience.

### Static

To use `uvw` as a compiled library, set the `BUILD_UVW_LIBS` options in cmake
To use `uvw` as a compiled library, set the `UVW_BUILD_LIBS` options in cmake
before including the project.<br/>
This option triggers the generation of a targets named
`uvw::uvw-static`. The matching version of `libuv` is also
Expand Down Expand Up @@ -209,7 +209,7 @@ else.
To build the tests:

* `$ cd build`
* `$ cmake .. -DBUILD_TESTING=ON`
* `$ cmake .. -DUVW_BUILD_TESTING=ON`
* `$ make`
* `$ ctest -j4 -R uvw`

Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ function(add_uvw_library LIB_NAME)
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

if(USE_ASAN)
if(UVW_USE_ASAN)
target_compile_options(${LIB_NAME} PUBLIC $<$<CONFIG:Debug>:-fsanitize=address -fno-omit-frame-pointer>)
target_link_libraries(${LIB_NAME} PUBLIC $<$<CONFIG:Debug>:-fsanitize=address>)
endif()

if(USE_UBSAN)
if(UVW_USE_UBSAN)
target_compile_options(${LIB_NAME} PUBLIC $<$<CONFIG:Debug>:-fsanitize=undefined -fno-sanitize-recover=all -fno-omit-frame-pointer>)
target_link_libraries(${LIB_NAME} PUBLIC $<$<CONFIG:Debug>:-fsanitize=undefined>)
endif()

if(HAS_LIBCPP)
if(UVW_HAS_LIBCPP)
target_compile_options(${LIB_NAME} BEFORE PUBLIC -stdlib=libc++)
endif()
endfunction()
Expand All @@ -61,7 +61,7 @@ endfunction()
# Build and install libraries
#

if (BUILD_UVW_SHARED_LIB)
if (UVW_BUILD_SHARED_LIB)
add_library(uvw SHARED)
add_library(uvw::uvw-shared ALIAS uvw)
# If libuv is not fetched by ourselves, it's the caller's responsibility to make sure of the linkage.
Expand Down
Loading

0 comments on commit fb2f8d9

Please sign in to comment.