scons: Track and reuse object nodes for a given source file.

scons gets upset if two different environments are used to set up a
particular object file. This change adds two dicts to the SourceFile
class, one for static and one for shared object files, which are keyed
off of the appropriate suffix. If a suffix hasn't been set up yet,
a new node of the appropriate type is set up and stored in the cache,
and then whatever is in the cache (new or old) is returned.

Change-Id: Ice4b4fc728b438a4d3316c3ff6667c0480d2a6d7
Reviewed-on: https://gem5-review.googlesource.com/6224
Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Maintainer: Gabe Black <gabeblack@google.com>
diff --git a/src/SConscript b/src/SConscript
index 73e9f82..8890308 100755
--- a/src/SConscript
+++ b/src/SConscript
@@ -102,6 +102,10 @@
     of those.  A source file also specifies a set of tags which
     describing arbitrary properties of the source file.'''
     __metaclass__ = SourceMeta
+
+    static_objs = {}
+    shared_objs = {}
+
     def __init__(self, source, tags=None, add_tags=None):
         if tags is None:
             tags='gem5 lib'
@@ -124,6 +128,18 @@
             if issubclass(base, SourceFile):
                 base.all.append(self)
 
+    def static(self, env):
+        key = (self.tnode, env['OBJSUFFIX'])
+        if not key in self.static_objs:
+            self.static_objs[key] = env.StaticObject(self.tnode)
+        return self.static_objs[key]
+
+    def shared(self, env):
+        key = (self.tnode, env['OBJSUFFIX'])
+        if not key in self.shared_objs:
+            self.shared_objs[key] = env.SharedObject(self.tnode)
+        return self.shared_objs[key]
+
     @property
     def filename(self):
         return str(self.tnode)
@@ -960,9 +976,6 @@
     new_env.Label = label
     new_env.Append(**kwargs)
 
-    make_static = lambda source: new_env.StaticObject(source.tnode)
-    make_shared = lambda source: new_env.SharedObject(source.tnode)
-
     lib_sources = Source.all.with_tag('gem5 lib')
 
     # Without Python, leave out all Python content from the library
@@ -974,16 +987,16 @@
     shared_objs = []
 
     for s in lib_sources.with_tag(Source.ungrouped_tag):
-        static_objs.append(make_static(s))
-        shared_objs.append(make_shared(s))
+        static_objs.append(s.static(new_env))
+        shared_objs.append(s.shared(new_env))
 
     for group in Source.source_groups:
         srcs = lib_sources.with_tag(Source.link_group_tag(group))
         if not srcs:
             continue
 
-        group_static = [ make_static(s) for s in srcs ]
-        group_shared = [ make_shared(s) for s in srcs ]
+        group_static = [ s.static(new_env) for s in srcs ]
+        group_shared = [ s.shared(new_env) for s in srcs ]
 
         # If partial linking is disabled, add these sources to the build
         # directly, and short circuit this loop.
@@ -1004,11 +1017,11 @@
         partial = env.PartialShared(target=target, source=group_shared)
         shared_objs.extend(partial)
 
-    static_date = make_static(date_source)
+    static_date = date_source.static(new_env)
     new_env.Depends(static_date, static_objs)
     static_objs.extend(static_date)
 
-    shared_date = make_shared(date_source)
+    shared_date = date_source.shared(new_env)
     new_env.Depends(shared_date, shared_objs)
     shared_objs.extend(shared_date)
 
@@ -1018,11 +1031,11 @@
     shared_lib = new_env.SharedLibrary(libname, shared_objs)
 
     # Now link a stub with main() and the static library.
-    main_objs = [ make_static(s) for s in Source.all.with_tag('main') ]
+    main_objs = [ s.static(new_env) for s in Source.all.with_tag('main') ]
 
     for test in UnitTest.all:
         test_sources = Source.all.with_tag(str(test.target))
-        test_objs = [ make_static(s) for s in test_sources ]
+        test_objs = [ s.static(new_env) for s in test_sources ]
         if test.main:
             test_objs += main_objs
         path = 'unittest/%s.%s' % (test.target, label)
@@ -1033,7 +1046,7 @@
     gtest_env.Append(CPPFLAGS=gtest_env['GTEST_CPPFLAGS'])
     for test in GTest.all:
         test_sources = Source.all.with_tag(str(test.target))
-        test_objs = [ gtest_env.StaticObject(s.tnode) for s in test_sources ]
+        test_objs = [ s.static(gtest_env) for s in test_sources ]
         gtest_env.Program(test.dir.File('%s.%s' % (test.target, label)),
                           test_objs)