build: Add -isystem and set specialized flags as a last configuration step

This change prevents global flags from interacting with library/feature
detection scripts.

Change-Id: Id4d1d3f6cba6672ef04309b881bcff36a106f236
Refs: #4296
diff --git a/.waf-tools/default-compiler-flags.py b/.waf-tools/default-compiler-flags.py
index c946d1f..1521d2a 100644
--- a/.waf-tools/default-compiler-flags.py
+++ b/.waf-tools/default-compiler-flags.py
@@ -17,15 +17,15 @@
         if ccver < (4, 8, 2):
             errmsg = ('The version of gcc you are using is too old.\n'
                       'The minimum supported gcc version is 4.8.2.')
-        flags = GccFlags()
+        conf.flags = GccFlags()
     elif cxx == 'clang':
         if ccver < (3, 4, 0):
             errmsg = ('The version of clang you are using is too old.\n'
                       'The minimum supported clang version is 3.4.0.')
-        flags = ClangFlags()
+        conf.flags = ClangFlags()
     else:
         warnmsg = 'Note: %s compiler is unsupported' % cxx
-        flags = CompilerFlags()
+        conf.flags = CompilerFlags()
 
     if errmsg:
         conf.end_msg('.'.join(conf.env['CC_VERSION']), color='RED')
@@ -36,29 +36,31 @@
     else:
         conf.end_msg('.'.join(conf.env['CC_VERSION']))
 
-    areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
+    conf.areCustomCxxflagsPresent = (len(conf.env.CXXFLAGS) > 0)
 
     # General flags are always applied (e.g., selecting C++11 mode)
-    generalFlags = flags.getGeneralFlags(conf)
+    generalFlags = conf.flags.getGeneralFlags(conf)
     conf.add_supported_cxxflags(generalFlags['CXXFLAGS'])
     conf.add_supported_linkflags(generalFlags['LINKFLAGS'])
     conf.env.DEFINES += generalFlags['DEFINES']
 
+@Configure.conf
+def check_compiler_flags(conf):
     # Debug or optimized CXXFLAGS and LINKFLAGS are applied only if the
     # corresponding environment variables are not set.
     # DEFINES are always applied.
     if conf.options.debug:
-        extraFlags = flags.getDebugFlags(conf)
-        if areCustomCxxflagsPresent:
+        extraFlags = conf.flags.getDebugFlags(conf)
+        if conf.areCustomCxxflagsPresent:
             missingFlags = [x for x in extraFlags['CXXFLAGS'] if x not in conf.env.CXXFLAGS]
             if len(missingFlags) > 0:
                 Logs.warn("Selected debug mode, but CXXFLAGS is set to a custom value '%s'"
                           % " ".join(conf.env.CXXFLAGS))
                 Logs.warn("Default flags '%s' are not activated" % " ".join(missingFlags))
     else:
-        extraFlags = flags.getOptimizedFlags(conf)
+        extraFlags = conf.flags.getOptimizedFlags(conf)
 
-    if not areCustomCxxflagsPresent:
+    if not conf.areCustomCxxflagsPresent:
         conf.add_supported_cxxflags(extraFlags['CXXFLAGS'])
         conf.add_supported_linkflags(extraFlags['LINKFLAGS'])
 
@@ -75,9 +77,10 @@
     self.start_msg('Checking supported CXXFLAGS')
 
     supportedFlags = []
-    for flag in cxxflags:
-        if self.check_cxx(cxxflags=['-Werror', flag], mandatory=False):
-            supportedFlags += [flag]
+    for flags in cxxflags:
+        flags = Utils.to_list(flags)
+        if self.check_cxx(cxxflags=['-Werror'] + flags, mandatory=False):
+            supportedFlags += flags
 
     self.end_msg(' '.join(supportedFlags))
     self.env.prepend_value('CXXFLAGS', supportedFlags)
@@ -93,9 +96,10 @@
     self.start_msg('Checking supported LINKFLAGS')
 
     supportedFlags = []
-    for flag in linkflags:
-        if self.check_cxx(linkflags=['-Werror', flag], mandatory=False):
-            supportedFlags += [flag]
+    for flags in linkflags:
+        flags = Utils.to_list(flags)
+        if self.check_cxx(linkflags=['-Werror'] + flags, mandatory=False):
+            supportedFlags += flags
 
     self.end_msg(' '.join(supportedFlags))
     self.env.prepend_value('LINKFLAGS', supportedFlags)
@@ -171,9 +175,10 @@
 class ClangFlags(GccBasicFlags):
     def getGeneralFlags(self, conf):
         flags = super(ClangFlags, self).getGeneralFlags(conf)
-        if Utils.unversioned_sys_platform() == 'darwin':
-            flags['CXXFLAGS'] += ['-stdlib=libc++']
-            flags['LINKFLAGS'] += ['-stdlib=libc++']
+        version = tuple(int(i) for i in conf.env['CC_VERSION'])
+        if Utils.unversioned_sys_platform() == 'darwin' and version >= (9, 0, 0): # Bug #4296
+            flags['CXXFLAGS'] += [['-isystem', '/usr/local/include'], # for Homebrew
+                                  ['-isystem', '/opt/local/include']] # for MacPorts
         return flags
 
     def getDebugFlags(self, conf):
diff --git a/wscript b/wscript
index e0fee20..873b3c4 100644
--- a/wscript
+++ b/wscript
@@ -150,6 +150,8 @@
         conf.env['INCLUDES_CUSTOM_LOGGER'] = [conf.options.with_custom_logger]
         conf.env['HAVE_CUSTOM_LOGGER'] = 1
 
+    conf.check_compiler_flags()
+
     conf.load('coverage')
 
     conf.load('sanitizers')