Commit a91bbcde authored by Thomas Ives's avatar Thomas Ives
Browse files

Merge branch 'self-contained-headers' into 'main'

Check headers are self contained and sort includes

After looking into it, I think that IWYU is too much effort for what we get from doing it (decreased build times).

However, I still think it is worth making sure each include file is self contained and if we have take, we can start sorting our includes which gives us one less thing to nit about.

To check that the headers are self contained, we try to compile files containing only "#include <path/to/header>" which get generated.  To find the header files, I do a `file(GLOB_RECURSE ... CONFIGURE_DEPENDS ...)`, which will re-run the glob every time we run `cmake --build` to check that a file hasn't been added.  If a file has been added, then we will re-run the cmake configure.  This ensures we do not miss a new header file, but does add some overhead.  I have therefore hidden this all behind an (off by default) `TANGO_CHECK_HEADERS_SELF_CONTAINED` option.  The debug linux preset has this enabled.

For sorting, I have gone with the following groupings.  If anyone cares about this strongly please speak up:

```
<main>

"any in quotes"

<tango/internal/...>

<tango/...>

<dependencies: omniORB, zmq, opentelemetry etc.>

<system headers>

<stdlib>
```

TODO:

- [x] Check that log4tango and tests headers are also self contained
- [x] Update the clang-format config to do the above properly

Close #1587

See merge request !1557
parents bc8a0769 2f164eae
Loading
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -39,6 +39,22 @@ EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeBlocks: Regroup
IncludeCategories:
  - Regex:           '^"'
    Priority:        1
  - Regex:           '^("|<)tango/internal'
    Priority:        2
  - Regex:           '^("|<)tango/'
    Priority:        3
  - Regex:           '^<(catch2/|nlohmann/|omni|COS/|zmq|opentelemetry|jpeglib'
    Priority:        4
  - Regex:           '^<[[:alnum:]_]+>'
    Priority:        6
  - Regex:           '^<.+>'
    Priority:        5
IncludeIsMainRegex: "(_[[:alnum:]_]+)?$"
IncludeIsMainSourceRegex: "(_templ\\.h)$"
IndentCaseLabels: false
IndentPPDirectives: BeforeHash
IndentWidth: 4
@@ -49,6 +65,7 @@ Language: Cpp
LineEnding: LF
MacroBlockBegin: ''
MacroBlockEnd: ''
MainIncludeChar: AngleBracket
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PPIndentWidth: 2
@@ -64,7 +81,7 @@ ReflowComments: true
# RemoveParentheses: true # clang-format-17 only
RemoveSemicolon: true
SeparateDefinitionBlocks: Always
SortIncludes: false
SortIncludes: CaseInsensitive
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceBeforeAssignmentOperators: true
+20 −0
Original line number Diff line number Diff line
@@ -84,6 +84,26 @@ include(configure/CMakeLists.txt)
set(TANGO_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(TANGO_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})

option(TANGO_CHECK_HEADERS_SELF_CONTAINED "Add check_headers_self_contained target" OFF)

if (TANGO_CHECK_HEADERS_SELF_CONTAINED)
    function(tango_add_check_headers_self_contained)
        cmake_parse_arguments("A" "" "PREFIX" "INCLUDE_PATHS" ${ARGN})
        set(_check_headers_self_contained_sources "")
        foreach(_path IN ITEMS ${A_INCLUDE_PATHS})
            get_filename_component(_name ${_path} NAME_WLE)
            get_filename_component(_dir ${_path} DIRECTORY)
            set(_cpp_name "${CMAKE_CURRENT_BINARY_DIR}/header_self_contained/${_dir}/${_name}.cpp")
            if (NOT EXISTS ${_cpp_name})
                file(WRITE ${_cpp_name} "#include <${_path}>\n")
            endif()
            list(APPEND _check_headers_self_contained_sources "${_cpp_name}")
        endforeach()
        add_library(${A_PREFIX}_check_headers_self_contained OBJECT ${_check_headers_self_contained_sources})
        target_link_libraries(${A_PREFIX}_check_headers_self_contained PRIVATE Tango::Tango)
    endfunction()
endif()

#source code
add_subdirectory("log4tango")
add_subdirectory("src")
+2 −1
Original line number Diff line number Diff line
@@ -42,7 +42,8 @@
      ],
      "binaryDir": "${sourceDir}/build/debug",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug"
        "CMAKE_BUILD_TYPE": "Debug",
        "TANGO_CHECK_HEADERS_SELF_CONTAINED": "ON"
      }
    },
    {
+1 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */
#include "test_omniidl.h"

#include <iostream>

int main()
+5 −0
Original line number Diff line number Diff line
@@ -22,3 +22,8 @@ if(WIN32)
else()
    target_compile_options(log4tango_objects PRIVATE -fPIC)
endif()

if (TANGO_CHECK_HEADERS_SELF_CONTAINED)
    file(GLOB_RECURSE _all_headers LIST_DIRECTORIES FALSE RELATIVE "${LOG4TANGO_SOURCE_DIR}/include" CONFIGURE_DEPENDS "*.h")
    tango_add_check_headers_self_contained(PREFIX log4tango INCLUDE_PATHS ${_all_headers})
endif()
Loading