Generates the Makefile for your extension, passing along any options and
preprocessor constants that you may have generated through other methods.
The target name should correspond the name of the global function
name defined within your C extension, minus the ‘Init_’. For example,
if your C extension is defined as ‘Init_foo’, then your target would
simply be ‘foo’.
If any ‘/’ characters are present in the target name, only the last
name is interpreted as the target name, and the rest are considered
toplevel directory names, and the generated Makefile will be altered
accordingly to follow that directory structure.
For example, if you pass ‘test/foo’ as a target name, your extension
will be installed under the ‘test’ directory. This means that in order
to load the file within a Ruby program
later, that directory structure will have to be followed, e.g. “require
‘test/foo’”.
The srcprefix should be used when your source files are not in the
same directory as your build script. This will not only eliminate the need
for you to manually copy the source files into the same directory as your
build script, but it also sets the proper target_prefix in the
generated Makefile.
Setting the target_prefix will, in turn, install the generated binary in a directory
under your Config::CONFIG[‘sitearchdir’] that mimics your local
filesystem when you run ‘make install’.
For example, given the following file tree:
ext/
extconf.rb
test/
foo.c
And given the following code:
create_makefile('test/foo', 'test')
That will set the target_prefix in the generated Makefile to
‘test’. That, in turn, will create the following file tree when
installed via the ‘make install’ command:
/path/to/ruby/sitearchdir/test/foo.so
It is recommended that you use this approach to generate your makefiles,
instead of copying files around manually, because some third party
libraries may depend on the target_prefix being set properly.
The srcprefix argument can be used to override the default source
directory, i.e. the current directory . It is included as part of the VPATH
and added to the list of INCFLAGS.
Show source
def create_makefile(target, srcprefix = nil)
$target = target
libpath = $DEFLIBPATH|$LIBPATH
message "creating Makefile\n"
rm_f "conftest*"
if CONFIG["DLEXT"] == $OBJEXT
for lib in libs = $libs.split
lib.sub!(/-l(.*)/, %"lib\\1.#{$LIBEXT}"%)
end
$defs.push(format("-DEXTLIB='%s'", libs.join(",")))
end
if target.include?('/')
target_prefix, target = File.split(target)
target_prefix[0,0] = '/'
else
target_prefix = ""
end
srcprefix ||= '$(srcdir)'
RbConfig::expand(srcdir = srcprefix.dup)
if not $objs
$objs = []
srcs = Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
for f in srcs
obj = File.basename(f, ".*") << ".o"
$objs.push(obj) unless $objs.index(obj)
end
elsif !(srcs = $srcs)
srcs = $objs.collect {|o| o.sub(/\.o\z/, '.c')}
end
$srcs = srcs
for i in $objs
i.sub!(/\.o\z/, ".#{$OBJEXT}")
end
$objs = $objs.join(" ")
target = nil if $objs == ""
if target and EXPORT_PREFIX
if File.exist?(File.join(srcdir, target + '.def'))
deffile = "$(srcdir)/$(TARGET).def"
unless EXPORT_PREFIX.empty?
makedef = %{-pe "$_.sub!(/^(?=\\w)/,'#{EXPORT_PREFIX}') unless 1../^EXPORTS$/i"}
end
else
makedef = %{-e "puts 'EXPORTS', '#{EXPORT_PREFIX}Init_$(TARGET)'"}
end
if makedef
$distcleanfiles << '$(DEFFILE)'
origdef = deffile
deffile = "$(TARGET)-$(arch).def"
end
end
origdef ||= ''
if $extout and $INSTALLFILES
$cleanfiles.concat($INSTALLFILES.collect {|files, dir|File.join(dir, files.sub(/\A\.\//, ''))})
$distcleandirs.concat($INSTALLFILES.collect {|files, dir| dir})
end
if $extmk and not $extconf_h
create_header
end
libpath = libpathflag(libpath)
dllib = target ? "$(TARGET).#{CONFIG['DLEXT']}" : ""
staticlib = target ? "$(TARGET).#$LIBEXT" : ""
mfile = open("Makefile", "wb")
mfile.print(*configuration(srcprefix))
mfile.print "
libpath = #{($DEFLIBPATH|$LIBPATH).join(" ")}
LIBPATH = #{libpath}
DEFFILE = #{deffile}
CLEANFILES = #{$cleanfiles.join(' ')}
DISTCLEANFILES = #{$distcleanfiles.join(' ')}
DISTCLEANDIRS = #{$distcleandirs.join(' ')}
extout = #{$extout}
extout_prefix = #{$extout_prefix}
target_prefix = #{target_prefix}
LOCAL_LIBS = #{$LOCAL_LIBS}
LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS}
SRCS = #{srcs.collect(&File.method(:basename)).join(' ')}
OBJS = #{$objs}
TARGET = #{target}
DLLIB = #{dllib}
EXTSTATIC = #{$static || ""}
STATIC_LIB = #{staticlib unless $static.nil?}
#{!$extout && defined?($installed_list) ? "INSTALLED_LIST = #{$installed_list}\n" : ""}
"
install_dirs.each {|d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]}
n = ($extout ? '$(RUBYARCHDIR)/' : '') + '$(TARGET)'
mfile.print "
TARGET_SO = #{($extout ? '$(RUBYARCHDIR)/' : '')}$(DLLIB)
CLEANLIBS = #{n}.#{CONFIG['DLEXT']} #{config_string('cleanlibs') {|t| t.gsub(/\$\*/) {n}}}
CLEANOBJS = *.#{$OBJEXT} #{config_string('cleanobjs') {|t| t.gsub(/\$\*/, '$(TARGET)')}} *.bak
all: #{$extout ? "install" : target ? "$(DLLIB)" : "Makefile"}
static: $(STATIC_LIB)#{$extout ? " install-rb" : ""}
"
mfile.print CLEANINGS
fsep = config_string('BUILD_FILE_SEPARATOR') {|s| s unless s == "/"}
if fsep
sep = ":/=#{fsep}"
fseprepl = proc {|s|
s = s.gsub("/", fsep)
s = s.gsub(/(\$\(\w+)(\))/) {$1+sep+$2}
s = s.gsub(/(\$\{\w+)(\})/) {$1+sep+$2}
}
else
fseprepl = proc {|s| s}
sep = ""
end
dirs = []
mfile.print "install: install-so install-rb\n\n"
sodir = (dir = "$(RUBYARCHDIR)").dup
mfile.print("install-so: ")
if target
f = "$(DLLIB)"
dest = "#{dir}/#{f}"
mfile.puts dir, "install-so: #{dest}"
if $extout
mfile.print "clean-so::\n"
mfile.print "\t@-$(RM) #{fseprepl[dest]}\n"
mfile.print "\t@-$(RMDIRS) #{fseprepl[dir]}\n"
else
mfile.print "#{dest}: #{f}\n"
mfile.print "\t$(INSTALL_PROG) #{fseprepl[f]} #{fseprepl[dir]}\n"
if defined?($installed_list)
mfile.print "\t@echo #{dir}/#{File.basename(f)}>>$(INSTALLED_LIST)\n"
end
end
else
mfile.puts "Makefile"
end
mfile.print("install-rb: pre-install-rb install-rb-default\n")
mfile.print("install-rb-default: pre-install-rb-default\n")
mfile.print("pre-install-rb: Makefile\n")
mfile.print("pre-install-rb-default: Makefile\n")
for sfx, i in [["-default", [["lib/**/*.rb", "$(RUBYLIBDIR)", "lib"]]], ["", $INSTALLFILES]]
files = install_files(mfile, i, nil, srcprefix) or next
for dir, *files in files
unless dirs.include?(dir)
dirs << dir
mfile.print "pre-install-rb#{sfx}: #{dir}\n"
end
for f in files
dest = "#{dir}/#{File.basename(f)}"
mfile.print("install-rb#{sfx}: #{dest}\n")
mfile.print("#{dest}: #{f}\n")
mfile.print("\t$(#{$extout ? 'COPY' : 'INSTALL_DATA'}) ")
mfile.print("#{fseprepl[f]} $(@D#{sep})\n")
if defined?($installed_list) and !$extout
mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n")
end
if $extout
mfile.print("clean-rb#{sfx}::\n")
mfile.print("\t@-$(RM) #{fseprepl[dest]}\n")
end
end
end
if $extout
dirs.uniq!
dirs.reverse!
unless dirs.empty?
mfile.print("clean-rb#{sfx}::\n")
for dir in dirs
mfile.print("\t@-$(RMDIRS) #{fseprepl[dir]}\n")
end
end
end
end
dirs.unshift(sodir) if target and !dirs.include?(sodir)
dirs.each {|d| mfile.print "#{d}:\n\t$(MAKEDIRS) $@\n"}
mfile.print site-install: site-install-so site-install-rbsite-install-so: install-sosite-install-rb: install-rb
return unless target
mfile.puts SRC_EXT.collect {|ext| ".path.#{ext} = $(VPATH)"} if $nmake == bb
mfile.print ".SUFFIXES: .#{SRC_EXT.join(' .')} .#{$OBJEXT}\n"
mfile.print "\n"
CXX_EXT.each do |ext|
COMPILE_RULES.each do |rule|
mfile.printf(rule, ext, $OBJEXT)
mfile.printf("\n\t%s\n\n", COMPILE_CXX)
end
end
]c].each do |ext|
COMPILE_RULES.each do |rule|
mfile.printf(rule, ext, $OBJEXT)
mfile.printf("\n\t%s\n\n", COMPILE_C)
end
end
mfile.print "$(RUBYARCHDIR)/" if $extout
mfile.print "$(DLLIB): "
mfile.print "$(DEFFILE) " if makedef
mfile.print "$(OBJS) Makefile\n"
mfile.print "\t@-$(RM) $(@#{sep})\n"
mfile.print "\t@-$(MAKEDIRS) $(@D)\n" if $extout
link_so = LINK_SO.gsub(/^/, "\t")
if srcs.any?(&%\.(?:#{CXX_EXT.join('|')})\z".method(:===))
link_so = link_so.sub(/\bLDSHARED\b/, '\&XX')
end
mfile.print link_so, "\n\n"
unless $static.nil?
mfile.print "$(STATIC_LIB): $(OBJS)\n\t@-$(RM) $(@#{sep})\n\t"
mfile.print "$(AR) #{config_string('ARFLAGS') || 'cru '}$@ $(OBJS)"
config_string('RANLIB') do |ranlib|
mfile.print "\n\t@-#{ranlib} $(DLLIB) 2> /dev/null || true"
end
end
mfile.print "\n\n"
if makedef
mfile.print "$(DEFFILE): #{origdef}\n"
mfile.print "\t$(RUBY) #{makedef} #{origdef} > $@\n\n"
end
depend = File.join(srcdir, "depend")
if File.exist?(depend)
mfile.print("###\n", *depend_rules(File.read(depend)))
else
headers = ]$(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h]
if RULE_SUBST
headers.each {|h| h.sub!(/.*/, &RULE_SUBST.method(:%))}
end
headers << $config_h
headers << '$(RUBY_EXTCONF_H)' if $extconf_h
mfile.print "$(OBJS): ", headers.join(' '), "\n"
end
$makefile_created = true
ensure
mfile.close if mfile
end