#
#  Copyright (C) 2019-2025 Roman Pauer
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy of
#  this software and associated documentation files (the "Software"), to deal in
#  the Software without restriction, including without limitation the rights to
#  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
#  of the Software, and to permit persons to whom the Software is furnished to do
#  so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in all
#  copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#  SOFTWARE.
#

import os

udis86_path = './udis86-1.7.2/'

# set help text
vars = Variables('custom.py')
vars.Add(BoolVariable('debug', 'Set to build debug version', False))
vars.Add(BoolVariable('mingw', 'Set to use MinGW instead of default compiler', False))
vars.Add(BoolVariable('judy', 'Set to use Judy library instead of C++ containers', False))
env = Environment(variables = vars)
Help(vars.GenerateHelpText(env))

Help("""
Judy library:
  If the library is not provided by your system, then you can use following environment variables to specify locations to it:
    - JUDY_INC: path to Judy include files
    - JUDY_LIB: path to Judy library files

  Judy library source code is available here: https://sourceforge.net/projects/judy/
  Source code with some fixes is available here: https://github.com/dlmiles/libjudy
""")

debug = env['debug']
judy = env['judy']
mingw = env['mingw']

if mingw:
    env = Environment(tools = ['mingw'])

# default settings
env.Append(CPPPATH = [udis86_path])

if env['CC'] == 'cl':
    env.Append(CCFLAGS = '/O2 /W3 /D_CRT_SECURE_NO_WARNINGS')
    if judy:
        env.Append(CCFLAGS = '/DUSE_JUDY')
    else:
        env.Append(CCFLAGS = '/EHsc /std:c++20')
    if debug:
        env.Append(CCFLAGS = '/Zi /D__DEBUG__')
else:
    env.Append(CCFLAGS = '-O2 -Wall -DHAVE_STRING_H=1')
    if judy:
        env.Append(CCFLAGS = '-DUSE_JUDY')
    else:
        env.Append(LIBS = ['stdc++'])
        if env['CXX'] == 'g++':
            env.Append(LINKFLAGS = '-static-libgcc -static-libstdc++')
    if debug:
        env.Append(CCFLAGS = '-g -D__DEBUG__')

if judy:
    env.Append(LIBS = ['Judy'])
    if 'JUDY_INC' in os.environ:
        env.Append(CPPPATH = [os.environ['JUDY_INC']])
    if 'JUDY_LIB' in os.environ:
        env.Append(LIBPATH = [os.environ['JUDY_LIB']])

sources = Glob('*.c') + Glob(udis86_path + '/libudis86/*.c')
if not judy:
    sources = sources + Glob('*.cpp')

env.Program('SRW.exe', sources)
