Skip to content

Commit 17feef3

Browse files
authored
Add ccflags to config and test (#141)
1 parent aa5920c commit 17feef3

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

pygccxml/parser/config.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(
4949
define_symbols=None,
5050
undefine_symbols=None,
5151
cflags="",
52+
ccflags="",
5253
compiler=None,
5354
xml_generator=None,
5455
keep_xml=False,
@@ -73,6 +74,8 @@ def __init__(
7374

7475
self.__cflags = cflags
7576

77+
self.__ccflags = ccflags
78+
7679
self.__compiler = compiler
7780

7881
self.__xml_generator = xml_generator
@@ -197,6 +200,26 @@ def cflags(self, val):
197200
def append_cflags(self, val):
198201
self.__cflags = self.__cflags + ' ' + val
199202

203+
@property
204+
def ccflags(self):
205+
"""
206+
additional cross-compatible flags to pass directly
207+
to internal simulated compiler.
208+
Castxml removes any definitions of its
209+
pre-defined macros (e.g. -fopenmp). To propagate these down to the
210+
compiler, these flags must also be passed here.
211+
See `cc-opt` on castxml's documentation page:
212+
https://github.com/CastXML/CastXML/blob/master/doc/manual/castxml.1.rst
213+
"""
214+
return self.__ccflags
215+
216+
@ccflags.setter
217+
def ccflags(self, val):
218+
self.__ccflags = val
219+
220+
def append_ccflags(self, val):
221+
self.__ccflags = self.__ccflags + ' ' + val
222+
200223
def __ensure_dir_exists(self, dir_path, meaning):
201224
if os.path.isdir(dir_path):
202225
return
@@ -245,6 +268,7 @@ def __init__(
245268
start_with_declarations=None,
246269
ignore_gccxml_output=False,
247270
cflags="",
271+
ccflags="",
248272
compiler=None,
249273
xml_generator=None,
250274
keep_xml=False,
@@ -258,7 +282,7 @@ def __init__(
258282
include_paths=include_paths,
259283
define_symbols=define_symbols,
260284
undefine_symbols=undefine_symbols,
261-
cflags=cflags,
285+
ccflags=ccflags,
262286
compiler=compiler,
263287
xml_generator=xml_generator,
264288
keep_xml=keep_xml,
@@ -398,6 +422,8 @@ def load_xml_generator_configuration(configuration, **defaults):
398422
cfg.keep_xml = value
399423
elif name == 'cflags':
400424
cfg.cflags = value
425+
elif name == 'ccflags':
426+
cfg.ccflags = value
401427
elif name == 'flags':
402428
cfg.flags = value
403429
elif name == 'compiler_path':

pygccxml/parser/source_reader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def __create_command_line_castxml(self, source_file, xmlfile):
137137
if self.__config.compiler == 'msvc9':
138138
cmd.append('"-D_HAS_TR1=0"')
139139
else:
140-
141140
# On mac or linux, use gcc or clang (the flag is the same)
142141
cmd.append('--castxml-cc-gnu ')
143142

@@ -146,9 +145,14 @@ def __create_command_line_castxml(self, source_file, xmlfile):
146145
else:
147146
std_flag = ' ' + self.__cxx_std.stdcxx + ' '
148147

148+
ccflags = self.__config.ccflags
149149
if std_flag:
150+
ccflags += std_flag
151+
152+
if ccflags:
153+
all_cc_opts = self.__config.compiler_path + ' ' + ccflags
150154
cmd.append(
151-
'"(" ' + self.__config.compiler_path + std_flag + '")"')
155+
'"(" ' + all_cc_opts + ' ")"')
152156
else:
153157
cmd.append(self.__config.compiler_path)
154158

unittests/data/test_ccflags.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Will only be defined when -fopenmp flag is included
2+
// in ccflags of corresponding config object.
3+
#ifdef _OPENMP
4+
namespace ccflags_test_namespace{}
5+
#endif

unittests/test_all.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
from . import test_deprecation
8888
from . import test_warn_missing_include_dirs
8989
from . import test_overrides
90+
from . import test_ccflags
9091

9192
testers = [
9293
pep8_tester,
@@ -171,6 +172,9 @@
171172
# Known to fail under windows with VS2013
172173
testers.append(example_tester)
173174

175+
# Awaiting Windows CI machine
176+
testers.append(test_ccflags)
177+
174178
if 'posix' in os.name:
175179
testers.append(copy_constructor_tester)
176180

unittests/test_ccflags.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2014-2021 Insight Software Consortium.
2+
# Copyright 2004-2009 Roman Yakovenko.
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# See http://www.boost.org/LICENSE_1_0.txt
5+
6+
import unittest
7+
8+
from . import parser_test_case
9+
10+
from pygccxml import parser
11+
from pygccxml import declarations
12+
13+
14+
class Test(parser_test_case.parser_test_case_t):
15+
global_ns = None
16+
17+
def __init__(self, *args):
18+
parser_test_case.parser_test_case_t.__init__(self, *args)
19+
self.header = "test_ccflags.hpp"
20+
self.global_ns = None
21+
self.config.castxml_epic_version = 1
22+
self.config.append_cflags("-fopenmp")
23+
24+
def _parse_src(self):
25+
decls = parser.parse([self.header], self.config)
26+
Test.global_ns = declarations.get_global_namespace(decls)
27+
Test.xml_generator_from_xml_file = (
28+
self.config.xml_generator_from_xml_file
29+
)
30+
self.xml_generator_from_xml_file = Test.xml_generator_from_xml_file
31+
32+
self.global_ns = Test.global_ns
33+
34+
def _add_ccflags(self):
35+
if "clang++" in self.config.compiler_path:
36+
self.config.append_ccflags("-Xpreprocessor")
37+
38+
self.config.append_ccflags("-fopenmp")
39+
40+
def test(self):
41+
# First check that macro is not defined.
42+
self._parse_src()
43+
namespace_names = [
44+
n.name for n in self.global_ns.namespaces(allow_empty=True)
45+
]
46+
self.assertNotIn("ccflags_test_namespace", namespace_names)
47+
48+
# Next check that macro is defined when passed directly as ccflag
49+
self._add_ccflags()
50+
self._parse_src()
51+
namespace_names = [n.name for n in self.global_ns.namespaces()]
52+
self.assertIn("ccflags_test_namespace", namespace_names)
53+
54+
55+
def create_suite():
56+
suite = unittest.TestSuite()
57+
suite.addTest(unittest.makeSuite(Test))
58+
return suite
59+
60+
61+
def run_suite():
62+
unittest.TextTestRunner(verbosity=2).run(create_suite())
63+
64+
65+
if __name__ == "__main__":
66+
run_suite()

0 commit comments

Comments
 (0)