#! /usr/bin/env python
# encoding: utf-8

from waflib import Logs, ConfigSet

######################
###### options #######
######################
def options (opt):
	opt.add_option ("--with-optim", action="store_true", dest="WITH_OPTIM",
			help = "install IbexOptim plugin")

######################
##### configure ######
######################
def configure (conf):
	conf.env.WITH_OPTIM = conf.options.WITH_OPTIM

	conf.start_msg ("plugin IbexOpt")
	if not conf.env.WITH_OPTIM:
		conf.end_msg ("not used")
		return

	conf.end_msg ("enabled")

	conf.env.append_unique ("IBEX_PLUGIN_USE_LIST", "OPTIM")

	# check that conf.env.LP_LIB is not set to NONE (i.e., we need a LP library
	# for the optim plugin).
	if conf.env.LP_LIB == "NONE":
		conf.fatal ("Optim plugin requires a LP library (--lp-lib)")

	# We need -std=c++11 to compile ibexopt
	conf.check_cxx(cxxflags = "--std=c++11", uselib_store="IBEXOPT")

	# To fix Windows compilation problem (strdup with std=c++11, see issue #287)
	conf.check_cxx(cxxflags = "-U__STRICT_ANSI__", uselib_store="IBEXOPT")
	
	# Add information in ibex_Setting
	conf.setting_define ("WITH_OPTIM", 1)

	# add OPTIM plugin include directory
	for f in conf.path.ant_glob ("src/**", dir = True, src = False):
		conf.env.append_unique("INCLUDES_OPTIM", f.abspath())

	# The build and install steps will be run from the main src/wscript script so
	# we need to give path relative to the main src directory
	mainsrc = conf.srcnode.make_node ("src")

	# add OPTIM headers
	for f in conf.path.ant_glob ("src/**/ibex_*.h"):
		conf.env.append_unique ("IBEX_HDR", f.path_from (mainsrc))

	# add OPTIM source files
	for f in conf.path.ant_glob ("src/**/ibex_*.cpp"):
		conf.env.append_unique ("IBEX_SRC", f.path_from (mainsrc))

	# The utest step will be run from the main tests/wscript script so we need to
	# give path relative to the main tests directory
	maintests = conf.srcnode.make_node ("tests")

	# add OPTIM test files
	for f in conf.path.ant_glob ("tests/**/Test*.cpp"):
		conf.env.append_unique ('TEST_SRC', f.path_from (maintests))

	# Add optim/tests directory to list of INCLUDES for TESTS
	testsnode = conf.path.make_node ("tests")
	conf.env.append_unique ("INCLUDES_TESTS", testsnode.abspath ())

######################
####### build ########
######################
def build (bld):
	
	if bld.env.WITH_OPTIM:
		# build optim binary
		bld.program (
		target = "ibexopt",
		use = [ "ibex", "IBEXOPT" ], # add dependency on ibex library
		source = bld.path.ant_glob ("main/**/*.cpp"),
		install_path = bld.env.BINDIR,
		)

######################
##### benchmarks #####
######################
def benchmarks (bch):
	# Build the benchmark program
	bch.program (source = "benchmark_optim.cpp",
	             target = "benchmark_optim",
	             use = "ibex"
	            )

	gnuplotnode = bch.path.make_node ("benchmark_optim.gnuplot")
	# Benchmarks on all files ending with .bch in the 'benchs' subdirectory
	for category in bch.categories:
		bchfiles = bch.path.ant_glob ("benchs/%s/**/*.bch" % category)
		name = "optim_serie1_" + str(category)
		bch.benchmarks (source = bchfiles, bench_bin = "benchmark_optim",
		                graph_scriptfile = gnuplotnode.abspath(), name = name)
