From 3cc2054726843bc99b8efe37964db7d94aeec1e7 Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Thu, 5 Sep 2002 15:33:09 +0000 Subject: Refactor SCons.Util.Detect() into an Environment method. --- src/engine/SCons/Environment.py | 17 +++++++++++++++++ src/engine/SCons/EnvironmentTests.py | 30 ++++++++++++++++++++++++++++++ src/engine/SCons/Tool/ToolTests.py | 17 ++++++++++++++--- src/engine/SCons/Tool/ar.py | 4 ++-- src/engine/SCons/Tool/dvipdf.py | 2 +- src/engine/SCons/Tool/dvips.py | 2 +- src/engine/SCons/Tool/g++.py | 4 ++-- src/engine/SCons/Tool/g77.py | 4 ++-- src/engine/SCons/Tool/gas.py | 4 ++-- src/engine/SCons/Tool/gcc.py | 4 ++-- src/engine/SCons/Tool/gnulink.py | 4 ++-- src/engine/SCons/Tool/icc.py | 2 +- src/engine/SCons/Tool/ifl.py | 2 +- src/engine/SCons/Tool/ilink.py | 2 +- src/engine/SCons/Tool/latex.py | 2 +- src/engine/SCons/Tool/lex.py | 2 +- src/engine/SCons/Tool/lib.py | 2 +- src/engine/SCons/Tool/masm.py | 2 +- src/engine/SCons/Tool/mslink.py | 2 +- src/engine/SCons/Tool/msvc.py | 2 +- src/engine/SCons/Tool/nasm.py | 2 +- src/engine/SCons/Tool/pdflatex.py | 2 +- src/engine/SCons/Tool/pdftex.py | 2 +- src/engine/SCons/Tool/tar.py | 4 ++-- src/engine/SCons/Tool/tex.py | 2 +- src/engine/SCons/Tool/yacc.py | 2 +- src/engine/SCons/Util.py | 14 -------------- 27 files changed, 91 insertions(+), 47 deletions(-) diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 6566427..cd7aced 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -440,6 +440,23 @@ class Environment: except KeyError: return None + def Detect(self, progs): + """Return the first available program in progs. + """ + path = None + pathext = None + if self.has_key('ENV'): + if self['ENV'].has_key('PATH'): + path = self['ENV']['PATH'] + if self['ENV'].has_key('PATHEXT'): + pathext = self['ENV']['PATHEXT'] + if not SCons.Util.is_List(progs): + progs = [ progs ] + for prog in progs: + path = SCons.Util.WhereIs(prog, path, pathext) + if path: return prog + return None + class VarInterpolator: def __init__(self, dest, src, prefix, suffix): self.dest = dest diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 8154990..21d241d 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -23,8 +23,10 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import os import string import sys +import TestCmd import unittest from SCons.Environment import * @@ -580,7 +582,35 @@ class EnvironmentTestCase(unittest.TestCase): assert dict['_CPPINCFLAGS'][17] == '$)', \ dict['_CPPINCFLAGS'][17] + def test_Detect(self): + """Test Detect()ing tools""" + test = TestCmd.TestCmd(workdir = '') + test.subdir('sub1', 'sub2') + test.write(['sub1', 'xxx.exe'], "sub1/xxx.exe\n") + test.write(['sub2', 'xxx.exe'], "sub2/xxx.exe\n") + sub1 = test.workpath('sub1') + sub2 = test.workpath('sub2') + env = Environment(ENV = { 'PATH' : [sub1, sub2] }) + x = env.Detect('xxx.exe') + assert x is None, x + + sub2_xxx_exe = test.workpath('sub2', 'xxx.exe') + os.chmod(sub2_xxx_exe, 0755) + + env = Environment(ENV = { 'PATH' : [sub1, sub2] }) + x = env.Detect('xxx.exe') + assert x == 'xxx.exe' + + sub1_xxx_exe = test.workpath('sub1', 'xxx.exe') + os.chmod(sub1_xxx_exe, 0755) + + x = env.Detect('xxx.exe') + assert x == 'xxx.exe' + + env = Environment(ENV = { 'PATH' : [] }) + x = env.Detect('xxx.exe') + assert x is None, x def test_platform(self): """Test specifying a platform callable when instantiating.""" diff --git a/src/engine/SCons/Tool/ToolTests.py b/src/engine/SCons/Tool/ToolTests.py index cfd84b3..23901f7 100644 --- a/src/engine/SCons/Tool/ToolTests.py +++ b/src/engine/SCons/Tool/ToolTests.py @@ -32,10 +32,21 @@ import SCons.Tool class ToolTestCase(unittest.TestCase): def test_Tool(self): """Test the Tool() function""" - # FIXME - this might fail, since there might be no C++ compiler on the system. - # How do we handle this? + class Environment: + def __init__(self): + self.dict = {} + def Detect(self, progs): + if not SCons.Util.is_List(progs): + progs = [ progs ] + return progs[0] + def __getitem__(self, key): + return self.dict[key] + def __setitem__(self, key, val): + self.dict[key] = val + env = Environment() + env['BUILDERS'] = {} + env['ENV'] = {} t = SCons.Tool.Tool('g++') - env= { 'BUILDERS' : {}, 'ENV' : {} } t(env, 'foo') assert (env['CXX'] == 'c++' or env['CXX'] == 'g++'), env['CXX'] assert env['CXXFLAGS'] == '$CCFLAGS', env['CXXFLAGS'] diff --git a/src/engine/SCons/Tool/ar.py b/src/engine/SCons/Tool/ar.py index 0535b23..5eda938 100644 --- a/src/engine/SCons/Tool/ar.py +++ b/src/engine/SCons/Tool/ar.py @@ -44,7 +44,7 @@ def generate(env, platform): arcom = '$AR $ARFLAGS $TARGET $SOURCES' ranlib = 'ranlib' - if SCons.Util.Detect([ranlib], env): + if env.Detect(ranlib): arcom = arcom + '\n$RANLIB $RANLIBFLAGS $TARGET' env['AR'] = 'ar' @@ -57,4 +57,4 @@ def generate(env, platform): env['SHLINKCOM'] = '$SHLINK $SHLINKFLAGS -o $TARGET $SOURCES $_LIBDIRFLAGS $_LIBFLAGS' def exists(env): - return SCons.Util.Detect(['ar'], env) + return env.Detect('ar') diff --git a/src/engine/SCons/Tool/dvipdf.py b/src/engine/SCons/Tool/dvipdf.py index c99c333..e9b3839 100644 --- a/src/engine/SCons/Tool/dvipdf.py +++ b/src/engine/SCons/Tool/dvipdf.py @@ -49,4 +49,4 @@ def generate(env, platform): env['PDFCOM'] = '$DVIPDF $DVIPDFFLAGS $SOURCES $TARGET' def exists(env): - return SCons.Util.Detect(['dvipdf'], env) + return env.Detect('dvipdf') diff --git a/src/engine/SCons/Tool/dvips.py b/src/engine/SCons/Tool/dvips.py index dc8ad0d..91ad550 100644 --- a/src/engine/SCons/Tool/dvips.py +++ b/src/engine/SCons/Tool/dvips.py @@ -51,4 +51,4 @@ def generate(env, platform): env['PSCOM'] = '$DVIPS $DVIPSFLAGS -o $TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(['dvips'], env) + return env.Detect('dvips') diff --git a/src/engine/SCons/Tool/g++.py b/src/engine/SCons/Tool/g++.py index 9dba2c2..d2274a5 100644 --- a/src/engine/SCons/Tool/g++.py +++ b/src/engine/SCons/Tool/g++.py @@ -53,7 +53,7 @@ def generate(env, platform): static_obj.add_action(suffix, SCons.Defaults.CXXAction) shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction) - env['CXX'] = SCons.Util.Detect(compilers, env) or 'c++' + env['CXX'] = env.Detect(compilers) or 'c++' env['CXXFLAGS'] = '$CCFLAGS' env['CXXCOM'] = '$CXX $CXXFLAGS $CPPFLAGS $_CPPINCFLAGS -c -o $TARGET $SOURCES' env['SHCXX'] = '$CXX' @@ -65,4 +65,4 @@ def generate(env, platform): env['CXXFILESUFFIX'] = '.cc' def exists(env): - return SCons.Util.Detect(compilers, env) + return env.Detect(compilers) diff --git a/src/engine/SCons/Tool/g77.py b/src/engine/SCons/Tool/g77.py index c3ddcbf..8075bd9 100644 --- a/src/engine/SCons/Tool/g77.py +++ b/src/engine/SCons/Tool/g77.py @@ -60,7 +60,7 @@ def generate(env, platform): static_obj.add_action(suffix, SCons.Defaults.F77PPAction) shared_obj.add_action(suffix, SCons.Defaults.ShF77PPAction) - env['F77'] = SCons.Util.Detect(compilers, env) or 'g77' + env['F77'] = env.Detect(compilers) or 'g77' env['F77FLAGS'] = '' env['F77COM'] = '$F77 $F77FLAGS $_F77INCFLAGS -c -o $TARGET $SOURCES' env['F77PPCOM'] = '$F77 $F77FLAGS $CPPFLAGS $_F77INCFLAGS -c -o $TARGET $SOURCES' @@ -70,4 +70,4 @@ def generate(env, platform): env['SHF77PPCOM'] = '$SHF77 $SHF77FLAGS $CPPFLAGS $_F77INCFLAGS -c -o $TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(compilers, env) + return env.Detect(compilers) diff --git a/src/engine/SCons/Tool/gas.py b/src/engine/SCons/Tool/gas.py index a8c2864..8d24348 100644 --- a/src/engine/SCons/Tool/gas.py +++ b/src/engine/SCons/Tool/gas.py @@ -58,10 +58,10 @@ def generate(env, platform): for suffix in ASPPSuffixes: static_obj.add_action(suffix, SCons.Defaults.ASPPAction) - env['AS'] = SCons.Util.Detect(assemblers, env) or 'as' + env['AS'] = env.Detect(assemblers) or 'as' env['ASFLAGS'] = '' env['ASCOM'] = '$AS $ASFLAGS -o $TARGET $SOURCES' env['ASPPCOM'] = '$CC $ASFLAGS $CPPFLAGS -o $TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(assemblers, env) + return env.Detect(assemblers) diff --git a/src/engine/SCons/Tool/gcc.py b/src/engine/SCons/Tool/gcc.py index 6dbe0af..f1c085a 100644 --- a/src/engine/SCons/Tool/gcc.py +++ b/src/engine/SCons/Tool/gcc.py @@ -53,7 +53,7 @@ def generate(env, platform): static_obj.add_action(suffix, SCons.Defaults.CAction) shared_obj.add_action(suffix, SCons.Defaults.ShCAction) - env['CC'] = SCons.Util.Detect(compilers, env) or 'cc' + env['CC'] = env.Detect(compilers) or 'cc' env['CCFLAGS'] = '' env['CCCOM'] = '$CC $CCFLAGS $CPPFLAGS $_CPPINCFLAGS -c -o $TARGET $SOURCES' env['SHCC'] = '$CC' @@ -66,4 +66,4 @@ def generate(env, platform): env['CFILESUFFIX'] = '.c' def exists(env): - return SCons.Util.Detect(compilers, env) + return env.Detect(compilers) diff --git a/src/engine/SCons/Tool/gnulink.py b/src/engine/SCons/Tool/gnulink.py index 0a6b22e..7d4d273 100644 --- a/src/engine/SCons/Tool/gnulink.py +++ b/src/engine/SCons/Tool/gnulink.py @@ -47,7 +47,7 @@ def generate(env, platform): env['SHLINKFLAGS'] = '$LINKFLAGS -shared' env['SHLINKCOM'] = '$SHLINK $SHLINKFLAGS -o $TARGET $SOURCES $_LIBDIRFLAGS $_LIBFLAGS' env['SHLIBEMITTER']= None - env['LINK'] = SCons.Util.Detect(linkers, env) or 'c++' + env['LINK'] = env.Detect(linkers) or 'c++' env['LINKFLAGS'] = '' env['LINKCOM'] = '$LINK $LINKFLAGS -o $TARGET $SOURCES $_LIBDIRFLAGS $_LIBFLAGS' env['LIBDIRPREFIX']='-L' @@ -56,4 +56,4 @@ def generate(env, platform): env['LIBLINKSUFFIX']='' def exists(env): - return SCons.Util.Detect(linkers, env) + return env.Detect(linkers) diff --git a/src/engine/SCons/Tool/icc.py b/src/engine/SCons/Tool/icc.py index 5a09cb8..b21069a 100644 --- a/src/engine/SCons/Tool/icc.py +++ b/src/engine/SCons/Tool/icc.py @@ -68,4 +68,4 @@ def generate(env, platform): env['CXXFILESUFFIX'] = '.cc' def exists(env): - return SCons.Util.Detect(['icc'], env) + return env.Detect('icc') diff --git a/src/engine/SCons/Tool/ifl.py b/src/engine/SCons/Tool/ifl.py index 69a1ffd..dcf4412 100644 --- a/src/engine/SCons/Tool/ifl.py +++ b/src/engine/SCons/Tool/ifl.py @@ -68,4 +68,4 @@ def generate(env, platform): env['SHF77PPCOM'] = '$SHF77 $SHF77FLAGS $CPPFLAGS $_F77INCFLAGS /c $SOURCES /Fo$TARGET' def exists(env): - return SCons.Util.Detect(['ifl'], env) + return env.Detect('ifl') diff --git a/src/engine/SCons/Tool/ilink.py b/src/engine/SCons/Tool/ilink.py index f3e5659..aa396dc 100644 --- a/src/engine/SCons/Tool/ilink.py +++ b/src/engine/SCons/Tool/ilink.py @@ -48,4 +48,4 @@ def generate(env, platform): env['LIBLINKSUFFIX']='$LIBSUFFIX' def exists(env): - return SCons.Util.Detect(['ilink'], env) + return env.Detect('ilink') diff --git a/src/engine/SCons/Tool/latex.py b/src/engine/SCons/Tool/latex.py index 192ccf0..bfc30d9 100644 --- a/src/engine/SCons/Tool/latex.py +++ b/src/engine/SCons/Tool/latex.py @@ -55,4 +55,4 @@ def generate(env, platform): env['LATEXCOM'] = '$LATEX $LATEXFLAGS $SOURCES' def exists(env): - return SCons.Util.Detect(['latex'], env) + return env.Detect('latex') diff --git a/src/engine/SCons/Tool/lex.py b/src/engine/SCons/Tool/lex.py index 2a936e5..d7c61f8 100644 --- a/src/engine/SCons/Tool/lex.py +++ b/src/engine/SCons/Tool/lex.py @@ -48,4 +48,4 @@ def generate(env, platform): env['LEXCOM'] = '$LEX $LEXFLAGS -t $SOURCES > $TARGET' def exists(env): - return SCons.Util.Detect(['lex'], env) + return env.Detect('lex') diff --git a/src/engine/SCons/Tool/lib.py b/src/engine/SCons/Tool/lib.py index 7b5e0cd..d1b3bf0 100644 --- a/src/engine/SCons/Tool/lib.py +++ b/src/engine/SCons/Tool/lib.py @@ -45,4 +45,4 @@ def generate(env, platform): env['ARCOM'] = '$AR $ARFLAGS /OUT:$TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(['lib'], env) + return env.Detect('lib') diff --git a/src/engine/SCons/Tool/masm.py b/src/engine/SCons/Tool/masm.py index 6581e42..d634b09 100644 --- a/src/engine/SCons/Tool/masm.py +++ b/src/engine/SCons/Tool/masm.py @@ -61,4 +61,4 @@ def generate(env, platform): env['ASPPCOM'] = '$CC $ASFLAGS $CPPFLAGS /c /Fo$TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(['ml'], env) + return env.Detect('ml') diff --git a/src/engine/SCons/Tool/mslink.py b/src/engine/SCons/Tool/mslink.py index eb8fa26..9d399cd 100644 --- a/src/engine/SCons/Tool/mslink.py +++ b/src/engine/SCons/Tool/mslink.py @@ -153,4 +153,4 @@ def generate(env, platform): env['ENV']['PATH'] = exe_path def exists(env): - return SCons.Util.Detect(['link'], env) + return env.Detect('link') diff --git a/src/engine/SCons/Tool/msvc.py b/src/engine/SCons/Tool/msvc.py index fadebc6..37d3e1d 100644 --- a/src/engine/SCons/Tool/msvc.py +++ b/src/engine/SCons/Tool/msvc.py @@ -222,4 +222,4 @@ def generate(env, platform): env['CXXFILESUFFIX'] = '.cc' def exists(env): - return SCons.Util.Detect(['cl'], env) + return env.Detect('cl') diff --git a/src/engine/SCons/Tool/nasm.py b/src/engine/SCons/Tool/nasm.py index 7be7091..9dd5f05 100644 --- a/src/engine/SCons/Tool/nasm.py +++ b/src/engine/SCons/Tool/nasm.py @@ -61,4 +61,4 @@ def generate(env, platform): env['ASPPCOM'] = '$CC $ASFLAGS $CPPFLAGS -c -o $TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(['nasm'], env) + return env.Detect('nasm') diff --git a/src/engine/SCons/Tool/pdflatex.py b/src/engine/SCons/Tool/pdflatex.py index 2051717..980530a 100644 --- a/src/engine/SCons/Tool/pdflatex.py +++ b/src/engine/SCons/Tool/pdflatex.py @@ -54,4 +54,4 @@ def generate(env, platform): env['PDFLATEXCOM'] = '$PDFLATEX $PDFLATEXFLAGS $SOURCES $TARGET' def exists(env): - return SCons.Util.Detect(['pdflatex'], env) + return env.Detect('pdflatex') diff --git a/src/engine/SCons/Tool/pdftex.py b/src/engine/SCons/Tool/pdftex.py index 2d538b7..b43d8c6 100644 --- a/src/engine/SCons/Tool/pdftex.py +++ b/src/engine/SCons/Tool/pdftex.py @@ -50,4 +50,4 @@ def generate(env, platform): env['PDFTEXCOM'] = '$PDFTEX $PDFTEXFLAGS $SOURCES $TARGET' def exists(env): - return SCons.Util.Detect(['pdftex'], env) + return env.Detect('pdftex') diff --git a/src/engine/SCons/Tool/tar.py b/src/engine/SCons/Tool/tar.py index 4cf88ba..9edd76e 100644 --- a/src/engine/SCons/Tool/tar.py +++ b/src/engine/SCons/Tool/tar.py @@ -53,10 +53,10 @@ def generate(env, platform): bld = TarBuilder env['BUILDERS']['Tar'] = bld - env['TAR'] = SCons.Util.Detect(tars, env) or 'gtar' + env['TAR'] = env.Detect(tars) or 'gtar' env['TARFLAGS'] = '-c' env['TARCOM'] = '$TAR $TARFLAGS -f $TARGET $SOURCES' env['TARSUFFIX'] = '.tar' def exists(env): - return SCons.Util.Detect(tars, env) + return env.Detect(tars) diff --git a/src/engine/SCons/Tool/tex.py b/src/engine/SCons/Tool/tex.py index deed4cb..449b2dc 100644 --- a/src/engine/SCons/Tool/tex.py +++ b/src/engine/SCons/Tool/tex.py @@ -50,4 +50,4 @@ def generate(env, platform): env['TEXCOM'] = '$TEX $TEXFLAGS $SOURCES' def exists(env): - return SCons.Util.Detect(['tex'], env) + return env.Detect('tex') diff --git a/src/engine/SCons/Tool/yacc.py b/src/engine/SCons/Tool/yacc.py index 4e63c19..52e5d68 100644 --- a/src/engine/SCons/Tool/yacc.py +++ b/src/engine/SCons/Tool/yacc.py @@ -47,4 +47,4 @@ def generate(env, platform): env['YACCCOM'] = '$YACC $YACCFLAGS -o $TARGET $SOURCES' def exists(env): - return SCons.Util.Detect(['yacc'], env) + return env.Detect('yacc') diff --git a/src/engine/SCons/Util.py b/src/engine/SCons/Util.py index bcf0e14..63c992e 100644 --- a/src/engine/SCons/Util.py +++ b/src/engine/SCons/Util.py @@ -489,17 +489,3 @@ else: if stat.S_IMODE(st[stat.ST_MODE]) & 0111: return f return None - -def Detect(progs, env=None): - "Return the first available program in progs" - path = None - pathext = None - if env and env.has_key('ENV'): - if env['ENV'].has_key('PATH'): - path = env['ENV']['PATH'] - if env['ENV'].has_key('PATHEXT'): - pathext = env['ENV']['PATHEXT'] - for prog in progs: - path = WhereIs(prog, path, pathext) - if path: return prog - return None -- cgit v0.12