Add TempFileMunge support to AIX, Cygwin, HP-UX, Linux/UNIX and SunOS. (Leanid Nazdr...
[scons.git] / src / engine / SCons / Platform / __init__.py
1 """SCons.Platform
2
3 SCons platform selection.
4
5 This looks for modules that define a callable object that can modify a
6 construction environment as appropriate for a given platform.
7
8 Note that we take a more simplistic view of "platform" than Python does.
9 We're looking for a single string that determines a set of
10 tool-independent variables with which to initialize a construction
11 environment.  Consequently, we'll examine both sys.platform and os.name
12 (and anything else that might come in to play) in order to return some
13 specification which is unique enough for our purposes.
14
15 Note that because this subsysem just *selects* a callable that can
16 modify a construction environment, it's possible for people to define
17 their own "platform specification" in an arbitrary callable function.
18 No one needs to use or tie in to this subsystem in order to roll
19 their own platform definition.
20 """
21
22 #
23 # __COPYRIGHT__
24
25 # Permission is hereby granted, free of charge, to any person obtaining
26 # a copy of this software and associated documentation files (the
27 # "Software"), to deal in the Software without restriction, including
28 # without limitation the rights to use, copy, modify, merge, publish,
29 # distribute, sublicense, and/or sell copies of the Software, and to
30 # permit persons to whom the Software is furnished to do so, subject to
31 # the following conditions:
32 #
33 # The above copyright notice and this permission notice shall be included
34 # in all copies or substantial portions of the Software.
35 #
36 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
37 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
38 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
40 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
41 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
42 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 #
44
45 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
46
47 import imp
48 import os
49 import string
50 import sys
51 import tempfile
52
53 import SCons.Errors
54 import SCons.Tool
55
56 def platform_default():
57     """Return the platform string for our execution environment.
58
59     The returned value should map to one of the SCons/Platform/*.py
60     files.  Since we're architecture independent, though, we don't
61     care about the machine architecture.
62     """
63     osname = os.name
64     if osname == 'java':
65         osname = os._osType
66     if osname == 'posix':
67         if sys.platform == 'cygwin':
68             return 'cygwin'
69         elif string.find(sys.platform, 'irix') != -1:
70             return 'irix'
71         elif string.find(sys.platform, 'sunos') != -1:
72             return 'sunos'
73         elif string.find(sys.platform, 'hp-ux') != -1:
74             return 'hpux'
75         elif string.find(sys.platform, 'aix') != -1:
76             return 'aix'
77         elif string.find(sys.platform, 'darwin') != -1:
78             return 'darwin'
79         else:
80             return 'posix'
81     elif os.name == 'os2':
82         return 'os2'
83     else:
84         return sys.platform
85
86 def platform_module(name = platform_default()):
87     """Return the imported module for the platform.
88
89     This looks for a module name that matches the specified argument.
90     If the name is unspecified, we fetch the appropriate default for
91     our execution environment.
92     """
93     full_name = 'SCons.Platform.' + name
94     if not sys.modules.has_key(full_name):
95         if os.name == 'java':
96             eval(full_name)
97         else:
98             try:
99                 file, path, desc = imp.find_module(name,
100                                         sys.modules['SCons.Platform'].__path__)
101                 mod = imp.load_module(full_name, file, path, desc)
102                 setattr(SCons.Platform, name, mod)
103             except ImportError:
104                 raise SCons.Errors.UserError, "No platform named '%s'" % name
105             if file:
106                 file.close()
107     return sys.modules[full_name]
108
109 def DefaultToolList(platform, env):
110     """Select a default tool list for the specified platform.
111     """
112     return SCons.Tool.tool_list(platform, env)
113
114 class PlatformSpec:
115     def __init__(self, name):
116         self.name = name
117
118     def __str__(self):
119         return self.name
120         
121 class TempFileMunge:
122     """A callable class.  You can set an Environment variable to this,
123     then call it with a string argument, then it will perform temporary
124     file substitution on it.  This is used to circumvent the long command
125     line limitation.
126
127     Example usage:
128     env["TEMPFILE"] = TempFileMunge
129     env["LINKCOM"] = "${TEMPFILE('$LINK $TARGET $SOURCES')}"
130     """
131     def __init__(self, cmd):
132         self.cmd = cmd
133
134     def __call__(self, target, source, env, for_signature):
135         if for_signature:
136             return self.cmd
137         cmd = env.subst_list(self.cmd, 0, target, source)[0]
138         try:
139             maxline = int(env.subst('$MAXLINELENGTH'))
140         except ValueError:
141             maxline = 2048
142         if (reduce(lambda x, y: x + len(y), cmd, 0) + len(cmd)) <= maxline:
143             return self.cmd
144         else:
145             # We do a normpath because mktemp() has what appears to be
146             # a bug in Win32 that will use a forward slash as a path
147             # delimiter.  Win32's link mistakes that for a command line
148             # switch and barfs.
149             #
150             # We use the .lnk suffix for the benefit of the Phar Lap
151             # linkloc linker, which likes to append an .lnk suffix if
152             # none is given.
153             tmp = os.path.normpath(tempfile.mktemp('.lnk'))
154             native_tmp = SCons.Util.get_native_path(tmp)
155
156             if env['SHELL'] and env['SHELL'] == 'sh':
157                 # The sh shell will try to escape the backslashes in the
158                 # path, so unescape them.
159                 native_tmp = string.replace(native_tmp, '\\', r'\\\\')
160                 # In Cygwin, we want to use rm to delete the temporary
161                 # file, because del does not exist in the sh shell.
162                 rm = env.Detect('rm') or 'del'
163             else:
164                 # Don't use 'rm' if the shell is not sh, because rm won't
165                 # work with the win32 shells (cmd.exe or command.com) or
166                 # win32 path names.
167                 rm = 'del'
168
169             args = map(SCons.Util.quote_spaces, cmd[1:])
170             open(tmp, 'w').write(string.join(args, " ") + "\n")
171             # XXX Using the SCons.Action.print_actions value directly
172             # like this is bogus, but expedient.  This class should
173             # really be rewritten as an Action that defines the
174             # __call__() and strfunction() methods and lets the
175             # normal action-execution logic handle whether or not to
176             # print/execute the action.  The problem, though, is all
177             # of that is decided before we execute this method as
178             # part of expanding the $TEMPFILE construction variable.
179             # Consequently, refactoring this will have to wait until
180             # we get more flexible with allowing Actions to exist
181             # independently and get strung together arbitrarily like
182             # Ant tasks.  In the meantime, it's going to be more
183             # user-friendly to not let obsession with architectural
184             # purity get in the way of just being helpful, so we'll
185             # reach into SCons.Action directly.
186             if SCons.Action.print_actions:
187                 print("Using tempfile "+native_tmp+" for command line:\n"+
188                       str(cmd[0]) + " " + string.join(args," "))
189             return [ cmd[0], '@' + native_tmp + '\n' + rm, native_tmp ]
190     
191 def Platform(name = platform_default()):
192     """Select a canned Platform specification.
193     """
194     module = platform_module(name)
195     spec = PlatformSpec(name)
196     spec.__call__ = module.generate
197     return spec