From f8803865c83c4d64bac6e6e5d7ef711d8ccc8fcd Mon Sep 17 00:00:00 2001
From: Chris Johns <chrisj@rtems.org>
Date: Wed, 20 Oct 2021 13:47:29 +1100
Subject: [PATCH] waf: Move the tools/BSP include path to be last

- This change lets you rebuild libbsd after it is installed
---
 waf_libbsd.py | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/waf_libbsd.py b/waf_libbsd.py
index 81818956..dd42b837 100644
--- a/waf_libbsd.py
+++ b/waf_libbsd.py
@@ -2,7 +2,7 @@
 """LibBSD build configuration to waf integration module.
 """
 
-# Copyright (c) 2015, 2020 Chris Johns <chrisj@rtems.org>. All rights reserved.
+# Copyright (c) 2015, 2021 Chris Johns <chrisj@rtems.org>. All rights reserved.
 #
 # Copyright (c) 2009, 2015 embedded brains GmbH.  All rights reserved.
 #
@@ -62,6 +62,13 @@ def _add_flags_if_not_present(current_flags, addional_flags):
         if flag not in current_flags:
             current_flags.append(flag)
 
+def _remove_bsp_include_path(bsp_include_path, current_flags):
+    # this does not handle quted strings; maybe needed
+    for bsp_path in bsp_include_path:
+        if bsp_path in current_flags:
+            current_flags = [flag for flag in current_flags if flag != bsp_path]
+    return current_flags
+
 #
 # The waf builder for libbsd.
 #
@@ -237,7 +244,7 @@ class Builder(builder.ModuleManager):
         inc_paths = sorted(include_paths)
         inc_paths.remove('build')
         inc_paths.remove('cpu')
-        includes = {}
+        includes = { 'bsp': [p[2:] for p in bld.env.IFLAGS] }
         for inc in inc_paths:
             includes[inc] = include_paths[inc]
         # cpu include paths must be the first searched
@@ -432,7 +439,7 @@ class Builder(builder.ModuleManager):
             bld.objects(target='kvmsymbols',
                         features='c',
                         cflags=cflags,
-                        includes=kvmsymbols_includes + includes['kernel'],
+                        includes=kvmsymbols_includes + includes['kernel'] + includes['bsp'],
                         source=kvmsymbols['files']['all']['default'][0])
             libbsd_use += ["kvmsymbols"]
 
@@ -487,7 +494,7 @@ class Builder(builder.ModuleManager):
                     bld.objects(target='lex_%s' % (lex['sym']),
                                 features='c',
                                 cflags=cflags,
-                                includes=lexIncludes + includes['user'],
+                                includes=lexIncludes + includes['user'] + includes['bsp'],
                                 defines=defines + lexDefines,
                                 source=lex['file'][:-2] + '.c')
                 libbsd_use += ['lex_%s' % (lex['sym'])]
@@ -527,7 +534,7 @@ class Builder(builder.ModuleManager):
                     bld.objects(target='yacc_%s' % (yaccSym),
                                 features='c',
                                 cflags=cflags,
-                                includes=yaccIncludes + includes['user'],
+                                includes=yaccIncludes + includes['user'] + includes['bsp'],
                                 defines=defines + yaccDefines,
                                 source=yaccFile[:-2] + '.c')
                 libbsd_use += ['yacc_%s' % (yaccSym)]
@@ -561,7 +568,7 @@ class Builder(builder.ModuleManager):
                             cflags=cflags + bld_cflags,
                             cxxflags=cxxflags,
                             includes=sorted(build.get('includes', [])) +
-                            includes[space],
+                            includes[space] + includes['bsp'],
                             defines=defines,
                             source=bld_sources)
                 libbsd_use += [target]
@@ -581,7 +588,7 @@ class Builder(builder.ModuleManager):
                   features='c cxx',
                   cflags=cflags,
                   cxxflags=cxxflags,
-                  includes=includes['kernel'],
+                  includes=includes['kernel'] + includes['bsp'],
                   defines=defines,
                   source=bld_sources,
                   use=libbsd_use)
@@ -658,7 +665,7 @@ class Builder(builder.ModuleManager):
                 bld.program(target='%s.exe' % (testName),
                             features='cprogram',
                             cflags=cflags,
-                            includes=includes['user'],
+                            includes=includes['user'] + includes['bsp'],
                             source=test_sources,
                             use=['bsd'],
                             lib=libs,
-- 
2.25.1

