375346ddfdf874fea0b07a4e05fdfc61242668ea
[samba.git] / third_party / waf / waflib / Tools / perl.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #!/usr/bin/env python
6 # encoding: utf-8
7 # andersg at 0x63.nu 2007
8 # Thomas Nagy 2016-2018 (ita)
9
10 """
11 Support for Perl extensions. A C/C++ compiler is required::
12
13         def options(opt):
14                 opt.load('compiler_c perl')
15         def configure(conf):
16                 conf.load('compiler_c perl')
17                 conf.check_perl_version((5,6,0))
18                 conf.check_perl_ext_devel()
19                 conf.check_perl_module('Cairo')
20                 conf.check_perl_module('Devel::PPPort 4.89')
21         def build(bld):
22                 bld(
23                         features     = 'c cshlib perlext',
24                         source       = 'Mytest.xs',
25                         target       = 'Mytest',
26                         install_path = '${ARCHDIR_PERL}/auto')
27                 bld.install_files('${ARCHDIR_PERL}', 'Mytest.pm')
28 """
29
30 import os
31 from waflib import Task, Options, Utils, Errors
32 from waflib.Configure import conf
33 from waflib.TaskGen import extension, feature, before_method
34
35 @before_method('apply_incpaths', 'apply_link', 'propagate_uselib_vars')
36 @feature('perlext')
37 def init_perlext(self):
38         """
39         Change the values of *cshlib_PATTERN* and *cxxshlib_PATTERN* to remove the
40         *lib* prefix from library names.
41         """
42         self.uselib = self.to_list(getattr(self, 'uselib', []))
43         if not 'PERLEXT' in self.uselib:
44                 self.uselib.append('PERLEXT')
45         self.env.cshlib_PATTERN = self.env.cxxshlib_PATTERN = self.env.perlext_PATTERN
46
47 @extension('.xs')
48 def xsubpp_file(self, node):
49         """
50         Create :py:class:`waflib.Tools.perl.xsubpp` tasks to process *.xs* files
51         """
52         outnode = node.change_ext('.c')
53         self.create_task('xsubpp', node, outnode)
54         self.source.append(outnode)
55
56 class xsubpp(Task.Task):
57         """
58         Process *.xs* files
59         """
60         run_str = '${PERL} ${XSUBPP} -noprototypes -typemap ${EXTUTILS_TYPEMAP} ${SRC} > ${TGT}'
61         color   = 'BLUE'
62         ext_out = ['.h']
63
64 @conf
65 def check_perl_version(self, minver=None):
66         """
67         Check if Perl is installed, and set the variable PERL.
68         minver is supposed to be a tuple
69         """
70         res = True
71         if minver:
72                 cver = '.'.join(map(str,minver))
73         else:
74                 cver = ''
75
76         self.start_msg('Checking for minimum perl version %s' % cver)
77
78         perl = self.find_program('perl', var='PERL', value=getattr(Options.options, 'perlbinary', None))
79         version = self.cmd_and_log(perl + ["-e", 'printf \"%vd\", $^V'])
80         if not version:
81                 res = False
82                 version = "Unknown"
83         elif not minver is None:
84                 ver = tuple(map(int, version.split(".")))
85                 if ver < minver:
86                         res = False
87
88         self.end_msg(version, color=res and 'GREEN' or 'YELLOW')
89         return res
90
91 @conf
92 def check_perl_module(self, module):
93         """
94         Check if specified perlmodule is installed.
95
96         The minimum version can be specified by specifying it after modulename
97         like this::
98
99                 def configure(conf):
100                         conf.check_perl_module("Some::Module 2.92")
101         """
102         cmd = self.env.PERL + ['-e', 'use %s' % module]
103         self.start_msg('perl module %s' % module)
104         try:
105                 r = self.cmd_and_log(cmd)
106         except Errors.WafError:
107                 self.end_msg(False)
108                 return None
109         self.end_msg(r or True)
110         return r
111
112 @conf
113 def check_perl_ext_devel(self):
114         """
115         Check for configuration needed to build perl extensions.
116
117         Sets different xxx_PERLEXT variables in the environment.
118
119         Also sets the ARCHDIR_PERL variable useful as installation path,
120         which can be overridden by ``--with-perl-archdir`` option.
121         """
122
123         env = self.env
124         perl = env.PERL
125         if not perl:
126                 self.fatal('find perl first')
127
128         def cmd_perl_config(s):
129                 return perl + ['-MConfig', '-e', 'print \"%s\"' % s]
130         def cfg_str(cfg):
131                 return self.cmd_and_log(cmd_perl_config(cfg))
132         def cfg_lst(cfg):
133                 return Utils.to_list(cfg_str(cfg))
134         def find_xsubpp():
135                 for var in ('privlib', 'vendorlib'):
136                         xsubpp = cfg_lst('$Config{%s}/ExtUtils/xsubpp$Config{exe_ext}' % var)
137                         if xsubpp and os.path.isfile(xsubpp[0]):
138                                 return xsubpp
139                 return self.find_program('xsubpp')
140
141         env.LINKFLAGS_PERLEXT = cfg_lst('$Config{lddlflags}')
142         env.INCLUDES_PERLEXT = cfg_lst('$Config{archlib}/CORE')
143         env.CFLAGS_PERLEXT = cfg_lst('$Config{ccflags} $Config{cccdlflags}')
144         env.EXTUTILS_TYPEMAP = cfg_lst('$Config{privlib}/ExtUtils/typemap')
145         env.XSUBPP = find_xsubpp()
146
147         if not getattr(Options.options, 'perlarchdir', None):
148                 env.ARCHDIR_PERL = cfg_str('$Config{sitearch}')
149         else:
150                 env.ARCHDIR_PERL = getattr(Options.options, 'perlarchdir')
151
152         env.perlext_PATTERN = '%s.' + cfg_str('$Config{dlext}')
153
154 def options(opt):
155         """
156         Add the ``--with-perl-archdir`` and ``--with-perl-binary`` command-line options.
157         """
158         opt.add_option('--with-perl-binary', type='string', dest='perlbinary', help = 'Specify alternate perl binary', default=None)
159         opt.add_option('--with-perl-archdir', type='string', dest='perlarchdir', help = 'Specify directory where to install arch specific files', default=None)
160