1from waflib import Utils
2from waflib.Configure import conf
3from samba_utils import get_string
4done = {}
5
6@conf
7def SAMBA_CHECK_PERL(conf, mandatory=True, version=(5,0,0)):
8    if "done" in done:
9        return
10    done["done"] = True
11    conf.find_program('perl', var='PERL', mandatory=mandatory)
12    conf.load('perl')
13    path_perl = conf.find_program('perl')
14    conf.env.PERL_SPECIFIED = (conf.env.PERL != path_perl)
15    conf.check_perl_version(version)
16
17    def read_perl_config_var(cmd):
18        output = Utils.cmd_output([conf.env.get_flat('PERL'), '-MConfig', '-e', cmd])
19        if not isinstance(output, str):
20            output = get_string(output)
21        return Utils.to_list(output)
22
23    def check_perl_config_var(var):
24        conf.start_msg("Checking for perl $Config{%s}:" % var)
25        try:
26            v = read_perl_config_var('print $Config{%s}' % var)[0]
27            conf.end_msg("'%s'" % (v), 'GREEN')
28            return v
29        except IndexError:
30            conf.end_msg(False, 'YELLOW')
31            pass
32        return None
33
34    vendor_prefix = check_perl_config_var('vendorprefix')
35
36    perl_arch_install_dir = None
37    if vendor_prefix == conf.env.PREFIX:
38        perl_arch_install_dir = check_perl_config_var('vendorarch');
39    if perl_arch_install_dir is None:
40        perl_arch_install_dir = "${LIBDIR}/perl5";
41    conf.start_msg("PERL_ARCH_INSTALL_DIR: ")
42    conf.end_msg("'%s'" % (perl_arch_install_dir), 'GREEN')
43    conf.env.PERL_ARCH_INSTALL_DIR = perl_arch_install_dir
44
45    perl_lib_install_dir = None
46    if vendor_prefix == conf.env.PREFIX:
47        perl_lib_install_dir = check_perl_config_var('vendorlib');
48    if perl_lib_install_dir is None:
49        perl_lib_install_dir = "${DATADIR}/perl5";
50    conf.start_msg("PERL_LIB_INSTALL_DIR: ")
51    conf.end_msg("'%s'" % (perl_lib_install_dir), 'GREEN')
52    conf.env.PERL_LIB_INSTALL_DIR = perl_lib_install_dir
53
54    perl_inc = read_perl_config_var('print "@INC"')
55    if '.' in perl_inc:
56        perl_inc.remove('.')
57    conf.start_msg("PERL_INC: ")
58    conf.end_msg("%s" % (perl_inc), 'GREEN')
59    conf.env.PERL_INC = perl_inc
60