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