From e974820ca479a8522eb3581aae256dc0460c1093 Mon Sep 17 00:00:00 2001 From: Ole Schuett Date: Sun, 6 Feb 2022 14:13:52 +0100 Subject: [PATCH] precommit: Add simple Makefile formatter --- Makefile | 50 ++++++++++++++--------------- src/dbm/Makefile | 18 +++++------ src/grid/Makefile | 44 ++++++++++++------------- tools/Fun2D/Makefile | 2 +- tools/docker/scripts/test_python.sh | 1 + tools/precommit/format_makefile.py | 44 +++++++++++++++++++++++++ tools/precommit/precommit.py | 3 ++ 7 files changed, 105 insertions(+), 57 deletions(-) create mode 100755 tools/precommit/format_makefile.py diff --git a/Makefile b/Makefile index c3810582b6..057485d41b 100644 --- a/Makefile +++ b/Makefile @@ -69,18 +69,18 @@ include $(EXTSHOME)/Makefile.inc endif # Declare PHONY targets ===================================================== -.PHONY : $(VERSION) $(EXE_NAMES) \ - dirs makedep default_target all \ - toolversions exts extversions extclean \ - libcp2k cp2k_shell pkgconfig python-bindings \ - pre-commit pre-commit-clean \ - pretty precommit precommitclean doxygenclean doxygen \ - fpretty fprettyclean \ - doxify doxifyclean \ - install clean realclean distclean mrproper help \ - test testbg testclean testrealclean \ - data \ - $(EXTSPACKAGES) +.PHONY: $(VERSION) $(EXE_NAMES) \ + dirs makedep default_target all \ + toolversions exts extversions extclean \ + libcp2k cp2k_shell pkgconfig python-bindings \ + pre-commit pre-commit-clean \ + pretty precommit precommitclean doxygenclean doxygen \ + fpretty fprettyclean \ + doxify doxifyclean \ + install clean realclean distclean mrproper help \ + test testbg testclean testrealclean \ + data \ + $(EXTSPACKAGES) # Discover files and directories ============================================ ALL_SRC_DIRS := $(shell find $(SRCDIR) -type d ! -name preprettify | awk '{printf("%s:",$$1)}') @@ -175,12 +175,12 @@ libcp2k: pkgconfig makedep | dirs exts python-bindings: libcp2k @cd $(SRCDIR)/start/python ; \ - env CC='$(CC)' LDSHARED='$(LD) -shared' CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS) $(LDFLAGS_C) $(LIBS)' \ - $(PYTHON) setup.py build_ext \ - --build-temp="$(OBJDIR)/python" \ - --build-lib="$(LIBDIR)/python" \ - --library-dirs="$(LIBDIR)" \ - --libraries="$(patsubst -l%,%,$(filter -l%,$(LIBS)))" + env CC='$(CC)' LDSHARED='$(LD) -shared' CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS) $(LDFLAGS_C) $(LIBS)' \ + $(PYTHON) setup.py build_ext \ + --build-temp="$(OBJDIR)/python" \ + --build-lib="$(LIBDIR)/python" \ + --library-dirs="$(LIBDIR)" \ + --libraries="$(patsubst -l%,%,$(filter -l%,$(LIBS)))" exts: $(EXTSPACKAGES) @@ -291,9 +291,9 @@ help: @echo "=================== Binaries ====================" @echo "all Builds all executables (default target)" @for i in $(ALL_EXE_FILES); do \ - basename $$i | sed 's/^\(.*\)\..*/\1/' | awk '{printf "%-28s", $$1}'; \ - grep "brief" $$i | head -n 1 | sed 's/^.*\\brief\s*//'; \ - done + basename $$i | sed 's/^\(.*\)\..*/\1/' | awk '{printf "%-28s", $$1}'; \ + grep "brief" $$i | head -n 1 | sed 's/^.*\\brief\s*//'; \ + done @echo "libcp2k Builds CP2K as a single library archive" @echo "cp2k_shell Creates symlink for backward compatibility" @echo "" @@ -521,10 +521,10 @@ cp2k_info.o: $(GIT_REF) # Add some practical metadata about the build. FCFLAGS += -D__COMPILE_ARCH="\"$(ARCH)\""\ - -D__COMPILE_DATE="\"$(shell date)\""\ - -D__COMPILE_HOST="\"$(shell hostname 2>/dev/null || hostnamectl --transient)\""\ - -D__COMPILE_REVISION="\"$(strip $(REVISION))\""\ - -D__DATA_DIR="\"$(DATA_DIR)\"" + -D__COMPILE_DATE="\"$(shell date)\""\ + -D__COMPILE_HOST="\"$(shell hostname 2>/dev/null || hostnamectl --transient)\""\ + -D__COMPILE_REVISION="\"$(strip $(REVISION))\""\ + -D__DATA_DIR="\"$(DATA_DIR)\"" # $(FCLOGPIPE) can be used to store compiler output, e.g. warnings, for each F-file separately. # This is used e.g. by the convention checker. diff --git a/src/dbm/Makefile b/src/dbm/Makefile index c205ccb5e5..803d38db07 100644 --- a/src/dbm/Makefile +++ b/src/dbm/Makefile @@ -11,15 +11,15 @@ LIBS := -lm -lblas -lstdc++ ALL_HEADERS := $(shell find . -name "*.h") $(shell find ../offload/ -name "*.h") ALL_OBJECTS := ../offload/offload_library.o \ - dbm_distribution.o \ - dbm_library.o \ - dbm_matrix.o \ - dbm_mempool.o \ - dbm_mpi.o \ - dbm_multiply.o \ - dbm_multiply_comm.o \ - dbm_multiply_cpu.o \ - dbm_shard.o + dbm_distribution.o \ + dbm_library.o \ + dbm_matrix.o \ + dbm_mempool.o \ + dbm_mpi.o \ + dbm_multiply.o \ + dbm_multiply_comm.o \ + dbm_multiply_cpu.o \ + dbm_shard.o # Enable Cuda when nvcc compiler is present. NVCC := $(shell which nvcc) diff --git a/src/grid/Makefile b/src/grid/Makefile index 29d6f37085..116f7f1d16 100644 --- a/src/grid/Makefile +++ b/src/grid/Makefile @@ -11,26 +11,26 @@ LIBS := -lm -lblas ALL_HEADERS := $(shell find . -name "*.h") $(shell find ../offload/ -name "*.h") ALL_OBJECTS := ../offload/offload_buffer.o \ - ../offload/offload_library.o \ - grid_replay.o \ - grid_task_list.o \ - common/grid_library.o \ - common/grid_basis_set.o \ - common/grid_sphere_cache.o \ - ref/grid_ref_task_list.o \ - ref/grid_ref_collocate.o \ - ref/grid_ref_integrate.o \ - ref/grid_ref_prepare_pab.o \ - cpu/grid_context_cpu.o \ - cpu/coefficients.o \ - cpu/grid_collocate_dgemm.o \ - cpu/grid_integrate_dgemm.o \ - cpu/non_orthorombic_corrections.o \ - cpu/utils.o \ - cpu/collocation_integration.o \ - cpu/grid_context_cpu.o \ - cpu/grid_prepare_pab_dgemm.o \ - cpu/tensor_local.o + ../offload/offload_library.o \ + grid_replay.o \ + grid_task_list.o \ + common/grid_library.o \ + common/grid_basis_set.o \ + common/grid_sphere_cache.o \ + ref/grid_ref_task_list.o \ + ref/grid_ref_collocate.o \ + ref/grid_ref_integrate.o \ + ref/grid_ref_prepare_pab.o \ + cpu/grid_context_cpu.o \ + cpu/coefficients.o \ + cpu/grid_collocate_dgemm.o \ + cpu/grid_integrate_dgemm.o \ + cpu/non_orthorombic_corrections.o \ + cpu/utils.o \ + cpu/collocation_integration.o \ + cpu/grid_context_cpu.o \ + cpu/grid_prepare_pab_dgemm.o \ + cpu/tensor_local.o # Enable Cuda when nvcc compiler is present. NVCC := $(shell which nvcc) @@ -38,8 +38,8 @@ ifneq ($(NVCC),) LIBS += -lcudart -lcuda -lcublas -L${CUDA_PATH}/lib64 CFLAGS += -I${CUDA_PATH}/include -D__GRID_CUDA ALL_OBJECTS += gpu/grid_gpu_task_list.o \ - gpu/grid_gpu_collocate.o \ - gpu/grid_gpu_integrate.o + gpu/grid_gpu_collocate.o \ + gpu/grid_gpu_integrate.o %.o: %.cu $(ALL_HEADERS) cd $(dir $<); $(NVCC) -c $(NVFLAGS) $(notdir $<) diff --git a/tools/Fun2D/Makefile b/tools/Fun2D/Makefile index 00ae6ff3eb..c4a5665454 100644 --- a/tools/Fun2D/Makefile +++ b/tools/Fun2D/Makefile @@ -75,7 +75,7 @@ recurse.x: recurse.o $(RECURSEOBJ) # evalf is a quick hack # evalf.x: evalf.o mpfr_cutoff_gamma.o mpfr.o - $(FC) -o drvrec_mpfr.x evalf.o mpfr_cutoff_gamma.o mpfr.o $(LIBS) + $(FC) -o drvrec_mpfr.x evalf.o mpfr_cutoff_gamma.o mpfr.o $(LIBS) # generated files need special dependencies t_c_g0.f90: recurse.x drvrec_mpfr.x diff --git a/tools/docker/scripts/test_python.sh b/tools/docker/scripts/test_python.sh index 7dab94b5c0..83648fb628 100755 --- a/tools/docker/scripts/test_python.sh +++ b/tools/docker/scripts/test_python.sh @@ -31,6 +31,7 @@ run_test mypy --strict ./tools/dashboard/generate_dashboard.py run_test mypy --strict ./tools/regtesting/do_regtest.py run_test mypy --strict ./tools/regtesting/optimize_test_dirs.py run_test mypy --strict ./tools/precommit/check_file_properties.py +run_test mypy --strict ./tools/precommit/format_makefile.py run_test mypy --strict ./tools/docker/generate_dockerfiles.py # Test generate_dashboard.py. Running it twice to also execute its caching. diff --git a/tools/precommit/format_makefile.py b/tools/precommit/format_makefile.py new file mode 100755 index 0000000000..43d14c8f2b --- /dev/null +++ b/tools/precommit/format_makefile.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +# author: Ole Schuett + +import re +import sys +from pathlib import Path + + +def main() -> None: + if len(sys.argv) != 2: + print("Usage: format_makefile.py ") + sys.exit(1) + makefile = Path(sys.argv[1]) + + lines_out = [] + continuation = False + for line in makefile.read_text(encoding="utf8").split("\n"): + + # Remove trailing whitespaces. + line = line.rstrip() + + # Detect continued lines. + prev_continuation = continuation + continuation = line.endswith("\\") + + # Continued lines are indented 8 spaces. + if prev_continuation: + lines_out.append(" " * 8 + line.strip()) + + # Tabbed lines are indented with excatly one tab. + elif line.startswith("\t"): + lines_out.append("\t" + line.strip()) + + # All other lines are not indented. + else: + lines_out.append(line.strip()) + + makefile.write_text("\n".join(lines_out), encoding="utf8") + + +main() + +# EOF diff --git a/tools/precommit/precommit.py b/tools/precommit/precommit.py index f84a3cb130..9a7fe2b9cd 100755 --- a/tools/precommit/precommit.py +++ b/tools/precommit/precommit.py @@ -207,6 +207,9 @@ def process_file(fn, allow_modifications): if re.match(r"./data/.*POTENTIALS?$", fn): check_data_files() + if re.match(r".*/Makefile", fn): + run_local_tool("./tools/precommit/format_makefile.py", fn) + run_check_file_properties(fn) new_content = Path(fn).read_bytes()