diff --git a/cmake/build.cmake b/cmake/build.cmake index 45855b6..e461c33 100644 --- a/cmake/build.cmake +++ b/cmake/build.cmake @@ -7,7 +7,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, +# distributed under the License is distributed on an "AS IS"BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. @@ -18,21 +18,43 @@ include(CheckCXXCompilerFlag) # manger) puts it. set(LIBHAL_SCRIPT_PATH ${CMAKE_CURRENT_LIST_DIR}) -# Check if clang-tidy exists on the system and if so, evaluate each file -# against -find_program(LIBHAL_CLANG_TIDY_PROGRAM NAMES "clang-tidy") - -# If ti exists, add it as an additional check for each source file -if(DEFINED LIBHAL_CLANG_TIDY_PROGRAM) - message(STATUS "LIBHAL: clang-tidy AVAILABLE!") - set(LIBHAL_CLANG_TIDY_CONFIG_FILE - "${LIBHAL_SCRIPT_PATH}/clang-tidy.conf") - set(LIBHAL_CLANG_TIDY "${LIBHAL_CLANG_TIDY_PROGRAM}" - "--config-file=${LIBHAL_CLANG_TIDY_CONFIG_FILE}") +# Colored LIBHAL text +set(LIBHAL_TITLE "${BoldMagenta}[LIBHAL]:${ColourReset}") + +# Find clang-tidy +find_program(LIBHAL_CLANG_TIDY_PROGRAM NAMES "clang-tidy" DOC + "Path to clang-tidy executable") + +if(NOT LIBHAL_CLANG_TIDY_PROGRAM) + message(STATUS "${LIBHAL_TITLE} clang-tidy not found.") else() - message(WARNING - "LIBHAL:'clang-tidy' program is NOT available! Install it to run checks!") -endif(DEFINED LIBHAL_CLANG_TIDY_PROGRAM) + # Execute clang-tidy --version and parse the output + execute_process( + COMMAND ${LIBHAL_CLANG_TIDY_PROGRAM} --version + OUTPUT_VARIABLE clang_tidy_version_output + ) + + # Regex to extract version number + string(REGEX MATCH ".* version ([0-9]+)\\.([0-9]+)\\.([0-9]+)" + _ + ${clang_tidy_version_output}) + set(clang_tidy_major_version ${CMAKE_MATCH_1}) + set(clang_tidy_minor_version ${CMAKE_MATCH_2}) + set(clang_tidy_patch_version ${CMAKE_MATCH_3}) + + # Check if version is 15 or higher + if(clang_tidy_major_version LESS 15) + message(WARNING "clang-tidy version is too old. Version 15 or newer is required. Found: ${clang_tidy_major_version}.${clang_tidy_minor_version}.${clang_tidy_patch_version}") + else() + # Set clang-tidy as a CXX language standard option + set(CMAKE_CXX_CLANG_TIDY ${LIBHAL_CLANG_TIDY_PROGRAM}) + message(STATUS "${LIBHAL_TITLE} clang-tidy version ${clang_tidy_major_version}.${clang_tidy_minor_version}.${clang_tidy_patch_version} AVAILABLE!") + set(LIBHAL_CLANG_TIDY_CONFIG_FILE + "${LIBHAL_SCRIPT_PATH}/clang-tidy.conf") + set(LIBHAL_CLANG_TIDY "${LIBHAL_CLANG_TIDY_PROGRAM}" + "--config-file=${LIBHAL_CLANG_TIDY_CONFIG_FILE}") + endif() +endif() # Adds clang tidy check to target for host builds (skipped if a cross build) function(_libhal_add_clang_tidy_check TARGET) @@ -40,8 +62,7 @@ function(_libhal_add_clang_tidy_check TARGET) set_target_properties(${TARGET} PROPERTIES CXX_CLANG_TIDY "${LIBHAL_CLANG_TIDY}") else() - message(STATUS - "LIBHAL: Cross compiling, skipping clang-tidy checks for \"${TARGET}\"") + message(STATUS "${LIBHAL_TITLE} Cross compiling, skipping clang-tidy checks for \"${TARGET}\"") endif() endfunction() @@ -122,11 +143,11 @@ function(libhal_unit_test) endblock() if(${ADDRESS_SANITIZER_SUPPORT}) - message(STATUS "LIBHAL: Address Sanitizer available! Using it for tests!") + message(STATUS "${LIBHAL_TITLE} Address Sanitizer available! Using it for tests!") target_compile_options(unit_test PRIVATE -fsanitize=address) target_link_options(unit_test PRIVATE -fsanitize=address) else() - message(STATUS "LIBHAL: Address Sanitizer not supported!") + message(STATUS "${LIBHAL_TITLE} Address Sanitizer not supported!") endif(${ADDRESS_SANITIZER_SUPPORT}) _libhal_add_clang_tidy_check(unit_test) @@ -254,7 +275,7 @@ function(libhal_build_demos) foreach(demo ${DEMO_ARGS_DEMOS}) set(elf ${demo}.elf) - message(STATUS "LIBHAL: Generating Demo for \"${elf}\"") + message(STATUS "${LIBHAL_TITLE} Generating Demo for \"${elf}\"") add_executable(${elf} ${CMAKE_SOURCE_DIR}/applications/${demo}.cpp) target_compile_features(${elf} PRIVATE cxx_std_20) target_include_directories(${elf} PUBLIC ${DEMO_ARGS_INCLUDES}) diff --git a/cmake/colors.cmake b/cmake/colors.cmake new file mode 100644 index 0000000..0e11945 --- /dev/null +++ b/cmake/colors.cmake @@ -0,0 +1,33 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if(NOT WIN32) + string(ASCII 27 Esc) + set(ColourReset "${Esc}[m") + set(ColourBold "${Esc}[1m") + set(Red "${Esc}[31m") + set(Green "${Esc}[32m") + set(Yellow "${Esc}[33m") + set(Blue "${Esc}[34m") + set(Magenta "${Esc}[35m") + set(Cyan "${Esc}[36m") + set(White "${Esc}[37m") + set(BoldRed "${Esc}[1;31m") + set(BoldGreen "${Esc}[1;32m") + set(BoldYellow "${Esc}[1;33m") + set(BoldBlue "${Esc}[1;34m") + set(BoldMagenta "${Esc}[1;35m") + set(BoldCyan "${Esc}[1;36m") + set(BoldWhite "${Esc}[1;37m") +endif() diff --git a/conanfile.py b/conanfile.py index 237a39c..133d557 100644 --- a/conanfile.py +++ b/conanfile.py @@ -23,7 +23,7 @@ class libhal_cmake_util_conan(ConanFile): name = "libhal-cmake-util" - version = "2.2.0" + version = "3.0.0" license = "Apache-2.0" url = "https://github.com/conan-io/conan-center-index" homepage = "https://libhal.github.io/libhal-armcortex" @@ -63,6 +63,8 @@ def package_info(self): self.package_folder, "cmake/optimize_debug_build.cmake") build_path = os.path.join( self.package_folder, "cmake/build.cmake") + colors_path = os.path.join( + self.package_folder, "cmake/colors.cmake") clang_tidy_config_path = os.path.join( self.package_folder, "cmake/clang-tidy.conf") @@ -76,10 +78,15 @@ def package_info(self): "tools.cmake.cmaketoolchain:user_toolchain", optimize_debug_build_path) + self.conf_info.append( + "tools.cmake.cmaketoolchain:user_toolchain", + colors_path) + self.conf_info.append( "tools.cmake.cmaketoolchain:user_toolchain", build_path) + self.output.info( f"clang_tidy_config_path: {clang_tidy_config_path}") self.output.info(