@@ -203,23 +203,28 @@ def programSucceeds(config, program, args=None):
203203
204204
205205@_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+
206219def hasCompileFlag (config , flag ):
207220 """
208221 Return whether the compiler in the configuration supports a given compiler flag.
209222
210223 This is done by executing the %{cxx} substitution with the given flag and
211224 checking whether that succeeds.
212225 """
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
223228
224229
225230@_memoizeExpensiveOperation (lambda c , s : (c .substitutions , c .environment , s ))
@@ -348,10 +353,15 @@ def _getSubstitution(substitution, config):
348353def _appendToSubstitution (substitutions , key , value ):
349354 return [(k , v + " " + value ) if k == key else (k , v ) for (k , v ) in substitutions ]
350355
351-
352356def _prependToSubstitution (substitutions , key , value ):
353357 return [(k , value + " " + v ) if k == key else (k , v ) for (k , v ) in substitutions ]
354358
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+
355365
356366class ConfigAction (object ):
357367 """
@@ -427,9 +437,7 @@ def __init__(self, flag):
427437
428438 def applyTo (self , config ):
429439 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 )
433441 config .substitutions = _appendToSubstitution (
434442 config .substitutions , "%{flags}" , flag
435443 )
@@ -473,9 +481,7 @@ def __init__(self, flag):
473481
474482 def applyTo (self , config ):
475483 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 )
479485 config .substitutions = _appendToSubstitution (
480486 config .substitutions , "%{compile_flags}" , flag
481487 )
@@ -497,9 +503,7 @@ def __init__(self, flag):
497503
498504 def applyTo (self , config ):
499505 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 )
503507 config .substitutions = _appendToSubstitution (
504508 config .substitutions , "%{link_flags}" , flag
505509 )
@@ -521,9 +525,7 @@ def __init__(self, flag):
521525
522526 def applyTo (self , config ):
523527 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 )
527529 config .substitutions = _prependToSubstitution (
528530 config .substitutions , "%{link_flags}" , flag
529531 )
0 commit comments