Browse Source

Build system fixes

* added a FindPackage module for clang-format
* fixed library dependencies
Eyck Jentzsch 1 month ago
parent
commit
c08c553e9e
3 changed files with 76 additions and 13 deletions
  1. 6
    1
      CMakeLists.txt
  2. 34
    0
      cmake/FindClangFormat.cmake
  3. 36
    12
      cmake/clang-format.cmake

+ 6
- 1
CMakeLists.txt View File

@@ -79,7 +79,12 @@ set_target_properties(${LIBRARY_NAME} PROPERTIES
79 79
   PUBLIC_HEADER "${LibHeaders}" # specify the public headers
80 80
 )
81 81
 
82
-target_link_libraries(${LIBRARY_NAME} scc-util )
82
+target_link_libraries(${LIBRARY_NAME} scc-util ${CMAKE_DL_LIBS})
83
+if(${CMAKE_VERSION} VERSION_LESS "3.2.0")    
84
+    target_link_libraries(${LIBRARY_NAME} ${CMAKE_THREAD_LIBS_INIT})
85
+else()
86
+    target_link_libraries(${LIBRARY_NAME} Threads::Threads)
87
+endif()
83 88
 
84 89
 if(CCI_FOUND)
85 90
     target_compile_definitions(${LIBRARY_NAME} PUBLIC -DWITH_CCI)

+ 34
- 0
cmake/FindClangFormat.cmake View File

@@ -0,0 +1,34 @@
1
+# Find Clang format
2
+# 
3
+# 
4
+if(NOT CLANG_FORMAT_BIN_NAME)
5
+    set(CLANG_FORMAT_BIN_NAME clang-format)
6
+endif()
7
+
8
+# if custom path check there first
9
+if(CLANG_FORMAT_ROOT_DIR)
10
+    find_program(CLANG_FORMAT_BIN 
11
+        NAMES
12
+        ${CLANG_FORMAT_BIN_NAME}
13
+        PATHS
14
+        "${CLANG_FORMAT_ROOT_DIR}"
15
+        NO_DEFAULT_PATH)
16
+endif()
17
+
18
+find_program(CLANG_FORMAT_BIN NAMES ${CLANG_FORMAT_BIN_NAME})
19
+
20
+include(FindPackageHandleStandardArgs)
21
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(
22
+    CLANG_FORMAT
23
+    DEFAULT_MSG 
24
+    CLANG_FORMAT_BIN)
25
+
26
+mark_as_advanced(
27
+    CLANG_FORMAT_BIN)
28
+
29
+if(CLANG_FORMAT_FOUND)
30
+    # A CMake script to find all source files and setup clang-format targets for them
31
+    include(clang-format)
32
+else()
33
+    message("clang-format not found. Not setting up format targets")
34
+endif()

+ 36
- 12
cmake/clang-format.cmake View File

@@ -1,20 +1,44 @@
1 1
 # additional target to perform clang-format run, requires clang-format
2 2
 
3
-# get all project files
4
-file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h)
5
-foreach (SOURCE_FILE ${ALL_SOURCE_FILES})
6
-    foreach(PROJECT_TRDPARTY_DIR  ${PROJECT_3PARTY_DIRS})
7
-        string(FIND ${SOURCE_FILE} ${PROJECT_TRDPARTY_DIR} PROJECT_EXTERNAL_DIR_FOUND)
8
-        if (NOT ${PROJECT_EXTERNAL_DIR_FOUND} EQUAL -1)
9
-            #message( STATUS "Skipping ${SOURCE_FILE} for clang-format." )
3
+set(CLANG_FORMAT_CXX_FILE_EXTENSIONS ${CLANG_FORMAT_CXX_FILE_EXTENSIONS} *.cpp *.h *.cxx *.hxx *.hpp *.cc *.ipp)
4
+file(GLOB_RECURSE ALL_SOURCE_FILES ${CLANG_FORMAT_CXX_FILE_EXTENSIONS})
5
+
6
+# Don't include some common build folders
7
+set(CLANG_FORMAT_EXCLUDE_PATTERNS ${CLANG_FORMAT_EXCLUDE_PATTERNS} "/CMakeFiles/" "cmake")
8
+
9
+# get all project files file
10
+foreach (SOURCE_FILE ${ALL_SOURCE_FILES}) 
11
+    foreach (EXCLUDE_PATTERN ${CLANG_FORMAT_EXCLUDE_PATTERNS})
12
+        string(FIND ${SOURCE_FILE} ${EXCLUDE_PATTERN} EXCLUDE_FOUND) 
13
+        if (NOT ${EXCLUDE_FOUND} EQUAL -1) 
10 14
             list(REMOVE_ITEM ALL_SOURCE_FILES ${SOURCE_FILE})
11
-        endif ()
12
-    endforeach()
15
+        endif () 
16
+    endforeach ()
13 17
 endforeach ()
14 18
 
15
-add_custom_target(
16
-        clangformat
17
-        COMMAND clang-format
19
+add_custom_target(clangformat
20
+    COMMENT "Running clang-format to change files"
21
+        COMMAND ${CLANG_FORMAT_BIN}
18 22
         -i
19 23
         ${ALL_SOURCE_FILES}
20 24
 )
25
+
26
+add_custom_target(format
27
+    COMMENT "Running clang-format to change files"
28
+    COMMAND ${CLANG_FORMAT_BIN}
29
+    -style=file
30
+    -i
31
+    ${ALL_SOURCE_FILES}
32
+)
33
+
34
+
35
+add_custom_target(format-check
36
+    COMMENT "Checking clang-format changes"
37
+    # Use ! to negate the result for correct output
38
+    COMMAND !
39
+    ${CLANG_FORMAT_BIN}
40
+    -style=file
41
+    -output-replacements-xml
42
+    ${ALL_SOURCE_FILES}
43
+    | grep -q "replacement offset" 
44
+)