import os
import sys
import re
import subprocess
import glob

from SCons.Script import *


default_exclude = set('''
  advapi32.dll kernel32.dll msvcrt.dll ole32.dll user32.dll ws2_32.dll
  comdlg32.dll gdi32.dll imm32.dll oleaut32.dll shell32.dll winmm.dll
  winspool.drv wldap32.dll ntdll.dll d3d9.dll mpr.dll crypt32.dll dnsapi.dll
  shlwapi.dllversion.dll iphlpapi.dll msimg32.dll setupapi.dll
  opengl32.dll glu32.dll wsock32.dll ws32.dll gdiplus.dll usp10.dll
  comctl32.dll'''.split())


def find_in_path(filename):
    for path in os.environ["PATH"].split(os.pathsep):

        candidate = os.path.join(path.strip('"'), filename)
        if os.path.isfile(candidate): return candidate

        for name in os.listdir(path):
            if name.lower() == filename.lower():
                return os.path.join(path, name)


def find_dlls(env, path, exclude = set()):
    prog = env.get('FIND_DLLS_OBJDUMP')
    cmd = [prog, '-p', path]
    p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
    out, err = p.communicate()

    if p.returncode:
        raise Exception('Call to %s failed: %s' % (prog, err))

    for line in out.splitlines():
        if line.startswith('\tDLL Name: '):
            lib = line[11:].strip().lower()
        else: continue

        if not lib in exclude:
            exclude.add(lib)

            path = find_in_path(lib)
            if path is None:
                if env.get('FIND_DLLS_IGNORE_MISSING'): continue
                raise Exception('Lib "%s" not found' % lib)

            yield path
            for path in find_dlls(env, path, exclude):
                yield path


def FindDLLs(env, source):
    if env.get('FIND_DLLS_DEFAULT_EXCLUDES'): exclude = set(default_exclude)
    else: exclude = set()

    for src in source:
        for path in glob.glob(env.subst(str(src))):
            for dll in find_dlls(env, path, exclude):
                yield dll


def generate(env):
    env.SetDefault(FIND_DLLS_DEFAULT_EXCLUDES = True)
    env.SetDefault(FIND_DLLS_IGNORE_MISSING = True)
    env.SetDefault(FIND_DLLS_OBJDUMP = 'objdump')

    env.AddMethod(FindDLLs)


def exists():
    return True

@@ -863,24 +863,171 @@
 e = set()):%0A
+    if env%5B'PLATFORM'%5D == 'win32':%0A        prog = env.get('FIND_DLLS_DUMPBIN')%0A        cmd = %5Bprog, '/DEPENDENTS', '/NOLOGO', path%5D%0A%0A    else:%0A    
     prog = e
@@ -1054,16 +1054,20 @@
 JDUMP')%0A
+    
     cmd 
@@ -1087,16 +1087,18 @@
 , path%5D%0A
+%0A%0A
     p = 
@@ -1343,51 +1343,139 @@
  '):
-%0A            lib = line%5B11:%5D.strip().lower(
+ lib = line%5B11:%5D%0A        else:%0A            m = re.match(r'%5E%5Cs+(%5B%5E%5Cs.%5D+%5C.%5BdD%5D%5BlL%5D%5BlL%5D)%5Cs*$', line)%0A            if m: lib = m.group(1
 )%0A  
@@ -1476,24 +1476,28 @@
 (1)%0A        
+    
 else: contin
@@ -1500,16 +1500,51 @@
 ntinue%0A%0A
+        lib = lib.strip().lower()%0A%0A
         
@@ -2357,16 +2357,66 @@
 bjdump')
+%0A    env.SetDefault(FIND_DLLS_DUMPBIN = 'dumpbin')
 %0A%0A    en

Use dumpbin on Windows