瀏覽代碼

cmake: Splt compiler flag into seperate library to reuse with utils.

- Add common_compiler_flags INTERFACE library for common compiler flags
- Introduce new INTERFACE common_utils used in src/utils/*.
- Link compiler flags to common and common_utils
- CMake lint
Xenofon Karamanos 8 月之前
父節點
當前提交
8cbe99cf08
共有 5 個文件被更改,包括 76 次插入65 次删除
  1. 2 1
      CMakeLists.txt
  2. 21 21
      cmake/compiler-specific.cmake
  3. 38 43
      cmake/defs.cmake
  4. 3 0
      cmake/utils.cmake
  5. 12 0
      utils/CMakeLists.txt

+ 2 - 1
CMakeLists.txt

@@ -45,12 +45,13 @@ set(CFG_NAME
     CACHE STRING "Config name")
     CACHE STRING "Config name")
 
 
 include(GNUInstallDirs)
 include(GNUInstallDirs)
+include(${CMAKE_SOURCE_DIR}/cmake/defs.cmake)
 
 
 # Add the source directory
 # Add the source directory
 add_subdirectory(src)
 add_subdirectory(src)
 
 
 # Add utils
 # Add utils
-add_subdirectory(utils/kamctl)
+add_subdirectory(utils)
 
 
 # TODO: Packaging stuuf. These should be on different file propably
 # TODO: Packaging stuuf. These should be on different file propably
 set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
 set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})

+ 21 - 21
cmake/compiler-specific.cmake

@@ -5,60 +5,60 @@
 
 
 # Define the common flags and options for GCC
 # Define the common flags and options for GCC
 option(PROFILE "Enable profiling" OFF)
 option(PROFILE "Enable profiling" OFF)
+add_library(common_compiler_flags INTERFACE)
 
 
 # Define the flags for the C compiler
 # Define the flags for the C compiler
 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
 
 
   if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
   if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
-    target_compile_definitions(common INTERFACE CC_GCC_LIKE_ASM)
+    target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
 
 
     target_compile_options(
     target_compile_options(
-      common
+      common_compiler_flags
       INTERFACE -Wall -funroll-loops -Wcast-align
       INTERFACE -Wall -funroll-loops -Wcast-align
-                -Werror=implicit-function-declaration -Werror=implicit-int
-    )
+                -Werror=implicit-function-declaration -Werror=implicit-int)
 
 
     # If GCC version is greater than 4.2.0, enable the following flags
     # If GCC version is greater than 4.2.0, enable the following flags
     if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
     if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
       target_compile_options(
       target_compile_options(
-        common INTERFACE -m64 -minline-all-stringops -falign-loops
-                         -ftree-vectorize -fno-strict-overflow -mtune=generic
-      )
+        common_compiler_flags
+        INTERFACE -m64 -minline-all-stringops -falign-loops -ftree-vectorize
+                  -fno-strict-overflow -mtune=generic)
+      target_link_options(common_compiler_flags INTERFACE -m64)
     endif()
     endif()
   elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
   elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
-    target_compile_definitions(common INTERFACE CC_GCC_LIKE_ASM)
-    target_compile_options(common INTERFACE -m64)
-    target_link_options(common INTERFACE -m64)
+    target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
+    target_compile_options(common_compiler_flags INTERFACE -m64)
+    target_link_options(common_compiler_flags INTERFACE -m64)
   endif()
   endif()
 
 
 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i386|i486|i586|i686")
 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i386|i486|i586|i686")
 
 
   if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
   if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
-    target_compile_definitions(common INTERFACE CC_GCC_LIKE_ASM)
+    target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
 
 
     target_compile_options(
     target_compile_options(
-      common
+      common_compiler_flags
       INTERFACE -Wall -funroll-loops -Wcast-align
       INTERFACE -Wall -funroll-loops -Wcast-align
-                -Werror=implicit-function-declaration -Werror=implicit-int
-    )
+                -Werror=implicit-function-declaration -Werror=implicit-int)
 
 
     # If GCC version is greater than 4.2.0, enable the following flags
     # If GCC version is greater than 4.2.0, enable the following flags
     if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
     if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
       target_compile_options(
       target_compile_options(
-        common INTERFACE -m32 -minline-all-stringops -falign-loops
-                         -ftree-vectorize -fno-strict-overflow -mtune=generic
-      )
+        common_compiler_flags
+        INTERFACE -m32 -minline-all-stringops -falign-loops -ftree-vectorize
+                  -fno-strict-overflow -mtune=generic)
     endif()
     endif()
   elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
   elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
-    target_compile_definitions(common INTERFACE CC_GCC_LIKE_ASM)
-    target_compile_options(common INTERFACE -m32)
-    target_link_options(common INTERFACE -m32)
+    target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
+    target_compile_options(common_compiler_flags INTERFACE -m32)
+    target_link_options(common_compiler_flags INTERFACE -m32)
   endif()
   endif()
 
 
 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
 
 
   if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
   if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
-    target_compile_definitions(common INTERFACE CC_GCC_LIKE_ASM)
+    target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
 
 
     # target_compile_options(common INTERFACE -O0 # <$<$<BOOL:${PROFILE}>:-pg> )
     # target_compile_options(common INTERFACE -O0 # <$<$<BOOL:${PROFILE}>:-pg> )
 
 

+ 38 - 43
cmake/defs.cmake

@@ -32,8 +32,7 @@ message(STATUS "Target Processor Alias: ${TARGET_ARCH}")
 set(flavours kamailio)
 set(flavours kamailio)
 set(FLAVOUR
 set(FLAVOUR
     "kamailio"
     "kamailio"
-    CACHE STRING "Flavour of the project"
-)
+    CACHE STRING "Flavour of the project")
 set_property(CACHE FLAVOUR PROPERTY STRINGS ${flavours})
 set_property(CACHE FLAVOUR PROPERTY STRINGS ${flavours})
 
 
 # Verbose option (for debugging purposes) (was quiet in Makefile.defs) Probably
 # Verbose option (for debugging purposes) (was quiet in Makefile.defs) Probably
@@ -104,8 +103,7 @@ endif()
 set(LIBSSL_SET_MUTEX_SHARED
 set(LIBSSL_SET_MUTEX_SHARED
     ON
     ON
     CACHE BOOL
     CACHE BOOL
-          "enable workaround for libssl 1.1+ to set shared mutex attribute"
-)
+          "enable workaround for libssl 1.1+ to set shared mutex attribute")
 if(NOT ${LIBSSL_SET_MUTEX_SHARED})
 if(NOT ${LIBSSL_SET_MUTEX_SHARED})
   message(
   message(
     STATUS
     STATUS
@@ -114,7 +112,8 @@ if(NOT ${LIBSSL_SET_MUTEX_SHARED})
 
 
   # TODO: This can probably be reduced to a just a find_package(OpenSSL) call
   # TODO: This can probably be reduced to a just a find_package(OpenSSL) call
   # and then check the version
   # and then check the version
-  # If we are cross-compiling, cmake should search for library on the target or both target/host
+  # If we are cross-compiling, cmake should search for library on the target
+  # or both target/host
   if(NOT DEFINED CMAKE_CROSSCOMPILING OR NOT ${CMAKE_CROSSCOMPILING})
   if(NOT DEFINED CMAKE_CROSSCOMPILING OR NOT ${CMAKE_CROSSCOMPILING})
     message(STATUS "Checking for OpenSSL 1.1.0")
     message(STATUS "Checking for OpenSSL 1.1.0")
     find_package(OpenSSL 1.1.0)
     find_package(OpenSSL 1.1.0)
@@ -123,8 +122,7 @@ if(NOT ${LIBSSL_SET_MUTEX_SHARED})
       if(${OPENSSL_VERSION} VERSION_GREATER_EQUAL "1.1.0")
       if(${OPENSSL_VERSION} VERSION_GREATER_EQUAL "1.1.0")
         message(
         message(
           STATUS
           STATUS
-            "Enabling workaround for libssl 1.1+ to set shared mutex attribute"
-        )
+            "Enabling workaround for libssl 1.1+ to set shared mutex attribute")
         set(LIBSSL_SET_MUTEX_SHARED ON)
         set(LIBSSL_SET_MUTEX_SHARED ON)
       endif()
       endif()
     endif()
     endif()
@@ -137,13 +135,11 @@ endif()
 # -----------------------
 # -----------------------
 
 
 option(USE_FAST_LOCK "Use fast locking if available" ON)
 option(USE_FAST_LOCK "Use fast locking if available" ON)
-# Fast-lock not available for all platforms like mips Check the system processor
-# type and set USE_FAST_LOCK accordingly
-#
+# Fast-lock not available for all platforms like mips
+# Check the system processor type and set USE_FAST_LOCK accordingly
 if(USE_FAST_LOCK)
 if(USE_FAST_LOCK)
   if(CMAKE_SYSTEM_PROCESSOR MATCHES
   if(CMAKE_SYSTEM_PROCESSOR MATCHES
-     "i386|i486|i586|i686|x86_64|sparc64|sparc|ppc|ppc64|alpha|mips2|mips64"
-  )
+     "i386|i486|i586|i686|x86_64|sparc64|sparc|ppc|ppc64|alpha|mips2|mips64")
     set(USE_FAST_LOCK YES)
     set(USE_FAST_LOCK YES)
   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
     set(USE_FAST_LOCK NO)
     set(USE_FAST_LOCK NO)
@@ -160,8 +156,7 @@ if(USE_FAST_LOCK)
   elseif()
   elseif()
     message(
     message(
       STATUS
       STATUS
-        "Fast locking not available for this platform, disabling USE_FAST_LOCK"
-    )
+        "Fast locking not available for this platform, disabling USE_FAST_LOCK")
     set(USE_FAST_LOCK NO)
     set(USE_FAST_LOCK NO)
   endif()
   endif()
 endif()
 endif()
@@ -169,17 +164,15 @@ endif()
 # Add definitions if USE_FAST_LOCK is YES
 # Add definitions if USE_FAST_LOCK is YES
 message(STATUS "Fast lock available: ${USE_FAST_LOCK}")
 message(STATUS "Fast lock available: ${USE_FAST_LOCK}")
 if(USE_FAST_LOCK)
 if(USE_FAST_LOCK)
-  target_compile_definitions(
-    common INTERFACE FAST_LOCK ADAPTIVE_WAIT ADAPTIVE_WAIT_LOOPS=1024
-  )
+  target_compile_definitions(common INTERFACE FAST_LOCK ADAPTIVE_WAIT
+                                              ADAPTIVE_WAIT_LOOPS=1024)
 endif()
 endif()
 
 
 # List of locking methods in option
 # List of locking methods in option
 set(locking_methods USE_FUTEX USE_PTHREAD_MUTEX USE_POSIX_SEM USE_SYSV_SEM)
 set(locking_methods USE_FUTEX USE_PTHREAD_MUTEX USE_POSIX_SEM USE_SYSV_SEM)
 set(LOCK_METHOD
 set(LOCK_METHOD
     USE_FUTEX
     USE_FUTEX
-    CACHE STRING "Locking method to use"
-)
+    CACHE STRING "Locking method to use")
 set_property(CACHE LOCK_METHOD PROPERTY STRINGS ${locking_methods})
 set_property(CACHE LOCK_METHOD PROPERTY STRINGS ${locking_methods})
 
 
 # set(LOCKING_DEFINITION "${locking_method}")
 # set(LOCKING_DEFINITION "${locking_method}")
@@ -310,30 +303,29 @@ endif()
 string(TOLOWER ${OS} OS_LOWER)
 string(TOLOWER ${OS} OS_LOWER)
 target_compile_definitions(
 target_compile_definitions(
   common
   common
-  INTERFACE
-    NAME="${MAIN_NAME}"
-    VERSION="${RELEASE}"
-    ARCH="${TARGET_ARCH}"
-    OS=${OS}
-    OS_QUOTED="${OS}"
-    COMPILER="${COMPILER_NAME} ${CMAKE_C_COMPILER_VERSION}"
-    # ${HOST_ARCH}
-    __CPU_${TARGET_ARCH}
-    __OS_${OS_LOWER}
-    VERSIONVAL=${VERSIONVAL}
-    CFG_DIR="${CMAKE_INSTALL_FULL_SYSCONFDIR}/${CFG_NAME}/"
-    SHARE_DIR="${CMAKE_INSTALL_FULL_DATADIR}/${MAIN_NAME}/"
-    # Absolute path this run is always /var/run/kamailio either for local or
-    # system installs
-    RUN_DIR="${RUN_PREFIX}/${RUN_DIR}"
-    ${LOCK_METHOD}
-    # Module stuff?
-    PIC
-    # TODO: We can use the generator expression to define extra flags instead of
-    # checking the options each time
-    $<$<BOOL:${USE_SCTP}>:USE_SCTP>
-    $<$<BOOL:${STATISTICS}>:STATISTICS>
-)
+  INTERFACE NAME="${MAIN_NAME}"
+            VERSION="${RELEASE}"
+            ARCH="${TARGET_ARCH}"
+            OS=${OS}
+            OS_QUOTED="${OS}"
+            COMPILER="${COMPILER_NAME} ${CMAKE_C_COMPILER_VERSION}"
+            # ${HOST_ARCH}
+            __CPU_${TARGET_ARCH}
+            __OS_${OS_LOWER}
+            VERSIONVAL=${VERSIONVAL}
+            CFG_DIR="${CMAKE_INSTALL_FULL_SYSCONFDIR}/${CFG_NAME}/"
+            SHARE_DIR="${CMAKE_INSTALL_FULL_DATADIR}/${MAIN_NAME}/"
+            # Absolute path this run is always /var/run/kamailio either for
+            # local or system installs
+            RUN_DIR="${RUN_PREFIX}/${RUN_DIR}"
+            ${LOCK_METHOD}
+            # Module stuff?
+            # PIC
+            # TODO: We can use the generator expression to define extra flags
+            # instead of checking the options each time
+            $<$<BOOL:${USE_SCTP}>:USE_SCTP>
+            $<$<BOOL:${STATISTICS}>:STATISTICS>)
+target_link_libraries(common INTERFACE common_compiler_flags)
 
 
 # -----------------------
 # -----------------------
 add_library(common_modules INTERFACE)
 add_library(common_modules INTERFACE)
@@ -341,3 +333,6 @@ target_compile_options(common_modules INTERFACE -fPIC)
 
 
 # TODO: Do we need all the option from common as well?
 # TODO: Do we need all the option from common as well?
 target_link_libraries(common_modules INTERFACE common)
 target_link_libraries(common_modules INTERFACE common)
+
+add_library(common_utils INTERFACE)
+target_link_libraries(common_utils INTERFACE common_compiler_flags)

+ 3 - 0
cmake/utils.cmake

@@ -0,0 +1,3 @@
+set(UTIL_NAME "${NAME}")
+
+target_compile_options(${NAME} PRIVATE MOD_NAME="utils/${UTIL_NAME}")

+ 12 - 0
utils/CMakeLists.txt

@@ -0,0 +1,12 @@
+file(
+  GLOB children
+  RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
+  LIST_DIRECTORIES true
+  *)
+
+foreach(child ${children})
+  if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${child}
+     AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${child}/CMakeLists.txt)
+    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${child})
+  endif()
+endforeach()