Import('env', 'brushlib')
import sys, os

try:
    import numpy
except ImportError:
    print 'You need to have numpy installed.'
    print
    raise

# Use a copy of the environment so new flags can be added
# without affecting the other builds.
mypaintlib_env = env.Clone()
env = mypaintlib_env

def build_py_module(env, *args, **kwargs):
    if sys.platform == "win32": # there 's a better way to do this
        kwargs["SHLIBSUFFIX"]=".pyd"
    elif sys.platform == "darwin":
        kwargs["SHLIBSUFFIX"]=".so"
    else:
        pass
    return env.SharedLibrary(*args, **kwargs)


# Build against brushlib
env.Prepend(LIBS=['mypaint-tests', "mypaint"])
env.Append(LIBPATH="../brushlib")
env.Append(CPPPATH=['../brushlib', '../brushlib/tests'])

# Normal dependencies
env.ParseConfig('pkg-config --cflags --libs glib-2.0')
env.ParseConfig('pkg-config --cflags --libs libpng')
env.ParseConfig('pkg-config --cflags --libs lcms2')

env.ParseConfig('pkg-config --cflags --libs gtk+-3.0')
env.Append(CPPDEFINES=['HAVE_GTK3']) # possibly useful while we're porting
pygobject = 'pygobject-3.0'   # keep in step?

if env['enable_openmp']:
    env.Append(CXXFLAGS=['-fopenmp'])
    env.Append(LINKFLAGS=['-fopenmp'])

env.ParseConfig('pkg-config --cflags --libs ' + pygobject)

# Get the numpy include path (for numpy/arrayobject.h).
numpy_path = numpy.get_include()
env.Append(CPPPATH=numpy_path)


# Python dependencies


if sys.platform == "win32":
    # official python shipped with no pc file on windows so get from current python
    from distutils import sysconfig
    pre,inc = sysconfig.get_config_vars('exec_prefix', 'INCLUDEPY')
    env.Append(CPPPATH=inc, LIBPATH=pre+'\libs', LIBS='python'+sys.version[0]+sys.version[2])
else:
    # some distros use python2.x-config, others python-config2.x
    try:
        env.ParseConfig(env['python_config'] + ' --cflags')
        env.ParseConfig(env['python_config'] + ' --ldflags')
    except OSError:
        print '%r does not work, trying python-config instead' % env['python_config']
        env.ParseConfig('python-config --ldflags')
        env.ParseConfig('python-config --cflags')


# Renable some build flags that python-config etc. may have turned off.
# Duplicates some stuff from ../SConstruct, but needed.

# Make sure assertions are enabled
if env.get('CPPDEFINES'):
    env['CPPDEFINES'].remove('NDEBUG')

# Override Python's optimization flags if we're in HEAVY_DEBUG mode.
if env['debug']:
    env.Append(CCFLAGS='-O0', LINKFLAGS='-O0')


# python extension module

env.Append(SWIGFLAGS="-Wall -noproxydel -python -c++")
#env.Append(SWIGCXXFILESUFFIX="_wrap.cpp") #No: SCons 2.3.1 removes ".ext" (!)
module_src = [
        'mypaintlib.i',
        'fill.cpp',
        'eventhack.cpp',
        'gdkpixbuf2numpy.cpp',
        'pixops.cpp',
    ]
module = build_py_module(
    env,
    target='#_mypaintlib',
    source=module_src,
    SHLIBPREFIX="",
    )
env.Requires("mypaintlib_wrap.cc", brushlib)  # establish order
env.Requires(module, brushlib)  # just to be certain
env.Depends(module, brushlib)   # not sufficient by itself

Return('module')

# vim:syntax=python
