From 10cb4b437f6081233881f1bed1c3cdf450373317 Mon Sep 17 00:00:00 2001 From: Stephen Sorley Date: Tue, 3 Jan 2017 12:21:54 -0500 Subject: [PATCH 1/3] Updated CMake minimum version to 2.8.12. Changed the way usage requirements (include dirs, compile defs, dependent libraries) are specified, to match the recommended standard practice for modern CMake. This involves using target-specific functions (target_include_directories, target_compile_definitions, etc.), along with the PUBLIC, PRIVATE or INTERFACE modifiers. Updated chain-building support to imitate new-style Find modules (import libs), instead of old-style Find modules (cache variables). --- CMakeLists.txt | 91 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 698ad7b3..f926be41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,11 @@ -CMAKE_MINIMUM_REQUIRED (VERSION 2.8) +CMAKE_MINIMUM_REQUIRED (VERSION 2.8.12) + +# Fix for CMake policy warning when building with CMake >= 3.0. +# TODO: remove this fix once minimum version is updated to 3.0 or newer. +IF (NOT CMAKE_VERSION VERSION_LESS 3.0) + CMAKE_POLICY (SET CMP0048 NEW) +ENDIF () + INCLUDE (CheckIncludeFiles) INCLUDE (CheckTypeSize) INCLUDE (CheckFunctionExists) @@ -48,17 +55,6 @@ IF (NOT DEFINED CARES_INSTALL_LOCATION_BIN) ENDIF () -# When chain building C-Ares, we should set some of the same variables that a -# standard Find_Package(CAres) would set. It just references the internal -# build paths rather than an already-installed version. -SET (CARES_FOUND 1 CACHE INTERNAL "CARES LIBRARY FOUND") -SET (CARES_INCLUDE_DIRS "${PROJECT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "CARES INCLUDE DIRECTORIES") -SET (CARES_LIBRARIES ${PROJECT_NAME} CACHE INTERNAL "CARES LIBRARIES") -IF (CARES_STATIC AND NOT CARES_SHARED) - SET (CARES_DEFINITIONS "-DCARES_STATICLIB" CACHE INTERNAL "CARES DEFINITIONS") -ENDIF () - - # Look for dependent/required libraries CHECK_LIBRARY_EXISTS (resolv res_servicename "" HAVE_RES_SERVICENAME_IN_LIBRESOLV) IF (HAVE_RES_SERVICENAME_IN_LIBRESOLV) @@ -493,11 +489,6 @@ CONFIGURE_FILE (ares_build.h.cmake ${PROJECT_BINARY_DIR}/ares_build.h) CONFIGURE_FILE (ares_config.h.cmake ${PROJECT_BINARY_DIR}/ares_config.h) -INCLUDE_DIRECTORIES ( - ${CARES_INCLUDE_DIRS} -) - - # TRANSFORM_MAKEFILE_INC # # This function consumes the "Makefile.inc" autotools file, and converts it into @@ -538,7 +529,6 @@ include(${PROJECT_BINARY_DIR}/Makefile.inc.cmake) # Build the dynamic/shared library IF (CARES_SHARED) ADD_LIBRARY (${PROJECT_NAME} SHARED ${CSOURCES}) - SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DHAVE_CONFIG_H=1 -DCARES_BUILDING_LIBRARY" OUTPUT_NAME cares) # Convert CARES_LIB_VERSIONINFO libtool version format into VERSION and SOVERSION # Convert from ":" separated into CMake list format using ";" @@ -549,26 +539,57 @@ IF (CARES_SHARED) MATH (EXPR CARES_LIB_VERSION_MAJOR "${CARES_LIB_VERSION_CURRENT} - ${CARES_LIB_VERSION_AGE}") SET (CARES_LIB_VERSION_MINOR "${CARES_LIB_VERSION_AGE}") SET (CARES_LIB_VERSION_RELEASE "${CARES_LIB_VERSION_REVISION}") - SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES VERSION "${CARES_LIB_VERSION_MAJOR}.${CARES_LIB_VERSION_MINOR}.${CARES_LIB_VERSION_RELEASE}") - SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES SOVERSION "${CARES_LIB_VERSION_MAJOR}") - TARGET_LINK_LIBRARIES (${PROJECT_NAME} ${CARES_DEPENDENT_LIBS}) + SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES + OUTPUT_NAME cares + VERSION "${CARES_LIB_VERSION_MAJOR}.${CARES_LIB_VERSION_MINOR}.${CARES_LIB_VERSION_RELEASE}" + SOVERSION "${CARES_LIB_VERSION_MAJOR}" + ) + + TARGET_INCLUDE_DIRECTORIES (${PROJECT_NAME} PUBLIC "${PROJECT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}") + + TARGET_COMPILE_DEFINITIONS (${PROJECT_NAME} PRIVATE HAVE_CONFIG_H=1 CARES_BUILDING_LIBRARY) + + TARGET_LINK_LIBRARIES (${PROJECT_NAME} PUBLIC ${CARES_DEPENDENT_LIBS}) + IF (CARES_INSTALL) INSTALL (TARGETS ${PROJECT_NAME} DESTINATION ${CARES_INSTALL_LOCATION_LIBS}) ENDIF () SET (STATIC_SUFFIX "_static") + + # For chain building: add alias targets that look like import libs that would be returned by find_package(Cares). + ADD_LIBRARY (Cares::cares_shared ALIAS ${PROJECT_NAME}) + ADD_LIBRARY (Cares::cares ALIAS ${PROJECT_NAME}) ENDIF () # Build the static library IF (CARES_STATIC) - ADD_LIBRARY (${PROJECT_NAME}${STATIC_SUFFIX} STATIC ${CSOURCES}) - SET_TARGET_PROPERTIES (${PROJECT_NAME}${STATIC_SUFFIX} PROPERTIES COMPILE_FLAGS "-DHAVE_CONFIG_H=1 -DCARES_STATICLIB" OUTPUT_NAME cares) + SET (LIBNAME ${PROJECT_NAME}${STATIC_SUFFIX}) + + ADD_LIBRARY (${LIBNAME} STATIC ${CSOURCES}) + + SET_TARGET_PROPERTIES (${LIBNAME} PROPERTIES OUTPUT_NAME cares) IF (CARES_STATIC_PIC) - SET_TARGET_PROPERTIES (${PROJECT_NAME}${STATIC_SUFFIX} PROPERTIES POSITION_INDEPENDENT_CODE True) + SET_TARGET_PROPERTIES (${LIBNAME} PROPERTIES POSITION_INDEPENDENT_CODE True) ENDIF () - TARGET_LINK_LIBRARIES (${PROJECT_NAME}${STATIC_SUFFIX} ${CARES_DEPENDENT_LIBS}) + + TARGET_INCLUDE_DIRECTORIES (${LIBNAME} PUBLIC "${PROJECT_BINARY_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}") + + TARGET_COMPILE_DEFINITIONS (${LIBNAME} + PUBLIC CARES_STATICLIB + PRIVATE HAVE_CONFIG_H=1 + ) + + TARGET_LINK_LIBRARIES (${LIBNAME} PUBLIC ${CARES_DEPENDENT_LIBS}) IF (CARES_INSTALL) - INSTALL (TARGETS ${PROJECT_NAME}${STATIC_SUFFIX} DESTINATION ${CARES_INSTALL_LOCATION_LIBS}) + INSTALL (TARGETS ${LIBNAME} DESTINATION ${CARES_INSTALL_LOCATION_LIBS}) + ENDIF () + + # For chain building: add alias targets that look like import libs that would be returned by find_package(Cares). + ADD_LIBRARY (Cares::cares_static ALIAS ${LIBNAME}) + IF (NOT TARGET Cares::cares) + # Only use static for the generic alias if shared lib wasn't built. + ADD_LIBRARY (Cares::cares ALIAS ${LIBNAME}) ENDIF () ENDIF () @@ -580,10 +601,16 @@ IF (CARES_INSTALL) ENDIF () +# Legacy chain-building variables (provided for compatibility with old code). +# Don't use these, external code should be updated to refer to the aliases directly (e.g., Cares::cares). +SET (CARES_FOUND 1 CACHE INTERNAL "CARES LIBRARY FOUND") +SET (CARES_LIBRARIES Cares::cares CACHE INTERNAL "CARES LIBRARIES") + + # Build ahost ADD_EXECUTABLE (ahost ahost.c ${SAMPLESOURCES}) -SET_TARGET_PROPERTIES(ahost PROPERTIES COMPILE_FLAGS "-DHAVE_CONFIG_H=1 ${CARES_DEFINITIONS}") -TARGET_LINK_LIBRARIES (ahost ${CARES_LIBRARIES} ${CARES_DEPENDENT_LIBS}) +TARGET_COMPILE_DEFINITIONS (ahost PRIVATE HAVE_CONFIG_H=1) +TARGET_LINK_LIBRARIES (ahost PRIVATE Cares::cares) IF (CARES_INSTALL) INSTALL (TARGETS ahost RUNTIME DESTINATION ${CARES_INSTALL_LOCATION_BIN}) ENDIF () @@ -591,8 +618,8 @@ ENDIF () # Build adig ADD_EXECUTABLE (adig adig.c ${SAMPLESOURCES}) -SET_TARGET_PROPERTIES(adig PROPERTIES COMPILE_FLAGS "-DHAVE_CONFIG_H=1 ${CARES_DEFINITIONS}") -TARGET_LINK_LIBRARIES (adig ${CARES_LIBRARIES} ${CARES_DEPENDENT_LIBS}) +TARGET_COMPILE_DEFINITIONS (adig PRIVATE HAVE_CONFIG_H=1) +TARGET_LINK_LIBRARIES (adig PRIVATE Cares::cares) IF (CARES_INSTALL) INSTALL (TARGETS adig RUNTIME DESTINATION ${CARES_INSTALL_LOCATION_BIN}) ENDIF () @@ -600,8 +627,8 @@ ENDIF () # Build acountry ADD_EXECUTABLE (acountry acountry.c ${SAMPLESOURCES}) -SET_TARGET_PROPERTIES(acountry PROPERTIES COMPILE_FLAGS "-DHAVE_CONFIG_H=1 ${CARES_DEFINITIONS}") -TARGET_LINK_LIBRARIES (acountry ${CARES_LIBRARIES} ${CARES_DEPENDENT_LIBS}) +TARGET_COMPILE_DEFINITIONS (acountry PRIVATE HAVE_CONFIG_H=1) +TARGET_LINK_LIBRARIES (acountry PRIVATE Cares::cares) IF (CARES_INSTALL) INSTALL (TARGETS acountry RUNTIME DESTINATION ${CARES_INSTALL_LOCATION_BIN}) ENDIF () From 49b79065edfee5298da6dec58523fd45f24709f7 Mon Sep 17 00:00:00 2001 From: Stephen Sorley Date: Tue, 3 Jan 2017 15:50:18 -0500 Subject: [PATCH 2/3] Updated Travis to pull CMake 2.8.12 from kubuntu-backports ppa. --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0fd70bfd..7e56154c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,10 @@ addons: apt: sources: - ubuntu-toolchain-r-test + - kubuntu-backports # For CMake 2.8.12 - llvm-toolchain-precise-3.7 packages: + - cmake - gcc-4.8 - g++-4.8 - lcov From 5f4cb4d093e9cd819e0794f6abca4cf8b013bfd1 Mon Sep 17 00:00:00 2001 From: Stephen Sorley Date: Wed, 4 Jan 2017 09:12:18 -0500 Subject: [PATCH 3/3] Changed executables to depend directly on internal libcares target, instead of against the external-facing alias targets. --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f926be41..69d56bc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -610,7 +610,7 @@ SET (CARES_LIBRARIES Cares::cares CACHE INTERNAL "CARES LIBRARIES") # Build ahost ADD_EXECUTABLE (ahost ahost.c ${SAMPLESOURCES}) TARGET_COMPILE_DEFINITIONS (ahost PRIVATE HAVE_CONFIG_H=1) -TARGET_LINK_LIBRARIES (ahost PRIVATE Cares::cares) +TARGET_LINK_LIBRARIES (ahost PRIVATE ${PROJECT_NAME}) IF (CARES_INSTALL) INSTALL (TARGETS ahost RUNTIME DESTINATION ${CARES_INSTALL_LOCATION_BIN}) ENDIF () @@ -619,7 +619,7 @@ ENDIF () # Build adig ADD_EXECUTABLE (adig adig.c ${SAMPLESOURCES}) TARGET_COMPILE_DEFINITIONS (adig PRIVATE HAVE_CONFIG_H=1) -TARGET_LINK_LIBRARIES (adig PRIVATE Cares::cares) +TARGET_LINK_LIBRARIES (adig PRIVATE ${PROJECT_NAME}) IF (CARES_INSTALL) INSTALL (TARGETS adig RUNTIME DESTINATION ${CARES_INSTALL_LOCATION_BIN}) ENDIF () @@ -628,7 +628,7 @@ ENDIF () # Build acountry ADD_EXECUTABLE (acountry acountry.c ${SAMPLESOURCES}) TARGET_COMPILE_DEFINITIONS (acountry PRIVATE HAVE_CONFIG_H=1) -TARGET_LINK_LIBRARIES (acountry PRIVATE Cares::cares) +TARGET_LINK_LIBRARIES (acountry PRIVATE ${PROJECT_NAME}) IF (CARES_INSTALL) INSTALL (TARGETS acountry RUNTIME DESTINATION ${CARES_INSTALL_LOCATION_BIN}) ENDIF ()