1#!/usr/bin/env python
2#
3# Copyright 2021 Nikita Melekhin. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Launch the script from root of the project to have the correct paths
8
9import os
10import sys
11from os import fdopen, remove
12
13walk_dir = sys.argv[1]
14
15print('walk_dir = ' + walk_dir)
16print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
17
18all_includes = []
19
20
21def is_guard(line):
22 if line.startswith("#ifndef _"):
23 return True
24 return False
25
26
27def get_guard(line):
28 return line[8:-1]
29
30
31def new_guard(line, path):
32 gen = path.split('/')[1:]
33 gen = list(filter(lambda a: a != "libs", gen))
34 gen = list(filter(lambda a: a != "include", gen))
35 line = "_"
36 for l in gen:
37 line += l + "_"
38 line = line.replace(".", "_")
39 line = line.replace("-", "_")
40 line = line.upper()
41 return line[:-1]
42
43
44def fix_guards(file):
45 data = []
46 guard = None
47 with open(file) as old_file:
48 for line in old_file:
49 data.append(line)
50 if is_guard(line) and guard is None:
51 guard = get_guard(line)
52
53 if guard is None:
54 return
55
56 ng = new_guard(guard, file)
57
58 with open(file, 'w') as new_file:
59 for i in data:
60 i = i.replace(guard, ng)
61 new_file.write(i)
62
63
64for root, subdirs, files in os.walk(walk_dir):
65 for x in files:
66 if x.endswith(".h") or x.endswith(".hpp") or root.find("/libcxx/include") != -1:
67 fix_guards(os.path.join(root, x))