Skip to content

Commit fa1d252

Browse files
committed
Merge pull request #6790
8c15f33 [trivial] Update contrib/devtools/README.md (MarcoFalke) 338f62f [devtools] add clang-format.py (MarcoFalke)
2 parents b2b173a + 8c15f33 commit fa1d252

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

contrib/devtools/README.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
Contents
2-
===========
2+
========
33
This directory contains tools for developers working on this repository.
44

5+
clang-format.py
6+
===============
7+
8+
A script to format cpp source code according to [.clang-format](../../src/.clang-format). This should only be applied to new files or files which are currently not actively developed on. Also, git subtrees are not subject to formatting.
9+
510
github-merge.sh
6-
==================
11+
===============
712

813
A small script to automate merging pull-requests securely and sign them with GPG.
914

@@ -37,23 +42,31 @@ Configuring the github-merge tool for the bitcoin repository is done in the foll
3742
git config --global user.signingkey mykeyid (if you want to GPG sign)
3843

3944
fix-copyright-headers.py
40-
===========================
45+
========================
4146

4247
Every year newly updated files need to have its copyright headers updated to reflect the current year.
4348
If you run this script from src/ it will automatically update the year on the copyright header for all
4449
.cpp and .h files if these have a git commit from the current year.
4550

4651
For example a file changed in 2014 (with 2014 being the current year):
52+
4753
```// Copyright (c) 2009-2013 The Bitcoin Core developers```
4854

4955
would be changed to:
56+
5057
```// Copyright (c) 2009-2014 The Bitcoin Core developers```
5158

59+
optimize-pngs.py
60+
================
61+
62+
A script to optimize png files in the bitcoin
63+
repository (requires pngcrush).
64+
5265
symbol-check.py
53-
==================
66+
===============
5467

5568
A script to check that the (Linux) executables produced by gitian only contain
56-
allowed gcc, glibc and libstdc++ version symbols. This makes sure they are
69+
allowed gcc, glibc and libstdc++ version symbols. This makes sure they are
5770
still compatible with the minimum supported Linux distribution versions.
5871

5972
Example usage after a gitian build:
@@ -70,7 +83,7 @@ If there are 'unsupported' symbols, the return value will be 1 a list like this
7083
.../64/test_bitcoin: symbol _ZNSt8__detail15_List_nod from unsupported version GLIBCXX_3.4.15
7184

7285
update-translations.py
73-
=======================
86+
======================
7487

7588
Run this script from the root of the repository to update all translations from transifex.
7689
It will do the following automatically:
@@ -93,4 +106,5 @@ maintained:
93106
* for sec/leveldb: https://github.com/bitcoin/leveldb.git (branch bitcoin-fork)
94107

95108
Usage: git-subtree-check.sh DIR COMMIT
109+
96110
COMMIT may be omitted, in which case HEAD is used.

contrib/devtools/clang-format.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
'''
3+
Wrapper script for clang-format
4+
5+
Copyright (c) 2015 MarcoFalke
6+
Copyright (c) 2015 The Bitcoin Core developers
7+
Distributed under the MIT software license, see the accompanying
8+
file COPYING or http://www.opensource.org/licenses/mit-license.php.
9+
'''
10+
11+
import os
12+
import sys
13+
import subprocess
14+
15+
tested_versions = ['3.6.0', '3.6.1', '3.6.2'] # A set of versions known to produce the same output
16+
accepted_file_extensions = ('.h', '.cpp') # Files to format
17+
18+
def check_clang_format_version(clang_format_exe):
19+
try:
20+
output = subprocess.check_output([clang_format_exe, '-version'])
21+
for ver in tested_versions:
22+
if ver in output:
23+
print "Detected clang-format version " + ver
24+
return
25+
raise RuntimeError("Untested version: " + output)
26+
except Exception as e:
27+
print 'Could not verify version of ' + clang_format_exe + '.'
28+
raise e
29+
30+
def check_command_line_args(argv):
31+
required_args = ['{clang-format-exe}', '{files}']
32+
example_args = ['clang-format-3.x', 'src/main.cpp', 'src/wallet/*']
33+
34+
if(len(argv) < len(required_args) + 1):
35+
for word in (['Usage:', argv[0]] + required_args):
36+
print word,
37+
print ''
38+
for word in (['E.g:', argv[0]] + example_args):
39+
print word,
40+
print ''
41+
sys.exit(1)
42+
43+
def run_clang_format(clang_format_exe, files):
44+
for target in files:
45+
if os.path.isdir(target):
46+
for path, dirs, files in os.walk(target):
47+
run_clang_format(clang_format_exe, (os.path.join(path, f) for f in files))
48+
elif target.endswith(accepted_file_extensions):
49+
print "Format " + target
50+
subprocess.check_call([clang_format_exe, '-i', '-style=file', target], stdout=open(os.devnull, 'wb'), stderr=subprocess.STDOUT)
51+
else:
52+
print "Skip " + target
53+
54+
def main(argv):
55+
check_command_line_args(argv)
56+
clang_format_exe = argv[1]
57+
files = argv[2:]
58+
check_clang_format_version(clang_format_exe)
59+
run_clang_format(clang_format_exe, files)
60+
61+
if __name__ == "__main__":
62+
main(sys.argv)

0 commit comments

Comments
 (0)