ENH: Update FreeFOAM contributions to GPL v3
[freefoam.git] / data / utilities / postProcFreeFOAMDeps.py.in
blob002ed6797ad2ce59f59695409dbc307480567bec
1 #!@PYTHON_EXECUTABLE@
2 #-------------------------------------------------------------------------------
3 # ______ _ ____ __ __
4 # | ____| _| |_ / __ \ /\ | \/ |
5 # | |__ _ __ ___ ___ / \| | | | / \ | \ / |
6 # | __| '__/ _ \/ _ ( (| |) ) | | |/ /\ \ | |\/| |
7 # | | | | | __/ __/\_ _/| |__| / ____ \| | | |
8 # |_| |_| \___|\___| |_| \____/_/ \_\_| |_|
10 # FreeFOAM: The Cross-Platform CFD Toolkit
12 # Copyright (C) 2008-2012 Michael Wild <themiwi@users.sf.net>
13 # Gerber van der Graaf <gerber_graaf@users.sf.net>
14 #-------------------------------------------------------------------------------
15 # License
16 # This file is part of FreeFOAM.
18 # FreeFOAM is free software: you can redistribute it and/or modify it
19 # under the terms of the GNU General Public License as published by the
20 # Free Software Foundation, either version 3 of the License, or (at your
21 # option) any later version.
23 # FreeFOAM is distributed in the hope that it will be useful, but WITHOUT
24 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 # for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with FreeFOAM. If not, see <http://www.gnu.org/licenses/>.
31 # Script
32 # postProcFreeFOAMDeps
34 # Description
35 # Patches up the dependency files generated when compiling with -MD.
37 #------------------------------------------------------------------------------
39 """Usage: generateOpenFOAMDeps
41 Patches up the dependency files generated when compiling with -MD.
43 """
45 import sys
46 sys.path.insert(0, '@FOAM_PYTHON_DIR@')
47 from FreeFOAM.compat import *
48 import re
49 import os
50 import os.path as op
52 blacklistPatterns = (
53 re.compile(r'^/usr/'),
54 re.compile(r'ImplI?\.[CH]'),
57 def checkBlacklistPatterns(f):
58 for p in blacklistPatterns:
59 if p.search(f) is not None:
60 return True
61 return False
63 substPatterns = (
64 (re.compile(r'_(\.[CH]|/)'), r'\1'),
65 (re.compile(r'_subst(\.[CH]|/)'), r'\1'),
66 (re.compile(r'(src/OSspecific)/OSspecific/(sig\w+\.[CH])'), r'\1/POSIX/signals/\2'),
67 (re.compile(r'(src/OSspecific)/OSspecific/((\w+)\.[CH])'), r'\1/POSIX/\3/\2'),
70 SOURCE_DIR = "@CMAKE_SOURCE_DIR@"
71 BINARY_DIR = "@CMAKE_BINARY_DIR@"
72 incdir = op.join(BINARY_DIR, 'include')
74 for parent, dirs, files in os.walk(BINARY_DIR):
75 for f in filter(lambda f: op.splitext(f)[1]=='.d', files):
76 f = op.join(parent, f)
77 # skip blacklisted files
78 if checkBlacklistPatterns(f):
79 continue
80 lines = []
81 # sanitize strings
82 for l in open(f, 'rt'):
83 lines += l.strip().replace(' \\', '').split()
84 # remove first entry
85 del lines[0]
86 length = len(lines)
87 # resolve wrapper entries, remove blacklisted files, perform
88 # substitutions and strip prefix
89 for i, l in enumerate(reversed(lines)):
90 i = length-i-1
91 # resolve wrapped entries
92 l = op.normpath(l)
93 # remove the wrappers
94 if l.startswith(incdir):
95 del lines[i]
96 continue
97 # remove blacklisted files
98 if checkBlacklistPatterns(l):
99 del lines[i]
100 continue
101 # strip SOURCE_DIR
102 if l.startswith(SOURCE_DIR):
103 l = op.relpath(l, SOURCE_DIR)
104 # perform substitutions
105 for p, r in substPatterns:
106 l = p.sub(r, l)
107 # write back
108 lines[i] = l
109 # remove duplicates and sort
110 src = lines[0]
111 lines = sorted(set(lines[1:]))
112 lines.insert(0, src)
113 open(op.splitext(f)[0]+'.ffd', 'wt').write(('\n'.join(lines))+'\n')
115 # ------------------------- vim: set sw=3 sts=3 et: --------------- end-of-file