# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder

name = "CutPursuitBuilder"
version = v"0.0.3"

# Collection of sources required to build CutPursuit
sources = [
    "https://github.com/FugroRoames/cut-pursuit.git" =>
    "f621b1a6850a195e145de2806e2c7fda09815641",
    "https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_68_0.tar.bz2" =>
    "7f6130bc3cf65f56a618888ce9d5ea704fa10b462be126ad053e80e553d6d8b7",
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/boost_1_68_0/
./bootstrap.sh --prefix=$prefix
apk add python-dev
./b2 -j$nproc --layout=system install

cd $WORKSPACE/srcdir/cut-pursuit/src
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=$prefix -DCMAKE_TOOLCHAIN_FILE=/opt/$target/$target.toolchain -DJlCxx_DIR=../../../destdir/lib/cmake/JlCxx/ ..

# NOTE: This is a hack to remove -std=c++14 which is not supported by gcc-4.8
echo "# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.11

# compile CXX with /opt/x86_64-linux-gnu/bin/x86_64-linux-gnu-g++
CXX_FLAGS =  -Wall -std=c++11 -fopenmp -O3 -fPIC  -std=gnu++1y

CXX_DEFINES = -DJULIA_ENABLE_THREADING -Dcpjl_EXPORTS

CXX_INCLUDES = -I/workspace/srcdir/cut-pursuit/src/../include -isystem /workspace/destdir/include -isystem /workspace/destdir/include/julia" > CMakeFiles/cpjl.dir/flags.make

make -j${nproc}
cp libcpjl.so $prefix/lib
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
# platforms = supported_platforms()
platforms = [
    Linux(:x86_64, libc=:glibc)
]

# The products that we will ensure are always built
products(prefix) = [
    LibraryProduct(prefix, "libcpjl", :libcpjl),
    LibraryProduct(prefix, "libboost_math_tr1f", :math_tr1f),
    LibraryProduct(prefix, "libboost_thread", :thread),
    LibraryProduct(prefix, "libboost_unit_test_framework", :unit_test_framework),
    LibraryProduct(prefix, "libboost_type_erasure", :type_erasure),
    LibraryProduct(prefix, "libboost_chrono", :chrono),
    LibraryProduct(prefix, "libboost_math_c99l", :math_c99l),
    LibraryProduct(prefix, "libboost_locale", :locale),
    LibraryProduct(prefix, "libboost_program_options", :program_options),
    LibraryProduct(prefix, "libboost_date_time", :date_time),
    LibraryProduct(prefix, "libboost_graph", :graph),
    LibraryProduct(prefix, "libboost_signals", :signals),
    LibraryProduct(prefix, "libboost_iostreams", :iostreams),
    LibraryProduct(prefix, "libboost_stacktrace_addr2line", :stacktrace_addr2line),
    LibraryProduct(prefix, "libboost_system", :system),
    LibraryProduct(prefix, "libboost_wave", :wave),
    LibraryProduct(prefix, "libboost_wserialization", :wserialization),
    LibraryProduct(prefix, "libboost_math_tr1l", :math_tr1l),
    LibraryProduct(prefix, "libboost_math_tr1", :math_tr1),
    LibraryProduct(prefix, "libboost_filesystem", :filesystem),
    LibraryProduct(prefix, "libboost_random", :random),
    LibraryProduct(prefix, "libboost_coroutine", :coroutine),
    LibraryProduct(prefix, "libboost_serialization", :serialization),
    LibraryProduct(prefix, "libboost_context", :context),
    LibraryProduct(prefix, "libboost_container", :container),
    LibraryProduct(prefix, "libboost_stacktrace_noop", :stacktrace_noop),
    LibraryProduct(prefix, "libboost_contract", :contract),
    LibraryProduct(prefix, "libboost_prg_exec_monitor", :prg_exec_monitor),
    LibraryProduct(prefix, "libboost_regex", :regex),
    LibraryProduct(prefix, "libboost_log_setup", :log_setup),
    LibraryProduct(prefix, "libboost_math_c99", :math_c99),
    LibraryProduct(prefix, "libboost_timer", :timer),
    LibraryProduct(prefix, "libboost_stacktrace_basic", :stacktrace_basic),
    LibraryProduct(prefix, "libboost_math_c99f", :math_c99f),
    LibraryProduct(prefix, "libboost_log", :log),
    LibraryProduct(prefix, "libboost_atomic", :atomic),
    LibraryProduct(prefix, "libcxxwrap_julia", :libcxxwrap)
]

# Dependencies that must be installed before this package can be built
dependencies = [
    "https://github.com/JuliaInterop/libcxxwrap-julia/releases/download/v0.5.1/build_libcxxwrap-julia-1.0.v0.5.1.jl",
    "https://github.com/JuliaPackaging/JuliaBuilder/releases/download/v1.0.0-2/build_Julia.v1.0.0.jl",
    #"https://github.com/twadleigh/BoostBuilder/releases/download/v1.68.0-4/build_Boost.v1.68.0.jl"
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)
