ruby.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # daniel.svensson at purplescout.se 2008
  4. # Thomas Nagy 2016-2018 (ita)
  5. """
  6. Support for Ruby extensions. A C/C++ compiler is required::
  7. def options(opt):
  8. opt.load('compiler_c ruby')
  9. def configure(conf):
  10. conf.load('compiler_c ruby')
  11. conf.check_ruby_version((1,8,0))
  12. conf.check_ruby_ext_devel()
  13. conf.check_ruby_module('libxml')
  14. def build(bld):
  15. bld(
  16. features = 'c cshlib rubyext',
  17. source = 'rb_mytest.c',
  18. target = 'mytest_ext',
  19. install_path = '${ARCHDIR_RUBY}')
  20. bld.install_files('${LIBDIR_RUBY}', 'Mytest.rb')
  21. """
  22. import os
  23. from waflib import Errors, Options, Task, Utils
  24. from waflib.TaskGen import before_method, feature, extension
  25. from waflib.Configure import conf
  26. @feature('rubyext')
  27. @before_method('apply_incpaths', 'process_source', 'apply_bundle', 'apply_link')
  28. def init_rubyext(self):
  29. """
  30. Add required variables for ruby extensions
  31. """
  32. self.install_path = '${ARCHDIR_RUBY}'
  33. self.uselib = self.to_list(getattr(self, 'uselib', ''))
  34. if not 'RUBY' in self.uselib:
  35. self.uselib.append('RUBY')
  36. if not 'RUBYEXT' in self.uselib:
  37. self.uselib.append('RUBYEXT')
  38. @feature('rubyext')
  39. @before_method('apply_link', 'propagate_uselib_vars')
  40. def apply_ruby_so_name(self):
  41. """
  42. Strip the *lib* prefix from ruby extensions
  43. """
  44. self.env.cshlib_PATTERN = self.env.cxxshlib_PATTERN = self.env.rubyext_PATTERN
  45. @conf
  46. def check_ruby_version(self, minver=()):
  47. """
  48. Checks if ruby is installed.
  49. If installed the variable RUBY will be set in environment.
  50. The ruby binary can be overridden by ``--with-ruby-binary`` command-line option.
  51. """
  52. ruby = self.find_program('ruby', var='RUBY', value=Options.options.rubybinary)
  53. try:
  54. version = self.cmd_and_log(ruby + ['-e', 'puts defined?(VERSION) ? VERSION : RUBY_VERSION']).strip()
  55. except Errors.WafError:
  56. self.fatal('could not determine ruby version')
  57. self.env.RUBY_VERSION = version
  58. try:
  59. ver = tuple(map(int, version.split('.')))
  60. except Errors.WafError:
  61. self.fatal('unsupported ruby version %r' % version)
  62. cver = ''
  63. if minver:
  64. cver = '> ' + '.'.join(str(x) for x in minver)
  65. if ver < minver:
  66. self.fatal('ruby is too old %r' % ver)
  67. self.msg('Checking for ruby version %s' % cver, version)
  68. @conf
  69. def check_ruby_ext_devel(self):
  70. """
  71. Check if a ruby extension can be created
  72. """
  73. if not self.env.RUBY:
  74. self.fatal('ruby detection is required first')
  75. if not self.env.CC_NAME and not self.env.CXX_NAME:
  76. self.fatal('load a c/c++ compiler first')
  77. version = tuple(map(int, self.env.RUBY_VERSION.split(".")))
  78. def read_out(cmd):
  79. return Utils.to_list(self.cmd_and_log(self.env.RUBY + ['-rrbconfig', '-e', cmd]))
  80. def read_config(key):
  81. return read_out('puts RbConfig::CONFIG[%r]' % key)
  82. cpppath = archdir = read_config('archdir')
  83. if version >= (1, 9, 0):
  84. ruby_hdrdir = read_config('rubyhdrdir')
  85. cpppath += ruby_hdrdir
  86. if version >= (2, 0, 0):
  87. cpppath += read_config('rubyarchhdrdir')
  88. cpppath += [os.path.join(ruby_hdrdir[0], read_config('arch')[0])]
  89. self.check(header_name='ruby.h', includes=cpppath, errmsg='could not find ruby header file', link_header_test=False)
  90. self.env.LIBPATH_RUBYEXT = read_config('libdir')
  91. self.env.LIBPATH_RUBYEXT += archdir
  92. self.env.INCLUDES_RUBYEXT = cpppath
  93. self.env.CFLAGS_RUBYEXT = read_config('CCDLFLAGS')
  94. self.env.rubyext_PATTERN = '%s.' + read_config('DLEXT')[0]
  95. # ok this is really stupid, but the command and flags are combined.
  96. # so we try to find the first argument...
  97. flags = read_config('LDSHARED')
  98. while flags and flags[0][0] != '-':
  99. flags = flags[1:]
  100. # we also want to strip out the deprecated ppc flags
  101. if len(flags) > 1 and flags[1] == "ppc":
  102. flags = flags[2:]
  103. self.env.LINKFLAGS_RUBYEXT = flags
  104. self.env.LINKFLAGS_RUBYEXT += read_config('LIBS')
  105. self.env.LINKFLAGS_RUBYEXT += read_config('LIBRUBYARG_SHARED')
  106. if Options.options.rubyarchdir:
  107. self.env.ARCHDIR_RUBY = Options.options.rubyarchdir
  108. else:
  109. self.env.ARCHDIR_RUBY = read_config('sitearchdir')[0]
  110. if Options.options.rubylibdir:
  111. self.env.LIBDIR_RUBY = Options.options.rubylibdir
  112. else:
  113. self.env.LIBDIR_RUBY = read_config('sitelibdir')[0]
  114. @conf
  115. def check_ruby_module(self, module_name):
  116. """
  117. Check if the selected ruby interpreter can require the given ruby module::
  118. def configure(conf):
  119. conf.check_ruby_module('libxml')
  120. :param module_name: module
  121. :type module_name: string
  122. """
  123. self.start_msg('Ruby module %s' % module_name)
  124. try:
  125. self.cmd_and_log(self.env.RUBY + ['-e', 'require \'%s\';puts 1' % module_name])
  126. except Errors.WafError:
  127. self.end_msg(False)
  128. self.fatal('Could not find the ruby module %r' % module_name)
  129. self.end_msg(True)
  130. @extension('.rb')
  131. def process(self, node):
  132. return self.create_task('run_ruby', node)
  133. class run_ruby(Task.Task):
  134. """
  135. Task to run ruby files detected by file extension .rb::
  136. def options(opt):
  137. opt.load('ruby')
  138. def configure(ctx):
  139. ctx.check_ruby_version()
  140. def build(bld):
  141. bld.env.RBFLAGS = '-e puts "hello world"'
  142. bld(source='a_ruby_file.rb')
  143. """
  144. run_str = '${RUBY} ${RBFLAGS} -I ${SRC[0].parent.abspath()} ${SRC}'
  145. def options(opt):
  146. """
  147. Add the ``--with-ruby-archdir``, ``--with-ruby-libdir`` and ``--with-ruby-binary`` options
  148. """
  149. opt.add_option('--with-ruby-archdir', type='string', dest='rubyarchdir', help='Specify directory where to install arch specific files')
  150. opt.add_option('--with-ruby-libdir', type='string', dest='rubylibdir', help='Specify alternate ruby library path')
  151. opt.add_option('--with-ruby-binary', type='string', dest='rubybinary', help='Specify alternate ruby binary')