# Copyright (c) 2023 Klemens D. Morgenstern
#
# 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 feature ;
import ../../config/checks/config : requires ;


project : requirements
  <define>BOOST_ASIO_NO_DEPRECATED
  <toolset>msvc:<define>_SCL_SECURE_NO_WARNINGS
  <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
  <toolset>msvc:<cxxflags>/bigobj
  <target-os>windows:<define>WIN32_LEAN_AND_MEAN
  <target-os>linux:<linkflags>-lpthread
  : source-location ../src
;


feature.feature boost.cobalt.pmr : std boost-container custom no : propagated composite ;
feature.compose <boost.cobalt.pmr>std             : <define>BOOST_COBALT_USE_STD_PMR=1 ;
feature.compose <boost.cobalt.pmr>boost-container : <define>BOOST_COBALT_USE_BOOST_CONTAINER_PMR=1 ;
feature.compose <boost.cobalt.pmr>custom          : <define>BOOST_COBALT_USE_CUSTOM_PMR=1 ;
feature.compose <boost.cobalt.pmr>no              : <define>BOOST_COBALT_NO_PMR=1 ;

feature.feature boost.cobalt.executor : any_io_executor use_io_context custom : propagated composite ;
feature.compose <boost.cobalt.executor>any_io_executor : ;
feature.compose <boost.cobalt.executor>use_io_context  : <define>BOOST_COBALT_USE_IO_CONTEXT=1 ;
feature.compose <boost.cobalt.executor>custom_executor : <define>BOOST_COBALT_CUSTOM_EXECUTOR=1 ;

local config-binding = [ modules.binding config ] ;
config-binding ?= "" ;

alias cobalt_sources
   : detail/exception.cpp
     detail/util.cpp
     channel.cpp
     error.cpp
     main.cpp
     this_thread.cpp
     thread.cpp
   ;

explicit cobalt_sources ;

lib boost_cobalt
   : cobalt_sources
   : requirements <define>BOOST_COBALT_SOURCE=1
     <link>shared:<define>BOOST_COBALT_DYN_LINK=1
     [ requires 
        cxx20_hdr_concepts
     ]
     <boost.cobalt.pmr>boost-container:<library>/boost//container
      [ check-target-builds
          $(config-binding:D)//cpp_lib_memory_resource
          cpp_lib_memory_resource
      : <conditional>@set-pmr-std
      : <conditional>@set-pmr-boost
      ]

   : usage-requirements
     <boost.cobalt.pmr>boost-container:<library>/boost//container
     <link>shared:<define>BOOST_COBALT_DYN_LINK=1
      [ check-target-builds
          $(config-binding:D)//cpp_lib_memory_resource
          cpp_lib_memory_resource
      : <conditional>@set-pmr-std
      : <conditional>@set-pmr-boost
      ]
  ;

rule set-pmr-boost ( props * )
{
    if ! <boost.cobalt.pmr> in $(props:G)
    {
        return <boost.cobalt.pmr>boost-container ;
    }

    if <boost.cobalt.pmr>boost-container in $(props)
    {
        return <boost.cobalt.pmr>boost-container ;
    }
}

rule set-pmr-std ( props * )
{
    if ! <boost.cobalt.pmr> in $(props:G)
    {
        return <boost.cobalt.pmr>std ;
    }

    if <boost.cobalt.pmr>std in $(props)
    {
        return <boost.cobalt.pmr>std ;
    }
}

boost-install boost_cobalt ;

