#
# Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
#

import subprocess

env = DefaultEnvironment()

vpath = '#third_party/rabbitmq-c'
working_dir = Dir('.').abspath

cmake_flags = [
    '-DBUILD_SHARED_LIBS=OFF',
    '-DBUILD_EXAMPLES=OFF',
    '-DBUILD_STATIC_LIBS=ON',
    '-DENABLE_SSL_SUPPORT=ON',
    '-DCMAKE_INSTALL_PREFIX=' + Dir('#build').abspath,
    '-DCMAKE_INSTALL_LIBDIR=' + Dir('#build/lib').abspath
]

def build_rabbitmq_c(target, source, env):
    env.EnsureBuildDependency('cmake')

    subprocess.call(['cmake'] + cmake_flags + [Dir(vpath).abspath], cwd=working_dir)
    subprocess.call(['cmake', '--build', '.', '--target', 'install'], cwd=working_dir)

header_files = [
    'amqp.h',
    'amqp_framing.h',
    'amqp_tcp_socket.h',
    'amqp_ssl_socket.h',
]
header_files = [File('#build/include/' + h) for h in header_files]

expected_lib = File('#build/lib/' + env['LIBPREFIX'] + 'rabbitmq' + env['LIBSUFFIX'])

librabbitmq = env.Command([expected_lib] + header_files, Dir(vpath), build_rabbitmq_c)
