run_build_test.py
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:6k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. #!/usr/bin/python
  2. """
  3. @file   run_build_test.py
  4. @author Nat Goodspeed
  5. @date   2009-09-03
  6. @brief  Helper script to allow CMake to run some command after setting
  7.         environment variables.
  8. CMake has commands to run an external program. But remember that each CMake
  9. command must be backed by multiple build-system implementations. Unfortunately
  10. it seems CMake can't promise that every target build system can set specified
  11. environment variables before running the external program of interest.
  12. This helper script is a workaround. It simply sets the requested environment
  13. variables and then executes the program specified on the rest of its command
  14. line.
  15. Example:
  16. python run_build_test.py -DFOO=bar myprog somearg otherarg
  17. sets environment variable FOO=bar, then runs:
  18. myprog somearg otherarg
  19. $LicenseInfo:firstyear=2009&license=mit$
  20. Copyright (c) 2009-2010, Linden Research, Inc.
  21. Permission is hereby granted, free of charge, to any person obtaining a copy
  22. of this software and associated documentation files (the "Software"), to deal
  23. in the Software without restriction, including without limitation the rights
  24. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25. copies of the Software, and to permit persons to whom the Software is
  26. furnished to do so, subject to the following conditions:
  27. The above copyright notice and this permission notice shall be included in
  28. all copies or substantial portions of the Software.
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  35. THE SOFTWARE.
  36. $/LicenseInfo$
  37. """
  38. import os
  39. import sys
  40. import subprocess
  41. def main(command, libpath=[], vars={}):
  42.     """Pass:
  43.     command is a sequence (e.g. a list) of strings. The first item in the list
  44.     must be the command name, the rest are its arguments.
  45.     libpath is a sequence of directory pathnames. These will be appended to
  46.     the platform-specific dynamic library search path environment variable.
  47.     vars is a dict of arbitrary (var, value) pairs to be added to the
  48.     environment before running 'command'.
  49.     This function runs the specified command, waits for it to terminate and
  50.     returns its return code. This will be negative if the command terminated
  51.     with a signal, else it will be the process's specified exit code.
  52.     """
  53.     # Handle platform-dependent libpath first.
  54.     if sys.platform == "win32":
  55.         lpvars = ["PATH"]
  56.     elif sys.platform == "darwin":
  57.         lpvars = ["LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH"]
  58.     elif sys.platform.startswith("linux"):
  59.         lpvars = ["LD_LIBRARY_PATH"]
  60.     else:
  61.         # No idea what the right pathname might be! But only crump if this
  62.         # feature is requested.
  63.         if libpath:
  64.             raise NotImplemented("run_build_test: unknown platform %s" % sys.platform)
  65.         lpvars = []
  66.     for var in lpvars:
  67.         # Split the existing path. Bear in mind that the variable in question
  68.         # might not exist; instead of KeyError, just use an empty string.
  69.         dirs = os.environ.get(var, "").split(os.pathsep)
  70.         # Append the sequence in libpath
  71.         print "%s += %r" % (var, libpath)
  72.         dirs.extend(libpath)
  73.         # Now rebuild the path string. This way we use a minimum of separators
  74.         # -- and we avoid adding a pointless separator when libpath is empty.
  75.         os.environ[var] = os.pathsep.join(dirs)
  76.     # Now handle arbitrary environment variables. The tricky part is ensuring
  77.     # that all the keys and values we try to pass are actually strings.
  78.     if vars:
  79.          print "Setting:"
  80.          for key, value in vars.iteritems():
  81.              print "%s=%s" % (key, value)
  82.     os.environ.update(dict([(str(key), str(value)) for key, value in vars.iteritems()]))
  83.     # Run the child process.
  84.     print "Running: %s" % " ".join(command)
  85.     return subprocess.call(command)
  86. if __name__ == "__main__":
  87.     from optparse import OptionParser
  88.     parser = OptionParser(usage="usage: %prog [options] command args...")
  89.     # We want optparse support for the options we ourselves handle -- but we
  90.     # DO NOT want it looking at options for the executable we intend to run,
  91.     # rejecting them as invalid because we don't define them. So configure the
  92.     # parser to stop looking for options as soon as it sees the first
  93.     # positional argument (traditional Unix syntax).
  94.     parser.disable_interspersed_args()
  95.     parser.add_option("-D", "--define", dest="vars", default=[], action="append",
  96.                       metavar="VAR=value",
  97.                       help="Add VAR=value to the env variables defined")
  98.     parser.add_option("-l", "--libpath", dest="libpath", default=[], action="append",
  99.                       metavar="DIR",
  100.                       help="Add DIR to the platform-dependent DLL search path")
  101.     opts, args = parser.parse_args()
  102.     # What we have in opts.vars is a list of strings of the form "VAR=value"
  103.     # or possibly just "VAR". What we want is a dict. We can build that dict by
  104.     # constructing a list of ["VAR", "value"] pairs -- so split each
  105.     # "VAR=value" string on the '=' sign (but only once, in case we have
  106.     # "VAR=some=user=string"). To handle the case of just "VAR", append "" to
  107.     # the list returned by split(), then slice off anything after the pair we
  108.     # want.
  109.     rc = main(command=args, libpath=opts.libpath,
  110.               vars=dict([(pair.split('=', 1) + [""])[:2] for pair in opts.vars]))
  111.     if rc not in (None, 0):
  112.         print >>sys.stderr, "Failure running: %s" % " ".join(args)
  113.         print >>sys.stderr, "Error: %s" % rc
  114.     sys.exit((rc < 0) and 255 or rc)