perl.py 4.4 KB

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