all_sources = bld.path.ant_glob('*.cpp scene-ideas/*.cc scene-terrain/*.cpp')
common_sources = [f for f in all_sources if f.name.find('canvas-') == -1 and
                                            f.name.find('android') == -1 and
                                            f.name.find('native-state-') == -1 and
                                            f.name.find('gl-state-') == -1]
common_uselibs = ['libpng']
common_defines = ['USE_EXCEPTIONS']

libmatrix_sources = [f for f in bld.path.ant_glob('libmatrix/*.cc')
                     if not f.name.endswith('test.cc')]
flavor_sources = {
  'x11-gl' : ['canvas-generic.cpp', 'native-state-x11.cpp', 'gl-state-glx.cpp'],
  'x11-glesv2' : ['canvas-generic.cpp', 'native-state-x11.cpp', 'gl-state-egl.cpp'],
  'drm-gl' : ['canvas-generic.cpp', 'native-state-drm.cpp', 'gl-state-egl.cpp'],
  'drm-glesv2' : ['canvas-generic.cpp', 'native-state-drm.cpp', 'gl-state-egl.cpp'],
  'mir-gl' : ['canvas-generic.cpp', 'native-state-mir.cpp', 'gl-state-egl.cpp'],
  'mir-glesv2' : ['canvas-generic.cpp', 'native-state-mir.cpp', 'gl-state-egl.cpp'],
  'wayland-gl' : ['canvas-generic.cpp', 'native-state-wayland.cpp', 'gl-state-egl.cpp'],
  'wayland-glesv2' : ['canvas-generic.cpp', 'native-state-wayland.cpp', 'gl-state-egl.cpp']
}
flavor_uselibs = {
  'x11-gl' : ['x11', 'gl', 'matrix-gl'],
  'x11-glesv2' : ['x11', 'egl', 'glesv2', 'matrix-glesv2'],
  'drm-gl' : ['drm', 'gbm', 'egl', 'gl', 'matrix-gl'],
  'drm-glesv2' : ['drm', 'gbm', 'egl', 'glesv2', 'matrix-gl'],
  'mir-gl' : ['mirclient', 'egl', 'gl', 'matrix-gl'],
  'mir-glesv2' : ['mirclient', 'egl', 'glesv2', 'matrix-gl'],
  'wayland-gl' : ['wayland-client', 'wayland-egl', 'egl', 'gl', 'matrix-gl'],
  'wayland-glesv2' : ['wayland-client', 'wayland-egl', 'egl', 'glesv2', 'matrix-gl']
}
flavor_defines = {
  'x11-gl' : ['GLMARK2_USE_X11', 'GLMARK2_USE_GL', 'GLMARK2_USE_GLX'],
  'x11-glesv2' : ['GLMARK2_USE_X11', 'GLMARK2_USE_GLESv2', 'GLMARK2_USE_EGL'],
  'drm-gl' : ['GLMARK2_USE_DRM', 'GLMARK2_USE_GL', 'GLMARK2_USE_EGL', '__GBM__'],
  'drm-glesv2' : ['GLMARK2_USE_DRM', 'GLMARK2_USE_GLESv2', 'GLMARK2_USE_EGL', '__GBM__'],
  'mir-gl' : ['GLMARK2_USE_MIR', 'GLMARK2_USE_GL', 'GLMARK2_USE_EGL'],
  'mir-glesv2' : ['GLMARK2_USE_MIR', 'GLMARK2_USE_GLESv2', 'GLMARK2_USE_EGL'],
  'wayland-gl' : ['GLMARK2_USE_WAYLAND', 'GLMARK2_USE_GL', 'GLMARK2_USE_EGL'],
  'wayland-glesv2' : ['GLMARK2_USE_WAYLAND', 'GLMARK2_USE_GLESv2', 'GLMARK2_USE_EGL']
}

includes = ['.', 'scene-ideas', 'scene-terrain']

all_uselibs = set()

for name in bld.env.keys():
    if name.startswith('FLAVOR_') and bld.env[name]:
        flavor = name.replace('FLAVOR_', '').lower().replace('_', '-')
        target = bld.env[name]
        bld(
            features     = ['cxx', 'cprogram'],
            source       = common_sources + flavor_sources[flavor],
            target       = target,
            use          = common_uselibs + flavor_uselibs[flavor],
            lib          = ['m', 'jpeg', 'dl'],
            includes     = includes,
            defines      = common_defines + flavor_defines[flavor]
            )
        all_uselibs |= set(flavor_uselibs[flavor])
        
if 'matrix-gl' in all_uselibs:
    bld(
        features = ['cxx', 'cxxstlib'],
        source   = libmatrix_sources,
        target   = 'matrix-gl',
        lib      = ['m'],
        includes = ['.'],
        export_includes = 'libmatrix',
        defines  = ['GLMARK2_USE_GL', 'USE_EXCEPTIONS']
        )

if 'matrix-glesv2' in all_uselibs:
    bld(
        features = ['cxx', 'cxxstlib'],
        source   = libmatrix_sources,
        target   = 'matrix-glesv2',
        lib      = ['m'],
        includes = ['.'],
        export_includes = 'libmatrix',
        defines  = ['GLMARK2_USE_GLESv2', 'USE_EXCEPTIONS']
        )
