4e8cf4e8aff0cd2218d658ca9782ac7375890c1e
[scons.git] / src / engine / SCons / Defaults.py
1 """SCons.Defaults
2
3 Builders and other things for the local site.  Here's where we'll
4 duplicate the functionality of autoconf until we move it into the
5 installation procedure or use something like qmconf.
6
7 The code that reads the registry to find MSVC components was borrowed
8 from distutils.msvccompiler.
9
10 """
11
12 #
13 # Copyright (c) 2001, 2002 Steven Knight
14 #
15 # Permission is hereby granted, free of charge, to any person obtaining
16 # a copy of this software and associated documentation files (the
17 # "Software"), to deal in the Software without restriction, including
18 # without limitation the rights to use, copy, modify, merge, publish,
19 # distribute, sublicense, and/or sell copies of the Software, and to
20 # permit persons to whom the Software is furnished to do so, subject to
21 # the following conditions:
22 #
23 # The above copyright notice and this permission notice shall be included
24 # in all copies or substantial portions of the Software.
25 #
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
27 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
28 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #
34
35 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
36
37
38
39 import os
40 import stat
41 import string
42 import sys
43 import os.path
44
45 import SCons.Action
46 import SCons.Builder
47 import SCons.Node.Alias
48 import SCons.Node.FS
49 import SCons.Scanner.C
50 import SCons.Scanner.Fortran
51 import SCons.Scanner.Prog
52
53 def alias_builder(env, target, source):
54     pass
55
56 Alias = SCons.Builder.Builder(action = alias_builder,
57                               target_factory = SCons.Node.Alias.default_ans.Alias,
58                               source_factory = SCons.Node.FS.default_fs.Entry,
59                               multi = 1)
60
61 CScan = SCons.Scanner.C.CScan()
62
63 FortranScan = SCons.Scanner.Fortran.FortranScan()
64
65 def yaccEmitter(target, source, env, **kw):
66     # Yacc can be configured to emit a .h file as well
67     # as a .c file, if -d is specified on the command line.
68     if len(source) and \
69        os.path.splitext(SCons.Util.to_String(source[0]))[1] in \
70        [ '.y', '.yy'] and \
71        '-d' in string.split(env.subst("$YACCFLAGS")):
72         target.append(os.path.splitext(SCons.Util.to_String(target[0]))[0] + \
73                       '.h')
74     return (target, source)
75
76 def CFile():
77     """Common function to generate a C file Builder."""
78     return SCons.Builder.Builder(action = {},
79                                  emitter = yaccEmitter,
80                                  suffix = '$CFILESUFFIX')
81
82 def CXXFile():
83     """Common function to generate a C++ file Builder."""
84     return SCons.Builder.Builder(action = {},
85                                  emitter = yaccEmitter,
86                                  suffix = '$CXXFILESUFFIX')
87
88 class SharedFlagChecker:
89     """This is a callable class that is used as
90     a build action for all objects, libraries, and programs.
91     Its job is to run before the "real" action that builds the
92     file, to make sure we aren't trying to link shared objects
93     into a static library/program, or static objects into a
94     shared library."""
95
96     def __init__(self, shared, set_target_flag):
97         self.shared = shared
98         self.set_target_flag = set_target_flag
99
100     def __call__(self, source, target, env, **kw):
101         if kw.has_key('shared'):
102             raise SCons.Errors.UserError, "The shared= parameter to Library() or Object() no longer works.\nUse SharedObject() or SharedLibrary() instead."
103         if self.set_target_flag:
104             for tgt in target:
105                 tgt.attributes.shared = self.shared
106
107         for src in source:
108             if hasattr(src.attributes, 'shared'):
109                 if self.shared and not src.attributes.shared:
110                     raise SCons.Errors.UserError, "Source file: %s is static and is not compatible with shared target: %s" % (src, target[0])
111                 elif not self.shared and src.attributes.shared:
112                     raise SCons.Errors.UserError, "Source file: %s is shared and is not compatible with static target: %s" % (src, target[0])
113
114 SharedCheck = SCons.Action.Action(SharedFlagChecker(1, 0))
115 StaticCheck = SCons.Action.Action(SharedFlagChecker(0, 0))
116 SharedCheckSet = SCons.Action.Action(SharedFlagChecker(1, 1))
117 StaticCheckSet = SCons.Action.Action(SharedFlagChecker(0, 1))
118
119 CAction = SCons.Action.Action([ StaticCheckSet, "$CCCOM" ])
120 ShCAction = SCons.Action.Action([ SharedCheckSet, "$SHCCCOM" ])
121 CXXAction = SCons.Action.Action([ StaticCheckSet, "$CXXCOM" ])
122 ShCXXAction = SCons.Action.Action([ SharedCheckSet, "$SHCXXCOM" ])
123
124 F77Action = SCons.Action.Action([ StaticCheckSet, "$F77COM" ])
125 ShF77Action = SCons.Action.Action([ SharedCheckSet, "$SHF77COM" ])
126 F77PPAction = SCons.Action.Action([ StaticCheckSet, "$F77PPCOM" ])
127 ShF77PPAction = SCons.Action.Action([ SharedCheckSet, "$SHF77PPCOM" ])
128
129 ASAction = SCons.Action.Action([ StaticCheckSet, "$ASCOM" ])
130 ASPPAction = SCons.Action.Action([ StaticCheckSet, "$ASPPCOM" ])
131
132
133 def StaticObject():
134     """A function for generating the static object Builder."""
135     return SCons.Builder.Builder(action = {},
136                                  emitter="$OBJEMITTER",
137                                  prefix = '$OBJPREFIX',
138                                  suffix = '$OBJSUFFIX',
139                                  src_builder = ['CFile', 'CXXFile'])
140
141 def SharedObject():
142     """A function for generating the shared object Builder."""
143     return SCons.Builder.Builder(action = {},
144                                  prefix = '$SHOBJPREFIX',
145                                  suffix = '$SHOBJSUFFIX',
146                                  emitter="$OBJEMITTER",
147                                  src_builder = ['CFile', 'CXXFile'])
148
149 ProgScan = SCons.Scanner.Prog.ProgScan()
150
151 StaticLibrary = SCons.Builder.Builder(action=[ StaticCheck, "$ARCOM" ],
152                                       prefix = '$LIBPREFIX',
153                                       suffix = '$LIBSUFFIX',
154                                       src_suffix = '$OBJSUFFIX',
155                                       src_builder = 'Object')
156
157 SharedLibrary = SCons.Builder.Builder(action=[ SharedCheck, "$SHLINKCOM" ],
158                                       emitter="$SHLIBEMITTER",
159                                       prefix = '$SHLIBPREFIX',
160                                       suffix = '$SHLIBSUFFIX',
161                                       scanner = ProgScan,
162                                       src_suffix = '$SHOBJSUFFIX',
163                                       src_builder = 'SharedObject')
164
165 def DVI():
166     """Common function to generate a DVI file Builder."""
167     return SCons.Builder.Builder(action = {},
168                                  # The suffix is not configurable via a
169                                  # construction variable like $DVISUFFIX
170                                  # because the output file name is
171                                  # hard-coded within TeX.
172                                  suffix = '.dvi')
173
174 def PDF():
175     """A function for generating the PDF Builder."""
176     return SCons.Builder.Builder(action = { },
177                                  prefix = '$PDFPREFIX',
178                                  suffix = '$PDFSUFFIX')
179
180 Program = SCons.Builder.Builder(action=[ StaticCheck, '$LINKCOM' ],
181                                 emitter='$PROGEMITTER',
182                                 prefix='$PROGPREFIX',
183                                 suffix='$PROGSUFFIX',
184                                 src_suffix='$OBJSUFFIX',
185                                 src_builder='Object',
186                                 scanner = ProgScan)
187
188 ConstructionEnvironment = {
189     'BUILDERS'   : { 'SharedLibrary'  : SharedLibrary,
190                      'Library'        : StaticLibrary,
191                      'StaticLibrary'  : StaticLibrary,
192                      'Alias'          : Alias,    
193                      'Program'        : Program },
194     'SCANNERS'   : [CScan, FortranScan],
195     'PDFPREFIX'  : '',
196     'PDFSUFFIX'  : '.pdf',
197     'PSPREFIX'   : '',
198     'PSSUFFIX'   : '.ps',
199     'ENV'        : {},
200     }