import os, glob

Import("env")
Import("configuredPackages")
Import("modules")

cpptests = []

for modName, mod in modules.items():
  libs, libPath, linkOpts, cppPath, frameworks = [], [], [], [], []

  ## this is needed to find SyFi.h
  cppPath = [os.path.join("..", ".."),
             os.path.abspath(os.path.join("..", "..", modName.lower()))]
  for d in mod.dependencies:
    if d in modules:
      # Internal dependency
      libs.append(d)
      libPath.insert(0, modules[d].fullpath)
      cppPath.insert(0, modules[d].fullpath)
      
    elif d in configuredPackages:
      # External (configured) dependency
      dep = configuredPackages[d]
      libs += dep.libs[0]              # The libs
      frameworks += list(dep.libs[1])  # The frameworks (Darwin)
      libPath += dep.libPath
      linkOpts += dep.linkOpts
      cppPath += dep.cppPath

  modEnv = env.Copy(CXXFLAGS=mod.cxxFlags, LINKFLAGS=mod.linkFlags,
                    CPPPATH=Dir("#"), LIBPATH=mod.fullpath, LIBS=modName.lower())
  modEnv.Append(LIBPATH=libPath)
  modEnv.Append(CPPPATH=cppPath)
  modEnv.Append(LIBS=libs)
  modEnv.Append(LINKFLAGS=linkOpts)
  if env["PLATFORM"] == "darwin":
    modEnv.Append(FRAMEWORKS=frameworks)
    modEnv.Append(CXXFLAGS="-bind_at_load")
    modEnv.Append(LDFLAGS="-bind_at_load")
  
  # the cpp tests!
  for cppSource in glob.glob("*.cpp"):
    testName = os.path.splitext(cppSource)[0]
    cpptests += modEnv.Program(target=testName, source=cppSource)
        
Return("cpptests")
