############################################################################
# Copyright 2016 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############################################################################
Import('*')
env.PartName('common')

math_headers = Pattern(src_dir='math',
                       includes=[
                           'bignum.h',
                           'ecgroup.h',
                           'finitefield.h',
                           'hash.h',
                           'pairing.h',
                           'printutils.h',
                           'ecdsa.h',
                       ],
                       excludes=[],
                       recursive=False).files()
math_internal_headers = Pattern(src_dir='math',
                                includes=[
                                    'bignum-internal.h',
                                    'ecgroup-internal.h',
                                    'finitefield-internal.h',
                                    'pairing-internal.h',
                                ],
                                excludes=[],
                                recursive=False).files()
common_headers = Pattern(src_dir='.',
                         includes=[
                             'bitsupplier.h',
                             'errors.h',
                             'file_parser.h',
                             'stdtypes.h',
                             'types.h',
                         ],
                         excludes=[],
                         recursive=False).files()
epid_common_headers = Pattern(src_dir='.',
                              includes=[
                                  'commitment.h',
                                  'endian_convert.h',
                                  'epid2params.h',
                                  'grouppubkey.h',
                                  'memory.h',
                                  'sigrlvalid.h',
                                  'stack.h',
                              ],
                              excludes=[],
                              recursive=False).files()
epid_params = Pattern(src_dir='.',
                      includes=[
                          'epid2params_ate.inc',
                      ],
                      excludes=[],
                      recursive=False).files()

src_files = Pattern(src_dir='.',
                    includes=['*.c'],
                    excludes=['*-test.cc'],
                    recursive=False).files()

math_src_files = Pattern(src_dir='math',
                         includes=['*.c'],
                         excludes=['*-test.cc'],
                         recursive=True).files()

parts_file = Pattern(src_dir='.',
                     includes=[
                         'common.parts',
                     ],
                     excludes=[],
                     recursive=False).files()

#unit tests
utest_files = Pattern(src_dir='.',
                      includes=['*-test.cc', '*-testhelper.cc'],
                      excludes=[],
                      recursive=True).files()

if 'install_package' in env['MODE']:
    env.InstallTopLevel(common_headers, sub_dir='epid/${PART_SHORT_NAME}')
    env.InstallTopLevel(epid_common_headers, sub_dir='epid/${PART_SHORT_NAME}')
    env.InstallTopLevel(src_files, sub_dir='epid/${PART_SHORT_NAME}')

    env.InstallTopLevel(math_headers, sub_dir='epid/${PART_SHORT_NAME}/math')
    env.InstallTopLevel(math_src_files, sub_dir='epid/${PART_SHORT_NAME}/math')
    env.InstallTopLevel(math_internal_headers,
                        sub_dir='epid/${PART_SHORT_NAME}/math')

    env.InstallTopLevel(utest_files,
                        sub_dir='epid/${PART_SHORT_NAME}/math/unittests')
    env.InstallTopLevel(epid_params, sub_dir='epid/${PART_SHORT_NAME}')
    env.InstallTopLevel(parts_file, sub_dir='epid/${PART_SHORT_NAME}')

else:
    env.DependsOn([
        Component('ippcp'),
        Component('ippcpepid'),
    ])

    env.Append(CPPPATH='#')

    testenv = env.Clone()

    env.SdkInclude(common_headers, sub_dir='epid/${PART_SHORT_NAME}')

    outputs = env.Library('${PART_NAME}', src_files + math_src_files)
    env.Sdk(outputs)
    if 'install_lib' in env['MODE']:
        env.InstallLib(outputs)
        env.InstallInclude(common_headers, sub_dir='${PART_SHORT_NAME}')

#unit tests
    testenv['UNIT_TEST_TARGET_NAME'] = "${PART_NAME}-${UNIT_TEST_TARGET}"
    testenv.UnitTest("utest",
                     utest_files,
                     command_args=[
                         '--gtest_color=yes',
                         '--gtest_print_time=1',
                         '--gtest_output=xml',
                         '--gtest_filter=**',
                     ],
                     make_pdb=(env.isConfigBasedOn('debug')),
                     depends=[Component('gtest'),
                              Component('common'),
                              Component('common-testhelper')],
                     INSTALL_BIN='${INSTALL_TEST_BIN}')
