From b912958a678437030f609264a70de5d18017f37d Mon Sep 17 00:00:00 2001 From: Andy Green Date: Thu, 28 Oct 2021 07:17:52 +0100 Subject: [PATCH] cmake: bring tls include requirement out as PUBLIC There's no problem for library build, also with LWS_WITH_MINIMAL_EXAMPLES, but after install at least on OSX, there are problems finding the installed lws include dir (concealed on most platforms by the path being in the default search list for the toolchain), and the references in the lws includes to the tls includes meaning that explicit paths for that must be available at consuming cmakes. This patch enhances the cmake config installed by lws to deal with adding the lws include paths to CMAKE_REQUIRED_INCLUDES and include_directories, so it can be found before the target is introduced. The tls include is passed back up the CMakeLists layers and the lws targets marked with target_include_directories(PUBLIC) with them, so they are understood as needed by consumers. More boilerplate is moved out of the example consuming cmakes. After this, on machines with previous installs of older lws, you may have to clean out the cmake install path, that is usually something like /usr/local/lib/cmake/libwebsockets/* before make installing lws and putting the latest content in there. --- CMakeLists.txt | 3 +++ cmake/libwebsockets-config.cmake.in | 19 ++++++++++++++++--- lib/CMakeLists.txt | 6 +++++- lib/tls/CMakeLists.txt | 12 ++++++++---- .../api-test-async-dns/CMakeLists.txt | 4 ---- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 402064569..62bb22271 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1055,6 +1055,9 @@ file(RELATIVE_PATH if (DEFINED REL_INCLUDE_DIR) set(LWS__INCLUDE_DIRS "\${LWS_CMAKE_DIR}/${REL_INCLUDE_DIR}") endif() +if (DEFINED OPENSSL_INCLUDE_DIRS) + set(LWS__INCLUDE_DIRS "${LWS__INCLUDE_DIRS};${OPENSSL_INCLUDE_DIRS}") +endif() configure_file(${PROJECT_SOURCE_DIR}/cmake/libwebsockets-config.cmake.in ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/libwebsockets-config.cmake diff --git a/cmake/libwebsockets-config.cmake.in b/cmake/libwebsockets-config.cmake.in index 49482ca1b..6247b2cbe 100644 --- a/cmake/libwebsockets-config.cmake.in +++ b/cmake/libwebsockets-config.cmake.in @@ -1,14 +1,14 @@ # - Config file for lws # It defines the following variables -# LIBWEBSOCKETS_INCLUDE_DIRS - include directories for FooBar +# LIBWEBSOCKETS_INCLUDE_DIRS - include directories for lws # LIBWEBSOCKETS_LIBRARIES - libraries to link against # Get the path of the current file. get_filename_component(LWS_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +list(APPEND CMAKE_MODULE_PATH ${libwebsockets_DIR}) -# Set the include directories. -set(LIBWEBSOCKETS_INCLUDE_DIRS "@LWS__INCLUDE_DIRS@") +set(LIBWEBSOCKETS_INCLUDE_DIRS "@LWS__INCLUDE_DIRS@" "@LWS_PUBLIC_INCLUDES@") # Include the project Targets file, this contains definitions for IMPORTED targets. include(${LWS_CMAKE_DIR}/LibwebsocketsTargets.cmake) @@ -17,8 +17,21 @@ include(${LWS_CMAKE_DIR}/LwsCheckRequirements.cmake) # IMPORTED targets from LibwebsocketsTargets.cmake set(LIBWEBSOCKETS_LIBRARIES websockets websockets_shared) +# These are additional include paths you will need +foreach(item "${LIBWEBSOCKETS_INCLUDE_DIRS}") + include_directories(${item}) + set(CMAKE_REQUIRED_INCLUDES "${CMAKE_REQUIRED_INCLUDES}" ${item}) +endforeach() + # These are additional libs that lws wants your app to also link to foreach(item "@LIB_LIST_AT_END@") list(APPEND LIBWEBSOCKETS_DEP_LIBS ${item}) endforeach() +# Move boilerplate for consuming cmake files into here + +include(CheckIncludeFile) +include(CheckCSourceCompiles) +include(LwsCheckRequirements) +set(requirements 1) + diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 88f14fa19..c55c61985 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -115,7 +115,7 @@ CHECK_C_SOURCE_COMPILES(" # after kernel 2.6.37 CHECK_C_SOURCE_COMPILES("#include \nint main(void) { return TCP_USER_TIMEOUT; }\n" LWS_HAVE_TCP_USER_TIMEOUT) - +set(LWS_PUBLIC_INCLUDES "") if (LWS_WITH_TLS) add_subdir_include_dirs(tls) endif() @@ -170,6 +170,8 @@ if (LWS_WITH_STATIC) ) target_include_directories(websockets PRIVATE ${LWS_LIB_BUILD_INC_PATHS}) target_compile_definitions(websockets PRIVATE LWS_BUILDING_STATIC) + target_include_directories(websockets PUBLIC ${LWS_PUBLIC_INCLUDES}) + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) if (WIN32) # Windows uses the same .lib ending for static libraries and shared @@ -196,6 +198,8 @@ if (LWS_WITH_SHARED) ) target_include_directories(websockets_shared PRIVATE ${LWS_LIB_BUILD_INC_PATHS}) target_compile_definitions(websockets_shared PRIVATE LWS_BUILDING_SHARED) + target_include_directories(websockets_shared PUBLIC ${LWS_PUBLIC_INCLUDES}) + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) # We want the shared lib to be named "libwebsockets" # not "libwebsocket_shared". diff --git a/lib/tls/CMakeLists.txt b/lib/tls/CMakeLists.txt index 91ca8be93..aaf4f4a26 100644 --- a/lib/tls/CMakeLists.txt +++ b/lib/tls/CMakeLists.txt @@ -217,12 +217,14 @@ if (LWS_WITH_SSL) if (LWS_WITH_CYASSL) foreach(inc ${WOLFSSL_INCLUDE_DIRS}) set(OPENSSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIRS} ${inc} ${inc}/cyassl) - include_directories("${inc}" "${inc}/cyassl") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} "${inc}" "${inc}/cyassl") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) endforeach() else() foreach(inc ${WOLFSSL_INCLUDE_DIRS}) set(OPENSSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIRS} ${inc} ${inc}/wolfssl) - include_directories("${inc}" "${inc}/wolfssl") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} "${inc}" "${inc}/wolfssl") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) endforeach() endif() set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${OPENSSL_INCLUDE_DIRS}) @@ -240,7 +242,8 @@ if (LWS_WITH_SSL) message("MBEDTLS libraries: ${MBEDTLS_LIBRARIES}") foreach(inc ${MBEDTLS_INCLUDE_DIRS}) - include_directories("${inc}" "${inc}/mbedtls") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} "${inc}" "${inc}/mbedtls") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) endforeach() list(INSERT LIB_LIST 0 "${MBEDTLS_LIBRARIES}") @@ -269,7 +272,8 @@ if (LWS_WITH_SSL) endif() if (OPENSSL_INCLUDE_DIRS) - include_directories("${OPENSSL_INCLUDE_DIRS}") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} "${OPENSSL_INCLUDE_DIRS}") + set(LWS_PUBLIC_INCLUDES ${LWS_PUBLIC_INCLUDES} PARENT_SCOPE) endif() if (NOT LWS_PLAT_FREERTOS) list(INSERT LIB_LIST 0 ${OPENSSL_LIBRARIES}) diff --git a/minimal-examples/api-tests/api-test-async-dns/CMakeLists.txt b/minimal-examples/api-tests/api-test-async-dns/CMakeLists.txt index 256726ca1..f2954d0fa 100644 --- a/minimal-examples/api-tests/api-test-async-dns/CMakeLists.txt +++ b/minimal-examples/api-tests/api-test-async-dns/CMakeLists.txt @@ -1,14 +1,10 @@ project(lws-api-test-async-dns C) cmake_minimum_required(VERSION 2.8.12) find_package(libwebsockets CONFIG REQUIRED) -list(APPEND CMAKE_MODULE_PATH ${LWS_CMAKE_DIR}) -include(CheckCSourceCompiles) -include(LwsCheckRequirements) set(SAMP lws-api-test-async-dns) set(SRCS main.c) -set(requirements 1) require_lws_config(LWS_ROLE_H1 1 requirements) require_lws_config(LWS_WITH_CLIENT 1 requirements) require_lws_config(LWS_WITH_SYS_ASYNC_DNS 1 requirements)