From c7877fbf66cf5a8e9c8334c1bbe36b0754c899cf Mon Sep 17 00:00:00 2001 From: stevenknight Date: Fri, 12 Oct 2001 19:31:00 +0000 Subject: [PATCH] Add tests and support for and . git-svn-id: http://scons.tigris.org/svn/scons/trunk@97 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- src/engine/SCons/Builder.py | 12 ++++---- src/engine/SCons/BuilderTests.py | 13 +++++++++ src/engine/SCons/Defaults.py | 13 +++++---- src/engine/SCons/Environment.py | 17 ++++------- src/engine/SCons/EnvironmentTests.py | 2 ++ src/engine/SCons/Node/NodeTests.py | 4 +++ src/engine/SCons/Node/__init__.py | 10 +++++-- test/CC.py | 42 ++++++++++++++++++++++++++-- test/CCFLAGS.py | 14 +++++----- 9 files changed, 91 insertions(+), 36 deletions(-) diff --git a/src/engine/SCons/Builder.py b/src/engine/SCons/Builder.py index ac24e7f4..dc2566f8 100644 --- a/src/engine/SCons/Builder.py +++ b/src/engine/SCons/Builder.py @@ -87,7 +87,6 @@ class BuilderBase: f = f + suf ret.append(f) return ret - tlist = scons_str2nodes(adjustixes(target, self.prefix, self.suffix), self.node_factory) @@ -223,10 +222,9 @@ class CommandAction(ActionBase): s = [s] loc['sources'] = PathList(map(os.path.normpath, s)) - try: - glob = self.env.Dictionary() - except: - glob = {} + glob = {} + if kw.has_key('env'): + glob = kw['env'] cmd = scons_subst(self.command, loc, glob) if print_actions: @@ -238,10 +236,10 @@ class CommandAction(ActionBase): # Child process. args = string.split(cmd) try: - ENV = kw['ENV'] + ENV = kw['env']['ENV'] except: import SCons.Defaults - ENV = SCons.Defaults.ENV + ENV = SCons.Defaults.ConstructionEnvironment['ENV'] os.execvpe(args[0], args, ENV) else: # Parent process. diff --git a/src/engine/SCons/BuilderTests.py b/src/engine/SCons/BuilderTests.py index 46e926c3..03528946 100644 --- a/src/engine/SCons/BuilderTests.py +++ b/src/engine/SCons/BuilderTests.py @@ -42,6 +42,11 @@ test = TestCmd.TestCmd(workdir = '') test.write('act.py', """import os, string, sys f = open(sys.argv[1], 'w') f.write("act.py: " + string.join(sys.argv[2:]) + "\\n") +try: + if sys.argv[3]: + f.write("act.py: " + os.environ[sys.argv[3]] + "\\n") +except: + pass f.close() sys.exit(0) """) @@ -147,6 +152,14 @@ class BuilderTestCase(unittest.TestCase): c = test.read(outfile, 'r') assert c == "act.py: three four\n", c + cmd5 = "python %s %s $target XYZZY" % (act_py, outfile) + + builder = SCons.Builder.Builder(action = cmd5) + r = builder.execute(target = 'out5', env = {'ENV' : {'XYZZY' : 'xyzzy'}}) + assert r == 0 + c = test.read(outfile, 'r') + assert c == "act.py: out5 XYZZY\nact.py: xyzzy\n", c + def function1(kw): open(kw['out'], 'w').write("function1\n") return 1 diff --git a/src/engine/SCons/Defaults.py b/src/engine/SCons/Defaults.py index 230f432c..af08fc28 100644 --- a/src/engine/SCons/Defaults.py +++ b/src/engine/SCons/Defaults.py @@ -38,12 +38,12 @@ import SCons.Builder Object = SCons.Builder.Builder(name = 'Object', - action = 'cc -c -o $target $sources', + action = '$CC $CCFLAGS -c -o $target $sources', src_suffix='.c', suffix='.o') Program = SCons.Builder.Builder(name = 'Program', - action = 'cc -o $target $sources', + action = '$CC -o $target $sources', builders = [ Object ]) Library = SCons.Builder.Builder(name = 'Library', @@ -52,6 +52,9 @@ Library = SCons.Builder.Builder(name = 'Library', suffix = '.a', builders = [ Object ]) -Builders = [Object, Program, Library] - -ENV = { 'PATH' : '/usr/local/bin:/bin:/usr/bin' } +ConstructionEnvironment = { + 'CC' : 'cc', + 'CCFLAGS' : '', + 'BUILDERS' : [Object, Program, Library], + 'ENV' : { 'PATH' : '/usr/local/bin:/bin:/usr/bin' }, +} diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 468c9b4f..dcac7160 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -72,17 +72,10 @@ class Environment: """ def __init__(self, **kw): - self._dict = {} - if kw.has_key('BUILDERS'): - builders = kw['BUILDERS'] - if not type(builders) is types.ListType: - kw['BUILDERS'] = [builders] - else: - import SCons.Defaults - kw['BUILDERS'] = SCons.Defaults.Builders[:] - if not kw.has_key('ENV'): - import SCons.Defaults - kw['ENV'] = SCons.Defaults.ENV.copy() + import SCons.Defaults + self._dict = copy.deepcopy(SCons.Defaults.ConstructionEnvironment) + if kw.has_key('BUILDERS') and type(kw['BUILDERS']) != type([]): + kw['BUILDERS'] = [kw['BUILDERS']] self._dict.update(copy.deepcopy(kw)) class BuilderWrapper: @@ -107,7 +100,7 @@ class Environment: kw['env'] = self apply(self.builder.execute, (), kw) - for b in kw['BUILDERS']: + for b in self._dict['BUILDERS']: setattr(self, b.name, BuilderWrapper(self, b)) diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 07e84a4e..66d64a05 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -117,6 +117,8 @@ class EnvironmentTestCase(unittest.TestCase): assert xxx == 'x' assert zzz == 'z' assert env.Dictionary().has_key('BUILDERS') + assert env.Dictionary().has_key('CC') + assert env.Dictionary().has_key('CCFLAGS') assert env.Dictionary().has_key('ENV') def test_ENV(self): diff --git a/src/engine/SCons/Node/NodeTests.py b/src/engine/SCons/Node/NodeTests.py index febb95ac..017b7f28 100644 --- a/src/engine/SCons/Node/NodeTests.py +++ b/src/engine/SCons/Node/NodeTests.py @@ -132,6 +132,8 @@ class NodeTestCase(unittest.TestCase): assert node.depends == ['one'] node.add_dependency(['two', 'three']) assert node.depends == ['one', 'two', 'three'] + node.add_dependency(['three', 'four', 'one']) + assert node.depends == ['one', 'two', 'three', 'four'] def test_add_source(self): """Test adding sources to a Node's list. @@ -146,6 +148,8 @@ class NodeTestCase(unittest.TestCase): assert node.sources == ['one'] node.add_source(['two', 'three']) assert node.sources == ['one', 'two', 'three'] + node.add_source(['three', 'four', 'one']) + assert node.sources == ['one', 'two', 'three', 'four'] def test_children(self): """Test fetching the "children" of a Node. diff --git a/src/engine/SCons/Node/__init__.py b/src/engine/SCons/Node/__init__.py index c0346cc5..8334d2d7 100644 --- a/src/engine/SCons/Node/__init__.py +++ b/src/engine/SCons/Node/__init__.py @@ -59,7 +59,7 @@ class Node: if not hasattr(self, "builder"): return None sources_str = string.join(map(lambda x: str(x), self.sources)) - stat = self.builder.execute(ENV = self.env.Dictionary('ENV'), + stat = self.builder.execute(env = self.env.Dictionary(), target = str(self), source = sources_str) if stat != 0: raise BuildError(node = self, stat = stat) @@ -84,13 +84,17 @@ class Node: """Adds dependencies. The depends argument must be a list.""" if type(depend) is not type([]): raise TypeError("depend must be a list") - self.depends.extend(depend) + depend = filter(lambda x, d=self.depends: x not in d, depend) + if len(depend): + self.depends.extend(depend) def add_source(self, source): """Adds sources. The source argument must be a list.""" if type(source) is not type([]): raise TypeError("source must be a list") - self.sources.extend(source) + source = filter(lambda x, s=self.sources: x not in s, source) + if len(source): + self.sources.extend(source) def children(self): return self.sources + self.depends diff --git a/test/CC.py b/test/CC.py index dec523df..ce18047a 100644 --- a/test/CC.py +++ b/test/CC.py @@ -24,15 +24,53 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import os import TestSCons test = TestSCons.TestSCons() -test.pass_test() #XXX Short-circuit until this is implemented. +test.write("ccwrapper.py", +"""import os +import string +import sys +open('%s', 'w').write("ccwrapper.py\\n") +os.system(string.join(["cc"] + sys.argv[1:], " ")) +""" % test.workpath('ccwrapper.out')) test.write('SConstruct', """ +foo = Environment() +bar = Environment(CC = 'python ccwrapper.py') +foo.Program(target = 'foo', source = 'foo.c') +bar.Program(target = 'bar', source = 'bar.c') """) -test.run(arguments = '.') +test.write('foo.c', """ +int +main(int argc, char *argv[]) +{ + argv[argc++] = "--"; + printf("foo.c\n"); + exit (0); +} +""") + +test.write('bar.c', """ +int +main(int argc, char *argv[]) +{ + argv[argc++] = "--"; + printf("foo.c\n"); + exit (0); +} +""") + + +test.run(arguments = 'foo') + +test.fail_test(os.path.exists(test.workpath('ccwrapper.out'))) + +test.run(arguments = 'bar') + +test.fail_test(test.read('ccwrapper.out') != "ccwrapper.py\n") test.pass_test() diff --git a/test/CCFLAGS.py b/test/CCFLAGS.py index 12c93afb..cc6ad737 100644 --- a/test/CCFLAGS.py +++ b/test/CCFLAGS.py @@ -28,13 +28,13 @@ import TestSCons test = TestSCons.TestSCons() -test.pass_test() #XXX Short-circuit until this is implemented. - test.write('SConstruct', """ foo = Environment(CCFLAGS = '-DFOO') bar = Environment(CCFLAGS = '-DBAR') -foo.Program(target = 'progfoo', source = 'prog.c') -bar.Program(target = 'progbar', source = 'prog.c') +foo.Object(target = 'foo.o', source = 'prog.c') +bar.Object(target = 'bar.o', source = 'prog.c') +foo.Program(target = 'foo', source = 'foo.o') +bar.Program(target = 'bar', source = 'bar.o') """) test.write('prog.c', """ @@ -53,9 +53,9 @@ main(int argc, char *argv[]) """) -test.run(arguments = 'progfoo progbar') +test.run(arguments = 'foo bar') -test.run(program = test.workpath('progfoo'), stdout = "prog.c: FOO\n") -test.run(program = test.workpath('progbar'), stdout = "prog.c: BAR\n") +test.run(program = test.workpath('foo'), stdout = "prog.c: FOO\n") +test.run(program = test.workpath('bar'), stdout = "prog.c: BAR\n") test.pass_test() -- 2.26.2