Toplevel directory cleanup
[senf.git] / tools / scons-1.2.0 / engine / SCons / Tool / dmd.py
1 """SCons.Tool.dmd
2
3 Tool-specific initialization for the Digital Mars D compiler.
4 (http://digitalmars.com/d)
5
6 Coded by Andy Friesen (andy@ikagames.com)
7 15 November 2003
8
9 There are a number of problems with this script at this point in time.
10 The one that irritates me the most is the Windows linker setup.  The D
11 linker doesn't have a way to add lib paths on the commandline, as far
12 as I can see.  You have to specify paths relative to the SConscript or
13 use absolute paths.  To hack around it, add '#/blah'.  This will link
14 blah.lib from the directory where SConstruct resides.
15
16 Compiler variables:
17     DC - The name of the D compiler to use.  Defaults to dmd or gdmd,
18     whichever is found.
19     DPATH - List of paths to search for import modules.
20     DVERSIONS - List of version tags to enable when compiling.
21     DDEBUG - List of debug tags to enable when compiling.
22
23 Linker related variables:
24     LIBS - List of library files to link in.
25     DLINK - Name of the linker to use.  Defaults to dmd or gdmd.
26     DLINKFLAGS - List of linker flags.
27
28 Lib tool variables:
29     DLIB - Name of the lib tool to use.  Defaults to lib.
30     DLIBFLAGS - List of flags to pass to the lib tool.
31     LIBS - Same as for the linker. (libraries to pull into the .lib)
32 """
33
34 #
35 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
36 #
37 # Permission is hereby granted, free of charge, to any person obtaining
38 # a copy of this software and associated documentation files (the
39 # "Software"), to deal in the Software without restriction, including
40 # without limitation the rights to use, copy, modify, merge, publish,
41 # distribute, sublicense, and/or sell copies of the Software, and to
42 # permit persons to whom the Software is furnished to do so, subject to
43 # the following conditions:
44 #
45 # The above copyright notice and this permission notice shall be included
46 # in all copies or substantial portions of the Software.
47 #
48 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
49 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
50 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
51 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
52 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
53 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
54 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 #
56
57 __revision__ = "src/engine/SCons/Tool/dmd.py 3842 2008/12/20 22:59:52 scons"
58
59 import os
60 import string
61
62 import SCons.Action
63 import SCons.Builder
64 import SCons.Defaults
65 import SCons.Scanner.D
66 import SCons.Tool
67
68 # Adapted from c++.py
69 def isD(source):
70     if not source:
71         return 0
72
73     for s in source:
74         if s.sources:
75             ext = os.path.splitext(str(s.sources[0]))[1]
76             if ext == '.d':
77                 return 1
78     return 0
79
80 smart_link = {}
81
82 smart_lib = {}
83
84 def generate(env):
85     global smart_link
86     global smart_lib
87
88     static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
89
90     DAction = SCons.Action.Action('$DCOM', '$DCOMSTR')
91
92     static_obj.add_action('.d', DAction)
93     shared_obj.add_action('.d', DAction)
94     static_obj.add_emitter('.d', SCons.Defaults.StaticObjectEmitter)
95     shared_obj.add_emitter('.d', SCons.Defaults.SharedObjectEmitter)
96
97     dc = env.Detect(['dmd', 'gdmd'])
98     env['DC'] = dc
99     env['DCOM'] = '$DC $_DINCFLAGS $_DVERFLAGS $_DDEBUGFLAGS $_DFLAGS -c -of$TARGET $SOURCES'
100     env['_DINCFLAGS'] = '$( ${_concat(DINCPREFIX, DPATH, DINCSUFFIX, __env__, RDirs, TARGET, SOURCE)}  $)'
101     env['_DVERFLAGS'] = '$( ${_concat(DVERPREFIX, DVERSIONS, DVERSUFFIX, __env__)}  $)'
102     env['_DDEBUGFLAGS'] = '$( ${_concat(DDEBUGPREFIX, DDEBUG, DDEBUGSUFFIX, __env__)} $)'
103     env['_DFLAGS'] = '$( ${_concat(DFLAGPREFIX, DFLAGS, DFLAGSUFFIX, __env__)} $)'
104
105     env['DPATH'] = ['#/']
106     env['DFLAGS'] = []
107     env['DVERSIONS'] = []
108     env['DDEBUG'] = []
109
110     if dc:
111         # Add the path to the standard library.
112         # This is merely for the convenience of the dependency scanner.
113         dmd_path = env.WhereIs(dc)
114         if dmd_path:
115             x = string.rindex(dmd_path, dc)
116             phobosDir = dmd_path[:x] + '/../src/phobos'
117             if os.path.isdir(phobosDir):
118                 env.Append(DPATH = [phobosDir])
119
120     env['DINCPREFIX'] = '-I'
121     env['DINCSUFFIX'] = ''
122     env['DVERPREFIX'] = '-version='
123     env['DVERSUFFIX'] = ''
124     env['DDEBUGPREFIX'] = '-debug='
125     env['DDEBUGSUFFIX'] = ''
126     env['DFLAGPREFIX'] = '-'
127     env['DFLAGSUFFIX'] = ''
128     env['DFILESUFFIX'] = '.d'
129
130     # Need to use the Digital Mars linker/lib on windows.
131     # *nix can just use GNU link.
132     if env['PLATFORM'] == 'win32':
133         env['DLINK'] = '$DC'
134         env['DLINKCOM'] = '$DLINK -of$TARGET $SOURCES $DFLAGS $DLINKFLAGS $_DLINKLIBFLAGS'
135         env['DLIB'] = 'lib'
136         env['DLIBCOM'] = '$DLIB $_DLIBFLAGS -c $TARGET $SOURCES $_DLINKLIBFLAGS'
137
138         env['_DLINKLIBFLAGS'] = '$( ${_concat(DLIBLINKPREFIX, LIBS, DLIBLINKSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
139         env['_DLIBFLAGS'] = '$( ${_concat(DLIBFLAGPREFIX, DLIBFLAGS, DLIBFLAGSUFFIX, __env__)} $)'
140         env['DLINKFLAGS'] = []
141         env['DLIBLINKPREFIX'] = ''
142         env['DLIBLINKSUFFIX'] = '.lib'
143         env['DLIBFLAGPREFIX'] = '-'
144         env['DLIBFLAGSUFFIX'] = ''
145         env['DLINKFLAGPREFIX'] = '-'
146         env['DLINKFLAGSUFFIX'] = ''
147
148         SCons.Tool.createStaticLibBuilder(env)
149
150         # Basically, we hijack the link and ar builders with our own.
151         # these builders check for the presence of D source, and swap out
152         # the system's defaults for the Digital Mars tools.  If there's no D
153         # source, then we silently return the previous settings.
154         linkcom = env.get('LINKCOM')
155         try:
156             env['SMART_LINKCOM'] = smart_link[linkcom]
157         except KeyError:
158             def _smartLink(source, target, env, for_signature,
159                            defaultLinker=linkcom):
160                 if isD(source):
161                     # XXX I'm not sure how to add a $DLINKCOMSTR variable
162                     # so that it works with this _smartLink() logic,
163                     # and I don't have a D compiler/linker to try it out,
164                     # so we'll leave it alone for now.
165                     return '$DLINKCOM'
166                 else:
167                     return defaultLinker
168             env['SMART_LINKCOM'] = smart_link[linkcom] = _smartLink
169
170         arcom = env.get('ARCOM')
171         try:
172             env['SMART_ARCOM'] = smart_lib[arcom]
173         except KeyError:
174             def _smartLib(source, target, env, for_signature,
175                          defaultLib=arcom):
176                 if isD(source):
177                     # XXX I'm not sure how to add a $DLIBCOMSTR variable
178                     # so that it works with this _smartLib() logic, and
179                     # I don't have a D compiler/archiver to try it out,
180                     # so we'll leave it alone for now.
181                     return '$DLIBCOM'
182                 else:
183                     return defaultLib
184             env['SMART_ARCOM'] = smart_lib[arcom] = _smartLib
185
186         # It is worth noting that the final space in these strings is
187         # absolutely pivotal.  SCons sees these as actions and not generators
188         # if it is not there. (very bad)
189         env['ARCOM'] = '$SMART_ARCOM '
190         env['LINKCOM'] = '$SMART_LINKCOM '
191     else: # assuming linux
192         linkcom = env.get('LINKCOM')
193         try:
194             env['SMART_LINKCOM'] = smart_link[linkcom]
195         except KeyError:
196             def _smartLink(source, target, env, for_signature,
197                            defaultLinker=linkcom, dc=dc):
198                 if isD(source):
199                     try:
200                         libs = env['LIBS']
201                     except KeyError:
202                         libs = []
203                     if 'phobos' not in libs:
204                         if dc is 'dmd':
205                             env.Append(LIBS = ['phobos'])
206                         elif dc is 'gdmd':
207                             env.Append(LIBS = ['gphobos'])
208                     if 'pthread' not in libs:
209                         env.Append(LIBS = ['pthread'])
210                     if 'm' not in libs:
211                         env.Append(LIBS = ['m'])
212                 return defaultLinker
213             env['SMART_LINKCOM'] = smart_link[linkcom] = _smartLink
214
215         env['LINKCOM'] = '$SMART_LINKCOM '
216
217 def exists(env):
218     return env.Detect(['dmd', 'gdmd'])