precommit: Add simple Makefile formatter

This commit is contained in:
Ole Schuett 2022-02-06 14:13:52 +01:00 committed by Ole Schütt
parent 5c1a921a00
commit e974820ca4
7 changed files with 105 additions and 57 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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 $<)

View file

@ -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

View file

@ -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.

View file

@ -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 <file>")
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

View file

@ -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()