Add a D language Tool. (Andy Friesen)
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 8 Mar 2004 03:55:27 +0000 (03:55 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 8 Mar 2004 03:55:27 +0000 (03:55 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@919 fdb21ef1-2011-0410-befe-b5e4ea1792b1

bin/files
doc/man/scons.1
src/CHANGES.txt
src/engine/MANIFEST.in
src/engine/SCons/Defaults.py
src/engine/SCons/Scanner/D.py [new file with mode: 0644]
src/engine/SCons/Tool/__init__.py
src/engine/SCons/Tool/dmd.py [new file with mode: 0644]
test/DMD.py [new file with mode: 0644]
test/import.py

index e15853ff716bc6c6a13bfbb23f9df975e6998c5e..08b1caa3bdff15b56566aac9e4d314a16477b40a 100644 (file)
--- a/bin/files
+++ b/bin/files
@@ -27,6 +27,7 @@
 ./SCons/Platform/sunos.py
 ./SCons/Platform/win32.py
 ./SCons/Scanner/C.py
+./SCons/Scanner/D.py
 ./SCons/Scanner/Fortran.py
 ./SCons/Scanner/IDL.py
 ./SCons/Scanner/Prog.py
@@ -47,6 +48,8 @@
 ./SCons/Tool/bcc32.py
 ./SCons/Tool/c++.py
 ./SCons/Tool/cc.py
+./SCons/Tool/CVS.py
+./SCons/Tool/dmd.py
 ./SCons/Tool/default.py
 ./SCons/Tool/dvipdf.py
 ./SCons/Tool/dvips.py
index bea43c32e501b9352bef204a3fd00a0cb7f71dbf..2d7886b79f18f45af641b15acb6653a38da84111 100644 (file)
@@ -958,6 +958,7 @@ as
 bcc32
 c++
 cc
+dmd
 dvipdf
 dvips
 f77
@@ -1506,9 +1507,9 @@ env.PostScript(target = 'bbb', source = 'bbb.dvi')
 '\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 .IP Program()
 .IP env.Program()
-Builds an executable given one or more object files or C, C++
-or Fortran source files.
-If any C, C++ or Fortran source files are specified,
+Builds an executable given one or more object files
+or C, C++, D, or Fortran source files.
+If any C, C++, D or Fortran source files are specified,
 then they will be automatically
 compiled to object files using the
 .B Object
@@ -1586,7 +1587,7 @@ env.RMIC(target = 'outdir3',
 Builds a shared library
 (.so on a POSIX system, .dll on WIN32)
 given one or more object files
-or C, C++ or Fortran source files.
+or C, C++, D or Fortran source files.
 If any source files are given,
 then they will be automatically
 compiled to object files.
@@ -1676,7 +1677,7 @@ env.SharedObject(target = 'fff.obj', source = 'fff.for')
 .IP StaticLibrary()
 .IP env.StaticLibrary()
 Builds a static library given one or more object files
-or C, C++ or Fortran source files.
+or C, C++, D or Fortran source files.
 If any source files are given,
 then they will be automatically
 compiled to object files.
@@ -1709,7 +1710,7 @@ will raise an error if there is any mismatch.
 .IP StaticObject()
 .IP env.StaticObject()
 Builds a static object file
-from one or more C, C++, or Fortran source files.
+from one or more C, C++, D, or Fortran source files.
 Source files must have one of the following extensions:
 
 .ES
@@ -1724,6 +1725,7 @@ Source files must have one of the following extensions:
   .cxx    C++ file
   .c++    C++ file
   .C++    C++ file
+  .d      D file
   .f      Fortran file
   .F      WIN32:  Fortran file
           POSIX:  Fortran file + C pre-processor
index e2d1b9708a3cf5d6dcf765a5c51cef95bd447052..08817e8ac93b993aa8bc1fb1b0c17e23e025b3b5 100644 (file)
@@ -73,6 +73,10 @@ RELEASE 0.95 - XXX
   - Preserve the ability to call BuildDir() multiple times with the
     same target and source directory arguments.
 
+  From Andy Friesen:
+
+  - Add support for the Digital Mars "D" programming language.
+
   From Scott Lystig Fritchie:
 
   - Fix the ability to use a custom _concat() function in the
index 4338e891a5788e2d5dae9abd3168769ad4ef532b..a8bac0a2cc6d457546d8a5ce873f18e5393ac716 100644 (file)
@@ -34,6 +34,7 @@ SCons/Platform/sunos.py
 SCons/Platform/win32.py
 SCons/Scanner/__init__.py
 SCons/Scanner/C.py
+SCons/Scanner/D.py
 SCons/Scanner/Fortran.py
 SCons/Scanner/IDL.py
 SCons/Scanner/Prog.py
@@ -57,6 +58,7 @@ SCons/Tool/BitKeeper.py
 SCons/Tool/c++.py
 SCons/Tool/cc.py
 SCons/Tool/CVS.py
+SCons/Tool/dmd.py
 SCons/Tool/default.py
 SCons/Tool/dvipdf.py
 SCons/Tool/dvips.py
index 78103ef83989c759d52573e386b8005579de2fc1..4c7b0cc987ae7cb10f06ff306d7102f96689f87f 100644 (file)
@@ -47,6 +47,7 @@ import SCons.Action
 import SCons.Builder
 import SCons.Environment
 import SCons.Scanner.C
+import SCons.Scanner.D
 import SCons.Scanner.Fortran
 import SCons.Scanner.Prog
 import SCons.Sig
@@ -95,10 +96,12 @@ SharedCheck = SCons.Action.Action(SharedFlagChecker, None)
 
 # Scanners and actions for common language(s).
 CScan = SCons.Scanner.C.CScan()
+DScan = SCons.Scanner.D.DScan()
 
 FortranScan = SCons.Scanner.Fortran.FortranScan()
 
 CAction = SCons.Action.Action("$CCCOM")
+DAction = SCons.Action.Action("$DCOM")
 ShCAction = SCons.Action.Action("$SHCCCOM")
 CXXAction = SCons.Action.Action("$CXXCOM")
 ShCXXAction = SCons.Action.Action("$SHCXXCOM")
@@ -249,7 +252,7 @@ class NullCmdGenerator:
 
 ConstructionEnvironment = {
     'BUILDERS'   : {},
-    'SCANNERS'   : [CScan, FortranScan],
+    'SCANNERS'   : [CScan, FortranScan, DScan],
     'PDFPREFIX'  : '',
     'PDFSUFFIX'  : '.pdf',
     'PSPREFIX'   : '',
diff --git a/src/engine/SCons/Scanner/D.py b/src/engine/SCons/Scanner/D.py
new file mode 100644 (file)
index 0000000..28f3c29
--- /dev/null
@@ -0,0 +1,54 @@
+"""SCons.Scanner.D
+
+Scanner for the Digital Mars "D" programming language.
+
+Coded by Andy Friesen
+17 Nov 2003
+
+"""
+
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+import SCons.Scanner
+
+def DScan(fs = SCons.Node.FS.default_fs):
+    """Return a prototype Scanner instance for scanning D source files"""
+    ds = DScanner(name = "DScan",
+                  suffixes = ['.d'],
+                  path_variable = 'DPATH',
+                  regex = 'import\s+([^\;]*)\;',
+                  fs = fs)
+    return ds
+
+class DScanner(SCons.Scanner.Classic):
+    def find_include(self, include, source_dir, path):
+        # translate dots (package separators) to slashes
+        inc = include.replace('.', '/')
+
+        i = SCons.Node.FS.find_file(inc + '.d',
+                                    (source_dir,) + path,
+                                    self.fs.File)
+        return i, include
index 97da344cd6216cb4dbb518fa7c83591b13bbfe91..6ddc537274c95c88f9776b7449fbdc23fc5951d5 100644 (file)
@@ -323,6 +323,7 @@ def tool_list(platform, env):
         ar = FindTool(ars, env) or ars[0]
 
     other_tools = FindAllTools(['BitKeeper', 'CVS',
+                                'dmd',
                                 'dvipdf', 'dvips', 'gs',
                                 'jar', 'javac', 'javah',
                                 'latex', 'lex', 'm4', 'midl', 'msvs',
diff --git a/src/engine/SCons/Tool/dmd.py b/src/engine/SCons/Tool/dmd.py
new file mode 100644 (file)
index 0000000..cf0e050
--- /dev/null
@@ -0,0 +1,196 @@
+"""SCons.Tool.dmd
+
+Tool-specific initialization for the Digital Mars D compiler.
+(http://digitalmars.com/d)
+
+Coded by Andy Friesen (andy@ikagames.com)
+15 November 2003
+
+There are a number of problems with this script at this point in time.
+The one that irritates me the most is the win32 linker setup.  The D
+linker doesn't have a way to add lib paths on the commandline, as far
+as I can see.  You have to specify paths relative to the SConscript or
+use absolute paths.  To hack around it, add '#/blah'.  This will link
+blah.lib from the directory where SConstruct resides.
+
+Compiler variables:
+    DC - The name of the D compiler to use.  Defaults to dmd.
+    DPATH - List of paths to search for import modules.
+    DVERSIONS - List of version tags to enable when compiling.
+    DDEBUG - List of debug tags to enable when compiling.
+
+Linker related variables:
+    LIBS - List of library files to link in.
+    DLINK - Name of the linker to use.  Defaults to dmd.
+    DLINKFLAGS - List of linker flags.
+
+Lib tool variables:
+    DLIB - Name of the lib tool to use.  Defaults to lib.
+    DLIBFLAGS - List of flags to pass to the lib tool.
+    LIBS - Same as for the linker. (libraries to pull into the .lib)
+"""
+
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+import os
+import string
+
+import SCons.Tool
+import SCons.Scanner.D
+import SCons.Builder
+
+# Adapted from c++.py
+def isD(source):
+    if not source:
+        return 0
+
+    for s in source:
+        if s.sources:
+            ext = os.path.splitext(str(s.sources[0]))[1]
+            if ext == '.d':
+                return 1
+    return 0
+
+smart_link = {}
+
+smart_lib = {}
+
+def generate(env):
+    global smart_link
+    global smart_lib
+
+    static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
+
+    static_obj.add_action('.d', '$DCOM')
+    shared_obj.add_action('.d', '$DCOM')
+
+    env['DC'] = 'dmd'
+    env['DCOM'] = '$DC $_DINCFLAGS $_DVERFLAGS $_DDEBUGFLAGS $_DFLAGS -c -of$TARGET $SOURCES'
+    env['_DINCFLAGS'] = '$( ${_concat(DINCPREFIX, DPATH, DINCSUFFIX, __env__, RDirs)}  $)'
+    env['_DVERFLAGS'] = '$( ${_concat(DVERPREFIX, DVERSIONS, DVERSUFFIX, __env__)}  $)'
+    env['_DDEBUGFLAGS'] = '$( ${_concat(DDEBUGPREFIX, DDEBUG, DDEBUGSUFFIX, __env__)} $)'
+    env['_DFLAGS'] = '$( ${_concat(DFLAGPREFIX, DFLAGS, DFLAGSUFFIX, __env__)} $)'
+
+    env['DPATH'] = ['#/']
+    env['DFLAGS'] = []
+    env['DVERSIONS'] = []
+    env['DDEBUG'] = []
+
+    # Add the path to the standard library.
+    # This is merely for the convenience of the dependency scanner.
+    dmd_path = env.WhereIs('dmd')
+    if dmd_path:
+        x = string.rindex(dmd_path, 'dmd')
+        phobosDir = dmd_path[:x] + '/../src/phobos'
+        if os.path.isdir(phobosDir):
+            env.Append(DPATH = [phobosDir])
+
+    env['DINCPREFIX'] = '-I'
+    env['DINCSUFFIX'] = ''
+    env['DVERPREFIX'] = '-version='
+    env['DVERSUFFIX'] = ''
+    env['DDEBUGPREFIX'] = '-debug='
+    env['DDEBUGSUFFIX'] = ''
+    env['DFLAGPREFIX'] = '-'
+    env['DFLAGSUFFIX'] = ''
+    env['DFILESUFFIX'] = '.d'
+
+    # Need to use the Digital Mars linker/lib on windows.
+    # *nix can just use GNU link.
+    if env['PLATFORM'] == 'win32':
+        env['DLINK'] = '$DC'
+        env['DLINKCOM'] = '$DLINK -of$TARGET $SOURCES $DFLAGS $DLINKFLAGS $_DLINKLIBFLAGS'
+        env['DLIB'] = 'lib'
+        env['DLIBCOM'] = '$DLIB $_DLIBFLAGS -c $TARGET $SOURCES $_DLINKLIBFLAGS'
+
+        env['_DLINKLIBFLAGS'] = '$( ${_concat(DLIBLINKPREFIX, LIBS, DLIBLINKSUFFIX, __env__, RDirs)} $)'
+        env['_DLIBFLAGS'] = '$( ${_concat(DLIBFLAGPREFIX, DLIBFLAGS, DLIBFLAGSUFFIX, __env__)} $)'
+        env['DLINKFLAGS'] = []
+        env['DLIBLINKPREFIX'] = ''
+        env['DLIBLINKSUFFIX'] = '.lib'
+        env['DLIBFLAGPREFIX'] = '-'
+        env['DLIBFLAGSUFFIX'] = ''
+        env['DLINKFLAGPREFIX'] = '-'
+        env['DLINKFLAGSUFFIX'] = ''
+
+        static_lib = SCons.Tool.createStaticLibBuilder(env)
+
+        # Basically, we hijack the link and ar builders with our own.
+        # these builders check for the presence of D source, and swap out
+        # the system's defaults for the Digital Mars tools.  If there's no D
+        # source, then we silently return the previous settings.
+        linkcom = env.get('LINKCOM')
+        try:
+            env['SMART_LINKCOM'] = smart_link[linkcom]
+        except KeyError:
+            def _smartLink(source, target, env, for_signature,
+                           defaultLinker=linkcom):
+                if isD(source):
+                    return '$DLINKCOM'
+                else:
+                    return defaultLinker
+            env['SMART_LINKCOM'] = smart_link[linkcom] = _smartLink
+
+        arcom = env.get('ARCOM')
+        try:
+            env['SMART_ARCOM'] = smart_lib[arcom]
+        except KeyError:
+            def _smartLib(source, target, env, for_signature,
+                         defaultLib=arcom):
+                if isD(source):
+                    return '$DLIBCOM'
+                else:
+                    return defaultLib
+            env['SMART_ARCOM'] = smart_lib[arcom] = _smartLib
+
+        # It is worth noting that the final space in these strings is
+        # absolutely pivotal.  SCons sees these as actions and not generators
+        # if it is not there. (very bad)
+        env['ARCOM'] = '$SMART_ARCOM '
+        env['LINKCOM'] = '$SMART_LINKCOM '
+    else: # assuming linux
+        linkcom = env.get('LINKCOM')
+        try:
+            env['SMART_LINKCOM'] = smart_link[linkcom]
+        except KeyError:
+            def _smartLink(source, target, env, for_signature,
+                           defaultLinker=linkcom):
+                if isD(source):
+                    try:
+                        libs = env['LIBS']
+                    except KeyError:
+                        libs = []
+                    if 'phobos' not in libs:
+                        env.Append(LIBS = ['phobos'])
+                    if 'pthread' not in libs:
+                        env.Append(LIBS = ['pthread'])
+                return defaultLinker
+            env['SMART_LINKCOM'] = smart_link[linkcom] = _smartLink
+
+        env['LINKCOM'] = '$SMART_LINKCOM '
+
+def exists(env):
+    return env.Detect('dmd')
diff --git a/test/DMD.py b/test/DMD.py
new file mode 100644 (file)
index 0000000..9bad5f4
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+import os
+import string
+import sys
+import TestSCons
+
+_exe = TestSCons._exe
+test = TestSCons.TestSCons()
+
+dmd = test.where_is('dmd')
+if not dmd:
+    print "dmd not found, skipping test"
+    test.pass_test(1)
+
+test.write('SConstruct', """\
+import os
+env = Environment(ENV=os.environ)
+if env['PLATFORM'] == 'cygwin': env['OBJSUFFIX'] = '.obj'  # trick DMD
+env.Program('foo', 'foo.d')
+""")
+
+test.write('foo.d', """\
+int main(char[][] args) {
+    printf("Hello!\n");
+    return 0;
+}
+""")
+
+test.run()
+
+test.run(program=test.workpath('foo'+_exe))
+
+test.fail_test(not test.stdout() in ["Hello!\n", "Hello!\r\n"])
+
+test.pass_test()
index ff7200c4f445200509dc24867888f26169119025..6d154ae8d34e4fac4ffacafb012c451e016728e2 100644 (file)
@@ -57,6 +57,7 @@ tools = [
     'cc',
     'CVS',
     'default',
+    'dmd',
     'dvipdf',
     'dvips',
     'f77',