mkrf – making C extensions for Ruby a bit easier

mkrf is a library for generating Rakefiles to build Ruby extension modules written in C. It is intended as a replacement for mkrf. The major difference between the two is that mkrf builds you a Rakefile instead of a Makefile.

Major goals of mkrf include

  • easy code reuse of its Availability class and

  • simple, well documented, use of the Generator class.

Basic Usage

mkrf works similarly to mkmf in that a user writes an extension configuration file and then runs it, generating a Rakefile in the current directory.

With mkmf it was customary to name the extension configuration file “extconf.rb”. With mkrf, you should name this file “mkrf_conf.rb”.

In general, mkrf_conf.rb should be placed in the root directory of the extension (ex. PROJECT_ROOT/ext/name_of_module) and it expects, by default, that files to be compiled have a .c extension and reside in that same directory. If your project contains multiple extension modules, then each one would get its own subdirectory under PROJECT_ROOT/ext/ and each would have its own mkrf_conf.rb file.

The most basic usage looks like the following, where the name of the extension module being built is “libtrivial”:

require 'mkrf'
Mkrf::Generator.new('libtrivial')

Extra arguments may be passed to the Rakefile generator in a block:

Mkrf::Generator.new('libtrivial') do |g|
  g.logger.level = Logger::WARN
  g.include_library('z')
end

Another example:

Mkrf::Generator.new('libxml') do |g|
  g.include_library('socket','socket')
  g.include_header('libxml/xmlversion.h', 
                   '/opt/include/libxml2', 
                   '/usr/local/include/libxml2', 
                   '/usr/include/libxml2')
end

Helpers

mkrf also comes with rakehelper.rb – a module which contains methods you may want to use in your project's top-level Rakefile. The docs on using rakehelper do not exist at the moment, but for the time being, have a look at examples/trivial/Rakefile to get an idea of how they're used.

Credits

  • Jim Weirich for writing Rake

Licence

mkrf is available under an MIT-style license.

Copyright © 2006 Kevin Clark

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.