FindLibUV: Add module to find libuv package

Add it to a private source directory that is not installed so that we
can use it for building CMake itself.  This will allow it to mature
before being distributed publicly.
This commit is contained in:
Brad King 2016-08-16 16:56:34 -04:00
parent 551d5aedbf
commit e56aa46297
5 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,131 @@
#[=======================================================================[.rst:
FindLibUV
---------
Find libuv includes and library.
Imported Targets
^^^^^^^^^^^^^^^^
An :ref:`imported target <Imported targets>` named
``LibUV::LibUV`` is provided if libuv has been found.
Result Variables
^^^^^^^^^^^^^^^^
This module defines the following variables:
``LibUV_FOUND``
True if libuv was found, false otherwise.
``LibUV_INCLUDE_DIRS``
Include directories needed to include libuv headers.
``LibUV_LIBRARIES``
Libraries needed to link to libuv.
``LibUV_VERSION``
The version of libuv found.
``LibUV_VERSION_MAJOR``
The major version of libuv.
``LibUV_VERSION_MINOR``
The minor version of libuv.
``LibUV_VERSION_PATCH``
The patch version of libuv.
Cache Variables
^^^^^^^^^^^^^^^
This module uses the following cache variables:
``LibUV_LIBRARY``
The location of the libuv library file.
``LibUV_INCLUDE_DIR``
The location of the libuv include directory containing ``uv.h``.
The cache variables should not be used by project code.
They may be set by end users to point at libuv components.
#]=======================================================================]
#=============================================================================
# Copyright 2014-2016 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
#-----------------------------------------------------------------------------
find_library(LibUV_LIBRARY
NAMES uv
)
mark_as_advanced(LibUV_LIBRARY)
find_path(LibUV_INCLUDE_DIR
NAMES uv.h
)
mark_as_advanced(LibUV_INCLUDE_DIR)
#-----------------------------------------------------------------------------
# Extract version number if possible.
set(_LibUV_H_REGEX "#[ \t]*define[ \t]+UV_VERSION_(MAJOR|MINOR|PATCH)[ \t]+[0-9]+")
if(LibUV_INCLUDE_DIR AND EXISTS "${LibUV_INCLUDE_DIR}/uv-version.h")
file(STRINGS "${LibUV_INCLUDE_DIR}/uv-version.h" _LibUV_H REGEX "${_LibUV_H_REGEX}")
elseif(LibUV_INCLUDE_DIR AND EXISTS "${LibUV_INCLUDE_DIR}/uv.h")
file(STRINGS "${LibUV_INCLUDE_DIR}/uv.h" _LibUV_H REGEX "${_LibUV_H_REGEX}")
else()
set(_LibUV_H "")
endif()
foreach(c MAJOR MINOR PATCH)
if(_LibUV_H MATCHES "#[ \t]*define[ \t]+UV_VERSION_${c}[ \t]+([0-9]+)")
set(_LibUV_VERSION_${c} "${CMAKE_MATCH_1}")
else()
unset(_LibUV_VERSION_${c})
endif()
endforeach()
if(DEFINED _LibUV_VERSION_MAJOR AND DEFINED _LibUV_VERSION_MINOR)
set(LibUV_VERSION_MAJOR "${_LibUV_VERSION_MAJOR}")
set(LibUV_VERSION_MINOR "${_LibUV_VERSION_MINOR}")
set(LibUV_VERSION "${LibUV_VERSION_MAJOR}.${LibUV_VERSION_MINOR}")
if(DEFINED _LibUV_VERSION_PATCH)
set(LibUV_VERSION_PATCH "${_LibUV_VERSION_PATCH}")
set(LibUV_VERSION "${LibUV_VERSION}.${LibUV_VERSION_PATCH}")
else()
unset(LibUV_VERSION_PATCH)
endif()
else()
set(LibUV_VERSION_MAJOR "")
set(LibUV_VERSION_MINOR "")
set(LibUV_VERSION_PATCH "")
set(LibUV_VERSION "")
endif()
unset(_LibUV_VERSION_MAJOR)
unset(_LibUV_VERSION_MINOR)
unset(_LibUV_VERSION_PATCH)
unset(_LibUV_H_REGEX)
unset(_LibUV_H)
#-----------------------------------------------------------------------------
include(${CMAKE_CURRENT_LIST_DIR}/../../Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibUV
FOUND_VAR LibUV_FOUND
REQUIRED_VARS LibUV_LIBRARY LibUV_INCLUDE_DIR
VERSION_VAR LibUV_VERSION
)
set(LIBUV_FOUND ${LibUV_FOUND})
#-----------------------------------------------------------------------------
# Provide documented result variables and targets.
if(LibUV_FOUND)
set(LibUV_INCLUDE_DIRS ${LibUV_INCLUDE_DIR})
set(LibUV_LIBRARIES ${LibUV_LIBRARY})
if(NOT TARGET LibUV::LibUV)
add_library(LibUV::LibUV UNKNOWN IMPORTED)
set_target_properties(LibUV::LibUV PROPERTIES
IMPORTED_LOCATION "${LibUV_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibUV_INCLUDE_DIRS}"
)
endif()
endif()

View File

@ -1375,6 +1375,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release
add_subdirectory(FindJsonCpp) add_subdirectory(FindJsonCpp)
endif() endif()
if(CMake_TEST_FindLibUV)
add_subdirectory(FindLibUV)
endif()
if(CMake_TEST_FindLTTngUST) if(CMake_TEST_FindLTTngUST)
add_subdirectory(FindLTTngUST) add_subdirectory(FindLTTngUST)
endif() endif()

View File

@ -0,0 +1,10 @@
add_test(NAME FindLibUV.Test COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindLibUV/Test"
"${CMake_BINARY_DIR}/Tests/FindLibUV/Test"
${build_generator_args}
--build-project TestFindLibUV
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)

View File

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.6)
project(TestFindLibUV C)
include(CTest)
# CMake does not actually provide FindLibUV publicly.
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../Source/Modules)
find_package(LibUV REQUIRED)
add_executable(test_libuv_tgt main.c)
target_link_libraries(test_libuv_tgt LibUV::LibUV)
add_test(NAME test_libuv_tgt COMMAND test_libuv_tgt)
add_executable(test_libuv_var main.c)
target_include_directories(test_libuv_var PRIVATE ${LibUV_INCLUDE_DIRS})
target_link_libraries(test_libuv_var PRIVATE ${LibUV_LIBRARIES})
add_test(NAME test_libuv_var COMMAND test_libuv_var)

View File

@ -0,0 +1,7 @@
#include <uv.h>
int main()
{
uv_loop_close(uv_default_loop());
return 0;
}