Path: | lib/mkmf.rb |
Last Update: | Fri Jul 03 13:24:46 +0000 2009 |
module to create Makefile for extension modules invoke like: ruby -r mkmf extconf.rb
CONFIG | = | Config::MAKEFILE_CONFIG |
ORIG_LIBPATH | = | ENV['LIB'] |
CXX_EXT | = | %w[cc cxx cpp] |
SRC_EXT | = | %w[c m] << CXX_EXT |
EXPORT_PREFIX | = | config_string('EXPORT_PREFIX') {|s| s.strip} |
COMMON_HEADERS | = | hdr.join("\n") |
COMMON_LIBS | = | config_string('COMMON_LIBS', &split) || [] |
COMPILE_RULES | = | config_string('COMPILE_RULES', &split) || %w[.%s.%s:] |
RULE_SUBST | = | config_string('RULE_SUBST') |
COMPILE_C | = | config_string('COMPILE_C') || '$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) -c $<' |
COMPILE_CXX | = | config_string('COMPILE_CXX') || '$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) -c $<' |
TRY_LINK | = | config_string('TRY_LINK') || "$(CC) #{OUTFLAG}conftest $(INCFLAGS) $(CPPFLAGS) " \ "$(CFLAGS) $(src) $(LIBPATH) $(LDFLAGS) $(ARCH_FLAG) $(LOCAL_LIBS) $(LIBS)" |
LINK_SO | = | config_string('LINK_SO') || if CONFIG["DLEXT"] == $OBJEXT |
Returns the size of the given type. You may optionally specify additional headers to search in for the type.
If found, a macro is passed as a preprocessor constant to the compiler using the type name, in uppercase, prepended with ‘SIZEOF_’, followed by the type name, followed by ’=X’ where ‘X’ is the actual size.
For example, if check_sizeof(‘mystruct’) returned 12, then the SIZEOF_MYSTRUCT=12 preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 922 922: def check_sizeof(type, headers = nil, &b) 923: expr = "sizeof(#{type})" 924: fmt = "%d" 925: def fmt.%(x) 926: x ? super : "failed" 927: end 928: checking_for checking_message("size of #{type}", headers), fmt do 929: if size = try_constant(expr, headers, &b) 930: $defs.push(format("-DSIZEOF_%s=%d", type.tr_cpp, size)) 931: size 932: end 933: end 934: end
Generates a header file consisting of the various macro definitions generated by other methods such as have_func and have_header. These are then wrapped in a custom ifndef based on the header file name, which defaults to ‘extconf.h’.
For example:
# extconf.rb require 'mkmf' have_func('realpath') have_header('sys/utime.h') create_header create_makefile('foo')
The above script would generate the following extconf.h file:
#ifndef EXTCONF_H #define EXTCONF_H #define HAVE_REALPATH 1 #define HAVE_SYS_UTIME_H 1 #endif
Given that the create_header method generates a file based on definitions set earlier in your extconf.rb file, you will probably want to make this one of the last methods you call in your script.
# File lib/mkmf.rb, line 1132 1132: def create_header(header = "extconf.h") 1133: message "creating %s\n", header 1134: sym = header.tr("a-z./\055", "A-Z___") 1135: hdr = ["#ifndef #{sym}\n#define #{sym}\n"] 1136: for line in $defs 1137: case line 1138: when /^-D([^=]+)(?:=(.*))?/ 1139: hdr << "#define #$1 #{$2 ? Shellwords.shellwords($2)[0] : 1}\n" 1140: when /^-U(.*)/ 1141: hdr << "#undef #$1\n" 1142: end 1143: end 1144: hdr << "#endif\n" 1145: hdr = hdr.join 1146: unless (IO.read(header) == hdr rescue false) 1147: open(header, "w") do |hfile| 1148: hfile.write(hdr) 1149: end 1150: end 1151: $extconf_h = header 1152: end
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.
# File lib/mkmf.rb, line 1412 1412: def create_makefile(target, srcprefix = nil) 1413: $target = target 1414: libpath = $DEFLIBPATH|$LIBPATH 1415: message "creating Makefile\n" 1416: rm_f "conftest*" 1417: if CONFIG["DLEXT"] == $OBJEXT 1418: for lib in libs = $libs.split 1419: lib.sub!(/-l(.*)/, %%"lib\\1.#{$LIBEXT}"%) 1420: end 1421: $defs.push(format("-DEXTLIB='%s'", libs.join(","))) 1422: end 1423: 1424: if target.include?('/') 1425: target_prefix, target = File.split(target) 1426: target_prefix[0,0] = '/' 1427: else 1428: target_prefix = "" 1429: end 1430: 1431: srcprefix ||= '$(srcdir)' 1432: Config::expand(srcdir = srcprefix.dup) 1433: 1434: if not $objs 1435: $objs = [] 1436: srcs = Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")] 1437: for f in srcs 1438: obj = File.basename(f, ".*") << ".o" 1439: $objs.push(obj) unless $objs.index(obj) 1440: end 1441: elsif !(srcs = $srcs) 1442: srcs = $objs.collect {|obj| obj.sub(/\.o\z/, '.c')} 1443: end 1444: $srcs = srcs 1445: for i in $objs 1446: i.sub!(/\.o\z/, ".#{$OBJEXT}") 1447: end 1448: $objs = $objs.join(" ") 1449: 1450: target = nil if $objs == "" 1451: 1452: if target and EXPORT_PREFIX 1453: if File.exist?(File.join(srcdir, target + '.def')) 1454: deffile = "$(srcdir)/$(TARGET).def" 1455: unless EXPORT_PREFIX.empty? 1456: makedef = %{-pe "sub!(/^(?=\\w)/,'#{EXPORT_PREFIX}') unless 1../^EXPORTS$/i"} 1457: end 1458: else 1459: makedef = %{-e "puts 'EXPORTS', '#{EXPORT_PREFIX}Init_$(TARGET)'"} 1460: end 1461: if makedef 1462: $distcleanfiles << '$(DEFFILE)' 1463: origdef = deffile 1464: deffile = "$(TARGET)-$(arch).def" 1465: end 1466: end 1467: origdef ||= '' 1468: 1469: libpath = libpathflag(libpath) 1470: 1471: dllib = target ? "$(TARGET).#{CONFIG['DLEXT']}" : "" 1472: staticlib = target ? "$(TARGET).#$LIBEXT" : "" 1473: mfile = open("Makefile", "wb") 1474: mfile.print configuration(srcprefix) 1475: mfile.print " 1476: libpath = #{($DEFLIBPATH|$LIBPATH).join(" ")} 1477: LIBPATH = #{libpath} 1478: DEFFILE = #{deffile} 1479: 1480: CLEANFILES = #{$cleanfiles.join(' ')} 1481: DISTCLEANFILES = #{$distcleanfiles.join(' ')} 1482: 1483: extout = #{$extout} 1484: extout_prefix = #{$extout_prefix} 1485: target_prefix = #{target_prefix} 1486: LOCAL_LIBS = #{$LOCAL_LIBS} 1487: LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS} 1488: SRCS = #{srcs.collect(&File.method(:basename)).join(' ')} 1489: OBJS = #{$objs} 1490: TARGET = #{target} 1491: DLLIB = #{dllib} 1492: EXTSTATIC = #{$static || ""} 1493: STATIC_LIB = #{staticlib unless $static.nil?} 1494: #{!$extout && defined?($installed_list) ? "INSTALLED_LIST = #{$installed_list}\n" : ""} 1495: " 1496: install_dirs.each {|d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]} 1497: n = ($extout ? '$(RUBYARCHDIR)/' : '') + '$(TARGET).' 1498: mfile.print " 1499: TARGET_SO = #{($extout ? '$(RUBYARCHDIR)/' : '')}$(DLLIB) 1500: CLEANLIBS = #{n}#{CONFIG['DLEXT']} #{n}il? #{n}tds #{n}map 1501: CLEANOBJS = *.#{$OBJEXT} *.#{$LIBEXT} *.s[ol] *.pdb *.exp *.bak 1502: 1503: all: #{$extout ? "install" : target ? "$(DLLIB)" : "Makefile"} 1504: static: $(STATIC_LIB)#{$extout ? " install-rb" : ""} 1505: " 1506: mfile.print CLEANINGS 1507: dirs = [] 1508: mfile.print "install: install-so install-rb\n\n" 1509: sodir = (dir = "$(RUBYARCHDIR)").dup 1510: mfile.print("install-so: ") 1511: if target 1512: f = "$(DLLIB)" 1513: dest = "#{dir}/#{f}" 1514: mfile.puts dir, "install-so: #{dest}" 1515: unless $extout 1516: mfile.print "#{dest}: #{f}\n" 1517: if (sep = config_string('BUILD_FILE_SEPARATOR')) 1518: f.gsub!("/", sep) 1519: dir.gsub!("/", sep) 1520: sep = ":/="+sep 1521: f.gsub!(/(\$\(\w+)(\))/) {$1+sep+$2} 1522: f.gsub!(/(\$\{\w+)(\})/) {$1+sep+$2} 1523: dir.gsub!(/(\$\(\w+)(\))/) {$1+sep+$2} 1524: dir.gsub!(/(\$\{\w+)(\})/) {$1+sep+$2} 1525: end 1526: mfile.print "\t$(INSTALL_PROG) #{f} #{dir}\n" 1527: if defined?($installed_list) 1528: mfile.print "\t@echo #{dir}/#{File.basename(f)}>>$(INSTALLED_LIST)\n" 1529: end 1530: end 1531: else 1532: mfile.puts "Makefile" 1533: end 1534: mfile.print("install-rb: pre-install-rb install-rb-default\n") 1535: mfile.print("install-rb-default: pre-install-rb-default\n") 1536: mfile.print("pre-install-rb: Makefile\n") 1537: mfile.print("pre-install-rb-default: Makefile\n") 1538: for sfx, i in [["-default", [["lib/**/*.rb", "$(RUBYLIBDIR)", "lib"]]], ["", $INSTALLFILES]] 1539: files = install_files(mfile, i, nil, srcprefix) or next 1540: for dir, *files in files 1541: unless dirs.include?(dir) 1542: dirs << dir 1543: mfile.print "pre-install-rb#{sfx}: #{dir}\n" 1544: end 1545: files.each do |f| 1546: dest = "#{dir}/#{File.basename(f)}" 1547: mfile.print("install-rb#{sfx}: #{dest}\n") 1548: mfile.print("#{dest}: #{f} #{dir}\n\t$(#{$extout ? 'COPY' : 'INSTALL_DATA'}) ") 1549: sep = config_string('BUILD_FILE_SEPARATOR') 1550: if sep 1551: f = f.gsub("/", sep) 1552: sep = ":/="+sep 1553: f = f.gsub(/(\$\(\w+)(\))/) {$1+sep+$2} 1554: f = f.gsub(/(\$\{\w+)(\})/) {$1+sep+$2} 1555: else 1556: sep = "" 1557: end 1558: mfile.print("#{f} $(@D#{sep})\n") 1559: if defined?($installed_list) and !$extout 1560: mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n") 1561: end 1562: end 1563: end 1564: end 1565: dirs.unshift(sodir) if target and !dirs.include?(sodir) 1566: dirs.each {|dir| mfile.print "#{dir}:\n\t$(MAKEDIRS) $@\n"} 1567: 1568: mfile.print "\nsite-install: site-install-so site-install-rb\nsite-install-so: install-so\nsite-install-rb: install-rb\n\n" 1569: 1570: return unless target 1571: 1572: mfile.puts SRC_EXT.collect {|ext| ".path.#{ext} = $(VPATH)"} if $nmake == ?b 1573: mfile.print ".SUFFIXES: .#{SRC_EXT.join(' .')} .#{$OBJEXT}\n" 1574: mfile.print "\n" 1575: 1576: CXX_EXT.each do |ext| 1577: COMPILE_RULES.each do |rule| 1578: mfile.printf(rule, ext, $OBJEXT) 1579: mfile.printf("\n\t%s\n\n", COMPILE_CXX) 1580: end 1581: end 1582: %w[c].each do |ext| 1583: COMPILE_RULES.each do |rule| 1584: mfile.printf(rule, ext, $OBJEXT) 1585: mfile.printf("\n\t%s\n\n", COMPILE_C) 1586: end 1587: end 1588: 1589: mfile.print "$(RUBYARCHDIR)/" if $extout 1590: mfile.print "$(DLLIB): ", (makedef ? "$(DEFFILE) " : ""), "$(OBJS)\n" 1591: mfile.print "\t@-$(RM) $@\n" 1592: mfile.print "\t@-$(MAKEDIRS) $(@D)\n" if $extout 1593: link_so = LINK_SO.gsub(/^/, "\t") 1594: mfile.print link_so, "\n\n" 1595: unless $static.nil? 1596: mfile.print "$(STATIC_LIB): $(OBJS)\n\t" 1597: mfile.print "$(AR) #{config_string('ARFLAGS') || 'cru '}$@ $(OBJS)" 1598: config_string('RANLIB') do |ranlib| 1599: mfile.print "\n\t@-#{ranlib} $(DLLIB) 2> /dev/null || true" 1600: end 1601: end 1602: mfile.print "\n\n" 1603: if makedef 1604: mfile.print "$(DEFFILE): #{origdef}\n" 1605: mfile.print "\t$(RUBY) #{makedef} #{origdef} > $@\n\n" 1606: end 1607: 1608: depend = File.join(srcdir, "depend") 1609: if File.exist?(depend) 1610: suffixes = [] 1611: depout = [] 1612: open(depend, "r") do |dfile| 1613: mfile.printf "###\n" 1614: cont = implicit = nil 1615: impconv = proc do 1616: COMPILE_RULES.each {|rule| depout << (rule % implicit[0]) << implicit[1]} 1617: implicit = nil 1618: end 1619: ruleconv = proc do |line| 1620: if implicit 1621: if /\A\t/ =~ line 1622: implicit[1] << line 1623: next 1624: else 1625: impconv[] 1626: end 1627: end 1628: if m = /\A\.(\w+)\.(\w+)(?:\s*:)/.match(line) 1629: suffixes << m[1] << m[2] 1630: implicit = [[m[1], m[2]], [m.post_match]] 1631: next 1632: elsif RULE_SUBST and /\A(?!\s*\w+\s*=)[$\w][^#]*:/ =~ line 1633: line.gsub!(%r"(\s)(?!\.)([^$(){}+=:\s\/\\,]+)(?=\s|\z)") {$1 + RULE_SUBST % $2} 1634: end 1635: depout << line 1636: end 1637: while line = dfile.gets() 1638: line.gsub!(/\.o\b/, ".#{$OBJEXT}") 1639: line.gsub!(/\$\((?:hdr|top)dir\)\/config.h/, $config_h) if $config_h 1640: if /(?:^|[^\\])(?:\\\\)*\\$/ =~ line 1641: (cont ||= []) << line 1642: next 1643: elsif cont 1644: line = (cont << line).join 1645: cont = nil 1646: end 1647: ruleconv.call(line) 1648: end 1649: if cont 1650: ruleconv.call(cont.join) 1651: elsif implicit 1652: impconv.call 1653: end 1654: end 1655: unless suffixes.empty? 1656: mfile.print ".SUFFIXES: .", suffixes.uniq.join(" ."), "\n\n" 1657: end 1658: mfile.print "$(OBJS): $(RUBY_EXTCONF_H)\n\n" if $extconf_h 1659: mfile.print depout 1660: else 1661: headers = %w[ruby.h defines.h] 1662: if RULE_SUBST 1663: headers.each {|h| h.sub!(/.*/) {|*m| RULE_SUBST % m}} 1664: end 1665: headers << $config_h if $config_h 1666: headers << "$(RUBY_EXTCONF_H)" if $extconf_h 1667: mfile.print "$(OBJS): ", headers.join(' '), "\n" 1668: end 1669: 1670: $makefile_created = true 1671: ensure 1672: mfile.close if mfile 1673: end
Sets a target name that the user can then use to configure various ‘with’ options with on the command line by using that name. For example, if the target is set to "foo", then the user could use the —with-foo-dir command line option.
You may pass along additional ‘include’ or ‘lib’ defaults via the idefault and ldefault parameters, respectively.
Note that dir_config only adds to the list of places to search for libraries and include files. It does not link the libraries into your application.
# File lib/mkmf.rb, line 1165 1165: def dir_config(target, idefault=nil, ldefault=nil) 1166: if dir = with_config(target + "-dir", (idefault unless ldefault)) 1167: defaults = Array === dir ? dir : dir.split(File::PATH_SEPARATOR) 1168: idefault = ldefault = nil 1169: end 1170: 1171: idir = with_config(target + "-include", idefault) 1172: $arg_config.last[1] ||= "${#{target}-dir}/include" 1173: ldir = with_config(target + "-lib", ldefault) 1174: $arg_config.last[1] ||= "${#{target}-dir}/lib" 1175: 1176: idirs = idir ? Array === idir ? idir : idir.split(File::PATH_SEPARATOR) : [] 1177: if defaults 1178: idirs.concat(defaults.collect {|dir| dir + "/include"}) 1179: idir = ([idir] + idirs).compact.join(File::PATH_SEPARATOR) 1180: end 1181: unless idirs.empty? 1182: idirs.collect! {|dir| "-I" + dir} 1183: idirs -= Shellwords.shellwords($CPPFLAGS) 1184: unless idirs.empty? 1185: $CPPFLAGS = (idirs.quote << $CPPFLAGS).join(" ") 1186: end 1187: end 1188: 1189: ldirs = ldir ? Array === ldir ? ldir : ldir.split(File::PATH_SEPARATOR) : [] 1190: if defaults 1191: ldirs.concat(defaults.collect {|dir| dir + "/lib"}) 1192: ldir = ([ldir] + ldirs).compact.join(File::PATH_SEPARATOR) 1193: end 1194: $LIBPATH = ldirs | $LIBPATH 1195: 1196: [idir, ldir] 1197: end
Tests for the presence of an —enable-config or —disable-config option. Returns true if the enable option is given, false if the disable option is given, and the default value otherwise.
This can be useful for adding custom definitions, such as debug information.
Example:
if enable_config("debug") $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG" end
# File lib/mkmf.rb, line 1094 1094: def enable_config(config, *defaults) 1095: if arg_config("--enable-"+config) 1096: true 1097: elsif arg_config("--disable-"+config) 1098: false 1099: elsif block_given? 1100: yield(config, *defaults) 1101: else 1102: return *defaults 1103: end 1104: end
Searches for the executable bin on path. The default path is your PATH environment variable. If that isn‘t defined, it will resort to searching /usr/local/bin, /usr/ucb, /usr/bin and /bin.
If found, it will return the full path, including the executable name, of where it was found.
Note that this method does not actually affect the generated Makefile.
# File lib/mkmf.rb, line 1033 1033: def find_executablefind_executable(bin, path = nil) 1034: checking_for checking_message(bin, path) do 1035: find_executable0(bin, path) 1036: end 1037: end
Instructs mkmf to search for the given header in any of the paths provided, and returns whether or not it was found in those paths.
If the header is found then the path it was found on is added to the list of included directories that are sent to the compiler (via the -I switch).
# File lib/mkmf.rb, line 768 768: def find_header(header, *paths) 769: message = checking_message(header, paths) 770: header = cpp_include(header) 771: checking_for message do 772: if try_cpp(header) 773: true 774: else 775: found = false 776: paths.each do |dir| 777: opt = "-I#{dir}".quote 778: if try_cpp(header, opt) 779: $INCFLAGS << " " << opt 780: found = true 781: break 782: end 783: end 784: found 785: end 786: end 787: end
Returns whether or not the entry point func can be found within the library lib in one of the paths specified, where paths is an array of strings. If func is nil , then the main() function is used as the entry point.
If lib is found, then the path it was found on is added to the list of library paths searched and linked against.
# File lib/mkmf.rb, line 684 684: def find_library(lib, func, *paths, &b) 685: func = "main" if !func or func.empty? 686: lib = with_config(lib+'lib', lib) 687: paths = paths.collect {|path| path.split(File::PATH_SEPARATOR)}.flatten 688: checking_for "#{func}() in #{LIBARG%lib}" do 689: libpath = $LIBPATH 690: libs = append_library($libs, lib) 691: begin 692: until r = try_func(func, libs, &b) or paths.empty? 693: $LIBPATH = libpath | [paths.shift] 694: end 695: if r 696: $libs = libs 697: libpath = nil 698: end 699: ensure 700: $LIBPATH = libpath if libpath 701: end 702: r 703: end 704: end
Returns where the static type type is defined.
You may also pass additional flags to opt which are then passed along to the compiler.
See also have_type.
# File lib/mkmf.rb, line 860 860: def find_type(type, opt, *headers, &b) 861: opt ||= "" 862: fmt = "not found" 863: def fmt.%(x) 864: x ? x.respond_to?(:join) ? x.join(",") : x : self 865: end 866: checking_for checking_message(type, nil, opt), fmt do 867: headers.find do |h| 868: try_type(type, h, opt, &b) 869: end 870: end 871: end
Returns whether or not the constant const is defined. You may optionally pass the type of const as [const, type], like as:
have_const(%w[PTHREAD_MUTEX_INITIALIZER pthread_mutex_t], "pthread.h")
You may also pass additional headers to check against in addition to the common header files, and additional flags to opt which are then passed along to the compiler.
If found, a macro is passed as a preprocessor constant to the compiler using the type name, in uppercase, prepended with ‘HAVE_CONST_’.
For example, if have_const(‘foo’) returned true, then the HAVE_CONST_FOO preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 906 906: def have_const(const, headers = nil, opt = "", &b) 907: checking_for checking_message([*const].compact.join(' '), headers, opt) do 908: try_const(const, headers, opt, &b) 909: end 910: end
Returns whether or not the function func can be found in the common header files, or within any headers that you provide. If found, a macro is passed as a preprocessor constant to the compiler using the function name, in uppercase, prepended with ‘HAVE_’.
For example, if have_func(‘foo’) returned true, then the HAVE_FOO preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 714 714: def have_func(func, headers = nil, &b) 715: checking_for checking_message("#{func}()", headers) do 716: if try_func(func, $libs, headers, &b) 717: $defs.push(format("-DHAVE_%s", func.tr_cpp)) 718: true 719: else 720: false 721: end 722: end 723: end
Returns whether or not the given header file can be found on your system. If found, a macro is passed as a preprocessor constant to the compiler using the header file name, in uppercase, prepended with ‘HAVE_’.
For example, if have_header(‘foo.h’) returned true, then the HAVE_FOO_H preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 751 751: def have_header(header, &b) 752: checking_for header do 753: if try_cpp(cpp_include(header), &b) 754: $defs.push(format("-DHAVE_%s", header.tr("a-z./\055", "A-Z___"))) 755: true 756: else 757: false 758: end 759: end 760: end
Returns whether or not the given entry point func can be found within lib. If func is nil, the ‘main()’ entry point is used by default. If found, it adds the library to list of libraries to be used when linking your extension.
If headers are provided, it will include those header files as the header files it looks in when searching for func.
The real name of the library to be linked can be altered by ’—with-FOOlib’ configuration option.
# File lib/mkmf.rb, line 659 659: def have_library(lib, func = nil, headers = nil, &b) 660: func = "main" if !func or func.empty? 661: lib = with_config(lib+'lib', lib) 662: checking_for checking_message("#{func}()", LIBARG%lib) do 663: if COMMON_LIBS.include?(lib) 664: true 665: else 666: libs = append_library($libs, lib) 667: if try_func(func, libs, headers, &b) 668: $libs = libs 669: true 670: else 671: false 672: end 673: end 674: end 675: end
Returns whether or not macro is defined either in the common header files or within any headers you provide.
Any options you pass to opt are passed along to the compiler.
# File lib/mkmf.rb, line 642 642: def have_macro(macro, headers = nil, opt = "", &b) 643: checking_for checking_message(macro, headers, opt) do 644: macro_defined?(macro, cpp_include(headers), opt, &b) 645: end 646: end
Returns whether or not the struct of type type contains member. If it does not, or the struct type can‘t be found, then false is returned. You may optionally specify additional headers in which to look for the struct (in addition to the common header files).
If found, a macro is passed as a preprocessor constant to the compiler using the member name, in uppercase, prepended with ‘HAVE_ST_’.
For example, if have_struct_member(‘struct foo’, ‘bar’) returned true, then the HAVE_ST_BAR preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 800 800: def have_struct_member(type, member, headers = nil, &b) 801: checking_for checking_message("#{type}.#{member}", headers) do 802: if try_compile("\#{COMMON_HEADERS}\n\#{cpp_include(headers)}\n/*top*/\nint main() { return 0; }\nint s = (char *)&((\#{type}*)0)->\#{member} - (char *)0;\n", &b) 803: $defs.push(format("-DHAVE_ST_%s", member.tr_cpp)) 804: true 805: else 806: false 807: end 808: end 809: end
Returns whether or not the static type type is defined. You may optionally pass additional headers to check against in addition to the common header files.
You may also pass additional flags to opt which are then passed along to the compiler.
If found, a macro is passed as a preprocessor constant to the compiler using the type name, in uppercase, prepended with ‘HAVE_TYPE_’.
For example, if have_type(‘foo’) returned true, then the HAVE_TYPE_FOO preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 847 847: def have_type(type, headers = nil, opt = "", &b) 848: checking_for checking_message(type, headers, opt) do 849: try_type(type, headers, opt, &b) 850: end 851: end
Returns whether or not the variable var can be found in the common header files, or within any headers that you provide. If found, a macro is passed as a preprocessor constant to the compiler using the variable name, in uppercase, prepended with ‘HAVE_’.
For example, if have_var(‘foo’) returned true, then the HAVE_FOO preprocessor macro would be passed to the compiler.
# File lib/mkmf.rb, line 733 733: def have_var(var, headers = nil, &b) 734: checking_for checking_message(var, headers) do 735: if try_var(var, headers, &b) 736: $defs.push(format("-DHAVE_%s", var.tr_cpp)) 737: true 738: else 739: false 740: end 741: end 742: end
# File lib/mkmf.rb, line 873 873: def try_consttry_const(const, headers = nil, opt = "", &b) 874: const, type = *const 875: if try_compile("\#{COMMON_HEADERS}\n\#{cpp_include(headers)}\n/*top*/\ntypedef \#{type || 'int'} conftest_type;\nconftest_type conftestval = \#{type ? '' : '(int)'}\#{const};\n", opt, &b) 876: $defs.push(format("-DHAVE_CONST_%s", const.tr_cpp)) 877: true 878: else 879: false 880: end 881: end
# File lib/mkmf.rb, line 818 818: def try_type(type, headers = nil, opt = "", &b) 819: if try_compile("\#{COMMON_HEADERS}\n\#{cpp_include(headers)}\n/*top*/\ntypedef \#{type} conftest_type;\nint conftestval[sizeof(conftest_type)?1:-1];\n", opt, &b) 820: $defs.push(format("-DHAVE_TYPE_%s", type.tr_cpp)) 821: true 822: else 823: false 824: end 825: end
Tests for the presence of a —with-config or —without-config option. Returns true if the with option is given, false if the without option is given, and the default value otherwise.
This can be useful for adding custom definitions, such as debug information.
Example:
if with_config("debug") $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG" end
# File lib/mkmf.rb, line 1061 1061: def with_config(config, *defaults) 1062: config = config.sub(/^--with[-_]/, '') 1063: val = arg_config("--with-"+config) do 1064: if arg_config("--without-"+config) 1065: false 1066: elsif block_given? 1067: yield(config, *defaults) 1068: else 1069: break *defaults 1070: end 1071: end 1072: case val 1073: when "yes" 1074: true 1075: when "no" 1076: false 1077: else 1078: val 1079: end 1080: end