Skip to content

Commit

Permalink
clang-tidy.sh more auto detects inlcude dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Moritz Maurer committed Oct 5, 2024
1 parent 64401bf commit b398d3a
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 65 deletions.
16 changes: 3 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,11 @@ REPLACE_CMD = $(BASHCMD) $(BASH_SCRIPT_DIR)/replace.sh
CLANG_TIDY_CMD = $(BASHCMD) $(BASH_SCRIPT_DIR)/clang-tidy.sh
CLANG_FORMAT_CMD = $(BASHCMD) $(BASH_SCRIPT_DIR)/clang-format.sh


CLANG_TIDY_CONFIG = $(SRC_DIR)/linter/.clang-tidy
CLANG_FORMAT_STYLE = $(SRC_DIR)/linter/.clang-format

# COMPILE_COMMANDS_FILE = compile_commands.json
# ifeq ($(wildcard $(DEBUG_DIR)/$(COMPILE_COMMANDS_FILE)),)
# ifeq ($(wildcard $(RELEASE_DIR)/$(COMPILE_COMMANDS_FILE)),)
# $(info Neither $(DEBUG_DIR)/$(COMPILE_COMMANDS_FILE) nor $(RELEASE_DIR)/$(COMPILE_COMMANDS_FILE) exist. \
# Run make debug or make release first.)
# else
# COMPILE_COMMANDS := $(RELEASE_DIR)/$(COMPILE_COMMANDS_FILE)
# endif
# else
# COMPILE_COMMANDS := $(DEBUG_DIR)/$(COMPILE_COMMANDS_FILE)
# endif
INCLUDE_LIBS_EXT = "/usr/include/eigen3 /usr/include/catch2 /usr/include/hdf5/serial"


all: debug release documentation

Expand Down Expand Up @@ -64,7 +54,7 @@ documentation:
linter: clang-tidy clang-format

clang-tidy:
$(CLANG_TIDY_CMD) $(PROJECT_DIR) $(COMPILE_COMMANDS) $(CLANG_TIDY_CONFIG) $(SRC_DIR) $(CPP_STD)
$(CLANG_TIDY_CMD) $(BUIL_DIR) $(SRC_DIR) $(CLANG_TIDY_CONFIG) $(CPP_STD) $(INCLUDE_LIBS_EXT)

clang-format:
$(CLANG_FORMAT_CMD) $(CLANG_FORMAT_STYLE) $(SRC_DIR)
Expand Down
6 changes: 4 additions & 2 deletions dev/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ add_subdirectory(app)
# Include tests
add_subdirectory(tests)


# Set compile options for different build types
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -O0 -tr")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

# Write include dirs to a file
include(cmake/WriteIncludeDirs.cmake)
39 changes: 17 additions & 22 deletions dev/app/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#include <constants/constants.h>
#include <data_processing/config.h>
#include <yaml-cpp/yaml.h>

#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

#include <boost/program_options.hpp>
#include <yaml-cpp/yaml.h>

#include <constants/constants.h>
#include <data_processing/config.h>

namespace po = boost::program_options;
namespace dpc = flash::data_processing::config;
namespace tp = flash::types;

po::variables_map loadCommandLineArguments(int argc, char* argv[]) {
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("config-file,i", po::value<std::string>()->default_value("config.yaml"), "set path to a config.yaml");
desc.add_options()("help,h", "produce help message")(
"config-file,i", po::value<std::string>()->default_value("config.yaml"), "set path to a config.yaml");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
Expand All @@ -30,33 +28,30 @@ po::variables_map loadCommandLineArguments(int argc, char* argv[]) {
return vm;
}

dpc::Config setupConfigParameters(po::variables_map commandLineArguments){

dpc::Config setupConfigParameters(po::variables_map commandLineArguments) {
auto configFile = commandLineArguments["config-file"].as<std::string>();
std::cout << "Load configuration from " << configFile << std::endl;
try{

try {
auto configNode = YAML::LoadFile(configFile);
auto params = dpc::Config(configNode);
params.validate();
return params;

} catch (YAML::BadFile BF){
std::cerr << "Flash is terminating: " << "Could not open " << configFile << std::endl;
} catch (dpc::InvalidKeyException IKE){

} catch (YAML::BadFile BF) {
std::cerr << "Flash is terminating: "
<< "Could not open " << configFile << std::endl;
} catch (dpc::InvalidKeyException IKE) {
std::cerr << "Flash is terminating: " << IKE.what() << std::endl;
} catch (dpc::MissingKeyException MKE){
} catch (dpc::MissingKeyException MKE) {
std::cerr << "Flash is terminating: " << MKE.what() << std::endl;
} catch (dpc::InvalidConfigurationException ICE){
} catch (dpc::InvalidConfigurationException ICE) {
std::cerr << "Flash is terminating: " << ICE.what() << std::endl;
}

}

int main(int argc, char* argv[]) {

auto commandLineArguments = loadCommandLineArguments(argc, argv);
auto configParameters = setupConfigParameters(commandLineArguments);
auto configParameters = setupConfigParameters(commandLineArguments);

return 0;
}
3 changes: 2 additions & 1 deletion dev/cmake/ConfigCatch2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
# "Path to Catch2 include directory"
# FORCE)

find_package(Catch2 REQUIRED)
find_package(Catch2 REQUIRED)
include_directories(${CATCH3_INCLUDE_DIRS})
3 changes: 2 additions & 1 deletion dev/cmake/ConfigEigen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
# # Optionally, include Armadillo headers if needed explicitly
# include_directories(${ARMADILLO_INCLUDE_DIRS})

find_package(Eigen3 REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIRS})
1 change: 0 additions & 1 deletion dev/cmake/ConfigHDF5.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
# FORCE)

find_package(HDF5 REQUIRED COMPONENTS C CXX)

include_directories(${HDF5_INCLUDE_DIRS})
2 changes: 1 addition & 1 deletion dev/cmake/ConfigYamlCpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
# # FetchContent_MakeAvailable(some_dependency)

find_package(yaml-cpp REQUIRED)

include_directories(${YAML_CPP_INCLUDE_DIRS})
13 changes: 13 additions & 0 deletions dev/cmake/WriteIncludeDirs.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Get the list of include directories (e.g., target_include_directories or include_directories)
get_property(INCLUDE_DIRS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)

# Specify the output file
set(OUTPUT_FILE "${CMAKE_BINARY_DIR}/include_paths.txt")

# Open the file for writing
file(WRITE ${OUTPUT_FILE} "")

# Iterate over the list of include directories and write each to the file
foreach(INCLUDE_DIR ${INCLUDE_DIRS})
file(APPEND ${OUTPUT_FILE} "${INCLUDE_DIR}\n")
endforeach()
13 changes: 5 additions & 8 deletions dev/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# types
add_subdirectory(types)

# constants
add_subdirectory(constants)

# config
add_subdirectory(data_processing)

# electrons
add_subdirectory(electrons)

add_subdirectory(types)
add_subdirectory(constants)
add_subdirectory(data_processing)
add_subdirectory(electrons)
5 changes: 3 additions & 2 deletions dev/libs/constants/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

add_library(constants INTERFACE)

target_include_directories(constants INTERFACE
include
target_include_directories(constants
INTERFACE
include
)
2 changes: 1 addition & 1 deletion dev/libs/data_processing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ target_link_libraries(data_processing

target_include_directories(data_processing
PUBLIC
${CMAKE_SOURCE_DIR}/include
include
PRIVATE
src
)
46 changes: 33 additions & 13 deletions scripts/bash/clang-tidy.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
#!/bin/bash

PROJECT_DIR=$1
COMPILE_COMMANDS=$2


BUILD_DIR=$1
SRC_DIR=$2
CONFIG_FILE=$3
SRC_DIR=$4
CPP_STD=$5
NUMBER_OF_ARGUMENTS=5
CPP_STD=$4

# Check if enough arguments are passed
NUMBER_OF_ARGUMENTS=5
if [ "$#" -ne ${NUMBER_OF_ARGUMENTS} ]; then
echo "Usage: $0 <project_dir> <compile_commands> <config_file> <src> <cpp_std>"
echo "Usage: $0 <BUILD_DIR> <SRC_DIR> <CONFIG_FILE> <CPP_STD> <INCLUDE_LIBS_EXT>"
exit 1
fi

# Check if compile_commands.json exists
if [ ! -f ${COMPILE_COMMANDS} ]; then
echo "${COMPILE_COMMANDS} not found!"
exit 1
fi
# Find compile_commands.json in build dir
COMPILE_COMMANDS_FILE="compile_commands.json"
INCLUDE_PATHS_EXT_FILE="include_paths.txt"
BUILDS=$(find "${BUILD_DIR}" -maxdepth 1 -type d)
COMPILE_COMMANDS=""
INCLUDE_PATHS_EXT=()
for BUILD in ${BUILDS}; do
if [ -d "${BUILD}" ]; then
COMPILE_COMMANDS="${BUILD}/${COMPILE_COMMANDS_FILE}"
mapfile -t INCLUDE_PATHS_EXT < "${BUILD}/${INCLUDE_PATHS_EXT_FILE}"
echo "Running clang-tidy with ${COMPILE_COMMANDS}..."
fi
done
if [ -z ${COMPILE_COMMANDS} ]; then\
echo "Could not find ${COMPILE_COMMANDS_FILE}. Exit..."
exit
fi


# Find all directories named "include" and generate the include string
INCLUDE_PATHS=$(find "$PROJECT_DIR" -type d -name "include" -print | awk '{printf "-I%s ", $0}')
INCLUDE_PATHS_INT=$(find "$SRC_DIR" -type d -name "include" -print)
INCLUDE_PATHS_LIST=("${INCLUDE_PATHS_EXT[@]}" "${INCLUDE_PATHS_INT[@]}")
INCLUDE_PATHS=""
for LIB in ${INCLUDE_PATHS_LIST[@]}; do
INCLUDE_PATHS+="-I${LIB} "
done


# Run clang-tidy for all cpp files
for file in $(find ${SRC_DIR} -name '*.cpp'); do
echo "Running clang-tidy on $file "
clang-tidy ${file} --config-file=${CONFIG_FILE} -- -std=${CPP_STD} ${INCLUDE_PATHS} "-I/usr/include/eigen3" "-I/usr/include/catch2"
clang-tidy ${file} --config-file=${CONFIG_FILE} -- -std=${CPP_STD} ${INCLUDE_PATHS}

done

0 comments on commit b398d3a

Please sign in to comment.