Dynamically check for the existence of utilities. (sam th)
[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 (c) 2001, 2002 Steven Knight
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 sys
50
51 import SCons.Errors
52
53 def platform_default():
54     """Return the platform string for our execution environment.
55
56     The returned value should map to one of the SCons/Platform/*.py
57     files.  Since we're architecture independent, though, we don't
58     care about the machine architecture.
59     """
60     if os.name == 'posix':
61         if sys.platform == 'cygwin':
62             return 'cygwin'
63         return 'posix'
64     elif os.name == 'os2':
65         return 'os2'
66     else:
67         return sys.platform
68
69 def platform_module(name = platform_default()):
70     """Return the imported module for the platform.
71
72     This looks for a module name that matches the specified argument.
73     If the name is unspecified, we fetch the appropriate default for
74     our execution environment.
75     """
76     full_name = 'SCons.Platform.' + name
77     if not sys.modules.has_key(full_name):
78         try:
79             file, path, desc = imp.find_module(name,
80                                         sys.modules['SCons.Platform'].__path__)
81             imp.load_module(full_name, file, path, desc)
82         except ImportError:
83             raise SCons.Errors.UserError, "No platform named '%s'" % name
84         if file:
85             file.close()
86     return sys.modules[full_name]
87
88 def DefaultToolList(name = platform_default()):
89     """Select a default tool list for the specified platform.
90     """
91     module = platform_module(name)
92     return SCons.Tool.tool_list()
93
94 class PlatformSpec:
95     def __init__(self, name):
96         self.name = name
97
98     def __str__(self):
99         return self.name
100     
101 def Platform(name = platform_default()):
102     """Select a canned Platform specification.
103     """
104     module = platform_module(name)
105     spec = PlatformSpec(name)
106     spec.__call__ = module.generate
107     return spec