Back to index

enigmail  1.4.3
cl.py
Go to the documentation of this file.
00001 # ***** BEGIN LICENSE BLOCK *****
00002 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003 #
00004 # The contents of this file are subject to the Mozilla Public License Version
00005 # 1.1 (the "License"); you may not use this file except in compliance with
00006 # the License. You may obtain a copy of the License at
00007 # http://www.mozilla.org/MPL/
00008 #
00009 # Software distributed under the License is distributed on an "AS IS" basis,
00010 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011 # for the specific language governing rights and limitations under the
00012 # License.
00013 #
00014 # The Original Code is cl.py: a python wrapper for cl to automatically generate
00015 # dependency information.
00016 #
00017 # The Initial Developer of the Original Code is
00018 #   Mozilla Foundation.
00019 # Portions created by the Initial Developer are Copyright (C) 2010
00020 # the Initial Developer. All Rights Reserved.
00021 #
00022 # Contributor(s):
00023 #   Kyle Huey <me@kylehuey.com>
00024 #
00025 # Alternatively, the contents of this file may be used under the terms of
00026 # either the GNU General Public License Version 2 or later (the "GPL"), or
00027 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028 # in which case the provisions of the GPL or the LGPL are applicable instead
00029 # of those above. If you wish to allow use of your version of this file only
00030 # under the terms of either the GPL or the LGPL, and not to allow others to
00031 # use your version of this file under the terms of the MPL, indicate your
00032 # decision by deleting the provisions above and replace them with the notice
00033 # and other provisions required by the GPL or the LGPL. If you do not delete
00034 # the provisions above, a recipient may use your version of this file under
00035 # the terms of any one of the MPL, the GPL or the LGPL.
00036 #
00037 # ***** END LICENSE BLOCK *****
00038 
00039 import os, os.path
00040 import subprocess
00041 import sys
00042 
00043 CL_INCLUDES_PREFIX = os.environ.get("CL_INCLUDES_PREFIX", "Note: including file:")
00044 
00045 def InvokeClWithDependencyGeneration(cmdline):
00046     target = ""
00047     # Figure out what the target is
00048     for arg in cmdline:
00049         if arg.startswith("-Fo"):
00050             target = arg[3:]
00051             break
00052 
00053     if target == None:
00054         print >>sys.stderr, "No target set" and sys.exit(1)
00055 
00056     # The deps target lives here
00057     depstarget = os.path.basename(target) + ".pp"
00058 
00059     cmdline += ['-showIncludes']
00060     cl = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
00061 
00062     deps = set()
00063     for line in cl.stdout:
00064         # cl -showIncludes prefixes every header with "Note: including file:"
00065         # and an indentation corresponding to the depth (which we don't need)
00066         if line.startswith(CL_INCLUDES_PREFIX):
00067             dep = line[len(CL_INCLUDES_PREFIX):].strip()
00068             # We can't handle pathes with spaces properly in mddepend.pl, but
00069             # we can assume that anything in a path with spaces is a system
00070             # header and throw it away.
00071             if dep.find(' ') == -1:
00072                 deps.add(dep)
00073         else:
00074             sys.stdout.write(line) # Make sure we preserve the relevant output
00075                                    # from cl
00076 
00077     ret = cl.wait()
00078     if ret != 0 or target == "":
00079         sys.exit(ret)
00080 
00081     depsdir = os.path.normpath(os.path.join(os.path.dirname(target), ".deps"))
00082     depstarget = os.path.join(depsdir, depstarget)
00083     if not os.path.isdir(depsdir):
00084         try:
00085             os.makedirs(depsdir)
00086         except OSError:
00087             pass # This suppresses the error we get when the dir exists, at the
00088                  # cost of masking failure to create the directory.  We'll just
00089                  # die on the next line though, so it's not that much of a loss.
00090 
00091     f = open(depstarget, "w")
00092     for dep in sorted(deps):
00093         print >>f, "%s: %s" % (target, dep)
00094 
00095 if __name__ == "__main__":
00096     InvokeClWithDependencyGeneration(sys.argv[1:])