When you are creating makefiles with mkmf it automatically searches for source files in the directory. This is great as you do not have to specify them, just throw your files in the same directory as extconf.rb and the makefile generated will build everything. But there are times were you have more source files in that directory and you only want to build the extension with a subset.
Recently I have been creating ruby bindings for a C library in my project (DRMAA libraries for GridWay) and the directory contained not only ruby bindings but also perl and python ones. When extconf.rb was creating the Makefile and wrappers for perl and python bindings where there, it tried to build every file for the ruby extension and, of course, could not build it. So I searched in mkmf documentation for a way to specify just the files I wanted to build for my extension and there was no method I could use to do this.
Fortunately mkmf library is small and easy to understand. It uses two global variables ($srcs and $objs) that are arrays of files to build. By default these variables are empty and when you call create_makefile the variables are filled with all the source files in the directory ($objs is filled with .o files that will be generated). If these variables are already set it won’t try to find files and will use the data contained in them. So the solution is easy, fill these variables by yourself. This is the code I’m using to do this:
build_files=['drmaa_r_wrap']
$srcs=Array.new
$objs=Array.new
build_files.each{|f|
$srcs<<f+'.c'
$objs<<f+'.o'
}
You only have to fill build_files with the names of the files (without .c extension) you want to build. Of course this can be changed to add extensions to it and then change the extension for .o in the loop, but this was enough for me.
