#!/usr/bin/python
import sys
import os
import Blender as B

def normpath(path):
    return os.path.abspath(os.path.normpath(path))

Import ('env')

oso_files = []

if env['WITH_BF_CYCLES_OSL']:
    shaders = env.Clone()

    # osl compiler
    osl_compiler = File(env.subst(env['BF_OSL_COMPILER'])).abspath

    # build directory
    root_build_dir = normpath(env['BF_BUILDDIR'])
    build_dir = os.path.join(root_build_dir, 'intern/cycles/kernel/shaders')

    # source directories and files
    source_dir = Dir('.').srcnode().path

    # dependencies
    dependencies = shaders.Glob('*.h')

    for f in os.listdir(source_dir):
        if f.endswith('.osl'):
            osl_file = os.path.join(source_dir, f)
            oso_file = os.path.join(build_dir, f.replace('.osl', '.oso'))

            command = "%s -O2 -I%s -o %s %s" % (osl_compiler, source_dir, oso_file, osl_file)

            shaders.Command(oso_file, f, command)
            shaders.Depends(oso_file, [f] + dependencies)

            oso_files.append(oso_file)


Return('oso_files')
