diff --git a/tools/clang-format/clang-format.yml b/tools/clang-format/clang-format.yml index 6d3cedb..c78e906 100644 --- a/tools/clang-format/clang-format.yml +++ b/tools/clang-format/clang-format.yml @@ -2,3 +2,6 @@ BasedOnStyle: LLVM # Use larger indentation IndentWidth: 4 + +# clang-tidy suppression markers +CommentPragmas: 'NOLINT' diff --git a/tools/clang-tidy/clang-tidy.cmake b/tools/clang-tidy/clang-tidy.cmake index 5062bd9..1ae80fd 100644 --- a/tools/clang-tidy/clang-tidy.cmake +++ b/tools/clang-tidy/clang-tidy.cmake @@ -1,12 +1,17 @@ if (DEV_MODE) find_program(clang_tidy_EXECUTABLE NAMES clang-tidy-13 clang-tidy) - #list(APPEND clang_tidy_command "${clang_tidy_EXECUTABLE}") - message(NOTICE ${clang_tidy_EXECUTABLE}) + list(APPEND clang_tidy_command "${clang_tidy_EXECUTABLE}" + "--config-file=${CMAKE_CURRENT_LIST_DIR}/clang-tidy.yml" + "--format-style=${clang_format_style}" + "--warnings-as-errors=*" + "--use-color") + + set_target_properties(progressia + PROPERTIES CXX_CLANG_TIDY "${clang_tidy_command}") add_custom_command(TARGET progressia PRE_BUILD COMMAND ${CMAKE_COMMAND} -E echo "Clang-tidy is enabled") - endif() diff --git a/tools/clang-tidy/clang-tidy.yml b/tools/clang-tidy/clang-tidy.yml new file mode 100644 index 0000000..4a7f056 --- /dev/null +++ b/tools/clang-tidy/clang-tidy.yml @@ -0,0 +1,7 @@ +Checks: "-*,\ + clang-analyzer-*,\ + cppcoreguidelines-*,\ + modernize-*,\ + performance-*,\ + readability-*" + diff --git a/tools/clang-tidy/use-clang-tidy.py b/tools/clang-tidy/use-clang-tidy.py deleted file mode 100644 index 63f77b6..0000000 --- a/tools/clang-tidy/use-clang-tidy.py +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env python3 - diff --git a/tools/embed/embed.cmake b/tools/embed/embed.cmake index df721c7..bf94e4e 100644 --- a/tools/embed/embed.cmake +++ b/tools/embed/embed.cmake @@ -90,62 +90,3 @@ function(compile_embeds target) COMMENT "Embedding assets" ) endfunction() - - -## Global variables. Yikes. FIXME -#set(tools ${PROJECT_SOURCE_DIR}/tools) -#set(generated ${PROJECT_BINARY_DIR}/generated) -#set(assets_to_embed "") -#set(assets_to_embed_args "") -# -#file(MAKE_DIRECTORY ${generated}) -# -#find_package(Vulkan COMPONENTS glslc REQUIRED) -#find_program(glslc_executable NAMES glslc HINTS Vulkan::glslc) -#set(shaders ${generated}/shaders) -#file(MAKE_DIRECTORY ${shaders}) -# -## Shedules compilation of shaders -## Adapted from https://stackoverflow.com/a/60472877/4463352 -#macro(compile_shader) -# foreach(source ${ARGV}) -# get_filename_component(source_basename ${source} NAME) -# set(tmp "${shaders}/${source_basename}.spv") -# add_custom_command( -# OUTPUT ${tmp} -# DEPENDS ${source} -# COMMAND ${glslc_executable} -# -o ${tmp} -# ${CMAKE_CURRENT_SOURCE_DIR}/${source} -# COMMENT "Compiling shader ${source}" -# ) -# list(APPEND assets_to_embed_args "${tmp};as;${source_basename}.spv") -# list(APPEND assets_to_embed "${tmp}") -# unset(tmp) -# unset(source_basename) -# endforeach() -#endmacro() -# -#compile_shader( -# desktop/graphics/shaders/shader.frag -# desktop/graphics/shaders/shader.vert -#) -# -## Generate embed files -#add_custom_command( -# OUTPUT ${generated}/embedded_resources.cpp -# ${generated}/embedded_resources.h -# -# COMMAND ${tools}/embed/embed.py -# --cpp ${generated}/embedded_resources.cpp -# --header ${generated}/embedded_resources.h -# -- -# ${assets_to_embed_args} -# -# DEPENDS "${assets_to_embed}" -# ${tools}/embed/embed.py -# -# WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} -# COMMENT "Embedding assets" -#) -# diff --git a/tools/glslc.cmake b/tools/glslc.cmake index 4172dcc..38dcd9f 100644 --- a/tools/glslc.cmake +++ b/tools/glslc.cmake @@ -51,62 +51,3 @@ function(compile_glsl target) target_embeds(${target} ${spv_path} AS "${source_basename}.spv") endforeach() endfunction() - - -## Global variables. Yikes. FIXME -#set(tools ${PROJECT_SOURCE_DIR}/tools) -#set(generated ${PROJECT_BINARY_DIR}/generated) -#set(assets_to_embed "") -#set(assets_to_embed_args "") -# -#file(MAKE_DIRECTORY ${generated}) -# -#find_package(Vulkan COMPONENTS glslc REQUIRED) -#find_program(glslc_executable NAMES glslc HINTS Vulkan::glslc) -#set(shaders ${generated}/shaders) -#file(MAKE_DIRECTORY ${shaders}) -# -## Shedules compilation of shaders -## Adapted from https://stackoverflow.com/a/60472877/4463352 -#macro(compile_shader) -# foreach(source ${ARGV}) -# get_filename_component(source_basename ${source} NAME) -# set(tmp "${shaders}/${source_basename}.spv") -# add_custom_command( -# OUTPUT ${tmp} -# DEPENDS ${source} -# COMMAND ${glslc_executable} -# -o ${tmp} -# ${CMAKE_CURRENT_SOURCE_DIR}/${source} -# COMMENT "Compiling shader ${source}" -# ) -# list(APPEND assets_to_embed_args "${tmp};as;${source_basename}.spv") -# list(APPEND assets_to_embed "${tmp}") -# unset(tmp) -# unset(source_basename) -# endforeach() -#endmacro() -# -#compile_shader( -# desktop/graphics/shaders/shader.frag -# desktop/graphics/shaders/shader.vert -#) -# -## Generate embed files -#add_custom_command( -# OUTPUT ${generated}/embedded_resources.cpp -# ${generated}/embedded_resources.h -# -# COMMAND ${tools}/embed/embed.py -# --cpp ${generated}/embedded_resources.cpp -# --header ${generated}/embedded_resources.h -# -- -# ${assets_to_embed_args} -# -# DEPENDS "${assets_to_embed}" -# ${tools}/embed/embed.py -# -# WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} -# COMMENT "Embedding assets" -#) -# diff --git a/tools/pre-commit.py b/tools/pre-commit.py new file mode 100755 index 0000000..013970a --- /dev/null +++ b/tools/pre-commit.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 + +usage = \ +'''Usage: %(me)s [--dry-run] [--verbose] [--dont-update] + or: %(me)s restore + or: %(me)s update +In the 1st form, run standard pre-commit procedure for Progressia. +In the 2nd form, attempt to restore workspace if the pre-commit hook failed. +In the 3rd form, only update git pre-commit hook + + --dry-run do not change anything in git or in the working tree + --verbose print commands and diagnostics + --dont-update do not update git pre-commit hook + --help display this help and exit + +Currently, the pre-commit procedure performs the following: + 1. format staged changes + 2. attempt to compile with staged changes only''' + +import sys +import os +import subprocess + +def fail(*args): + print(my_name + ':', *args, file=sys.stderr) + sys.exit(1) + +def invoke(*cmd, result_when_dry=None, quiet=True): + + if verbose: + print(my_name + ': command "' + '" "'.join(cmd) + '"') + + if dry_run and result_when_dry != None: + print(my_name + ': skipped: --dry-run') + return result_when_dry + + output = '' + popen = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + text=True, + universal_newlines=True) + + for line in popen.stdout: + if (not quiet): + print(line, end='') + output += line + + popen.stdout.close() + + return_code = popen.wait() + if return_code != 0: + raise subprocess.CalledProcessError(return_code, cmd) + + return output + +STASH_NAME = 'progressia_pre_commit_stash' + +def run_safety_checks(): + if invoke('git', 'stash', 'list', '--grep', f"^{STASH_NAME}$") != '': + fail(f"Cannot run pre-commit checks: stash {STASH_NAME} exists. " + + f"Use `{my_name} restore` to restore workspace and repository " + + f"state") + + # Let's hope there are no files with weird names + indexed_changes = \ + set(invoke('git', 'diff', '--name-only', '--cached') \ + .strip().split('\n')) + unindexed_changes = \ + set(invoke('git', 'diff', '--name-only') \ + .strip().split('\n')) + + both_changes = indexed_changes & unindexed_changes + + if len(both_changes) != 0: + fail(f"Cannot run pre-commit checks: files with indexed and " + + "unindexed changes exist:\n\n\t" + + "\n\t".join(both_changes) + + "\n") + + +if __name__ == '__main__': + my_name = os.path.basename(sys.argv[0]) + verbose = False + dry_run = False + + + + verbose = True + dry_run = True + run_safety_checks() + #update() + + unindexed_changes = invoke('git', 'diff', '--name-status') + if unindexed_changes != '': + print('Unindexed changes found in files:') + print(unindexed_changes) + print('These changes will be ignored') + + invoke('git', 'stash', 'push', + '--keep-index', + '--include-untracked', + '--message', STASH_NAME, + result_when_dry='') + + # check that ORIGINAL does not exist + # check that staged files & files with unstaged changes = 0 + # update pre-commit hook + # if any unstaged changes: + # stash ORIGINAL + # remove unstaged changes + # format staged files + # compile + # git add + # if any unstaged changes: + # unstash ORIGINAL +