import sys
Import( "env_etc" )

if not env_etc.no_boost_python:
  Import("env_boost_python_ext")
  env = env_boost_python_ext.Clone()
  env_etc.include_registry.prepend(
    env = env,
    paths = [ env_etc.boost_adaptbx_include ],
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_ext",
    source="graph_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_connected_component_algorithm_ext",
    source="connected_component_algorithm_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_breadth_first_search_ext",
    source="breadth_first_search_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_graph_structure_comparison_ext",
    source="graph_structure_comparison_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_maximum_clique_ext",
    source="maximum_clique_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_min_cut_max_flow_ext",
    source="min_cut_max_flow_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_utility_ext",
    source="utility_ext.cpp",
    )
  env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_metric_ext",
    source="metric_ext.cpp",
    )
  # Xcode 11 wants forward iterators for std::max_element in Boost.Graph
  # https://github.com/boostorg/graph/issues/175
  clustering_obj = env.SharedObject("clustering_algorithm_ext.cpp")
  clustering = env.SharedLibrary(
    target="#lib/boost_adaptbx_graph_clustering_algorithm_ext",
    source=clustering_obj,
    )
  if env_etc.compiler == 'darwin_conda' or (sys.platform == 'darwin' and
    env_etc.clang_version[0] >= 11):
    import os
    from libtbx.auto_build.install_base_packages import installer
    filename = os.path.join(env_etc.boost_include, 'boost', 'graph',
                            'bc_clustering.hpp')
    def patch_bc_clustering(target=None, source=None, env=None):
      from libtbx.auto_build.install_base_packages import installer
      if os.path.isfile(filename):
        installer.patch_src(
          filename,
          "#include <algorithm>",
          "#include <boost/algorithm/minmax_element.hpp>")
        installer.patch_src(
          filename,
          "= *max_element(edges_iters.first, edges_iters.second, cmp);",
          "= *boost::first_max_element(edges_iters.first, edges_iters.second, cmp);")
    def unpatch_bc_clustering(target=None, source=None, env=None):
      if os.path.isfile(filename):
        installer.patch_src(
          filename,
          "#include <boost/algorithm/minmax_element.hpp>",
          "#include <algorithm>")
        installer.patch_src(
          filename,
          "= *boost::first_max_element(edges_iters.first, edges_iters.second, cmp);",
          "= *max_element(edges_iters.first, edges_iters.second, cmp);")
    # unpatch in case of broken builds
    unpatch_bc_clustering()
    env.AddPreAction(clustering_obj, patch_bc_clustering)
    env.AddPostAction(clustering, unpatch_bc_clustering)
