# -*- python -*-
import os.path
Import("env libhammer_shared testruns targets")

perlenv = env.Clone()

perlenv.Append(CCFLAGS=["-fpic", '-DSWIG', '-Wno-all',
                        '-Wno-extra', '-Wno-error'],
               CPPPATH=["../.."],
               LIBS=['hammer'],
               LIBPATH=["../.."],
               SWIGFLAGS=["-DHAMMER_INTERNAL__NO_STDARG_H",
                          "-Isrc/", "-perl"])
perlenv['ENV']['CC'] = perlenv['CC']
import os
if 'PERL_MM_OPT' in os.environ:
    perlenv['ENV']['PERL_MM_OPT'] = os.environ['PERL_MM_OPT']
if 'PERL5LIB' in os.environ:
    perlenv['ENV']['PERL5LIB'] = os.environ['PERL5LIB']

swig = ['hammer.i']

hammer_wrap = perlenv.Command(['hammer_wrap.c', 'hammer.pm'], swig, "swig3.0 $SWIGFLAGS $SOURCE")
makefile = perlenv.Command(['Makefile'], ['Makefile.PL'], "perl $SOURCE CC=" + perlenv['ENV']['CC'])

targetdir = os.path.dirname(str(hammer_wrap[0].path))

libhammer_perl = perlenv.Command(['hammer.so'], makefile + hammer_wrap, "make -C " + targetdir)

Default(libhammer_perl)

perltestenv = perlenv.Clone()
perltestenv['ENV']['LD_LIBRARY_PATH'] = os.path.dirname(str(libhammer_shared[0]))
perltests = ['t/hammer.t']
perltestexec = perltestenv.Command(None, perltests + libhammer_perl + libhammer_shared, "make test -C " + targetdir)
perltest = Alias("testperl", [perltestexec], perltestexec)
AlwaysBuild(perltestexec)
testruns.append(perltest)

perlinstallexec = perlenv.Command(None, libhammer_perl, "make install -C " + targetdir)
perlinstall = Alias("installperl", [perlinstallexec], perlinstallexec)
targets.append(perlinstall)
