| 1 |
#!/usr/bin/env python3 |
| 2 |
# encoding: utf-8 |
| 3 |
# Bálint Aradi, 2016-2021 |
| 4 |
|
| 5 |
'''General module for using Fypp as preprocessor. |
| 6 |
|
| 7 |
This module implements the general framework for the Fypp preprocessor, but does |
| 8 |
not bind it to any task generator. If you want to use it to preprocessor Fortran |
| 9 |
source files (.fpp -> .f90), use the fypp_fortran module instead. Otherwise, |
| 10 |
you can generate your own binding as ususal:: |
| 11 |
|
| 12 |
def build(bld): |
| 13 |
bld(features='fypp', source=['test.fypp']) |
| 14 |
|
| 15 |
from waflib import TaskGen |
| 16 |
@TaskGen.extension('.fypp') |
| 17 |
def process(self, node): |
| 18 |
tsk = self.create_task('fypp_preprocessor', [node], node.change_ext('.out')) |
| 19 |
|
| 20 |
The preprocessor understands the following uselib attributes: |
| 21 |
|
| 22 |
* ``includes``: Directory/directories to search for include files |
| 23 |
* ``modules``: Python module(s) to import before preprocessing starts |
| 24 |
* ``defines``: Definition(s) to apply before preprocessing starts |
| 25 |
* ``inifiles``: Python file(s) to execute before preprosessing starts |
| 26 |
|
| 27 |
The example below demonstrates this:: |
| 28 |
|
| 29 |
def build(bld): |
| 30 |
bld(features='fypp', |
| 31 |
source=['trash.fypp'], |
| 32 |
includes='include', |
| 33 |
modules=['myfypp1', 'myfypp2'], |
| 34 |
defines='TEST=1 QUIET', |
| 35 |
inifiles='fyppini.py') |
| 36 |
''' |
| 37 |
|
| 38 |
import re |
| 39 |
import os.path |
| 40 |
from waflib import Configure, Logs, Task, TaskGen, Tools, Errors |
| 41 |
try: |
| 42 |
import fypp |
| 43 |
except ImportError: |
| 44 |
fypp = None |
| 45 |
|
| 46 |
|
| 47 |
Tools.ccroot.USELIB_VARS['fypp'] = set([ 'DEFINES', 'INCLUDES', 'MODULES', |
| 48 |
'INIFILES' ]) |
| 49 |
|
| 50 |
FYPP_INCPATH_ST = '-I%s' |
| 51 |
FYPP_DEFINES_ST = '-D%s' |
| 52 |
FYPP_LINENUM_FLAG = '-n' |
| 53 |
FYPP_MODULES_ST = '-m%s' |
| 54 |
FYPP_INIFILES_ST = '-i%s' |
| 55 |
|
| 56 |
|
| 57 |
class FyppPreprocError(Errors.WafError): |
| 58 |
pass |
| 59 |
|
| 60 |
|
| 61 |
################################################################################ |
| 62 |
# Configure |
| 63 |
################################################################################ |
| 64 |
|
| 65 |
def configure(conf): |
| 66 |
fypp_check(conf) |
| 67 |
fypp_add_user_flags(conf) |
| 68 |
|
| 69 |
|
| 70 |
@Configure.conf |
| 71 |
def fypp_add_user_flags(conf): |
| 72 |
'''Import user settings for Fypp.''' |
| 73 |
conf.add_os_flags('FYPP_FLAGS', dup=False) |
| 74 |
|
| 75 |
|
| 76 |
@Configure.conf |
| 77 |
def fypp_check(conf): |
| 78 |
'''Check for Fypp.''' |
| 79 |
conf.start_msg('Checking for fypp module') |
| 80 |
if fypp is None: |
| 81 |
conf.fatal('Python module \'fypp\' could not be imported.') |
| 82 |
version = fypp.VERSION |
| 83 |
version_regexp = re.compile(r'^(?P<major>\d+)\.(?P<minor>\d+)'\ |
| 84 |
'(?:\.(?P<patch>\d+))?$') |
| 85 |
match = version_regexp.search(version) |
| 86 |
if not match: |
| 87 |
conf.fatal('cannot parse fypp version string') |
| 88 |
version = (match.group('major'), match.group('minor')) |
| 89 |
conf.env['FYPP_VERSION'] = version |
| 90 |
conf.end_msg('found (version %s.%s)' % version) |
| 91 |
|
| 92 |
|
| 93 |
################################################################################ |
| 94 |
# Build |
| 95 |
################################################################################ |
| 96 |
|
| 97 |
class fypp_preprocessor(Task.Task): |
| 98 |
|
| 99 |
def keyword(self): |
| 100 |
return 'Preprocessing' |
| 101 |
|
| 102 |
def run(self): |
| 103 |
argparser = fypp.get_option_parser() |
| 104 |
args = [FYPP_LINENUM_FLAG] |
| 105 |
args += self.env.FYPP_FLAGS |
| 106 |
args += [FYPP_DEFINES_ST % ss for ss in self.env['DEFINES']] |
| 107 |
args += [FYPP_INCPATH_ST % ss for ss in self.env['INCLUDES']] |
| 108 |
args += [FYPP_INIFILES_ST % ss for ss in self.env['INIFILES']] |
| 109 |
args += [FYPP_MODULES_ST % ss for ss in self.env['MODULES']] |
| 110 |
opts, leftover = argparser.parse_args(args) |
| 111 |
infile = self.inputs[0].abspath() |
| 112 |
outfile = self.outputs[0].abspath() |
| 113 |
if Logs.verbose: |
| 114 |
Logs.debug('runner: fypp.Fypp %r %r %r' |
| 115 |
% (args, infile, outfile)) |
| 116 |
|
| 117 |
tool = fypp.Fypp(opts) |
| 118 |
try: |
| 119 |
tool.process_file(infile, outfile) |
| 120 |
except fypp.FyppError as err: |
| 121 |
msg = ("%s [%s:%d]" |
| 122 |
% (err.msg, err.fname, err.span[0] + 1)) |
| 123 |
raise FyppPreprocError(msg) |
| 124 |
return 0 |
| 125 |
|
| 126 |
def scan(self): |
| 127 |
parser = FyppIncludeParser(self.generator.includes_nodes) |
| 128 |
nodes, names = parser.parse(self.inputs[0]) |
| 129 |
if Logs.verbose: |
| 130 |
Logs.debug('deps: deps for %r: %r; unresolved: %r' |
| 131 |
% (self.inputs, nodes, names)) |
| 132 |
return (nodes, names) |
| 133 |
|
| 134 |
|
| 135 |
TaskGen.feature('fypp')(Tools.ccroot.propagate_uselib_vars) |
| 136 |
TaskGen.feature('fypp')(Tools.ccroot.apply_incpaths) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
################################################################################ |
| 141 |
# Helper routines |
| 142 |
################################################################################ |
| 143 |
|
| 144 |
class FyppIncludeParser(object): |
| 145 |
|
| 146 |
'''Parser for include directives in files preprocessed by Fypp. |
| 147 |
|
| 148 |
It can not handle conditional includes. |
| 149 |
''' |
| 150 |
|
| 151 |
# Include file pattern, opening and closing quoute must be replaced inside. |
| 152 |
INCLUDE_PATTERN = re.compile(r'^\s*#:include\s*(["\'])(?P<incfile>.+?)\1', |
| 153 |
re.MULTILINE) |
| 154 |
|
| 155 |
|
| 156 |
def __init__(self, incpaths): |
| 157 |
'''Initializes the parser. |
| 158 |
|
| 159 |
:param quotes: Tuple containing the opening and closing quote sign. |
| 160 |
:type quotes: tuple |
| 161 |
''' |
| 162 |
# Nodes still to be processed |
| 163 |
self._waiting = [] |
| 164 |
|
| 165 |
# Files we have already processed |
| 166 |
self._processed = set() |
| 167 |
|
| 168 |
# List of dependent nodes |
| 169 |
self._dependencies = [] |
| 170 |
|
| 171 |
# List of unresolved dependencies |
| 172 |
self._unresolved = set() |
| 173 |
|
| 174 |
# Paths to consider when checking for includes |
| 175 |
self._incpaths = incpaths |
| 176 |
|
| 177 |
|
| 178 |
def parse(self, node): |
| 179 |
'''Parser the includes in a given node. |
| 180 |
|
| 181 |
:return: Tuple with two elements: list of dependent nodes and list of |
| 182 |
unresolved depencies. |
| 183 |
''' |
| 184 |
self._waiting = [ node, ] |
| 185 |
# self._waiting is eventually extended during _process() -> iterate |
| 186 |
while self._waiting: |
| 187 |
curnode = self._waiting.pop(0) |
| 188 |
self._process(curnode) |
| 189 |
return (self._dependencies, list(self._unresolved)) |
| 190 |
|
| 191 |
|
| 192 |
def _process(self, node): |
| 193 |
incfiles = self._get_include_files(node) |
| 194 |
for incfile in incfiles: |
| 195 |
if incfile in self._processed: |
| 196 |
continue |
| 197 |
self._processed.add(incfile) |
| 198 |
incnode = self._find_include_node(node, incfile) |
| 199 |
if incnode: |
| 200 |
self._dependencies.append(incnode) |
| 201 |
self._waiting.append(incnode) |
| 202 |
else: |
| 203 |
self._unresolved.add(incfile) |
| 204 |
|
| 205 |
|
| 206 |
def _get_include_files(self, node): |
| 207 |
txt = node.read() |
| 208 |
matches = self.INCLUDE_PATTERN.finditer(txt) |
| 209 |
incs = [ match.group('incfile') for match in matches ] |
| 210 |
return incs |
| 211 |
|
| 212 |
|
| 213 |
def _find_include_node(self, node, filename): |
| 214 |
for incpath in self._incpaths: |
| 215 |
incnode = incpath.find_resource(filename) |
| 216 |
if incnode: |
| 217 |
break |
| 218 |
else: |
| 219 |
incnode = node.parent.find_resource(filename) |
| 220 |
return incnode |