ENH: Update FreeFOAM contributions to GPL v3
[freefoam.git] / data / utilities / foamNew.py.in
blob1f6c7090f67d0a41895bf0b6ff31f9c8d5504d46
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/>.
30 #-------------------------------------------------------------------------------
32 # Script
33 # freefoam-new.py.in
35 #------------------------------------------------------------------------------
37 """Usage:
38 freefoam@PY_SCRIPT_SUFFIX@ new [-h|-help]
39 freefoam@PY_SCRIPT_SUFFIX@ new source (H|C|I|IO|app) <name>
40 freefoam@PY_SCRIPT_SUFFIX@ new template (H|C|I|IO) <name>
41 freefoam@PY_SCRIPT_SUFFIX@ new cmake (lib|app)
43 Create new source and CMake files.
45 Options
46 -------
47 -d Output-directory, defaults to the current working directory.
49 -help Display this help message
51 In the following the arguments `source', `template' and `cmake' are explained:
53 `source':
54 Creates a new @PROJECT_NAME@ header (H), source (C), included implementation
55 (I), iostream operators (IO) or application (app) file. The last argument
56 specifies the class name and is used to construct the file name.
58 `template':
59 Creates a new @PROJECT_NAME@ header (H), source (C), included implementation
60 (I) or iostream operators (IO) file for a templated class. The next argument
61 specifies the class name and is used to construct the file name. All
62 following arguments are template parameters.
64 `cmake':
65 This type scans the output directory for sources and creates a suitable
66 CMakeLists.txt file. It takes an argument specifying whether the sources are
67 used to build a library (lib) or an application (app).
69 See Also
70 --------
71 wmakeToCMake - Converts simple wmake based build system to CMake.
73 """
74 import os
75 import os.path
76 import sys
77 import re
78 sys.path.insert(0, '@FOAM_PYTHON_DIR@')
79 from FreeFOAM.compat import *
81 def create_source(kind, name, outdir):
82 suffix = ''
83 if kind == 'C' or kind == 'H':
84 template = 'foamTemplate'
85 ext = '.'+kind
86 elif kind == 'I':
87 template = 'foamTemplateI'
88 ext = '.H'
89 suffix = kind
90 elif kind == 'IO':
91 template = 'foamTemplateIO'
92 ext = '.C'
93 suffix = kind
94 elif kind == 'App' or kind == 'app':
95 template = 'foamAppTemplate'
96 ext = '.C'
97 else:
98 echo('ERROR: Unknown sub-type "%s"'%kind, file=sys.stderr)
99 sys.exit(1)
100 template = os.path.join(
101 os.path.normpath('@FOAM_DATA_DIR@'),
102 'templates', template+ext)
103 outname = os.path.join(outdir, name+suffix+ext)
104 # sanity checks
105 if not os.path.isdir(outdir):
106 echo('ERROR: "%s" is not a directory'%dir, file=sys.stderr)
107 sys.exit(1)
108 if os.path.exists(outname):
109 echo("ERROR: Cannot make %s, file exists"%outname, file=sys.stderr)
110 sys.exit(1)
111 outfile = open(outname, 'wt')
112 for l in open(template):
113 outfile.write(l.replace('className', name))
114 outfile.close()
116 def create_template(kind, name, tArgs, outdir):
117 suffix = ''
118 if kind == 'C' or kind == 'H':
119 template = 'foamTemplateTemplate'
120 ext = '.'+kind
121 elif kind == 'I':
122 template = 'foamTemplateTemplateI'
123 ext = '.H'
124 suffix = kind
125 elif kind == 'IO':
126 template = 'foamTemplateTemplateIO'
127 ext = '.C'
128 suffix = kind
129 else:
130 echo('ERROR: Unknown sub-type "%s"'%kind, file=sys.stderr)
131 sys.exit(1)
132 template = os.path.join(
133 os.path.normpath('@FOAM_DATA_DIR@'),
134 'templates', template+ext)
135 outname = os.path.join(outdir, name+suffix+ext)
136 # sanity checks
137 if not os.path.isdir(outdir):
138 echo('ERROR: "%s" is not a directory'%dir, file=sys.stderr)
139 sys.exit(1)
140 if os.path.exists(outname):
141 echo("ERROR: Cannot make %s, file exists"%outname, file=sys.stderr)
142 sys.exit(1)
143 outfile = open(outname, 'wt')
144 for l in open(template):
145 l = l.replace(
146 'className', name).replace(
147 'TemplateClassArgument', 'class '+', class '.join(tArgs)).replace(
148 'TemplateArgument', ', '.join(tArgs))
149 outfile.write(l)
150 outfile.close()
152 def create_cmakelists(kind, outdir):
153 cml = os.path.join(outdir, 'CMakeLists.txt')
155 # sanity checks
156 if not os.path.isdir(outdir):
157 echo('ERROR: "%s" is not a directory'%outdir, file=sys.stderr)
158 sys.exit(1)
160 if os.path.isfile(cml):
161 echo('ERROR: "%s" already exists, exiting'%cml, file=sys.stderr)
162 sys.exit(1)
164 # all sources
165 sources = []
166 for parent, dirs, files in os.walk(outdir):
167 d = os.path.relpath(parent, outdir)
168 if d == '.':
169 d = ''
170 sources.extend(map(lambda f: os.path.join(d,f),
171 filter(lambda f: os.path.splitext(f)[1] == '.C', files)))
173 # the name of the application
174 target_name=os.path.basename(outdir)
175 if kind == 'lib':
176 target_kind = 'library'
177 else:
178 target_kind = 'executable'
180 # configure CMakeLists.txt
181 open(cml, 'wt').write(
183 cmake_minimum_required(VERSION 2.8)
184 project(%(target_name)s)
186 find_package(@PROJECT_NAME@ REQUIRED)
187 include(${FOAM_USE_FILE})
189 foam_add_%(target_kind)s(%(target_name)s
190 %(sources)s
193 target_link_libraries(%(target_name)s FOAM_finiteVolume)
195 # ------------------------- vim: set sw=2 sts=2 et: --------------- end-of-file
196 """%{
197 'target_kind': target_kind,
198 'target_name': target_name,
199 'sources': '\n '.join(sources),
203 # argument parsing
204 args = sys.argv[1:]
205 f_type = None
206 f_sub_type = None
207 class_name = None
208 f_dir = os.getcwd()
209 while len(args):
210 a = args[0]
211 if a == '-h' or a == '-help':
212 echo(__doc__)
213 sys.exit(0)
214 elif a == '-d':
215 f_dir = args[1]
216 del args[:2]
217 elif re.match(r'(?i)(class|template)', a) :
218 f_type = a.lower()
219 f_sub_type = args[1]
220 class_name = args[2]
221 del args[:3]
222 if f_type == 'class':
223 create_source(f_sub_type, class_name, f_dir)
224 else:
225 create_template(f_sub_type, class_name, args, f_dir)
226 break
227 elif a == 'cmake':
228 f_type = args[1]
229 del args[:2]
230 create_cmakelists(f_type, f_dir)
231 break
233 # ------------------------- vim: set sw=3 sts=3 et: --------------- end-of-file