#
# Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot 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)
#

import os ;
import sequence ;

path-constant this_dir : . ;

# The hostname to use for examples
local hostname = [ os.environ BOOST_MYSQL_SERVER_HOST ] ;
if $(hostname) = ""
{
    hostname = "127.0.0.1" ;
}

# Builds and run a "regular" example
rule run_regular_example (
    example_name :
    sources * :
    args *
)
{
    local arg_str = [ sequence.join $(args) : " " ] ;
    run
        /boost/mysql/test//boost_mysql_compiled
        /boost/mysql/test//launch_with_valgrind
        $(sources)
    : requirements
        <testing.arg>$(arg_str)
    : target-name $(example_name)
    ;
}

# Builds and runs a example that needs a Python runner
rule run_python_example (
    example_name :
    python_runner :
    sources *
)
{
    run
            $(sources)
            /boost/mysql/test//boost_mysql_compiled
        : requirements
            <testing.launcher>"python $(this_dir)/private/$(python_runner)"
            <testing.arg>$(hostname)
        : target-name $(example_name)
    ;
}

local regular_args = example_user example_password $(hostname) ;

# Tutorials
run_regular_example tutorial_sync             : 1_tutorial/1_sync.cpp             : $(regular_args) ;
run_regular_example tutorial_async            : 1_tutorial/2_async.cpp            : $(regular_args) ;
run_regular_example tutorial_with_params      : 1_tutorial/3_with_params.cpp      : $(regular_args) 1 ;
run_regular_example tutorial_static_interface : 1_tutorial/4_static_interface.cpp : $(regular_args) 1 ;

# Simple examples
run_regular_example callbacks : 2_simple/callbacks.cpp : $(regular_args) ;
run_regular_example coroutines_cpp11 : 2_simple/coroutines_cpp11.cpp /boost/mysql/test//boost_context_lib : $(regular_args) ;
run_python_example batch_inserts : run_batch_inserts.py : 2_simple/batch_inserts.cpp /boost/mysql/test//boost_json_lib ;
run_python_example batch_inserts_generic : run_batch_inserts.py : 2_simple/batch_inserts_generic.cpp /boost/mysql/test//boost_json_lib ;
run_python_example patch_updates : run_patch_updates.py : 2_simple/patch_updates.cpp ;
run_python_example dynamic_filters : run_dynamic_filters.py : 2_simple/dynamic_filters.cpp /boost/mysql/test//boost_context_lib ;
run_regular_example disable_tls : 2_simple/disable_tls.cpp :   $(regular_args) ;
run_regular_example tls_certificate_verification : 2_simple/tls_certificate_verification.cpp :   $(regular_args) ;
run_regular_example metadata : 2_simple/metadata.cpp :   $(regular_args) ;
run_regular_example prepared_statements : 2_simple/prepared_statements.cpp :   $(regular_args) "HGS" ;
run_regular_example timeouts : 2_simple/timeouts.cpp :   $(regular_args) "HGS" ;
run_regular_example pipeline : 2_simple/pipeline.cpp :   $(regular_args) "HGS" ;
run_regular_example multi_function : 2_simple/multi_function.cpp :   $(regular_args) ;
run_regular_example multi_queries_transactions : 2_simple/multi_queries_transactions.cpp :   $(regular_args) 1 "John" ;
run_regular_example source_script : 2_simple/source_script.cpp :  $(regular_args) $(this_dir)/private/test_script.sql ;

# UNIX. Don't run under Windows systems
run
    2_simple/unix_socket.cpp
    /boost/mysql/test//boost_mysql_compiled
    /boost/mysql/test//launch_with_valgrind
:
    requirements
        <target-os>windows:<build>no
        <testing.arg>"example_user example_password"
;

# Connection pool
run
        3_advanced/connection_pool/main.cpp
        3_advanced/connection_pool/repository.cpp
        3_advanced/connection_pool/handle_request.cpp
        3_advanced/connection_pool/server.cpp
        /boost/mysql/test//boost_mysql_compiled
        /boost/mysql/test//boost_context_lib
        /boost/mysql/test//boost_json_lib
        /boost/url//boost_url
        /boost/mysql/test//boost_beast_lib
    : requirements
            <testing.launcher>"python $(this_dir)/private/run_connection_pool.py"
            <testing.arg>$(hostname)
            # MSVC 14.1 fails with an internal compiler error while building server.cpp for this config
            <toolset>msvc-14.1,<address-model>32,<cxxstd>17,<variant>release:<build>no
            # Uses heavily Boost.Context coroutines, which aren't fully supported by asan
            <address-sanitizer>norecover:<build>no
            <address-sanitizer>enable:<build>no
    : target-name boost_mysql_example_connection_pool
    ;
