@@ -203,23 +203,28 @@ def programSucceeds(config, program, args=None):
203
203
204
204
205
205
@_memoizeExpensiveOperation (lambda c , f : (c .substitutions , c .environment , f ))
206
+ def tryCompileFlag (config , flag ):
207
+ """
208
+ Try using the given compiler flag and return the exit code along with stdout and stderr.
209
+ """
210
+ # fmt: off
211
+ with _makeConfigTest (config ) as test :
212
+ out , err , exitCode , timeoutInfo , _ = _executeWithFakeConfig (test , [
213
+ "%{{cxx}} -xc++ {} -Werror -fsyntax-only %{{flags}} %{{compile_flags}} {}" .format (os .devnull , flag )
214
+ ])
215
+ return exitCode , out , err
216
+ # fmt: on
217
+
218
+
206
219
def hasCompileFlag (config , flag ):
207
220
"""
208
221
Return whether the compiler in the configuration supports a given compiler flag.
209
222
210
223
This is done by executing the %{cxx} substitution with the given flag and
211
224
checking whether that succeeds.
212
225
"""
213
- with _makeConfigTest (config ) as test :
214
- out , err , exitCode , timeoutInfo , _ = _executeWithFakeConfig (
215
- test ,
216
- [
217
- "%{{cxx}} -xc++ {} -Werror -fsyntax-only %{{flags}} %{{compile_flags}} {}" .format (
218
- os .devnull , flag
219
- )
220
- ],
221
- )
222
- return exitCode == 0
226
+ (exitCode , _ , _ ) = tryCompileFlag (config , flag )
227
+ return exitCode == 0
223
228
224
229
225
230
@_memoizeExpensiveOperation (lambda c , s : (c .substitutions , c .environment , s ))
@@ -348,10 +353,15 @@ def _getSubstitution(substitution, config):
348
353
def _appendToSubstitution (substitutions , key , value ):
349
354
return [(k , v + " " + value ) if k == key else (k , v ) for (k , v ) in substitutions ]
350
355
351
-
352
356
def _prependToSubstitution (substitutions , key , value ):
353
357
return [(k , value + " " + v ) if k == key else (k , v ) for (k , v ) in substitutions ]
354
358
359
+ def _ensureFlagIsSupported (config , flag ):
360
+ (exitCode , out , err ) = tryCompileFlag (config , flag )
361
+ assert (
362
+ exitCode == 0
363
+ ), f"Trying to enable compiler flag { flag } , which is not supported. stdout was:\n { out } \n \n stderr was:\n { err } "
364
+
355
365
356
366
class ConfigAction (object ):
357
367
"""
@@ -427,9 +437,7 @@ def __init__(self, flag):
427
437
428
438
def applyTo (self , config ):
429
439
flag = self ._getFlag (config )
430
- assert hasCompileFlag (
431
- config , flag
432
- ), "Trying to enable flag {}, which is not supported" .format (flag )
440
+ _ensureFlagIsSupported (config , flag )
433
441
config .substitutions = _appendToSubstitution (
434
442
config .substitutions , "%{flags}" , flag
435
443
)
@@ -473,9 +481,7 @@ def __init__(self, flag):
473
481
474
482
def applyTo (self , config ):
475
483
flag = self ._getFlag (config )
476
- assert hasCompileFlag (
477
- config , flag
478
- ), "Trying to enable compile flag {}, which is not supported" .format (flag )
484
+ _ensureFlagIsSupported (config , flag )
479
485
config .substitutions = _appendToSubstitution (
480
486
config .substitutions , "%{compile_flags}" , flag
481
487
)
@@ -497,9 +503,7 @@ def __init__(self, flag):
497
503
498
504
def applyTo (self , config ):
499
505
flag = self ._getFlag (config )
500
- assert hasCompileFlag (
501
- config , flag
502
- ), "Trying to enable link flag {}, which is not supported" .format (flag )
506
+ _ensureFlagIsSupported (config , flag )
503
507
config .substitutions = _appendToSubstitution (
504
508
config .substitutions , "%{link_flags}" , flag
505
509
)
@@ -521,9 +525,7 @@ def __init__(self, flag):
521
525
522
526
def applyTo (self , config ):
523
527
flag = self ._getFlag (config )
524
- assert hasCompileFlag (
525
- config , flag
526
- ), "Trying to enable link flag {}, which is not supported" .format (flag )
528
+ _ensureFlagIsSupported (config , flag )
527
529
config .substitutions = _prependToSubstitution (
528
530
config .substitutions , "%{link_flags}" , flag
529
531
)
0 commit comments