cmake: add public headers to project source files
authorMichael Wallner <mike@php.net>
Tue, 11 May 2021 05:40:23 +0000 (07:40 +0200)
committerMichael Wallner <mike@php.net>
Tue, 11 May 2021 08:09:46 +0000 (10:09 +0200)
CMake/EnableDtrace.cmake
CMake/InstallPublicHeaders.cmake
CMakeLists.txt
include/CMakeLists.txt
include/libhashkit-1.0/CMakeLists.txt
src/libhashkit/CMakeLists.txt
src/libmemcached/CMakeLists.txt
src/libmemcachedprotocol/CMakeLists.txt
src/libmemcachedutil/CMakeLists.txt

index ea7499d7e52b3171e0b811a3cf5f8f16c4fbe33d..add0d7fd6c7f0da22386b27c00353be5b5f012ff 100644 (file)
@@ -1,9 +1,8 @@
 function(enable_dtrace_for TARGET PROBES_D PROBES_H)
     if(HAVE_DTRACE AND NOT CMAKE_CROSSCOMPILING)
-        target_sources(${TARGET} PRIVATE ${PROBES_H})
         add_custom_command(
                 OUTPUT
-                    ${PROBES_H}
+                    ${CMAKE_CURRENT_BINARY_DIR}/${PROBES_H}
                 MAIN_DEPENDENCY
                     ${PROBES_D}
                 COMMAND
@@ -11,20 +10,21 @@ function(enable_dtrace_for TARGET PROBES_D PROBES_H)
                 ARGS
                     -x nolibs -h
                     -s ${CMAKE_CURRENT_SOURCE_DIR}/${PROBES_D}
-                    -o ${PROBES_H}
+                    -o ${CMAKE_CURRENT_BINARY_DIR}/${PROBES_H}
         )
+        target_sources(${TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${PROBES_H})
         if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
             add_custom_command(
                     OUTPUT
-                        ${TARGET}_probes.o
+                        ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_probes.o
                     MAIN_DEPENDENCY
-                        ${PROBES_H}
+                        ${CMAKE_CURRENT_BINARY_DIR}/${PROBES_H}
                     COMMAND
                         ${DTRACE_EXECUTABLE}
                     ARGS
                         -x nolibs -G
                         -s ${CMAKE_CURRENT_SOURCE_DIR}/${PROBES_D}
-                        -o ${TARGET}_probes.o
+                        -o ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_probes.o
             )
             target_sources(${TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_probes.o)
             set_source_files_properties(${TARGET}_probes.o PROPERTIES
@@ -43,7 +43,7 @@ function(enable_dtrace_for TARGET PROBES_D PROBES_H)
                     TARGET ${TARGET}
                         PRE_LINK
                     DEPENDS
-                        ${PROBES_H}
+                        ${CMAKE_CURRENT_BINARY_DIR}/${PROBES_H}
                     COMMAND
                         rm -f ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${TARGET}.dir/${PROBES_C}.o
                     COMMAND
index 05629d67458f8855edd28c8fb716c7d9e1351566..7943ddb2eb03be2912d5f93fc1135d3b6442af21 100644 (file)
@@ -1,4 +1,4 @@
-function(install_public_headers DIRECTORY)
+macro(install_public_headers DIRECTORY)
     # validate current directory
     string(FIND ${CMAKE_CURRENT_SOURCE_DIR} /include/ INCDIR REVERSE)
     string(FIND ${CMAKE_CURRENT_SOURCE_DIR} /src/ SRCDIR REVERSE)
@@ -9,36 +9,41 @@ function(install_public_headers DIRECTORY)
             math(EXPR POSITION "${SRCDIR} + 5")
         endif()
         string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${POSITION} -1 CHKDIR)
-        if(NOT CHKDIR STREQUAL DIRECTORY)
-            message(SEND_ERROR "install_public_headers() directories do not match: ${CHKDIR} != ${DIRECTORY}")
+        if(NOT "${CHKDIR}" STREQUAL "${DIRECTORY}")
+            message(SEND_ERROR "install_public_headers() directories do not match: '${CHKDIR}' != '${DIRECTORY}'")
             set(ENV{INVALID_CONFIGURATION} 1)
         endif()
     endif()
 
+    string(REGEX MATCH "^[^/-]+" LIBRARY "${DIRECTORY}")
+
     # validate public interface version
-    get_filename_component(TOP ${DIRECTORY} NAME)
-    string(FIND ${TOP} "-" DASH)
+    string(FIND "${DIRECTORY}" "-" DASH)
     if(DASH GREATER 0)
-        string(SUBSTRING ${TOP} 0 ${DASH} LIBRARY)
-        string(TOUPPER ${LIBRARY} LIBRARY_UCASE)
+        string(SUBSTRING "${DIRECTORY}" 0 ${DASH} LIBRARY_BASE)
+        string(TOUPPER ${LIBRARY_BASE} LIBRARY_UCASE)
         math(EXPR DASH "${DASH} + 1")
-        string(SUBSTRING ${TOP} ${DASH} -1 VERSION)
+        string(SUBSTRING "${DIRECTORY}" ${DASH} -1 VERSION)
         if(NOT ${LIBRARY_UCASE}_VERSION_INC VERSION_EQUAL ${VERSION})
-            message(SEND_ERROR "${LIBRARY} public include directory version ${VERSION} != " ${${LIBRARY_UCASE}_VERSION_INC})
+            message(SEND_ERROR "${LIBRARY_BASE} public include directory version ${VERSION} != " ${${LIBRARY_UCASE}_VERSION_INC})
             set(ENV{INVALID_CONFIGURATION} 1)
         endif()
     endif()
 
     # change local includes to system includes
-    foreach(HEADER IN LISTS ARGN)
+    foreach(HEADER IN ITEMS ${ARGN})
         if(HEADER MATCHES "^@")
             string(SUBSTRING ${HEADER} 1 -1 HEADER)
             configure_file(${HEADER}.in ${HEADER})
             set(HEADER "${CMAKE_CURRENT_BINARY_DIR}/${HEADER}")
+        else()
+            set(HEADER "${CMAKE_CURRENT_SOURCE_DIR}/${HEADER}")
         endif()
         install(FILES ${HEADER}
                 COMPONENT dev
                 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${DIRECTORY}
                 )
+        list(APPEND ${LIBRARY}_includes ${HEADER})
     endforeach()
-endfunction()
+    set(${LIBRARY}_includes "${${LIBRARY}_includes}" PARENT_SCOPE)
+endmacro()
index 171e2469d25b4ea664a8806aa3ffc971882bd704..c476f76d7c8bbea2a5e5fd3f2ce4e63ee15a0eaa 100644 (file)
@@ -43,9 +43,9 @@ if(NOT BUILD_DOCSONLY)
             memtouch
             )
 
+    add_subdirectory(include)
     add_subdirectory(src)
     add_subdirectory(contrib)
-    add_subdirectory(include)
     add_subdirectory(support)
 
     # tests need c++17 support
index bc3dadc5323a5dffb3dd48289dde5760cd6faad2..a385e03c218695df7834df732e464b0a25a23f76 100644 (file)
@@ -1,5 +1,9 @@
 
 add_subdirectory(libhashkit-1.0)
+set(libhashkit_includes "${libhashkit_includes}" PARENT_SCOPE)
 add_subdirectory(libmemcached-1.0)
+set(libmemcached_includes "${libmemcached_includes}" PARENT_SCOPE)
 add_subdirectory(libmemcachedutil-1.0)
+set(libmemcachedutil_includes "${libmemcachedutil_includes}" PARENT_SCOPE)
 add_subdirectory(libmemcachedprotocol-0.0)
+set(libmemcachedprotocol_includes "${libmemcachedprotocol_includes}" PARENT_SCOPE)
index 24fefd5757477eafa282c24f920ff76843b5e9b6..39dabb296d3e8370ccf26e4afafdd816e95c2598 100644 (file)
@@ -1,5 +1,7 @@
 
-install_public_headers(libhashkit-1.0
+install_public_headers(
+        libhashkit-1.0
+
         @configure.h
         algorithm.h
         basic_string.h
index 48a9a1b9dab151aa8caf44ee7babf398d63319e5..355afabbb78239a68c4f38c372a4598f973f7ec4 100644 (file)
@@ -1,4 +1,4 @@
-add_library(libhashkit SHARED
+set(libhashkit_sources
         aes.cc
         algorithm.cc
         common.h
@@ -22,6 +22,7 @@ add_library(libhashkit SHARED
         strerror.cc
         string.cc
         )
+add_library(libhashkit SHARED)
 add_library(hashkit ALIAS libhashkit)
 set_target_properties(libhashkit PROPERTIES
         CXX_STANDARD ${CXX_STANDARD}
@@ -56,3 +57,7 @@ install_public_headers(
 
         hashkit.h
         )
+
+target_sources(libhashkit PRIVATE
+        ${libhashkit_sources}
+        ${libhashkit_includes})
index ffa1c0ada8789c51ca3a5d3c4118da07fe0897c6..81d84638d13767d71a2f46466d6c376d4ac0ae71 100644 (file)
@@ -1,9 +1,8 @@
-
 find_package(FLEX)
 find_package(BISON 2.3)
 
 if("${BISON_VERSION}" VERSION_GREATER_EQUAL 3.0)
-        set(BISON_WARNINGS -Wno-deprecated)
+    set(BISON_WARNINGS -Wno-deprecated)
 endif()
 
 file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/csl)
@@ -11,21 +10,23 @@ bison_target(CSL_PARSER csl/parser.yy ${CMAKE_CURRENT_BINARY_DIR}/csl/parser.cc
         DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/csl/parser.h
         COMPILE_FLAGS ${BISON_WARNINGS}
         )
-set_source_files_properties(${BISON_CSL_PARSER_OUTPUTS} PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
+set_source_files_properties(${BISON_CSL_PARSER_OUTPUTS} PROPERTIES
+        SKIP_UNITY_BUILD_INCLUSION ON)
 if(NOT HAVE_UNISTD_H)
-        set(FLEX_FLAGS --nounistd)
+    set(FLEX_FLAGS --nounistd)
 endif()
 flex_target(CSL_SCANNER csl/scanner.l ${CMAKE_CURRENT_BINARY_DIR}/csl/scanner.cc
         DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/csl/scanner.h
         COMPILE_FLAGS "${FLEX_FLAGS}"
         )
-set_source_files_properties(${FLEX_CSL_SCANNER_OUTPUTS} PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
+set_source_files_properties(${FLEX_CSL_SCANNER_OUTPUTS} PROPERTIES
+        SKIP_UNITY_BUILD_INCLUSION ON)
 add_flex_bison_dependency(CSL_SCANNER CSL_PARSER)
 
-set(LIBMEMCACHED_SOURCES
+set(libmemcached_sources
         csl/context.cc
-        ${BISON_CSL_PARSER_OUTPUTS}
         ${FLEX_CSL_SCANNER_OUTPUTS}
+        ${BISON_CSL_PARSER_OUTPUTS}
         allocators.cc
         analyze.cc
         array.cc
@@ -84,8 +85,7 @@ if(W_NO_DEPRECATED_REGISTER)
         )
 endif()
 
-add_library(libmemcached SHARED
-        ${LIBMEMCACHED_SOURCES})
+add_library(libmemcached SHARED)
 add_library(memcached ALIAS libmemcached)
 set_target_properties(libmemcached PROPERTIES
         CXX_STANDARD ${CXX_STANDARD}
@@ -139,8 +139,13 @@ install_public_headers(
         util.h
         )
 
-add_library(libmemcachedinternal STATIC
-        ${LIBMEMCACHED_SOURCES})
+target_sources(libmemcached PRIVATE
+        ${libmemcached_sources}
+        ${libmemcached_includes})
+
+# TODO: do we really need this?
+
+add_library(libmemcachedinternal STATIC)
 add_library(memcachedinternal ALIAS libmemcachedinternal)
 set_target_properties(libmemcachedinternal PROPERTIES CXX_STANDARD ${CXX_STANDARD} LIBRARY_OUTPUT_NAME memcachedinternal)
 target_compile_definitions(libmemcachedinternal PRIVATE -DBUILDING_LIBMEMCACHEDINTERNAL)
@@ -169,3 +174,7 @@ enable_dtrace_for(libmemcachedinternal libmemcached_probes.d dtrace_probes.h)
 # let libmemcached depend on libmemcachedinternal to ensure that they do not
 # compete for the same generated dependencies
 add_dependencies(libmemcached libmemcachedinternal)
+
+target_sources(libmemcachedinternal PRIVATE
+        ${libmemcached_sources}
+        ${libmemcached_includes})
index 91bfe36c53cdb1d6df4bad663231d53d387c8a68..b28afe7200ed258e4d6ae1c88b45df4d62fdb3ad 100644 (file)
@@ -1,6 +1,6 @@
 check_include(umem.h)
 
-add_library(libmemcachedprotocol SHARED
+set(libmemcachedprotocol_sources
         ascii_handler.c
         binary_handler.c
         cache.c
@@ -9,6 +9,7 @@ add_library(libmemcachedprotocol SHARED
         pedantic.c
         ../libmemcached/byteorder.cc
         )
+add_library(libmemcachedprotocol SHARED)
 add_library(memcachedprotocol ALIAS libmemcachedprotocol)
 set_target_properties(libmemcachedprotocol PROPERTIES
         CXX_STANDARD ${CXX_STANDARD}
@@ -46,3 +47,8 @@ export(EXPORT libmemcachedprotocol-targets NAMESPACE libmemcached::)
 install(EXPORT libmemcachedprotocol-targets NAMESPACE libmemcached::
         COMPONENT dev
         DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
+
+target_sources(libmemcachedprotocol PRIVATE
+        ${libmemcachedprotocol_sources}
+        ${libmemcachedprotocol_includes}
+        )
index d99b065edfb1c6bf1c5026dcb74b5c485bf6d034..78e87d305fe1bac2dac89c87a178d37bcda9512b 100644 (file)
@@ -1,5 +1,4 @@
-
-add_library(libmemcachedutil SHARED
+set(libmemcachedutil_sources
         ../libmemcached/backtrace.cc
         common.h
         flush.cc
@@ -7,10 +6,11 @@ add_library(libmemcachedutil SHARED
         ping.cc
         version.cc
         )
+add_library(libmemcachedutil SHARED)
+add_library(memcachedutil ALIAS libmemcachedutil)
 if(CMAKE_USE_PTHREADS_INIT)
     target_sources(libmemcachedutil PRIVATE pool.cc)
 endif()
-add_library(memcachedutil ALIAS libmemcachedutil)
 set_target_properties(libmemcachedutil PROPERTIES
         CXX_STANDARD ${CXX_STANDARD}
         LIBRARY_OUTPUT_NAME memcachedutil
@@ -49,3 +49,7 @@ export(EXPORT libmemcachedutil-targets NAMESPACE libmemcached::)
 install(EXPORT libmemcachedutil-targets NAMESPACE libmemcached::
         COMPONENT dev
         DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
+
+target_sources(libmemcachedutil PRIVATE
+    ${libmemcachedutil_sources}
+    ${libmemcachedutil_includes})