# Copyright (c) 2014 The renderer authors. All rights
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file.

from subprocess import call


# Sources for the karma app
karma_sources = '''
  renderer/common/initialize.cc
  renderer/common/render_handler.cc
  renderer/karma/app.cc
  renderer/karma/client.cc
  renderer/karma/main.cc
'''

# Prepare bin folder with a link to a mmap-ped data file
call(['mkdir', '-p', 'bin'])
call(['ln', '-s', '/cef/Resources/icudtl.dat', 'bin/icudtl.dat'])


# Flags for the compiler and linker
libs = ['cef_dll_wrapper', 'cef', 'glog', 'gflags', 'png']
flags = ['-Werror', '-Wall', '-std=c++11', '-DNDEBUG']
include_paths = ['#src', '/cef', '#obj']
lib_paths = ['/usr/local/lib', '/cef/build/libcef_dll', '/cef/Release']


# Prepare the environment with the gtk libraries
env = Environment()
env.ParseConfig('pkg-config --cflags --libs gtk+-2.0')
include_paths.append(env['CPPPATH'])


# Generates object files from sources, using a cache to avoid repetitions
# and WARNINGs from scons
obj_cache = {}
def create_objs(sources):
  objs = []
  for source in Split(sources):
    if not source in obj_cache:
      obj_cache[source] = env.Object(source, CPPPATH=include_paths, CPPFLAGS=flags)

    objs.append(obj_cache[source])

  return objs


# Prepare the objects of an app and compiles it
def compile(app, sources):
  env.Program('#bin/' + app, create_objs(sources), LIBS=libs, LIBPATH=lib_paths)


# Compile and link binaries
compile('karma', karma_sources)
