Skip to content

Commit

Permalink
build: option to build clang-tidy as an executable
Browse files Browse the repository at this point in the history
ported from: CleverRaven/Cataclysm-DDA#56506

Co-authored-by: Co-authored-by: Jianxiang Wang (王健翔) <[email protected]>
  • Loading branch information
scarf005 and Qrox committed Jul 22, 2023
1 parent 1908c7e commit 4ee9115
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 83 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ option(USE_PCH_HEADER "Use precompiled PCH header." "OFF")
option(JSON_FORMAT "Build JSON formatter" "OFF")
option(CATA_CCACHE "Try to find and build with ccache" "ON")
option(CATA_CLANG_TIDY_PLUGIN "Build Cata's custom clang-tidy plugin" "OFF")
option(CATA_CLANG_TIDY_EXECUTABLE "Build Cata's custom clang-tidy checks as an executable" "OFF")
set(CATA_CLANG_TIDY_INCLUDE_DIR "" CACHE STRING "Path to internal clang-tidy headers required for plugin (e.g. ClangTidy.h)")
set(CATA_CHECK_CLANG_TIDY "" CACHE STRING "Path to check_clang_tidy.py for plugin tests")
set(GIT_BINARY "" CACHE STRING "Git binary name or path.")
Expand Down Expand Up @@ -353,7 +354,7 @@ add_subdirectory(tests)
if (JSON_FORMAT)
add_subdirectory(tools/format)
endif()
if (CATA_CLANG_TIDY_PLUGIN)
if (CATA_CLANG_TIDY_PLUGIN OR CATA_CLANG_TIDY_EXECUTABLE)
add_subdirectory(tools/clang-tidy-plugin)
endif()

Expand Down
81 changes: 5 additions & 76 deletions doc/DEVELOPER_TOOLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,78 +237,6 @@ and execute the following command inside the `<python3_root>/Scripts` directory
pip install path/to/your/downloaded/file.whl
```

Currently, the CDDA source is still building the custom checks as a plugin,
which unfortunately is not supported on Windows, so the following patch needs to
be applied before the custom checks can be built as an executable.

```patch
diff --git a/tools/clang-tidy-plugin/CMakeLists.txt b/tools/clang-tidy-plugin/CMakeLists.txt
index cf0c237645..540d3e29a5 100644
--- a/tools/clang-tidy-plugin/CMakeLists.txt
+++ b/tools/clang-tidy-plugin/CMakeLists.txt
@@ -4,7 +4,7 @@ include(ExternalProject)
find_package(LLVM REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)

-add_library(CataAnalyzerPlugin MODULE
+add_executable(CataAnalyzerPlugin
AlmostNeverAutoCheck.cpp
AssertCheck.cpp
CataTidyModule.cpp
@@ -56,6 +56,11 @@ else ()
target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE ${CATA_CLANG_TIDY_INCLUDE_DIR})
endif ()

+target_link_libraries(
+ CataAnalyzerPlugin
+ clangTidyMain
+ )
+
target_compile_definitions(CataAnalyzerPlugin PRIVATE ${LLVM_DEFINITIONS})

# We need to turn off exceptions and RTTI to match the LLVM build.
diff --git a/tools/clang-tidy-plugin/CataTidyModule.cpp b/tools/clang-tidy-plugin/CataTidyModule.cpp
index b7cb4df22c..a83db0c60e 100644
--- a/tools/clang-tidy-plugin/CataTidyModule.cpp
+++ b/tools/clang-tidy-plugin/CataTidyModule.cpp
@@ -18,6 +18,7 @@
#include "TestFilenameCheck.h"
#include "TestsMustRestoreGlobalStateCheck.h"
#include "TextStyleCheck.h"
+#include "tool/ClangTidyMain.h"
#include "TranslatorCommentsCheck.h"
#include "UnsequencedCallsCheck.h"
#include "UnusedStaticsCheck.h"
@@ -80,3 +81,8 @@ X( "cata-module", "Adds Cataclysm-DDA checks." );

} // namespace tidy
} // namespace clang
+
+int main( int argc, const char **argv )
+{
+ return clang::tidy::clangTidyMain( argc, argv );
+}
diff --git a/tools/clang-tidy-plugin/test/lit.cfg b/tools/clang-tidy-plugin/test/lit.cfg
index 496804316a..43beb49653 100644
--- a/tools/clang-tidy-plugin/test/lit.cfg
+++ b/tools/clang-tidy-plugin/test/lit.cfg
@@ -17,11 +17,13 @@ else:
config.plugin_build_root, 'clang-tidy-plugin-support', 'bin',
'check_clang_tidy.py')

-cata_include = os.path.join( config.cata_source_dir, "src" )
+cata_include = os.path.join( config.cata_source_dir, "./src" )

cata_plugin = os.path.join(
config.plugin_build_root, 'libCataAnalyzerPlugin.so')

+cata_plugin = ''
+
config.substitutions.append(('%check_clang_tidy', check_clang_tidy))
config.substitutions.append(('%cata_include', cata_include))
config.substitutions.append(('%cata_plugin', cata_plugin))
```

The next step is to run CMake to generate the compilation database. The compilation
database contains compiler flags that clang-tidy uses to check the source files.

Expand All @@ -320,17 +248,18 @@ if you built it with the instructions in the previous section.
Then add the following CMake options to generate the compilation database
(substitute values inside `<>` with the actual paths), and build the CDDA source
and the custom clang-tidy executable with `mingw32-make`. In this tutorial we
run CMake and `mingw32-make` in the `build` subdirectory.
run CMake and `mingw32-make` in the `build` subdirectory. Note that `DCATA_CLANG_TIDY_EXECUTABLE`
is defined instead of `DCATA_CLANG_TIDY_PLUGIN`.

```sh
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DLLVM_DIR="<llvm-source-root>/build/lib/cmake/llvm"
-DClang_DIR="<llvm-source-root>/build/lib/cmake/clang"
-DLLVM_INCLUDE_DIRS="<llvm-source-root>/llvm/include"
-DCLANG_INCLUDE_DIRS="<llvm-source-root>/clang/include"
-DCATA_CLANG_TIDY_PLUGIN=ON
-DCATA_CLANG_TIDY_EXECUTABLE=ON
-DCATA_CLANG_TIDY_INCLUDE_DIR="<llvm-source-root>/clang-tools-extra/clang-tidy"
-DCATA_CHECK_CLANG_TIDY="<llvm-source-root>/clang-tools-extra/test/clang-tidy/check_clang_tidy.py -clang-tidy=<cdda-source-root>/build/tools/clang-tidy-plugin/CataAnalyzerPlugin.exe"
-DCATA_CHECK_CLANG_TIDY="<llvm-source-root>/clang-tools-extra/test/clang-tidy/check_clang_tidy.py -clang-tidy=<cdda-source-root>/build/tools/clang-tidy-plugin/CataAnalyzer.exe"
```

Next, change the directory back to the source root, and run `tools/fix-compilation-database.py`
Expand All @@ -352,7 +281,7 @@ to avoid compiler errors.

```sh
python3 <llvm-source-root>/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py \
-clang-tidy-binary=build/tools/clang-tidy-plugin/CataAnalyzerPlugin.exe \
-clang-tidy-binary=build/tools/clang-tidy-plugin/CataAnalyzer.exe \
-p=build "\.cpp$" \
-extra-arg=-target -extra-arg=x86_64-pc-windows-gnu -extra-arg=-pthread -extra-arg=-DSDL_DISABLE_ANALYZE_MACROS \
-extra-arg=-isystem -extra-arg=<llvm-source-root>/clang/lib/Headers
Expand Down
9 changes: 6 additions & 3 deletions tools/clang-tidy-plugin/CataTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
#include "XYCheck.h"

#if defined( CATA_CLANG_TIDY_EXECUTABLE )
#include <clang-tidy/tool/ClangTidyMain.h>
#include "tool/ClangTidyMain.h"
#endif

namespace clang::tidy
namespace clang
{
namespace tidy
{
namespace cata
{
Expand Down Expand Up @@ -113,7 +115,8 @@ class CataModule : public ClangTidyModule
static ClangTidyModuleRegistry::Add<cata::CataModule>
X( "cata-module", "Adds Cataclysm-DDA checks." );

} // namespace clang::tidy
} // namespace tidy
} // namespace clang

#if defined( CATA_CLANG_TIDY_EXECUTABLE )
int main( int argc, const char **argv )
Expand Down
13 changes: 10 additions & 3 deletions tools/clang-tidy-plugin/test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ else:

check_clang_tidy += ' -std=c++17'

cata_include = os.path.join( config.cata_source_dir, "src" )
# Note: backlash (`\`) in the path seems to cause it to not work properly
# on Windows, therefore the relative path should start with `./` and use `/`
# to delimit directories instead of using multiple arguments to `os.path.join`.
cata_include = os.path.join( config.cata_source_dir, "./src" )
test_include = os.path.join( config.cata_source_dir, "./tools/clang-tidy-plugin/test-include" )

cata_plugin = os.path.join(
config.plugin_build_root, 'libCataAnalyzerPlugin.so')
if config.cata_clang_tidy_executable == "ON":
cata_plugin = ''
else:
cata_plugin = os.path.join(
config.plugin_build_root, 'libCataAnalyzerPlugin.so')

config.substitutions.append(('%check_clang_tidy', check_clang_tidy))
config.substitutions.append(('%cata_include', cata_include))
Expand Down
1 change: 1 addition & 0 deletions tools/clang-tidy-plugin/test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import os
config.cata_source_dir = "@CMAKE_SOURCE_DIR@"
config.plugin_build_root = "@CMAKE_CURRENT_BINARY_DIR@"
config.cata_check_clang_tidy = "@CATA_CHECK_CLANG_TIDY@"
config.cata_clang_tidy_executable = "@CATA_CLANG_TIDY_EXECUTABLE@"

lit_config.load_config(
config, os.path.join("@CMAKE_CURRENT_SOURCE_DIR@", "test", "lit.cfg"))

0 comments on commit 4ee9115

Please sign in to comment.