import os
import inspect
import glob

# try to import an environment first
try:
  Import('env')
except:
  exec open("../build/build-env.py")
  env = Environment()

# find all .cus & .cpps in the current directory
sources = []
directories = ['.']
extensions = ['*.cu', '*.cpp']

if env['backend'] == 'cuda' or env['backend'] == 'ocelot':
  directories.append('cuda')
elif env['backend'] == 'tbb':
  directories.append('tbb')
elif env['backend'] == 'omp':
  directories.append('omp')
#TODO handle mixed/multiple systems

for dir in directories:
  for ext in extensions:
    regexp = os.path.join(dir, ext)
    sources.extend(glob.glob(regexp))

# compile examples
commands = []
for src in sources:
  program = env.Program(src)
  # add the program to the 'run_examples' alias
  program_alias = env.Alias('run_examples', [program], program[0].abspath)
  # always build the 'run_examples' target whether or not it needs it
  env.AlwaysBuild(program_alias)

