PageRenderTime 68ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/cmake/run_build_test.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 144 lines | 118 code | 9 blank | 17 comment | 5 complexity | 065049d5518db045e70c9d2bf1bd2242 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/env 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=viewerlgpl$
  20. Second Life Viewer Source Code
  21. Copyright (C) 2009-2010, Linden Research, Inc.
  22. This library is free software; you can redistribute it and/or
  23. modify it under the terms of the GNU Lesser General Public
  24. License as published by the Free Software Foundation;
  25. version 2.1 of the License only.
  26. This library is distributed in the hope that it will be useful,
  27. but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. Lesser General Public License for more details.
  30. You should have received a copy of the GNU Lesser General Public
  31. License along with this library; if not, write to the Free Software
  32. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  33. Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  34. $/LicenseInfo$
  35. """
  36. import os
  37. import sys
  38. import subprocess
  39. def main(command, libpath=[], vars={}):
  40. """Pass:
  41. command is a sequence (e.g. a list) of strings. The first item in the list
  42. must be the command name, the rest are its arguments.
  43. libpath is a sequence of directory pathnames. These will be appended to
  44. the platform-specific dynamic library search path environment variable.
  45. vars is a dict of arbitrary (var, value) pairs to be added to the
  46. environment before running 'command'.
  47. This function runs the specified command, waits for it to terminate and
  48. returns its return code. This will be negative if the command terminated
  49. with a signal, else it will be the process's specified exit code.
  50. """
  51. # Handle platform-dependent libpath first.
  52. if sys.platform == "win32":
  53. lpvars = ["PATH"]
  54. elif sys.platform == "darwin":
  55. lpvars = ["LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH"]
  56. elif sys.platform.startswith("linux"):
  57. lpvars = ["LD_LIBRARY_PATH"]
  58. else:
  59. # No idea what the right pathname might be! But only crump if this
  60. # feature is requested.
  61. if libpath:
  62. raise NotImplemented("run_build_test: unknown platform %s" % sys.platform)
  63. lpvars = []
  64. for var in lpvars:
  65. # Split the existing path. Bear in mind that the variable in question
  66. # might not exist; instead of KeyError, just use an empty string.
  67. dirs = os.environ.get(var, "").split(os.pathsep)
  68. # Append the sequence in libpath
  69. print "%s += %r" % (var, libpath)
  70. for dir in libpath:
  71. # append system paths at the end
  72. if dir in ('/lib', '/usr/lib'):
  73. dirs.append(dir)
  74. # prepend non-system paths
  75. else:
  76. dirs.insert(0, dir)
  77. # Filter out some useless pieces
  78. clean_dirs = []
  79. for dir in dirs:
  80. if dir and dir not in ('', '.'):
  81. clean_dirs.append(dir)
  82. # Now rebuild the path string. This way we use a minimum of separators
  83. # -- and we avoid adding a pointless separator when libpath is empty.
  84. os.environ[var] = os.pathsep.join(clean_dirs)
  85. print "%s = %r" % (var, os.environ[var])
  86. # Now handle arbitrary environment variables. The tricky part is ensuring
  87. # that all the keys and values we try to pass are actually strings.
  88. if vars:
  89. print "Setting:"
  90. for key, value in vars.iteritems():
  91. print "%s=%s" % (key, value)
  92. os.environ.update(dict([(str(key), str(value)) for key, value in vars.iteritems()]))
  93. # Run the child process.
  94. print "Running: %s" % " ".join(command)
  95. # Make sure we see all relevant output *before* child-process output.
  96. sys.stdout.flush()
  97. return subprocess.call(command)
  98. if __name__ == "__main__":
  99. from optparse import OptionParser
  100. parser = OptionParser(usage="usage: %prog [options] command args...")
  101. # We want optparse support for the options we ourselves handle -- but we
  102. # DO NOT want it looking at options for the executable we intend to run,
  103. # rejecting them as invalid because we don't define them. So configure the
  104. # parser to stop looking for options as soon as it sees the first
  105. # positional argument (traditional Unix syntax).
  106. parser.disable_interspersed_args()
  107. parser.add_option("-D", "--define", dest="vars", default=[], action="append",
  108. metavar="VAR=value",
  109. help="Add VAR=value to the env variables defined")
  110. parser.add_option("-l", "--libpath", dest="libpath", default=[], action="append",
  111. metavar="DIR",
  112. help="Add DIR to the platform-dependent DLL search path")
  113. opts, args = parser.parse_args()
  114. # What we have in opts.vars is a list of strings of the form "VAR=value"
  115. # or possibly just "VAR". What we want is a dict. We can build that dict by
  116. # constructing a list of ["VAR", "value"] pairs -- so split each
  117. # "VAR=value" string on the '=' sign (but only once, in case we have
  118. # "VAR=some=user=string"). To handle the case of just "VAR", append "" to
  119. # the list returned by split(), then slice off anything after the pair we
  120. # want.
  121. rc = main(command=args, libpath=opts.libpath,
  122. vars=dict([(pair.split('=', 1) + [""])[:2] for pair in opts.vars]))
  123. if rc not in (None, 0):
  124. print >>sys.stderr, "Failure running: %s" % " ".join(args)
  125. print >>sys.stderr, "Error: %s" % rc
  126. sys.exit((rc < 0) and 255 or rc)