[Bf-blender-cvs] [67ea2d0d6f5] temp-clang-format: Add 'make format' makefile target

Campbell Barton noreply at git.blender.org
Wed Jan 9 20:16:45 CET 2019


Commit: 67ea2d0d6f5417753805bf55a23654b7da9e6f82
Author: Campbell Barton
Date:   Thu Jan 10 06:15:44 2019 +1100
Branches: temp-clang-format
https://developer.blender.org/rB67ea2d0d6f5417753805bf55a23654b7da9e6f82

Add 'make format' makefile target

move from shell script to Python for formatting utility

===================================================================

M	GNUmakefile
D	clang-format-edited.sh
A	clang-format-migration.py
D	clang-format-migration.sh

===================================================================

diff --git a/GNUmakefile b/GNUmakefile
index 21183220b80..b375e02bdfb 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -260,6 +260,7 @@ help: .FORCE
 	@echo "                 to define your own command."
 	@echo "  * tgz        - create a compressed archive of the source code."
 	@echo "  * update     - updates git and all submodules"
+	@echo "  * format     - format source code using clang (uses PATHS if passed in)."
 	@echo ""
 	@echo "Environment Variables"
 	@echo "  * BUILD_CMAKE_ARGS    - arguments passed to CMake."
@@ -461,6 +462,9 @@ update: .FORCE
 	git submodule foreach git checkout master
 	git submodule foreach git pull --rebase origin master
 
+format: .FORCE
+	python3 clang-format-migration.py $(PATHS)
+
 
 # -----------------------------------------------------------------------------
 # Documentation
diff --git a/clang-format-edited.sh b/clang-format-edited.sh
deleted file mode 100755
index eaac91008b3..00000000000
--- a/clang-format-edited.sh
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/bin/bash
-
-FILES=$(
-	git ls-tree -r HEAD \
-		intern/clog/ \
-		intern/ghost/ \
-		intern/guardedalloc/ \
-		intern/string/ \
-		source/ \
-		--name-only |
-		egrep \\.\(c\|cc\|cpp\|cxx\|h\|hh\|hpp\|hxx\|m\|mm\|osl\|glsl\)$
-	 )
-
-if [ -z "$FILES" ]; then
-	echo "Nothing to clang-format, exiting!"
-	exit 0
-fi
-
-# Run clang-format.
-
-# Limit to 2gib (some files can use many gig's of memory).
-ulimit -Sv 2000000
-xargs clang-format -verbose -i <<< $FILES
diff --git a/clang-format-migration.py b/clang-format-migration.py
new file mode 100755
index 00000000000..6d6cda95ebb
--- /dev/null
+++ b/clang-format-migration.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+
+import os
+import sys
+import subprocess
+
+# Optionally pass in files to operate on.
+paths = sys.argv[1:]
+if not paths:
+    paths.extend((
+        "intern/clog",
+        "intern/ghost",
+        "intern/guardedalloc",
+        "intern/string",
+        "source",
+    ))
+
+if os.sep != "/":
+    paths = [f.replace("/", os.sep) for f in paths]
+
+extensions = (
+    ".c", ".cc", ".cpp", ".cxx",
+    ".h", ".hh", ".hpp", ".hxx",
+    ".m", ".mm",
+    ".osl", ".glsl",
+)
+
+print("Operating on:")
+for p in paths:
+    print(" ", p)
+
+def source_files_from_git():
+    cmd = ("git", "ls-tree", "-r", "HEAD", *paths, "--name-only", "-z")
+    files = subprocess.check_output(cmd).split(b'\0')
+    return [f.decode('ascii') for f in files]
+
+
+def convert_tabs_to_spaces(files):
+    for f in files:
+        print("TabExpand", f)
+        with open(f, 'r', encoding="utf-8") as fh:
+            data = fh.read()
+            data = data.expandtabs(4)
+        with open(f, 'w', encoding="utf-8") as fh:
+            fh.write(data)
+
+
+def clang_format(files):
+    for f in files:
+        cmd = (
+            "clang-format", "-i", "-verbose", f.encode("ascii")
+        )
+        subprocess.check_call(cmd)
+
+
+def main():
+    files = [
+        f for f in source_files_from_git()
+        if f.endswith(extensions)
+    ]
+
+    convert_tabs_to_spaces(files)
+    clang_format(files)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/clang-format-migration.sh b/clang-format-migration.sh
deleted file mode 100755
index 9477cb0018d..00000000000
--- a/clang-format-migration.sh
+++ /dev/null
@@ -1,45 +0,0 @@
-#!/bin/bash
-
-# Optionally pass in files to operate on.
-
-if [[ "$#" -eq 0 ]]; then
-	GIT_PATHS="\
-intern/clog \
-intern/ghost \
-intern/guardedalloc/ \
-intern/string/ \
-source/ \
-"
-else
-	GIT_PATHS="$@"
-fi
-
-echo "Operating on: $GIT_PATHS"
-
-FILES=$(
-	git ls-tree -r HEAD \
-		$GIT_PATHS \
-		--name-only |
-		egrep \\.\(c\|cc\|cpp\|cxx\|h\|hh\|hpp\|hxx\|m\|mm\|osl\|glsl\)$
-	 )
-
-# First expand tabs
-SCRIPT=$(cat <<EOF
-import sys
-for f in sys.stdin.read().splitlines():
-	print(f)
-	with open(f, 'r', encoding="utf-8") as fh:
-		data = fh.read()
-	data = data.expandtabs(4)
-	with open(f, 'w', encoding="utf-8") as fh:
-		fh.write(data)
-
-EOF
-)
-python -c "$SCRIPT" <<< $FILES
-
-# Run clang-format.
-
-# Limit to 2gib (some files can use many gig's of memory).
-ulimit -Sv 2000000
-xargs clang-format -verbose -i <<< $FILES



More information about the Bf-blender-cvs mailing list