#!/usr/bin/env python
# encoding: utf-8
# Copyright (c) 2013-2018 GomSpace A/S. All rights reserved.

import os
import gs_gcc
import gs_doc

APPNAME = 'param_client'


def options(ctx):
    ctx.load('gs_gcc gs_doc')
    gs_gcc.gs_recurse(ctx)

    gr = ctx.add_option_group('libparam client options')
    gr.add_option('--param_client-disable-cmd', action='store_true', help='Disable GOSH commands')
    gr.add_option('--param-enable-atomic-access', action='store_true', help='Enable atomic read/write of 16/32/float')


def configure(ctx):
    ctx.load('gs_gcc gs_doc')

    ctx.env.append_unique('USE_PARAM_CLIENT', ['gscsp', 'util'])

    ctx.env.append_unique('FILES_PARAM_CLIENT', ['src/*.c', 'src/rparam/*.c',
                                                 'src/pp/*.c', 'src/pp/i2c/*.c', 'src/pp/spi/*.c'])

    if not ctx.options.param_client_disable_cmd:
        ctx.env.append_unique('FILES_PARAM_CLIENT', ['src/rparam/cmd/*.c', 'src/pp/cmd/*.c'])

    if ctx.options.param_enable_atomic_access:
        ctx.env.append_unique('DEFINES_PARAM_CLIENT', ['GS_PARAM_ATOMIC_ACCESS=1'])

    ctx.gs_register_handler(function='param_gen_4_0', filepath='./tools/waf_param.py')
    ctx.gs_register_handler(function='param_gen_4_2', filepath='./tools/waf_param.py')
    ctx.gs_register_handler(function='param_gen_4_3', filepath='./tools/waf_param.py')

    ctx.gs_add_doxygen(exclude=['*/include/deprecated/param/*', '*/include/gs/param/internal/*'])

    gs_gcc.gs_recurse(ctx)


def build(ctx):
    gs_gcc.gs_recurse(ctx)

    public_include = ctx.gs_include(name=APPNAME,
                                    includes=['include', 'include/deprecated', 'include/deprecated/param'])

    if ctx.env.GS_ARCH not in ['avr8']:
        ctx.gs_objects(source=ctx.path.ant_glob(ctx.env.FILES_PARAM_CLIENT),
                       target=APPNAME,
                       defines=ctx.env.DEFINES_PARAM_CLIENT,
                       use=ctx.env.USE_PARAM_CLIENT + [public_include])

        ctx.gs_shlib(source=ctx.path.ant_glob(ctx.env.FILES_PARAM_CLIENT),
                     target=APPNAME,
                     defines=ctx.env.DEFINES_PARAM_CLIENT,
                     gs_use_shlib=ctx.env.USE_PARAM_CLIENT + [public_include])

        ctx.gs_python_bindings(source=ctx.path.ant_glob('src/bindings/python/pyparam.c'),
                               target=APPNAME,
                               gs_use_shlib=ctx.env.USE_PARAM_CLIENT + [APPNAME, public_include],
                               package='libparam')


def gs_dist(ctx):
    gs_gcc.gs_recurse(ctx)
    ctx.add_default_files(source_module=True)