#
# Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/json
#

import common ;
import os ;
import path ;
import property ;

import feature ;

path-constant HERE : . ;

feature.feature bench.option : : free optional ;
feature.feature bench.launcher : : free optional ;
feature.feature bench.file : : free optional ;

.BENCH_FILES = [ os.environ BENCH_FILES ] ;

local has_nlohmann_json = [
    glob lib/nlohmann/single_include/nlohmann/json.hpp ] ;
if $(has_nlohmann_json)
{
    has_nlohmann_json = "" ;
}

local has_rapidjson = [
    glob lib/rapidjson/include/rapidjson/rapidjson.h ] ;
if $(has_rapidjson)
{
    has_rapidjson = "" ;
}

exe bench :
    bench.cpp
    /boost/json//boost_json
    :
    <include>../test
    $(has_nlohmann_json)<define>BOOST_JSON_HAS_NLOHMANN_JSON
    $(has_rapidjson)<define>BOOST_JSON_HAS_RAPIDJSON
    ;

install bench-local : bench : <location>. <hardcode-dll-paths>true ;
explicit bench-local ;
always bench-local ;

local bench-files = [ glob $(HERE)/data/*.json ] ;
notfile run : @run-bench : bench : : <bench.file>$(bench-files) ;
explicit run ;

rule run-bench ( target : sources * : props * )
{
    local flags = [ property.select flags : $(props) ] ;
    flags += [ property.select bench.option : $(props) ] ;
    FLAGS on $(target) = $(flags:G=) ;

    local launcher = [ property.select bench.launcher : $(props) ] ;
    if $(launcher)
    {
        launcher = "$(launcher:G=) " ;
        launcher = "$(launcher:J=) " ;
    }
    else
    {
        launcher = "" ;
    }
    LAUNCHER on $(target) = $(launcher) ;

    local files = [ property.select bench.file : $(props) ] ;
    FILES on $(target) = $(files:G=) ;

    local dir = [ on $(target) return $(LOCATE) ] ;
    if $(dir)
    {
        dir = [ path.make $(dir) ] ;
        dir = $(dir)/$(target:G=) ;
        common.MkDir $(dir) ;
        DEPENDS $(target) : $(dir) ;
    }
}

actions run-bench
{
    $(LAUNCHER)$(>) $(FLAGS) $(FILES)
}
