From 4824cad580b46e56d6122b7f14eab6e68b8f76d7 Mon Sep 17 00:00:00 2001
From: Campbell Barton <campbell@blender.org>
Date: Thu, 28 Apr 2022 15:29:09 +1000
Subject: [PATCH] GNUmakefile: include autopep8 in the "make format" target

Run autopep8 as well as clang-format when calling "make format",
the PATHS argument is passed to both utilities which will only operate
on files they support.

For example: `make format PATHS=release/scripts` formats Python scripts,
`make format PATHS=source/blender/blenlib` would format C/C++.
If users really want they can format C/C++ & Python files at the same
time since both formatting utilities filter on file extension.

`make format PATHS="release/scripts/startup/nodeitems_builtins.py source/creator/creator.c"`

A LIBDIR variable has been added to the GNUmakefile to simplify
references to this directory which can be one of 3 possible values.

Reviewed By: sybren, brecht

Ref D14789
---
 GNUmakefile | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/GNUmakefile b/GNUmakefile
index 8dc2a2e2a9a..a82d1bedace 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -120,7 +120,7 @@ Utilities
      Updates git and all submodules but not svn.
 
    * format:
-     Format source code using clang (uses PATHS if passed in). For example::
+     Format source code using clang-format & autopep8 (uses PATHS if passed in). For example::
 
         make format PATHS="source/blender/blenlib source/blender/blenkernel"
 
@@ -130,6 +130,7 @@ Environment Variables
    * BUILD_DIR:             Override default build path.
    * PYTHON:                Use this for the Python command (used for checking tools).
    * NPROCS:                Number of processes to use building (auto-detect when omitted).
+   * AUTOPEP8:              Command used for Python code-formatting (used for the format target).
 
 Documentation Targets
    Not associated with building Blender.
@@ -206,6 +207,27 @@ ifeq ($(OS_NCASE),darwin)
 	endif
 endif
 
+# Set the LIBDIR, an empty string when not found.
+LIBDIR:=$(wildcard ../lib/${OS_NCASE}_${CPU})
+ifeq (, $(LIBDIR))
+	LIBDIR:=$(wildcard ../lib/${OS_NCASE}_centos7_${CPU})
+endif
+ifeq (, $(LIBDIR))
+	LIBDIR:=$(wildcard ../lib/${OS_NCASE})
+endif
+
+# Use the autopep8 module in ../lib/ (which can be executed via Python directly).
+# Otherwise the "autopep8" command can be used.
+ifndef AUTOPEP8
+	ifneq (, $(LIBDIR))
+		AUTOPEP8:=$(wildcard $(LIBDIR)/python/lib/python3.10/site-packages/autopep8.py)
+	endif
+	ifeq (, $(AUTOPEP8))
+		AUTOPEP8:=autopep8
+	endif
+endif
+
+
 # -----------------------------------------------------------------------------
 # additional targets for the build configuration
 
@@ -527,8 +549,8 @@ update_code: .FORCE
 	@$(PYTHON) ./build_files/utils/make_update.py --no-libraries
 
 format: .FORCE
-	@PATH="../lib/${OS_NCASE}_${CPU}/llvm/bin/:../lib/${OS_NCASE}_centos7_${CPU}/llvm/bin/:../lib/${OS_NCASE}/llvm/bin/:$(PATH)" \
-	    $(PYTHON) source/tools/utils_maintenance/clang_format_paths.py $(PATHS)
+	@PATH="${LIBDIR}/llvm/bin/:$(PATH)" $(PYTHON) source/tools/utils_maintenance/clang_format_paths.py $(PATHS)
+	@$(PYTHON) source/tools/utils_maintenance/autopep8_format_paths.py --autopep8-command="$(AUTOPEP8)" $(PATHS)
 
 
 # -----------------------------------------------------------------------------
-- 
GitLab