PageRenderTime 72ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/mkmf.rb

https://github.com/EarthJem/ruby
Ruby | 2613 lines | 2071 code | 157 blank | 385 comment | 155 complexity | 3d520450b3ec93643daead441bb9fa00 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. # -*- coding: us-ascii -*-
  2. # module to create Makefile for extension modules
  3. # invoke like: ruby -r mkmf extconf.rb
  4. require 'rbconfig'
  5. require 'fileutils'
  6. require 'shellwords'
  7. # :stopdoc:
  8. class String
  9. # Wraps a string in escaped quotes if it contains whitespace.
  10. def quote
  11. /\s/ =~ self ? "\"#{self}\"" : "#{self}"
  12. end
  13. # Escape whitespaces for Makefile.
  14. def unspace
  15. gsub(/\s/, '\\\\\\&')
  16. end
  17. # Generates a string used as cpp macro name.
  18. def tr_cpp
  19. strip.upcase.tr_s("^A-Z0-9_*", "_").tr_s("*", "P")
  20. end
  21. def funcall_style
  22. /\)\z/ =~ self ? dup : "#{self}()"
  23. end
  24. def sans_arguments
  25. self[/\A[^()]+/]
  26. end
  27. end
  28. class Array
  29. # Wraps all strings in escaped quotes if they contain whitespace.
  30. def quote
  31. map {|s| s.quote}
  32. end
  33. end
  34. # :startdoc:
  35. ##
  36. # mkmf.rb is used by ruby C extensions to generate a Makefile which will
  37. # correctly compile and link the C extension to ruby and a third-party
  38. # library.
  39. module MakeMakefile
  40. #### defer until this module become global-state free.
  41. # def self.extended(obj)
  42. # obj.init_mkmf
  43. # super
  44. # end
  45. #
  46. # def initialize(*args, rbconfig: RbConfig, **rest)
  47. # init_mkmf(rbconfig::MAKEFILE_CONFIG, rbconfig::CONFIG)
  48. # super(*args, **rest)
  49. # end
  50. ##
  51. # The makefile configuration using the defaults from when ruby was built.
  52. CONFIG = RbConfig::MAKEFILE_CONFIG
  53. ORIG_LIBPATH = ENV['LIB']
  54. ##
  55. # Extensions for files compiled with a C compiler
  56. C_EXT = %w[c m]
  57. ##
  58. # Extensions for files complied with a C++ compiler
  59. CXX_EXT = %w[cc mm cxx cpp]
  60. if File::FNM_SYSCASE.zero?
  61. CXX_EXT.concat(%w[C])
  62. end
  63. ##
  64. # Extensions for source files
  65. SRC_EXT = C_EXT + CXX_EXT
  66. ##
  67. # Extensions for header files
  68. HDR_EXT = %w[h hpp]
  69. $static = nil
  70. $config_h = '$(arch_hdrdir)/ruby/config.h'
  71. $default_static = $static
  72. unless defined? $configure_args
  73. $configure_args = {}
  74. args = CONFIG["configure_args"]
  75. if ENV["CONFIGURE_ARGS"]
  76. args << " " << ENV["CONFIGURE_ARGS"]
  77. end
  78. for arg in Shellwords::shellwords(args)
  79. arg, val = arg.split('=', 2)
  80. next unless arg
  81. arg.tr!('_', '-')
  82. if arg.sub!(/^(?!--)/, '--')
  83. val or next
  84. arg.downcase!
  85. end
  86. next if /^--(?:top|topsrc|src|cur)dir$/ =~ arg
  87. $configure_args[arg] = val || true
  88. end
  89. for arg in ARGV
  90. arg, val = arg.split('=', 2)
  91. next unless arg
  92. arg.tr!('_', '-')
  93. if arg.sub!(/^(?!--)/, '--')
  94. val or next
  95. arg.downcase!
  96. end
  97. $configure_args[arg] = val || true
  98. end
  99. end
  100. $libdir = CONFIG["libdir"]
  101. $rubylibdir = CONFIG["rubylibdir"]
  102. $archdir = CONFIG["archdir"]
  103. $sitedir = CONFIG["sitedir"]
  104. $sitelibdir = CONFIG["sitelibdir"]
  105. $sitearchdir = CONFIG["sitearchdir"]
  106. $vendordir = CONFIG["vendordir"]
  107. $vendorlibdir = CONFIG["vendorlibdir"]
  108. $vendorarchdir = CONFIG["vendorarchdir"]
  109. $mswin = /mswin/ =~ RUBY_PLATFORM
  110. $bccwin = /bccwin/ =~ RUBY_PLATFORM
  111. $mingw = /mingw/ =~ RUBY_PLATFORM
  112. $cygwin = /cygwin/ =~ RUBY_PLATFORM
  113. $netbsd = /netbsd/ =~ RUBY_PLATFORM
  114. $os2 = /os2/ =~ RUBY_PLATFORM
  115. $beos = /beos/ =~ RUBY_PLATFORM
  116. $haiku = /haiku/ =~ RUBY_PLATFORM
  117. $solaris = /solaris/ =~ RUBY_PLATFORM
  118. $universal = /universal/ =~ RUBY_PLATFORM
  119. $dest_prefix_pattern = (File::PATH_SEPARATOR == ';' ? /\A([[:alpha:]]:)?/ : /\A/)
  120. # :stopdoc:
  121. def config_string(key, config = CONFIG)
  122. s = config[key] and !s.empty? and block_given? ? yield(s) : s
  123. end
  124. module_function :config_string
  125. def dir_re(dir)
  126. Regexp.new('\$(?:\('+dir+'\)|\{'+dir+'\})(?:\$(?:\(target_prefix\)|\{target_prefix\}))?')
  127. end
  128. module_function :dir_re
  129. def relative_from(path, base)
  130. dir = File.join(path, "")
  131. if File.expand_path(dir) == File.expand_path(dir, base)
  132. path
  133. else
  134. File.join(base, path)
  135. end
  136. end
  137. INSTALL_DIRS = [
  138. [dir_re('commondir'), "$(RUBYCOMMONDIR)"],
  139. [dir_re('sitedir'), "$(RUBYCOMMONDIR)"],
  140. [dir_re('vendordir'), "$(RUBYCOMMONDIR)"],
  141. [dir_re('rubylibdir'), "$(RUBYLIBDIR)"],
  142. [dir_re('archdir'), "$(RUBYARCHDIR)"],
  143. [dir_re('sitelibdir'), "$(RUBYLIBDIR)"],
  144. [dir_re('vendorlibdir'), "$(RUBYLIBDIR)"],
  145. [dir_re('sitearchdir'), "$(RUBYARCHDIR)"],
  146. [dir_re('vendorarchdir'), "$(RUBYARCHDIR)"],
  147. [dir_re('rubyhdrdir'), "$(RUBYHDRDIR)"],
  148. [dir_re('sitehdrdir'), "$(SITEHDRDIR)"],
  149. [dir_re('vendorhdrdir'), "$(VENDORHDRDIR)"],
  150. [dir_re('bindir'), "$(BINDIR)"],
  151. ]
  152. def install_dirs(target_prefix = nil)
  153. if $extout
  154. dirs = [
  155. ['BINDIR', '$(extout)/bin'],
  156. ['RUBYCOMMONDIR', '$(extout)/common'],
  157. ['RUBYLIBDIR', '$(RUBYCOMMONDIR)$(target_prefix)'],
  158. ['RUBYARCHDIR', '$(extout)/$(arch)$(target_prefix)'],
  159. ['HDRDIR', '$(extout)/include/ruby$(target_prefix)'],
  160. ['ARCHHDRDIR', '$(extout)/include/$(arch)/ruby$(target_prefix)'],
  161. ['extout', "#$extout"],
  162. ['extout_prefix', "#$extout_prefix"],
  163. ]
  164. elsif $extmk
  165. dirs = [
  166. ['BINDIR', '$(bindir)'],
  167. ['RUBYCOMMONDIR', '$(rubylibdir)'],
  168. ['RUBYLIBDIR', '$(rubylibdir)$(target_prefix)'],
  169. ['RUBYARCHDIR', '$(archdir)$(target_prefix)'],
  170. ['HDRDIR', '$(rubyhdrdir)/ruby$(target_prefix)'],
  171. ['ARCHHDRDIR', '$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
  172. ]
  173. elsif $configure_args.has_key?('--vendor')
  174. dirs = [
  175. ['BINDIR', '$(DESTDIR)$(bindir)'],
  176. ['RUBYCOMMONDIR', '$(DESTDIR)$(vendordir)$(target_prefix)'],
  177. ['RUBYLIBDIR', '$(DESTDIR)$(vendorlibdir)$(target_prefix)'],
  178. ['RUBYARCHDIR', '$(DESTDIR)$(vendorarchdir)$(target_prefix)'],
  179. ['HDRDIR', '$(DESTDIR)$(rubyhdrdir)/ruby$(target_prefix)'],
  180. ['ARCHHDRDIR', '$(DESTDIR)$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
  181. ]
  182. else
  183. dirs = [
  184. ['BINDIR', '$(DESTDIR)$(bindir)'],
  185. ['RUBYCOMMONDIR', '$(DESTDIR)$(sitedir)$(target_prefix)'],
  186. ['RUBYLIBDIR', '$(DESTDIR)$(sitelibdir)$(target_prefix)'],
  187. ['RUBYARCHDIR', '$(DESTDIR)$(sitearchdir)$(target_prefix)'],
  188. ['HDRDIR', '$(DESTDIR)$(rubyhdrdir)/ruby$(target_prefix)'],
  189. ['ARCHHDRDIR', '$(DESTDIR)$(rubyhdrdir)/$(arch)/ruby$(target_prefix)'],
  190. ]
  191. end
  192. dirs << ['target_prefix', (target_prefix ? "/#{target_prefix}" : "")]
  193. dirs
  194. end
  195. def map_dir(dir, map = nil)
  196. map ||= INSTALL_DIRS
  197. map.inject(dir) {|d, (orig, new)| d.gsub(orig, new)}
  198. end
  199. topdir = File.dirname(File.dirname(__FILE__))
  200. path = File.expand_path($0)
  201. until (dir = File.dirname(path)) == path
  202. if File.identical?(dir, topdir)
  203. $extmk = true if %r"\A(?:ext|enc|tool|test)\z" =~ File.basename(path)
  204. break
  205. end
  206. path = dir
  207. end
  208. $extmk ||= false
  209. if not $extmk and File.exist?(($hdrdir = RbConfig::CONFIG["rubyhdrdir"]) + "/ruby/ruby.h")
  210. $topdir = $hdrdir
  211. $top_srcdir = $hdrdir
  212. $arch_hdrdir = RbConfig::CONFIG["rubyarchhdrdir"]
  213. elsif File.exist?(($hdrdir = ($top_srcdir ||= topdir) + "/include") + "/ruby.h")
  214. $topdir ||= RbConfig::CONFIG["topdir"]
  215. $arch_hdrdir = "$(extout)/include/$(arch)"
  216. else
  217. abort "mkmf.rb can't find header files for ruby at #{$hdrdir}/ruby.h"
  218. end
  219. OUTFLAG = CONFIG['OUTFLAG']
  220. COUTFLAG = CONFIG['COUTFLAG']
  221. CPPOUTFILE = CONFIG['CPPOUTFILE']
  222. CONFTEST_C = "conftest.c".freeze
  223. def rm_f(*files)
  224. opt = (Hash === files.last ? [files.pop] : [])
  225. FileUtils.rm_f(Dir[*files.flatten], *opt)
  226. end
  227. module_function :rm_f
  228. def rm_rf(*files)
  229. opt = (Hash === files.last ? [files.pop] : [])
  230. FileUtils.rm_rf(Dir[*files.flatten], *opt)
  231. end
  232. module_function :rm_rf
  233. # Returns time stamp of the +target+ file if it exists and is newer than or
  234. # equal to all of +times+.
  235. def modified?(target, times)
  236. (t = File.mtime(target)) rescue return nil
  237. Array === times or times = [times]
  238. t if times.all? {|n| n <= t}
  239. end
  240. def split_libs(*strs)
  241. strs.map {|s| s.split(/\s+(?=-|\z)/)}.flatten
  242. end
  243. def merge_libs(*libs)
  244. libs.inject([]) do |x, y|
  245. y = y.inject([]) {|ary, e| ary.last == e ? ary : ary << e}
  246. y.each_with_index do |v, yi|
  247. if xi = x.rindex(v)
  248. x[(xi+1)..-1] = merge_libs(y[(yi+1)..-1], x[(xi+1)..-1])
  249. x[xi, 0] = y[0...yi]
  250. break
  251. end
  252. end and x.concat(y)
  253. x
  254. end
  255. end
  256. # This is a custom logging module. It generates an mkmf.log file when you
  257. # run your extconf.rb script. This can be useful for debugging unexpected
  258. # failures.
  259. #
  260. # This module and its associated methods are meant for internal use only.
  261. #
  262. module Logging
  263. @log = nil
  264. @logfile = 'mkmf.log'
  265. @orgerr = $stderr.dup
  266. @orgout = $stdout.dup
  267. @postpone = 0
  268. @quiet = $extmk
  269. def self::log_open
  270. @log ||= File::open(@logfile, 'wb')
  271. @log.sync = true
  272. end
  273. def self::log_opened?
  274. @log and not @log.closed?
  275. end
  276. def self::open
  277. log_open
  278. $stderr.reopen(@log)
  279. $stdout.reopen(@log)
  280. yield
  281. ensure
  282. $stderr.reopen(@orgerr)
  283. $stdout.reopen(@orgout)
  284. end
  285. def self::message(*s)
  286. log_open
  287. @log.printf(*s)
  288. end
  289. def self::logfile file
  290. @logfile = file
  291. log_close
  292. end
  293. def self::log_close
  294. if @log and not @log.closed?
  295. @log.flush
  296. @log.close
  297. @log = nil
  298. end
  299. end
  300. def self::postpone
  301. tmplog = "mkmftmp#{@postpone += 1}.log"
  302. open do
  303. log, *save = @log, @logfile, @orgout, @orgerr
  304. @log, @logfile, @orgout, @orgerr = nil, tmplog, log, log
  305. begin
  306. log.print(open {yield @log})
  307. ensure
  308. @log.close if @log and not @log.closed?
  309. File::open(tmplog) {|t| FileUtils.copy_stream(t, log)} if File.exist?(tmplog)
  310. @log, @logfile, @orgout, @orgerr = log, *save
  311. @postpone -= 1
  312. MakeMakefile.rm_f tmplog
  313. end
  314. end
  315. end
  316. class << self
  317. attr_accessor :quiet
  318. end
  319. end
  320. def libpath_env
  321. # used only if native compiling
  322. if libpathenv = config_string("LIBPATHENV")
  323. pathenv = ENV[libpathenv]
  324. libpath = RbConfig.expand($DEFLIBPATH.join(File::PATH_SEPARATOR))
  325. {libpathenv => [libpath, pathenv].compact.join(File::PATH_SEPARATOR)}
  326. else
  327. {}
  328. end
  329. end
  330. def xsystem command, opts = nil
  331. varpat = /\$\((\w+)\)|\$\{(\w+)\}/
  332. if varpat =~ command
  333. vars = Hash.new {|h, k| h[k] = ''; ENV[k]}
  334. command = command.dup
  335. nil while command.gsub!(varpat) {vars[$1||$2]}
  336. end
  337. Logging::open do
  338. puts command.quote
  339. if opts and opts[:werror]
  340. result = nil
  341. Logging.postpone do |log|
  342. result = (system(libpath_env, command) and File.zero?(log.path))
  343. ""
  344. end
  345. result
  346. else
  347. system(libpath_env, command)
  348. end
  349. end
  350. end
  351. def xpopen command, *mode, &block
  352. Logging::open do
  353. case mode[0]
  354. when nil, /^r/
  355. puts "#{command} |"
  356. else
  357. puts "| #{command}"
  358. end
  359. IO.popen(libpath_env, command, *mode, &block)
  360. end
  361. end
  362. def log_src(src, heading="checked program was")
  363. src = src.split(/^/)
  364. fmt = "%#{src.size.to_s.size}d: %s"
  365. Logging::message <<"EOM"
  366. #{heading}:
  367. /* begin */
  368. EOM
  369. src.each_with_index {|line, no| Logging::message fmt, no+1, line}
  370. Logging::message <<"EOM"
  371. /* end */
  372. EOM
  373. end
  374. def create_tmpsrc(src)
  375. src = "#{COMMON_HEADERS}\n#{src}"
  376. src = yield(src) if block_given?
  377. src.gsub!(/[ \t]+$/, '')
  378. src.gsub!(/\A\n+|^\n+$/, '')
  379. src.sub!(/[^\n]\z/, "\\&\n")
  380. count = 0
  381. begin
  382. open(CONFTEST_C, "wb") do |cfile|
  383. cfile.print src
  384. end
  385. rescue Errno::EACCES
  386. if (count += 1) < 5
  387. sleep 0.2
  388. retry
  389. end
  390. end
  391. src
  392. end
  393. def have_devel?
  394. unless defined? $have_devel
  395. $have_devel = true
  396. $have_devel = try_link(MAIN_DOES_NOTHING)
  397. end
  398. $have_devel
  399. end
  400. def try_do(src, command, *opts, &b)
  401. unless have_devel?
  402. raise <<MSG
  403. The compiler failed to generate an executable file.
  404. You have to install development tools first.
  405. MSG
  406. end
  407. begin
  408. src = create_tmpsrc(src, &b)
  409. xsystem(command, *opts)
  410. ensure
  411. log_src(src)
  412. MakeMakefile.rm_rf 'conftest.dSYM'
  413. end
  414. end
  415. def link_command(ldflags, opt="", libpath=$DEFLIBPATH|$LIBPATH)
  416. librubyarg = $extmk ? $LIBRUBYARG_STATIC : "$(LIBRUBYARG)"
  417. conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote,
  418. 'src' => "#{CONFTEST_C}",
  419. 'arch_hdrdir' => $arch_hdrdir.quote,
  420. 'top_srcdir' => $top_srcdir.quote,
  421. 'INCFLAGS' => "#$INCFLAGS",
  422. 'CPPFLAGS' => "#$CPPFLAGS",
  423. 'CFLAGS' => "#$CFLAGS",
  424. 'ARCH_FLAG' => "#$ARCH_FLAG",
  425. 'LDFLAGS' => "#$LDFLAGS #{ldflags}",
  426. 'LOCAL_LIBS' => "#$LOCAL_LIBS #$libs",
  427. 'LIBS' => "#{librubyarg} #{opt} #$LIBS")
  428. conf['LIBPATH'] = libpathflag(libpath.map {|s| RbConfig::expand(s.dup, conf)})
  429. RbConfig::expand(TRY_LINK.dup, conf)
  430. end
  431. def cc_command(opt="")
  432. conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
  433. 'arch_hdrdir' => $arch_hdrdir.quote,
  434. 'top_srcdir' => $top_srcdir.quote)
  435. RbConfig::expand("$(CC) #$INCFLAGS #$CPPFLAGS #$CFLAGS #$ARCH_FLAG #{opt} -c #{CONFTEST_C}",
  436. conf)
  437. end
  438. def cpp_command(outfile, opt="")
  439. conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
  440. 'arch_hdrdir' => $arch_hdrdir.quote,
  441. 'top_srcdir' => $top_srcdir.quote)
  442. if $universal and (arch_flag = conf['ARCH_FLAG']) and !arch_flag.empty?
  443. conf['ARCH_FLAG'] = arch_flag.gsub(/(?:\G|\s)-arch\s+\S+/, '')
  444. end
  445. RbConfig::expand("$(CPP) #$INCFLAGS #$CPPFLAGS #$CFLAGS #{opt} #{CONFTEST_C} #{outfile}",
  446. conf)
  447. end
  448. def libpathflag(libpath=$DEFLIBPATH|$LIBPATH)
  449. libpath.map{|x|
  450. case x
  451. when "$(topdir)", /\A\./
  452. LIBPATHFLAG
  453. else
  454. LIBPATHFLAG+RPATHFLAG
  455. end % x.quote
  456. }.join
  457. end
  458. def with_werror(opt, opts = nil)
  459. if opts
  460. if opts[:werror] and config_string("WERRORFLAG") {|flag| opt = opt ? "#{opt} #{flag}" : flag}
  461. (opts = opts.dup).delete(:werror)
  462. end
  463. yield(opt, opts)
  464. else
  465. yield(opt)
  466. end
  467. end
  468. def try_link0(src, opt="", *opts, &b) # :nodoc:
  469. cmd = link_command("", opt)
  470. if $universal
  471. require 'tmpdir'
  472. Dir.mktmpdir("mkmf_", oldtmpdir = ENV["TMPDIR"]) do |tmpdir|
  473. begin
  474. ENV["TMPDIR"] = tmpdir
  475. try_do(src, cmd, *opts, &b)
  476. ensure
  477. ENV["TMPDIR"] = oldtmpdir
  478. end
  479. end
  480. else
  481. try_do(src, cmd, *opts, &b)
  482. end and File.executable?("conftest#{$EXEEXT}")
  483. end
  484. # Returns whether or not the +src+ can be compiled as a C source and linked
  485. # with its depending libraries successfully. +opt+ is passed to the linker
  486. # as options. Note that +$CFLAGS+ and +$LDFLAGS+ are also passed to the
  487. # linker.
  488. #
  489. # If a block given, it is called with the source before compilation. You can
  490. # modify the source in the block.
  491. #
  492. # [+src+] a String which contains a C source
  493. # [+opt+] a String which contains linker options
  494. def try_link(src, opt="", *opts, &b)
  495. try_link0(src, opt, *opts, &b)
  496. ensure
  497. MakeMakefile.rm_f "conftest*", "c0x32*"
  498. end
  499. # Returns whether or not the +src+ can be compiled as a C source. +opt+ is
  500. # passed to the C compiler as options. Note that +$CFLAGS+ is also passed to
  501. # the compiler.
  502. #
  503. # If a block given, it is called with the source before compilation. You can
  504. # modify the source in the block.
  505. #
  506. # [+src+] a String which contains a C source
  507. # [+opt+] a String which contains compiler options
  508. def try_compile(src, opt="", *opts, &b)
  509. with_werror(opt, *opts) {|_opt, *_opts| try_do(src, cc_command(_opt), *_opts, &b)} and
  510. File.file?("conftest.#{$OBJEXT}")
  511. ensure
  512. MakeMakefile.rm_f "conftest*"
  513. end
  514. # Returns whether or not the +src+ can be preprocessed with the C
  515. # preprocessor. +opt+ is passed to the preprocessor as options. Note that
  516. # +$CFLAGS+ is also passed to the preprocessor.
  517. #
  518. # If a block given, it is called with the source before preprocessing. You
  519. # can modify the source in the block.
  520. #
  521. # [+src+] a String which contains a C source
  522. # [+opt+] a String which contains preprocessor options
  523. def try_cpp(src, opt="", *opts, &b)
  524. try_do(src, cpp_command(CPPOUTFILE, opt), *opts, &b) and
  525. File.file?("conftest.i")
  526. ensure
  527. MakeMakefile.rm_f "conftest*"
  528. end
  529. alias_method :try_header, (config_string('try_header') || :try_cpp)
  530. def cpp_include(header)
  531. if header
  532. header = [header] unless header.kind_of? Array
  533. header.map {|h| String === h ? "#include <#{h}>\n" : h}.join
  534. else
  535. ""
  536. end
  537. end
  538. def with_cppflags(flags)
  539. cppflags = $CPPFLAGS
  540. $CPPFLAGS = flags
  541. ret = yield
  542. ensure
  543. $CPPFLAGS = cppflags unless ret
  544. end
  545. def try_cppflags(flags)
  546. with_cppflags(flags) do
  547. try_header("int main() {return 0;}")
  548. end
  549. end
  550. def with_cflags(flags)
  551. cflags = $CFLAGS
  552. $CFLAGS = flags
  553. ret = yield
  554. ensure
  555. $CFLAGS = cflags unless ret
  556. end
  557. def try_cflags(flags)
  558. with_cflags(flags) do
  559. try_compile("int main() {return 0;}")
  560. end
  561. end
  562. def with_ldflags(flags)
  563. ldflags = $LDFLAGS
  564. $LDFLAGS = flags
  565. ret = yield
  566. ensure
  567. $LDFLAGS = ldflags unless ret
  568. end
  569. def try_ldflags(flags)
  570. with_ldflags(flags) do
  571. try_link("int main() {return 0;}")
  572. end
  573. end
  574. def try_static_assert(expr, headers = nil, opt = "", &b)
  575. headers = cpp_include(headers)
  576. try_compile(<<SRC, opt, &b)
  577. #{headers}
  578. /*top*/
  579. int conftest_const[(#{expr}) ? 1 : -1];
  580. SRC
  581. end
  582. def try_constant(const, headers = nil, opt = "", &b)
  583. includes = cpp_include(headers)
  584. neg = try_static_assert("#{const} < 0", headers, opt)
  585. if CROSS_COMPILING
  586. if neg
  587. const = "-(#{const})"
  588. elsif try_static_assert("#{const} > 0", headers, opt)
  589. # positive constant
  590. elsif try_static_assert("#{const} == 0", headers, opt)
  591. return 0
  592. else
  593. # not a constant
  594. return nil
  595. end
  596. upper = 1
  597. lower = 0
  598. until try_static_assert("#{const} <= #{upper}", headers, opt)
  599. lower = upper
  600. upper <<= 1
  601. end
  602. return nil unless lower
  603. while upper > lower + 1
  604. mid = (upper + lower) / 2
  605. if try_static_assert("#{const} > #{mid}", headers, opt)
  606. lower = mid
  607. else
  608. upper = mid
  609. end
  610. end
  611. upper = -upper if neg
  612. return upper
  613. else
  614. src = %{#{includes}
  615. #include <stdio.h>
  616. /*top*/
  617. typedef#{neg ? '' : ' unsigned'}
  618. #ifdef PRI_LL_PREFIX
  619. #define PRI_CONFTEST_PREFIX PRI_LL_PREFIX
  620. LONG_LONG
  621. #else
  622. #define PRI_CONFTEST_PREFIX "l"
  623. long
  624. #endif
  625. conftest_type;
  626. conftest_type conftest_const = (conftest_type)(#{const});
  627. int main() {printf("%"PRI_CONFTEST_PREFIX"#{neg ? 'd' : 'u'}\\n", conftest_const); return 0;}
  628. }
  629. begin
  630. if try_link0(src, opt, &b)
  631. xpopen("./conftest") do |f|
  632. return Integer(f.gets)
  633. end
  634. end
  635. ensure
  636. MakeMakefile.rm_f "conftest*"
  637. end
  638. end
  639. nil
  640. end
  641. # You should use +have_func+ rather than +try_func+.
  642. #
  643. # [+func+] a String which contains a symbol name
  644. # [+libs+] a String which contains library names.
  645. # [+headers+] a String or an Array of strings which contains names of header
  646. # files.
  647. def try_func(func, libs, headers = nil, opt = "", &b)
  648. headers = cpp_include(headers)
  649. case func
  650. when /^&/
  651. decltype = proc {|x|"const volatile void *#{x}"}
  652. when /\)$/
  653. call = func
  654. else
  655. call = "#{func}()"
  656. decltype = proc {|x| "void ((*#{x})())"}
  657. end
  658. if opt and !opt.empty?
  659. [[:to_str], [:join, " "], [:to_s]].each do |meth, *args|
  660. if opt.respond_to?(meth)
  661. break opt = opt.send(meth, *args)
  662. end
  663. end
  664. opt = "#{opt} #{libs}"
  665. else
  666. opt = libs
  667. end
  668. decltype && try_link(<<"SRC", opt, &b) or
  669. #{headers}
  670. /*top*/
  671. extern int t(void);
  672. #{MAIN_DOES_NOTHING 't'}
  673. int t(void) { #{decltype["volatile p"]}; p = (#{decltype[]})#{func}; return 0; }
  674. SRC
  675. call && try_link(<<"SRC", opt, &b)
  676. #{headers}
  677. /*top*/
  678. extern int t(void);
  679. #{MAIN_DOES_NOTHING 't'}
  680. int t(void) { #{call}; return 0; }
  681. SRC
  682. end
  683. # You should use +have_var+ rather than +try_var+.
  684. def try_var(var, headers = nil, opt = "", &b)
  685. headers = cpp_include(headers)
  686. try_compile(<<"SRC", opt, &b)
  687. #{headers}
  688. /*top*/
  689. extern int t(void);
  690. #{MAIN_DOES_NOTHING 't'}
  691. int t(void) { const volatile void *volatile p; p = &(&#{var})[0]; return 0; }
  692. SRC
  693. end
  694. # Returns whether or not the +src+ can be preprocessed with the C
  695. # preprocessor and matches with +pat+.
  696. #
  697. # If a block given, it is called with the source before compilation. You can
  698. # modify the source in the block.
  699. #
  700. # [+pat+] a Regexp or a String
  701. # [+src+] a String which contains a C source
  702. # [+opt+] a String which contains preprocessor options
  703. #
  704. # NOTE: When pat is a Regexp the matching will be checked in process,
  705. # otherwise egrep(1) will be invoked to check it.
  706. def egrep_cpp(pat, src, opt = "", &b)
  707. src = create_tmpsrc(src, &b)
  708. xpopen(cpp_command('', opt)) do |f|
  709. if Regexp === pat
  710. puts(" ruby -ne 'print if #{pat.inspect}'")
  711. f.grep(pat) {|l|
  712. puts "#{f.lineno}: #{l}"
  713. return true
  714. }
  715. false
  716. else
  717. puts(" egrep '#{pat}'")
  718. begin
  719. stdin = $stdin.dup
  720. $stdin.reopen(f)
  721. system("egrep", pat)
  722. ensure
  723. $stdin.reopen(stdin)
  724. end
  725. end
  726. end
  727. ensure
  728. MakeMakefile.rm_f "conftest*"
  729. log_src(src)
  730. end
  731. # This is used internally by the have_macro? method.
  732. def macro_defined?(macro, src, opt = "", &b)
  733. src = src.sub(/[^\n]\z/, "\\&\n")
  734. try_compile(src + <<"SRC", opt, &b)
  735. /*top*/
  736. #ifndef #{macro}
  737. # error
  738. |:/ === #{macro} undefined === /:|
  739. #endif
  740. SRC
  741. end
  742. # Returns whether or not:
  743. # * the +src+ can be compiled as a C source,
  744. # * the result object can be linked with its depending libraries
  745. # successfully,
  746. # * the linked file can be invoked as an executable
  747. # * and the executable exits successfully
  748. #
  749. # +opt+ is passed to the linker as options. Note that +$CFLAGS+ and
  750. # +$LDFLAGS+ are also passed to the linker.
  751. #
  752. # If a block given, it is called with the source before compilation. You can
  753. # modify the source in the block.
  754. #
  755. # [+src+] a String which contains a C source
  756. # [+opt+] a String which contains linker options
  757. #
  758. # Returns true when the executable exits successfully, false when it fails,
  759. # or nil when preprocessing, compilation or link fails.
  760. def try_run(src, opt = "", &b)
  761. raise "cannot run test program while cross compiling" if CROSS_COMPILING
  762. if try_link0(src, opt, &b)
  763. xsystem("./conftest")
  764. else
  765. nil
  766. end
  767. ensure
  768. MakeMakefile.rm_f "conftest*"
  769. end
  770. def install_files(mfile, ifiles, map = nil, srcprefix = nil)
  771. ifiles or return
  772. ifiles.empty? and return
  773. srcprefix ||= "$(srcdir)/#{srcprefix}".chomp('/')
  774. RbConfig::expand(srcdir = srcprefix.dup)
  775. dirs = []
  776. path = Hash.new {|h, i| h[i] = dirs.push([i])[-1]}
  777. ifiles.each do |files, dir, prefix|
  778. dir = map_dir(dir, map)
  779. prefix &&= %r|\A#{Regexp.quote(prefix)}/?|
  780. if /\A\.\// =~ files
  781. # install files which are in current working directory.
  782. files = files[2..-1]
  783. len = nil
  784. else
  785. # install files which are under the $(srcdir).
  786. files = File.join(srcdir, files)
  787. len = srcdir.size
  788. end
  789. f = nil
  790. Dir.glob(files) do |fx|
  791. f = fx
  792. f[0..len] = "" if len
  793. case File.basename(f)
  794. when *$NONINSTALLFILES
  795. next
  796. end
  797. d = File.dirname(f)
  798. d.sub!(prefix, "") if prefix
  799. d = (d.empty? || d == ".") ? dir : File.join(dir, d)
  800. f = File.join(srcprefix, f) if len
  801. path[d] << f
  802. end
  803. unless len or f
  804. d = File.dirname(files)
  805. d.sub!(prefix, "") if prefix
  806. d = (d.empty? || d == ".") ? dir : File.join(dir, d)
  807. path[d] << files
  808. end
  809. end
  810. dirs
  811. end
  812. def install_rb(mfile, dest, srcdir = nil)
  813. install_files(mfile, [["lib/**/*.rb", dest, "lib"]], nil, srcdir)
  814. end
  815. def append_library(libs, lib) # :no-doc:
  816. format(LIBARG, lib) + " " + libs
  817. end
  818. def message(*s)
  819. unless Logging.quiet and not $VERBOSE
  820. printf(*s)
  821. $stdout.flush
  822. end
  823. end
  824. # This emits a string to stdout that allows users to see the results of the
  825. # various have* and find* methods as they are tested.
  826. #
  827. # Internal use only.
  828. #
  829. def checking_for(m, fmt = nil)
  830. f = caller[0][/in `([^<].*)'$/, 1] and f << ": " #` for vim #'
  831. m = "checking #{/\Acheck/ =~ f ? '' : 'for '}#{m}... "
  832. message "%s", m
  833. a = r = nil
  834. Logging::postpone do
  835. r = yield
  836. a = (fmt ? "#{fmt % r}" : r ? "yes" : "no") << "\n"
  837. "#{f}#{m}-------------------- #{a}\n"
  838. end
  839. message(a)
  840. Logging::message "--------------------\n\n"
  841. r
  842. end
  843. def checking_message(target, place = nil, opt = nil)
  844. [["in", place], ["with", opt]].inject("#{target}") do |msg, (pre, noun)|
  845. if noun
  846. [[:to_str], [:join, ","], [:to_s]].each do |meth, *args|
  847. if noun.respond_to?(meth)
  848. break noun = noun.send(meth, *args)
  849. end
  850. end
  851. msg << " #{pre} #{noun}" unless noun.empty?
  852. end
  853. msg
  854. end
  855. end
  856. # :startdoc:
  857. # Returns whether or not +macro+ is defined either in the common header
  858. # files or within any +headers+ you provide.
  859. #
  860. # Any options you pass to +opt+ are passed along to the compiler.
  861. #
  862. def have_macro(macro, headers = nil, opt = "", &b)
  863. checking_for checking_message(macro, headers, opt) do
  864. macro_defined?(macro, cpp_include(headers), opt, &b)
  865. end
  866. end
  867. # Returns whether or not the given entry point +func+ can be found within
  868. # +lib+. If +func+ is +nil+, the <code>main()</code> entry point is used by
  869. # default. If found, it adds the library to list of libraries to be used
  870. # when linking your extension.
  871. #
  872. # If +headers+ are provided, it will include those header files as the
  873. # header files it looks in when searching for +func+.
  874. #
  875. # The real name of the library to be linked can be altered by
  876. # <code>--with-FOOlib</code> configuration option.
  877. #
  878. def have_library(lib, func = nil, headers = nil, opt = "", &b)
  879. func = "main" if !func or func.empty?
  880. lib = with_config(lib+'lib', lib)
  881. checking_for checking_message(func.funcall_style, LIBARG%lib, opt) do
  882. if COMMON_LIBS.include?(lib)
  883. true
  884. else
  885. libs = append_library($libs, lib)
  886. if try_func(func, libs, headers, opt, &b)
  887. $libs = libs
  888. true
  889. else
  890. false
  891. end
  892. end
  893. end
  894. end
  895. # Returns whether or not the entry point +func+ can be found within the
  896. # library +lib+ in one of the +paths+ specified, where +paths+ is an array
  897. # of strings. If +func+ is +nil+ , then the <code>main()</code> function is
  898. # used as the entry point.
  899. #
  900. # If +lib+ is found, then the path it was found on is added to the list of
  901. # library paths searched and linked against.
  902. #
  903. def find_library(lib, func, *paths, &b)
  904. func = "main" if !func or func.empty?
  905. lib = with_config(lib+'lib', lib)
  906. paths = paths.collect {|path| path.split(File::PATH_SEPARATOR)}.flatten
  907. checking_for checking_message(func.funcall_style, LIBARG%lib) do
  908. libpath = $LIBPATH
  909. libs = append_library($libs, lib)
  910. begin
  911. until r = try_func(func, libs, &b) or paths.empty?
  912. $LIBPATH = libpath | [paths.shift]
  913. end
  914. if r
  915. $libs = libs
  916. libpath = nil
  917. end
  918. ensure
  919. $LIBPATH = libpath if libpath
  920. end
  921. r
  922. end
  923. end
  924. # Returns whether or not the function +func+ can be found in the common
  925. # header files, or within any +headers+ that you provide. If found, a macro
  926. # is passed as a preprocessor constant to the compiler using the function
  927. # name, in uppercase, prepended with +HAVE_+.
  928. #
  929. # To check functions in an additional library, you need to check that
  930. # library first using <code>have_library()</code>. The +func+ shall be
  931. # either mere function name or function name with arguments.
  932. #
  933. # For example, if <code>have_func('foo')</code> returned +true+, then the
  934. # +HAVE_FOO+ preprocessor macro would be passed to the compiler.
  935. #
  936. def have_func(func, headers = nil, opt = "", &b)
  937. checking_for checking_message(func.funcall_style, headers, opt) do
  938. if try_func(func, $libs, headers, opt, &b)
  939. $defs << "-DHAVE_#{func.sans_arguments.tr_cpp}"
  940. true
  941. else
  942. false
  943. end
  944. end
  945. end
  946. # Returns whether or not the variable +var+ can be found in the common
  947. # header files, or within any +headers+ that you provide. If found, a macro
  948. # is passed as a preprocessor constant to the compiler using the variable
  949. # name, in uppercase, prepended with +HAVE_+.
  950. #
  951. # To check variables in an additional library, you need to check that
  952. # library first using <code>have_library()</code>.
  953. #
  954. # For example, if <code>have_var('foo')</code> returned true, then the
  955. # +HAVE_FOO+ preprocessor macro would be passed to the compiler.
  956. #
  957. def have_var(var, headers = nil, opt = "", &b)
  958. checking_for checking_message(var, headers, opt) do
  959. if try_var(var, headers, opt, &b)
  960. $defs.push(format("-DHAVE_%s", var.tr_cpp))
  961. true
  962. else
  963. false
  964. end
  965. end
  966. end
  967. # Returns whether or not the given +header+ file can be found on your system.
  968. # If found, a macro is passed as a preprocessor constant to the compiler
  969. # using the header file name, in uppercase, prepended with +HAVE_+.
  970. #
  971. # For example, if <code>have_header('foo.h')</code> returned true, then the
  972. # +HAVE_FOO_H+ preprocessor macro would be passed to the compiler.
  973. #
  974. def have_header(header, preheaders = nil, opt = "", &b)
  975. checking_for header do
  976. if try_header(cpp_include(preheaders)+cpp_include(header), opt, &b)
  977. $defs.push(format("-DHAVE_%s", header.tr_cpp))
  978. true
  979. else
  980. false
  981. end
  982. end
  983. end
  984. # Returns whether or not the given +framework+ can be found on your system.
  985. # If found, a macro is passed as a preprocessor constant to the compiler
  986. # using the framework name, in uppercase, prepended with +HAVE_FRAMEWORK_+.
  987. #
  988. # For example, if <code>have_framework('Ruby')</code> returned true, then
  989. # the +HAVE_FRAMEWORK_RUBY+ preprocessor macro would be passed to the
  990. # compiler.
  991. #
  992. def have_framework(fw, &b)
  993. checking_for fw do
  994. src = cpp_include("#{fw}/#{fw}.h") << "\n" "int main(void){return 0;}"
  995. opt = " -framework #{fw}"
  996. if try_link(src, "-ObjC#{opt}", &b)
  997. $defs.push(format("-DHAVE_FRAMEWORK_%s", fw.tr_cpp))
  998. # TODO: non-worse way than this hack, to get rid of separating
  999. # option and its argument.
  1000. $LDFLAGS << " -ObjC" unless /(\A|\s)-ObjC(\s|\z)/ =~ $LDFLAGS
  1001. $LDFLAGS << opt
  1002. true
  1003. else
  1004. false
  1005. end
  1006. end
  1007. end
  1008. # Instructs mkmf to search for the given +header+ in any of the +paths+
  1009. # provided, and returns whether or not it was found in those paths.
  1010. #
  1011. # If the header is found then the path it was found on is added to the list
  1012. # of included directories that are sent to the compiler (via the
  1013. # <code>-I</code> switch).
  1014. #
  1015. def find_header(header, *paths)
  1016. message = checking_message(header, paths)
  1017. header = cpp_include(header)
  1018. checking_for message do
  1019. if try_header(header)
  1020. true
  1021. else
  1022. found = false
  1023. paths.each do |dir|
  1024. opt = "-I#{dir}".quote
  1025. if try_header(header, opt)
  1026. $INCFLAGS << " " << opt
  1027. found = true
  1028. break
  1029. end
  1030. end
  1031. found
  1032. end
  1033. end
  1034. end
  1035. # Returns whether or not the struct of type +type+ contains +member+. If
  1036. # it does not, or the struct type can't be found, then false is returned.
  1037. # You may optionally specify additional +headers+ in which to look for the
  1038. # struct (in addition to the common header files).
  1039. #
  1040. # If found, a macro is passed as a preprocessor constant to the compiler
  1041. # using the type name and the member name, in uppercase, prepended with
  1042. # +HAVE_+.
  1043. #
  1044. # For example, if <code>have_struct_member('struct foo', 'bar')</code>
  1045. # returned true, then the +HAVE_STRUCT_FOO_BAR+ preprocessor macro would be
  1046. # passed to the compiler.
  1047. #
  1048. # +HAVE_ST_BAR+ is also defined for backward compatibility.
  1049. #
  1050. def have_struct_member(type, member, headers = nil, opt = "", &b)
  1051. checking_for checking_message("#{type}.#{member}", headers) do
  1052. if try_compile(<<"SRC", opt, &b)
  1053. #{cpp_include(headers)}
  1054. /*top*/
  1055. int s = (char *)&((#{type}*)0)->#{member} - (char *)0;
  1056. #{MAIN_DOES_NOTHING}
  1057. SRC
  1058. $defs.push(format("-DHAVE_%s_%s", type.tr_cpp, member.tr_cpp))
  1059. $defs.push(format("-DHAVE_ST_%s", member.tr_cpp)) # backward compatibility
  1060. true
  1061. else
  1062. false
  1063. end
  1064. end
  1065. end
  1066. # Returns whether or not the static type +type+ is defined.
  1067. #
  1068. # See also +have_type+
  1069. #
  1070. def try_type(type, headers = nil, opt = "", &b)
  1071. if try_compile(<<"SRC", opt, &b)
  1072. #{cpp_include(headers)}
  1073. /*top*/
  1074. typedef #{type} conftest_type;
  1075. int conftestval[sizeof(conftest_type)?1:-1];
  1076. SRC
  1077. $defs.push(format("-DHAVE_TYPE_%s", type.tr_cpp))
  1078. true
  1079. else
  1080. false
  1081. end
  1082. end
  1083. # Returns whether or not the static type +type+ is defined. You may
  1084. # optionally pass additional +headers+ to check against in addition to the
  1085. # common header files.
  1086. #
  1087. # You may also pass additional flags to +opt+ which are then passed along to
  1088. # the compiler.
  1089. #
  1090. # If found, a macro is passed as a preprocessor constant to the compiler
  1091. # using the type name, in uppercase, prepended with +HAVE_TYPE_+.
  1092. #
  1093. # For example, if <code>have_type('foo')</code> returned true, then the
  1094. # +HAVE_TYPE_FOO+ preprocessor macro would be passed to the compiler.
  1095. #
  1096. def have_type(type, headers = nil, opt = "", &b)
  1097. checking_for checking_message(type, headers, opt) do
  1098. try_type(type, headers, opt, &b)
  1099. end
  1100. end
  1101. # Returns where the static type +type+ is defined.
  1102. #
  1103. # You may also pass additional flags to +opt+ which are then passed along to
  1104. # the compiler.
  1105. #
  1106. # See also +have_type+.
  1107. #
  1108. def find_type(type, opt, *headers, &b)
  1109. opt ||= ""
  1110. fmt = "not found"
  1111. def fmt.%(x)
  1112. x ? x.respond_to?(:join) ? x.join(",") : x : self
  1113. end
  1114. checking_for checking_message(type, nil, opt), fmt do
  1115. headers.find do |h|
  1116. try_type(type, h, opt, &b)
  1117. end
  1118. end
  1119. end
  1120. # Returns whether or not the constant +const+ is defined.
  1121. #
  1122. # See also +have_const+
  1123. #
  1124. def try_const(const, headers = nil, opt = "", &b)
  1125. const, type = *const
  1126. if try_compile(<<"SRC", opt, &b)
  1127. #{cpp_include(headers)}
  1128. /*top*/
  1129. typedef #{type || 'int'} conftest_type;
  1130. conftest_type conftestval = #{type ? '' : '(int)'}#{const};
  1131. SRC
  1132. $defs.push(format("-DHAVE_CONST_%s", const.tr_cpp))
  1133. true
  1134. else
  1135. false
  1136. end
  1137. end
  1138. # Returns whether or not the constant +const+ is defined. You may
  1139. # optionally pass the +type+ of +const+ as <code>[const, type]</code>,
  1140. # such as:
  1141. #
  1142. # have_const(%w[PTHREAD_MUTEX_INITIALIZER pthread_mutex_t], "pthread.h")
  1143. #
  1144. # You may also pass additional +headers+ to check against in addition to the
  1145. # common header files, and additional flags to +opt+ which are then passed
  1146. # along to the compiler.
  1147. #
  1148. # If found, a macro is passed as a preprocessor constant to the compiler
  1149. # using the type name, in uppercase, prepended with +HAVE_CONST_+.
  1150. #
  1151. # For example, if <code>have_const('foo')</code> returned true, then the
  1152. # +HAVE_CONST_FOO+ preprocessor macro would be passed to the compiler.
  1153. #
  1154. def have_const(const, headers = nil, opt = "", &b)
  1155. checking_for checking_message([*const].compact.join(' '), headers, opt) do
  1156. try_const(const, headers, opt, &b)
  1157. end
  1158. end
  1159. # :stopdoc:
  1160. STRING_OR_FAILED_FORMAT = "%s"
  1161. def STRING_OR_FAILED_FORMAT.%(x) # :nodoc:
  1162. x ? super : "failed"
  1163. end
  1164. def typedef_expr(type, headers)
  1165. typename, member = type.split('.', 2)
  1166. prelude = cpp_include(headers).split(/$/)
  1167. prelude << "typedef #{typename} rbcv_typedef_;\n"
  1168. return "rbcv_typedef_", member, prelude
  1169. end
  1170. def try_signedness(type, member, headers = nil, opts = nil)
  1171. raise ArgumentError, "don't know how to tell signedness of members" if member
  1172. if try_static_assert("(#{type})-1 < 0", headers, opts)
  1173. return -1
  1174. elsif try_static_assert("(#{type})-1 > 0", headers, opts)
  1175. return +1
  1176. end
  1177. end
  1178. # :startdoc:
  1179. # Returns the size of the given +type+. You may optionally specify
  1180. # additional +headers+ to search in for the +type+.
  1181. #
  1182. # If found, a macro is passed as a preprocessor constant to the compiler
  1183. # using the type name, in uppercase, prepended with +SIZEOF_+, followed by
  1184. # the type name, followed by <code>=X</code> where "X" is the actual size.
  1185. #
  1186. # For example, if <code>check_sizeof('mystruct')</code> returned 12, then
  1187. # the <code>SIZEOF_MYSTRUCT=12</code> preprocessor macro would be passed to
  1188. # the compiler.
  1189. #
  1190. def check_sizeof(type, headers = nil, opts = "", &b)
  1191. typedef, member, prelude = typedef_expr(type, headers)
  1192. prelude << "static #{typedef} *rbcv_ptr_;\n"
  1193. prelude = [prelude]
  1194. expr = "sizeof((*rbcv_ptr_)#{"." << member if member})"
  1195. fmt = STRING_OR_FAILED_FORMAT
  1196. checking_for checking_message("size of #{type}", headers), fmt do
  1197. if size = try_constant(expr, prelude, opts, &b)
  1198. $defs.push(format("-DSIZEOF_%s=%s", type.tr_cpp, size))
  1199. size
  1200. end
  1201. end
  1202. end
  1203. # Returns the signedness of the given +type+. You may optionally specify
  1204. # additional +headers+ to search in for the +type+.
  1205. #
  1206. # If the +type+ is found and is a numeric type, a macro is passed as a
  1207. # preprocessor constant to the compiler using the +type+ name, in uppercase,
  1208. # prepended with +SIGNEDNESS_OF_+, followed by the +type+ name, followed by
  1209. # <code>=X</code> where "X" is positive integer if the +type+ is unsigned
  1210. # and a negative integer if the +type+ is signed.
  1211. #
  1212. # For example, if +size_t+ is defined as unsigned, then
  1213. # <code>check_signedness('size_t')</code> would return +1 and the
  1214. # <code>SIGNEDNESS_OF_SIZE_T=+1</code> preprocessor macro would be passed to
  1215. # the compiler. The <code>SIGNEDNESS_OF_INT=-1</code> macro would be set
  1216. # for <code>check_signedness('int')</code>
  1217. #
  1218. def check_signedness(type, headers = nil, opts = nil, &b)
  1219. typedef, member, prelude = typedef_expr(type, headers)
  1220. signed = nil
  1221. checking_for("signedness of #{type}", STRING_OR_FAILED_FORMAT) do
  1222. signed = try_signedness(typedef, member, [prelude], opts, &b) or next nil
  1223. $defs.push("-DSIGNEDNESS_OF_%s=%+d" % [type.tr_cpp, signed])
  1224. signed < 0 ? "signed" : "unsigned"
  1225. end
  1226. signed
  1227. end
  1228. # Returns the convertible integer type of the given +type+. You may
  1229. # optionally specify additional +headers+ to search in for the +type+.
  1230. # _convertible_ means actually the same type, or typedef'd from the same
  1231. # type.
  1232. #
  1233. # If the +type+ is a integer type and the _convertible_ type is found,
  1234. # the following macros are passed as preprocessor constants to the compiler
  1235. # using the +type+ name, in uppercase.
  1236. #
  1237. # * +TYPEOF_+, followed by the +type+ name, followed by <code>=X</code>
  1238. # where "X" is the found _convertible_ type name.
  1239. # * +TYP2NUM+ and +NUM2TYP+,
  1240. # where +TYP+ is the +type+ name in uppercase with replacing an +_t+
  1241. # suffix with "T", followed by <code>=X</code> where "X" is the macro name
  1242. # to convert +type+ to an Integer object, and vice versa.
  1243. #
  1244. # For example, if +foobar_t+ is defined as unsigned long, then
  1245. # <code>convertible_int("foobar_t")</code> would return "unsigned long", and
  1246. # define these macros:
  1247. #
  1248. # #define TYPEOF_FOOBAR_T unsigned long
  1249. # #define FOOBART2NUM ULONG2NUM
  1250. # #define NUM2FOOBART NUM2ULONG
  1251. #
  1252. def convertible_int(type, headers = nil, opts = nil, &b)
  1253. type, macname = *type
  1254. checking_for("convertible type of #{type}", STRING_OR_FAILED_FORMAT) do
  1255. if UNIVERSAL_INTS.include?(type)
  1256. type
  1257. else
  1258. typedef, member, prelude = typedef_expr(type, headers, &b)
  1259. if member
  1260. prelude << "static rbcv_typedef_ rbcv_var;"
  1261. compat = UNIVERSAL_INTS.find {|t|
  1262. try_static_assert("sizeof(rbcv_var.#{member}) == sizeof(#{t})", [prelude], opts, &b)
  1263. }
  1264. else
  1265. next unless signed = try_signedness(typedef, member, [prelude])
  1266. u = "unsigned " if signed > 0
  1267. prelude << "extern rbcv_typedef_ foo();"
  1268. compat = UNIVERSAL_INTS.find {|t|
  1269. try_compile([prelude, "extern #{u}#{t} foo();"].join("\n"), opts, :werror=>true, &b)
  1270. }
  1271. end
  1272. if compat
  1273. macname ||= type.sub(/_(?=t\z)/, '').tr_cpp
  1274. conv = (compat == "long long" ? "LL" : compat.upcase)
  1275. compat = "#{u}#{compat}"
  1276. typename = type.tr_cpp
  1277. $defs.push(format("-DSIZEOF_%s=SIZEOF_%s", typename, compat.tr_cpp))
  1278. $defs.push(format("-DTYPEOF_%s=%s", typename, compat.quote))
  1279. $defs.push(format("-DPRI_%s_PREFIX=PRI_%s_PREFIX", macname, conv))
  1280. conv = (u ? "U" : "") + conv
  1281. $defs.push(format("-D%s2NUM=%s2NUM", macname, conv))
  1282. $defs.push(format("-DNUM2%s=NUM2%s", macname, conv))
  1283. compat
  1284. end
  1285. end
  1286. end
  1287. end
  1288. # :stopdoc:
  1289. # Used internally by the what_type? method to determine if +type+ is a scalar
  1290. # pointer.
  1291. def scalar_ptr_type?(type, member = nil, headers = nil, &b)
  1292. try_compile(<<"SRC", &b) # pointer
  1293. #{cpp_include(headers)}
  1294. /*top*/
  1295. volatile #{type} conftestval;
  1296. extern int t(void);
  1297. #{MAIN_DOES_NOTHING 't'}
  1298. int t(void) {return (int)(1-*(conftestval#{member ? ".#{member}" : ""}));}
  1299. SRC
  1300. end
  1301. # Used internally by the what_type? method to determine if +type+ is a scalar
  1302. # pointer.
  1303. def scalar_type?(type, member = nil, headers = nil, &b)
  1304. try_compile(<<"SRC", &b) # pointer
  1305. #{cpp_include(headers)}
  1306. /*top*/
  1307. volatile #{type} conftestval;
  1308. extern int t(void);
  1309. #{MAIN_DOES_NOTHING 't'}
  1310. int t(void) {return (int)(1-(conftestval#{member ? ".#{member}" : ""}));}
  1311. SRC
  1312. end
  1313. # Used internally by the what_type? method to check if the _typeof_ GCC
  1314. # extension is available.
  1315. def have_typeof?
  1316. return $typeof if defined?($typeof)
  1317. $typeof = %w[__typeof__ typeof].find do |t|
  1318. try_compile(<<SRC)
  1319. int rbcv_foo;
  1320. #{t}(rbcv_foo) rbcv_bar;
  1321. SRC
  1322. end
  1323. end
  1324. def what_type?(type, member = nil, headers = nil, &b)
  1325. m = "#{type}"
  1326. var = val = "*rbcv_var_"
  1327. func = "rbcv_func_(void)"
  1328. if member
  1329. m << "." << member
  1330. else
  1331. type, member = type.split('.', 2)
  1332. end
  1333. if member
  1334. val = "(#{var}).#{member}"
  1335. end
  1336. prelude = [cpp_include(headers).split(/^/)]
  1337. prelude << ["typedef #{type} rbcv_typedef_;\n",
  1338. "extern rbcv_typedef_ *#{func};\n",
  1339. "static rbcv_typedef_ #{var};\n",
  1340. ]
  1341. type = "rbcv_typedef_"
  1342. fmt = member && !(typeof = have_typeof?) ? "seems %s" : "%s"
  1343. if typeof
  1344. var = "*rbcv_member_"
  1345. func = "rbcv_mem_func_(void)"
  1346. member = nil
  1347. type = "rbcv_mem_typedef_"
  1348. prelude[-1] << "typedef #{typeof}(#{val}) #{type};\n"
  1349. prelude[-1] << "extern #{type} *#{func};\n"
  1350. prelude[-1] << "static #{type} #{var};\n"
  1351. val = var
  1352. end
  1353. def fmt.%(x)
  1354. x ? super : "unknown"
  1355. end
  1356. checking_for checking_message(m, headers), fmt do
  1357. if scalar_ptr_type?(type, member, prelude, &b)
  1358. if try_static_assert("sizeof(*#{var}) == 1", prelude)
  1359. return "string"
  1360. end
  1361. ptr = "*"
  1362. elsif scalar_type?(type, member, prelude, &b)
  1363. unless member and !typeof or try_static_assert("(#{type})-1 < 0", prelude)
  1364. unsigned = "unsigned"
  1365. end
  1366. ptr = ""
  1367. else
  1368. next
  1369. end
  1370. type = UNIVERSAL_INTS.find do |t|
  1371. pre = prelude
  1372. unless member
  1373. pre += [["static #{unsigned} #{t} #{ptr}#{var};\n",
  1374. "extern #{unsigned} #{t} #{ptr}*#{func};\n"]]
  1375. end
  1376. try_static_assert("sizeof(#{ptr}#{val}) == sizeof(#{unsigned} #{t})", pre)
  1377. end
  1378. type or next
  1379. [unsigned, type, ptr].join(" ").strip
  1380. end
  1381. end
  1382. # This method is used internally by the find_executable method.
  1383. #
  1384. # Internal use only.
  1385. #
  1386. def find_executable0(bin, path = nil)
  1387. executable_file = proc do |name|
  1388. begin
  1389. stat = File.stat(name)
  1390. rescue SystemCallError
  1391. else
  1392. next name if stat.file? and stat.executable?
  1393. end
  1394. end
  1395. exts = config_string('EXECUTABLE_EXTS') {|s| s.split} || config_string('EXEEXT') {|s| [s]}
  1396. if File.expand_path(bin) == bin
  1397. return bin if executable_file.call(bin)
  1398. if exts
  1399. exts.each {|ext| executable_file.call(file = bin + ext) and return file}
  1400. end
  1401. return nil
  1402. end
  1403. if path ||= ENV['PATH']
  1404. path = path.split(File::PATH_SEPARATOR)
  1405. else
  1406. path = %w[/usr/local/bin /usr/ucb /usr/bin /bin]
  1407. end
  1408. file = nil
  1409. path.each do |dir|
  1410. return file if executable_file.call(file = File.join(dir, bin))
  1411. if exts
  1412. exts.each {|ext| executable_file.call(ext = file + ext) and return ext}
  1413. end
  1414. end
  1415. nil
  1416. end
  1417. # :startdoc:
  1418. # Searches for the executable +bin+ on +path+. The default path is your
  1419. # +PATH+ environment variable. If that isn't defined, it will resort to
  1420. # searching /usr/local/bin, /usr/ucb, /usr/bin and /bin.
  1421. #
  1422. # If found, it will return the full path, including the executable name, of
  1423. # where it was found.
  1424. #
  1425. # Note that this method does not actually affect the generated Makefile.
  1426. #
  1427. def find_executable(bin, path = nil)
  1428. checking_for checking_message(bin, path) do
  1429. find_executable0(bin, path)
  1430. end
  1431. end
  1432. # :stopdoc:
  1433. def arg_config(config, default=nil, &block)
  1434. $arg_config << [config, default]
  1435. defaults = []
  1436. if default
  1437. defaults << default
  1438. elsif !block
  1439. defaults << nil
  1440. end
  1441. $configure_args.fetch(config.tr('_', '-'), *defaults, &block)
  1442. end
  1443. # :startdoc:
  1444. # Tests for the presence of a <tt>--with-</tt>_config_ or
  1445. # <tt>--without-</tt>_config_ option. Returns +true+ if the with option is
  1446. # given, +false+ if the without option is given, and the default value
  1447. # otherwise.
  1448. #
  1449. # This can be useful for adding custom definitions, such as debug
  1450. # information.
  1451. #
  1452. # Example:
  1453. #
  1454. # if with_config("debug")
  1455. # $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG"
  1456. # end
  1457. #
  1458. def with_config(config, default=nil)
  1459. config = config.sub(/^--with[-_]/, '')
  1460. val = arg_config("--with-"+config) do
  1461. if arg_config("--without-"+config)
  1462. false
  1463. elsif block_given?
  1464. yield(config, default)
  1465. else
  1466. break default
  1467. end
  1468. end
  1469. case val
  1470. when "yes"
  1471. true
  1472. when "no"
  1473. false
  1474. else
  1475. val
  1476. end
  1477. end
  1478. # Tests for the presence of an <tt>--enable-</tt>_config_ or
  1479. # <tt>--disable-</tt>_config_ option. Returns +true+ if the enable option is
  1480. # given, +false+ if the disable option is given, and the default value
  1481. # otherwise.
  1482. #
  1483. # This can be useful for adding custom definitions, such as debug
  1484. # information.
  1485. #
  1486. # Example:
  1487. #
  1488. # if enable_config("debug")
  1489. # $defs.push("-DOSSL_DEBUG") unless $defs.include? "-DOSSL_DEBUG"
  1490. # end
  1491. #
  1492. def enable_config(config, default=nil)
  1493. if arg_config("--enable-"+config)
  1494. true
  1495. elsif arg_config("--disable-"+config)
  1496. false
  1497. elsif block_given?
  1498. yield(config, default)
  1499. else
  1500. return default
  1501. end
  1502. end
  1503. # Generates a header file consisting of the various macro definitions
  1504. # generated by other methods such as have_func and have_header. These are
  1505. # then wrapped in a custom <code>#ifndef</code> based on the +header+ file
  1506. # name, which defaults to "extconf.h".
  1507. #
  1508. # For example:
  1509. #
  1510. # # extconf.rb
  1511. # require 'mkmf'
  1512. # have_func('realpath')
  1513. # have_header('sys/utime.h')
  1514. # create_header
  1515. # create_makefile('foo')
  1516. #
  1517. # The above script would generate the following extconf.h file:
  1518. #
  1519. # #ifndef EXTCONF_H
  1520. # #define EXTCONF_H
  1521. # #define HAVE_REALPATH 1
  1522. # #define HAVE_SYS_UTIME_H 1
  1523. # #endif
  1524. #
  1525. # Given that the create_header method generates a file based on definitions
  1526. # set earlier in your extconf.rb file, you will probably want to make this
  1527. # one of the last methods you call in your script.
  1528. #
  1529. def create_header(header = "extconf.h")
  1530. message "creating %s\n", header
  1531. sym = header.tr_cpp
  1532. hdr = ["#ifndef #{sym}\n#define #{sym}\n"]
  1533. for line in $defs
  1534. case line
  1535. when /^-D([^=]+)(?:=(.*))?/
  1536. hdr << "#define #$1 #{$2 ? Shellwords.shellwords($2)[0].gsub(/(?=\t+)/, "\\\n") : 1}\n"
  1537. when /^-U(.*)/
  1538. hdr << "#undef #$1\n"
  1539. end
  1540. end
  1541. hdr << "#endif\n"
  1542. hdr = hdr.join("")
  1543. log_src(hdr, "#{header} is")
  1544. unless (IO.read(header) == hdr rescue false)
  1545. open(header, "wb") do |hfile|
  1546. hfile.write(hdr)
  1547. end
  1548. end
  1549. $extconf_h = header
  1550. end
  1551. # Sets a +target+ name that the user can then use to configure various
  1552. # "with" options with on the command line by using that name. For example,
  1553. # if the target is set to "foo", then the user could use the
  1554. # <code>--with-foo-dir</code> command line option.
  1555. #
  1556. # You may pass along additional "include" or "lib" defaults via the
  1557. # +idefault+ and +ldefault+ parameters, respectively.
  1558. #
  1559. # Note that dir_config only adds to the list of places to search for
  1560. # libraries and include files. It does not link the libraries into your
  1561. # application.
  1562. #
  1563. def dir_config(target, idefault=nil, ldefault=nil)
  1564. if dir = with_config(target + "-dir", (idefault unless ldefault))
  1565. defaults = Array === dir ? dir : dir.split(File::PATH_SEPARATOR)
  1566. idefault = ldefault = nil
  1567. end
  1568. idir = with_config(target + "-include", idefault)
  1569. $arg_config.last[1] ||= "${#{target}-dir}/include"
  1570. ldir = with_config(target + "-lib", ldefault)
  1571. $arg_config.last[1] ||= "${#{target}-dir}/#{_libdir_basename}"
  1572. idirs = idir ? Array === idir ? idir.dup : idir.split(File::PATH_SEPARATOR) : []
  1573. if defaults
  1574. idirs.concat(defaults.collect {|d| d + "/include"})
  1575. idir = ([idir] + idirs).compact.join(File::PATH_SEPARATOR)
  1576. end
  1577. unless idirs.empty?
  1578. idirs.collect! {|d| "-I" + d}
  1579. idirs -= Shellwords.shellwords($CPPFLAGS)
  1580. unless idirs.empty?
  1581. $CPPFLAGS = (idirs.quote << $CPPFLAGS).join(" ")
  1582. end
  1583. end
  1584. ldirs = ldir ? Array === ldir ? ldir.dup : ldir.split(File::PATH_SEPARATOR) : []
  1585. if defaults
  1586. ldirs.concat(defaults.collect {|d| "#{d}/#{_libdir_basename}"})
  1587. ldir = ([ldir] + ldirs).compact.join(File::PATH_SEPARATOR)
  1588. end
  1589. $LIBPATH = ldirs | $LIBPATH
  1590. [idir, ldir]
  1591. end
  1592. # Returns compile/link information about an installed library in a
  1593. # tuple of <code>[cflags, ldflags, libs]</code>, by using the
  1594. # command found first in the following commands:
  1595. #
  1596. # 1. If <code>--with-{pkg}-config={command}</code> is given via
  1597. # command line option: <code>{command} {option}</code>
  1598. #
  1599. # 2. <code>{pkg}-config {option}</code>
  1600. #
  1601. # 3. <code>pkg-config {option} {pkg}</code>
  1602. #
  1603. # Where {option} is, for instance, <code>--cflags</code>.
  1604. #
  1605. # The values obtained are appended to +$CFLAGS+, +$LDFLAGS+ and
  1606. # +$libs+.
  1607. #
  1608. # If an <code>option</code> argument is given, the config command is
  1609. # invoked with the option and a stripped output string is returned
  1610. # without modifying any of the global values mentioned above.
  1611. def pkg_config(pkg, option=nil)
  1612. if pkgconfig = with_config("#{pkg}-config") and find_executable0(pkgconfig)
  1613. # iff package specific config command is given
  1614. get = proc {|opt| `#{pkgconfig} --#{opt}`.strip}
  1615. elsif ($PKGCONFIG ||=
  1616. (pkgconfig = with_config("pkg-config", ("pkg-config" unless CROSS_COMPILING))) &&
  1617. find_executable0(pkgconfig) && pkgconfig) and
  1618. system("#{$PKGCONFIG} --exists #{pkg}")
  1619. # default to pkg-config command
  1620. get = proc {|opt| `#{$PKGCONFIG} --#{opt} #{pkg}`.strip}
  1621. elsif find_executable0(pkgconfig = "#{pkg}-config")
  1622. # default to package specific config command, as a last resort.
  1623. get = proc {|opt| `#{pkgconfig} --#{opt}`.strip}
  1624. end
  1625. if get and option
  1626. get[option]
  1627. elsif get and try_ldflags(ldflags = get['libs'])
  1628. cflags = get['cflags']
  1629. libs = get['libs-only-l']
  1630. ldflags = (Shellwords.shellwords(ldflags) - Shellwords.shellwords(libs)).quote.join(" ")
  1631. $CFLAGS += " " << cflags
  1632. $LDFLAGS += " " << ldflags
  1633. $libs += " " << libs
  1634. Logging::message "package configuration for %s\n", pkg
  1635. Logging::message "cflags: %s\nldflags: %s\nlibs: %s\n\n",
  1636. cflags, ldflags, libs
  1637. [cflags, ldflags, libs]
  1638. else
  1639. Logging::message "package configuration for %s is not found\n", pkg
  1640. nil
  1641. end
  1642. end
  1643. # :stopdoc:
  1644. def with_destdir(dir)
  1645. return dir unless $extmk
  1646. dir = dir.sub($dest_prefix_pattern, '')
  1647. /\A\$[\(\{]/ =~ dir ? dir : "$(DESTDIR)"+dir
  1648. end
  1649. # Converts forward slashes to backslashes. Aimed at MS Windows.
  1650. #
  1651. # Internal use only.
  1652. #
  1653. def winsep(s)
  1654. s.tr('/', '\\')
  1655. end
  1656. # Converts native path to format acceptable in Makefile
  1657. #
  1658. # Internal use only.
  1659. #
  1660. if !CROSS_COMPILING
  1661. case CONFIG['build_os']
  1662. when 'mingw32'
  1663. def mkintpath(path)
  1664. # mingw uses make from msys and it needs special care
  1665. # converts from C:\some\path to /C/some/path
  1666. path = path.dup
  1667. path.tr!('\\', '/')
  1668. path.sub!(/\A([A-Za-z]):(?=\/)/, '/\1')
  1669. path
  1670. end
  1671. when 'cygwin'
  1672. if CONFIG['target_os'] != 'cygwin'
  1673. def mkintpath(path)
  1674. IO.popen(["cygpath", "-u", path], &:read).chomp
  1675. end
  1676. end
  1677. end
  1678. end
  1679. unless method_defined?(:mkintpath)
  1680. def mkintpath(path)
  1681. path
  1682. end
  1683. end
  1684. def configuration(srcdir)
  1685. mk = []
  1686. vpath = $VPATH.dup
  1687. CONFIG["hdrdir"] ||= $hdrdir
  1688. mk << %{
  1689. SHELL = /bin/sh
  1690. # V=0 quiet, V=1 verbose. other values don't work.
  1691. V = 0
  1692. Q1 = $(V:1=)
  1693. Q = $(Q1:0=@)
  1694. ECHO1 = $(V:1=@#{CONFIG['NULLCMD']})
  1695. ECHO = $(ECHO1:0=@echo)
  1696. #### Start of system configuration section. ####
  1697. #{"top_srcdir = " + $top_srcdir.sub(%r"\A#{Regexp.quote($topdir)}/", "$(topdir)/") if $extmk}
  1698. srcdir = #{srcdir.gsub(/\$\((srcdir)\)|\$\{(srcdir)\}/) {mkintpath(CONFIG[$1||$2]).unspace}}
  1699. topdir = #{mkintpath(topdir = $extmk ? CONFIG["topdir"] : $topdir).unspace}
  1700. hdrdir = #{(hdrdir = CONFIG["hdrdir"]) == topdir ? "$(topdir)" : mkintpath(hdrdir).unspace}
  1701. arch_hdrdir = #{$arch_hdrdir.quote}
  1702. PATH_SEPARATOR = #{CONFIG['PATH_SEPARATOR']}
  1703. VPATH = #{vpath.join(CONFIG['PATH_SEPARATOR'])}
  1704. }
  1705. if $extmk
  1706. mk << "RUBYLIB =\n""RUBYOPT = -\n"
  1707. end
  1708. prefix = mkintpath(CONFIG["prefix"])
  1709. if destdir = prefix[$dest_prefix_pattern, 1]
  1710. mk << "\nDESTDIR = #{destdir}\n"
  1711. end
  1712. mk << "prefix = #{with_destdir(prefix).unspace}\n"
  1713. CONFIG.each do |key, var|
  1714. mk << "#{key} = #{with_destdir(mkintpath(var)).unspace}\n" if /.prefix$/ =~ key
  1715. end
  1716. CONFIG.each do |key, var|
  1717. next if /^abs_/ =~ key
  1718. next if /^(?:src|top|hdr)dir$/ =~ key
  1719. next unless /dir$/ =~ key
  1720. mk << "#{key} = #{with_destdir(var)}\n"
  1721. end
  1722. if !$extmk and !$configure_args.has_key?('--ruby') and
  1723. sep = config_string('BUILD_FILE_SEPARATOR')
  1724. sep = ":/=#{sep}"
  1725. else
  1726. sep = ""
  1727. end
  1728. possible_command = (proc {|s| s if /top_srcdir/ !~ s} unless $extmk)
  1729. extconf_h = $extconf_h ? "-DRUBY_EXTCONF_H=\\\"$(RUBY_EXTCONF_H)\\\" " : $defs.join(" ") << " "
  1730. headers = %w[
  1731. $(hdrdir)/ruby.h
  1732. $(hdrdir)/ruby/ruby.h
  1733. $(hdrdir)/ruby/defines.h
  1734. $(hdrdir)/ruby/missing.h
  1735. $(hdrdir)/ruby/intern.h
  1736. $(hdrdir)/ruby/st.h
  1737. $(hdrdir)/ruby/subst.h
  1738. ]
  1739. if RULE_SUBST
  1740. headers.each {|h| h.sub!(/.*/, &RULE_SUBST.method(:%))}
  1741. end
  1742. headers << $config_h
  1743. headers << '$(RUBY_EXTCONF_H)' if $extconf_h
  1744. mk << %{
  1745. CC = #{CONFIG['CC']}
  1746. CXX = #{CONFIG['CXX']}
  1747. LIBRUBY = #{CONFIG['LIBRUBY']}
  1748. LIBRUBY_A = #{CONFIG['LIBRUBY_A']}
  1749. LIBRUBYARG_SHARED = #$LIBRUBYARG_SHARED
  1750. LIBRUBYARG_STATIC = #$LIBRUBYARG_STATIC
  1751. empty =
  1752. OUTFLAG = #{OUTFLAG}$(empty)
  1753. COUTFLAG = #{COUTFLAG}$(empty)
  1754. RUBY_EXTCONF_H = #{$extconf_h}
  1755. cflags = #{CONFIG['cflags']}
  1756. optflags = #{CONFIG['optflags']}
  1757. debugflags = #{CONFIG['debugflags']}
  1758. warnflags = #{$warnflags}
  1759. CCDLFLAGS = #{$static ? '' : CONFIG['CCDLFLAGS']}
  1760. CFLAGS = $(CCDLFLAGS) #$CFLAGS $(ARCH_FLAG)
  1761. INCFLAGS = -I. #$INCFLAGS
  1762. DEFS = #{CONFIG['DEFS']}
  1763. CPPFLAGS = #{extconf_h}#{$CPPFLAGS}
  1764. CXXFLAGS = $(CCDLFLAGS) #{CONFIG['CXXFLAGS']} $(ARCH_FLAG)
  1765. ldflags = #{$LDFLAGS}
  1766. dldflags = #{$DLDFLAGS} #{CONFIG['EXTDLDFLAGS']}
  1767. ARCH_FLAG = #{$ARCH_FLAG}
  1768. DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
  1769. LDSHARED = #{CONFIG['LDSHARED']}
  1770. LDSHAREDXX = #{config_string('LDSHAREDXX') || '$(LDSHARED)'}
  1771. AR = #{CONFIG['AR']}
  1772. EXEEXT = #{CONFIG['EXEEXT']}
  1773. }
  1774. CONFIG.each do |key, val|
  1775. mk << "#{key} = #{val}\n" if /^RUBY.*NAME/ =~ key
  1776. end
  1777. mk << %{
  1778. arch = #{CONFIG['arch']}
  1779. sitearch = #{CONFIG['sitearch']}
  1780. ruby_version = #{RbConfig::CONFIG['ruby_version']}
  1781. ruby = #{$ruby.sub(%r[\A#{Regexp.quote(RbConfig::CONFIG['bindir'])}(?=/|\z)]) {'$(bindir)'}}
  1782. RUBY = $(ruby#{sep})
  1783. ruby_headers = #{headers.join(' ')}
  1784. RM = #{config_string('RM', &possible_command) || '$(RUBY) -run -e rm -- -f'}
  1785. RM_RF = #{'$(RUBY) -run -e rm -- -rf'}
  1786. RMDIRS = #{config_string('RMDIRS', &possible_command) || '$(RUBY) -run -e rmdir -- -p'}
  1787. MAKEDIRS = #{config_string('MAKEDIRS', &possible_command) || '@$(RUBY) -run -e mkdir -- -p'}
  1788. INSTALL = #{config_string('INSTALL', &possible_command) || '@$(RUBY) -run -e install -- -vp'}
  1789. INSTALL_PROG = #{config_string('INSTALL_PROG') || '$(INSTALL) -m 0755'}
  1790. INSTALL_DATA = #{config_string('INSTALL_DATA') || '$(INSTALL) -m 0644'}
  1791. COPY = #{config_string('CP', &possible_command) || '@$(RUBY) -run -e cp -- -v'}
  1792. TOUCH = exit >
  1793. #### End of system configuration section. ####
  1794. preload = #{defined?($preload) && $preload ? $preload.join(' ') : ''}
  1795. }
  1796. if $nmake == ?b
  1797. mk.each do |x|
  1798. x.gsub!(/^(MAKEDIRS|INSTALL_(?:PROG|DATA))+\s*=.*\n/) do
  1799. "!ifndef " + $1 + "\n" +
  1800. $& +
  1801. "!endif\n"
  1802. end
  1803. end
  1804. end
  1805. mk
  1806. end
  1807. def timestamp_file(name, target_prefix = nil)
  1808. if target_prefix
  1809. pat = []
  1810. install_dirs.each do |n, d|
  1811. pat << n if /\$\(target_prefix\)\z/ =~ d
  1812. end
  1813. name = name.gsub(/\$\((#{pat.join("|")})\)/) {$&+target_prefix}
  1814. end
  1815. name = name.gsub(/(\$[({]|[})])|(\/+)|[^-.\w]+/) {$1 ? "" : $2 ? ".-." : "_"}
  1816. "$(TIMESTAMP_DIR)/.#{name}.time"
  1817. end
  1818. # :startdoc:
  1819. # creates a stub Makefile.
  1820. #
  1821. def dummy_makefile(srcdir)
  1822. configuration(srcdir) << <<RULES << CLEANINGS
  1823. CLEANFILES = #{$cleanfiles.join(' ')}
  1824. DISTCLEANFILES = #{$distcleanfiles.join(' ')}
  1825. all install static install-so install-rb: Makefile
  1826. .PHONY: all install static install-so install-rb
  1827. .PHONY: clean clean-so clean-static clean-rb
  1828. RULES
  1829. end
  1830. def each_compile_rules # :nodoc:
  1831. vpath_splat = /\$\(\*VPATH\*\)/
  1832. COMPILE_RULES.each do |rule|
  1833. if vpath_splat =~ rule
  1834. $VPATH.each do |path|
  1835. yield rule.sub(vpath_splat) {path}
  1836. end
  1837. else
  1838. yield rule
  1839. end
  1840. end
  1841. end
  1842. # Processes the data contents of the "depend" file. Each line of this file
  1843. # is expected to be a file name.
  1844. #
  1845. # Returns the output of findings, in Makefile format.
  1846. #
  1847. def depend_rules(depend)
  1848. suffixes = []
  1849. depout = []
  1850. cont = implicit = nil
  1851. impconv = proc do
  1852. each_compile_rules {|rule| depout << (rule % implicit[0]) << implicit[1]}
  1853. implicit = nil
  1854. end
  1855. ruleconv = proc do |line|
  1856. if implicit
  1857. if /\A\t/ =~ line
  1858. implicit[1] << line
  1859. next
  1860. else
  1861. impconv[]
  1862. end
  1863. end
  1864. if m = /\A\.(\w+)\.(\w+)(?:\s*:)/.match(line)
  1865. suffixes << m[1] << m[2]
  1866. implicit = [[m[1], m[2]], [m.post_match]]
  1867. next
  1868. elsif RULE_SUBST and /\A(?!\s*\w+\s*=)[$\w][^#]*:/ =~ line
  1869. line.gsub!(%r"(\s)(?!\.)([^$(){}+=:\s\/\\,]+)(?=\s|\z)") {$1 + RULE_SUBST % $2}
  1870. end
  1871. depout << line
  1872. end
  1873. depend.each_line do |line|
  1874. line.gsub!(/\.o\b/, ".#{$OBJEXT}")
  1875. line.gsub!(/\{\$\(VPATH\)\}/, "") unless $nmake
  1876. line.gsub!(/\$\((?:hdr|top)dir\)\/config.h/, $config_h)
  1877. line.gsub!(%r"\$\(hdrdir\)/(?!ruby(?![^:;/\s]))(?=[-\w]+\.h)", '\&ruby/')
  1878. if $nmake && /\A\s*\$\(RM|COPY\)/ =~ line
  1879. line.gsub!(%r"[-\w\./]{2,}"){$&.tr("/", "\\")}
  1880. line.gsub!(/(\$\((?!RM|COPY)[^:)]+)(?=\))/, '\1:/=\\')
  1881. end
  1882. if /(?:^|[^\\])(?:\\\\)*\\$/ =~ line
  1883. (cont ||= []) << line
  1884. next
  1885. elsif cont
  1886. line = (cont << line).join
  1887. cont = nil
  1888. end
  1889. ruleconv.call(line)
  1890. end
  1891. if cont
  1892. ruleconv.call(cont.join)
  1893. elsif implicit
  1894. impconv.call
  1895. end
  1896. unless suffixes.empty?
  1897. depout.unshift(".SUFFIXES: ." + suffixes.uniq.join(" .") + "\n\n")
  1898. end
  1899. depout.unshift("$(OBJS): $(RUBY_EXTCONF_H)\n\n") if $extconf_h
  1900. depout.flatten!
  1901. depout
  1902. end
  1903. # Generates the Makefile for your extension, passing along any options and
  1904. # preprocessor constants that you may have generated through other methods.
  1905. #
  1906. # The +target+ name should correspond the name of the global function name
  1907. # defined within your C extension, minus the +Init_+. For example, if your
  1908. # C extension is defined as +Init_foo+, then your target would simply be
  1909. # "foo".
  1910. #
  1911. # If any "/" characters are present in the target name, only the last name
  1912. # is interpreted as the target name, and the rest are considered toplevel
  1913. # directory names, and the generated Makefile will be altered accordingly to
  1914. # follow that directory structure.
  1915. #
  1916. # For example, if you pass "test/foo" as a target name, your extension will
  1917. # be installed under the "test" directory. This means that in order to
  1918. # load the file within a Ruby program later, that directory structure will
  1919. # have to be followed, e.g. <code>require 'test/foo'</code>.
  1920. #
  1921. # The +srcprefix+ should be used when your source files are not in the same
  1922. # directory as your build script. This will not only eliminate the need for
  1923. # you to manually copy the source files into the same directory as your
  1924. # build script, but it also sets the proper +target_prefix+ in the generated
  1925. # Makefile.
  1926. #
  1927. # Setting the +target_prefix+ will, in turn, install the generated binary in
  1928. # a directory under your <code>RbConfig::CONFIG['sitearchdir']</code> that
  1929. # mimics your local filesystem when you run <code>make install</code>.
  1930. #
  1931. # For example, given the following file tree:
  1932. #
  1933. # ext/
  1934. # extconf.rb
  1935. # test/
  1936. # foo.c
  1937. #
  1938. # And given the following code:
  1939. #
  1940. # create_makefile('test/foo', 'test')
  1941. #
  1942. # That will set the +target_prefix+ in the generated Makefile to "test".
  1943. # That, in turn, will create the following file tree when installed via the
  1944. # <code>make install</code> command:
  1945. #
  1946. # /path/to/ruby/sitearchdir/test/foo.so
  1947. #
  1948. # It is recommended that you use this approach to generate your makefiles,
  1949. # instead of copying files around manually, because some third party
  1950. # libraries may depend on the +target_prefix+ being set properly.
  1951. #
  1952. # The +srcprefix+ argument can be used to override the default source
  1953. # directory, i.e. the current directory. It is included as part of the
  1954. # +VPATH+ and added to the list of +INCFLAGS+.
  1955. #
  1956. def create_makefile(target, srcprefix = nil)
  1957. $target = target
  1958. libpath = $DEFLIBPATH|$LIBPATH
  1959. message "creating Makefile\n"
  1960. MakeMakefile.rm_f "conftest*"
  1961. if CONFIG["DLEXT"] == $OBJEXT
  1962. for lib in libs = $libs.split
  1963. lib.sub!(/-l(.*)/, %%"lib\\1.#{$LIBEXT}"%)
  1964. end
  1965. $defs.push(format("-DEXTLIB='%s'", libs.join(",")))
  1966. end
  1967. if target.include?('/')
  1968. target_prefix, target = File.split(target)
  1969. target_prefix[0,0] = '/'
  1970. else
  1971. target_prefix = ""
  1972. end
  1973. srcprefix ||= "$(srcdir)/#{srcprefix}".chomp('/')
  1974. RbConfig.expand(srcdir = srcprefix.dup)
  1975. ext = ".#{$OBJEXT}"
  1976. orig_srcs = Dir[File.join(srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
  1977. if not $objs
  1978. srcs = $srcs || orig_srcs
  1979. objs = srcs.inject(Hash.new {[]}) {|h, f| h[File.basename(f, ".*") << ext] <<= f; h}
  1980. $objs = objs.keys
  1981. unless objs.delete_if {|b, f| f.size == 1}.empty?
  1982. dups = objs.sort.map {|b, f|
  1983. "#{b[/.*\./]}{#{f.collect {|n| n[/([^.]+)\z/]}.join(',')}}"
  1984. }
  1985. abort "source files duplication - #{dups.join(", ")}"
  1986. end
  1987. else
  1988. $objs.collect! {|o| File.basename(o, ".*") << ext} unless $OBJEXT == "o"
  1989. srcs = $srcs || $objs.collect {|o| o.chomp(ext) << ".c"}
  1990. end
  1991. $srcs = srcs
  1992. hdrs = Dir[File.join(srcdir, "*.{#{HDR_EXT.join(%q{,})}}")]
  1993. target = nil if $objs.empty?
  1994. if target and EXPORT_PREFIX
  1995. if File.exist?(File.join(srcdir, target + '.def'))
  1996. deffile = "$(srcdir)/$(TARGET).def"
  1997. unless EXPORT_PREFIX.empty?
  1998. makedef = %{-pe "$_.sub!(/^(?=\\w)/,'#{EXPORT_PREFIX}') unless 1../^EXPORTS$/i"}
  1999. end
  2000. else
  2001. makedef = %{-e "puts 'EXPORTS', '$(TARGET_ENTRY)'"}
  2002. end
  2003. if makedef
  2004. $cleanfiles << '$(DEFFILE)'
  2005. origdef = deffile
  2006. deffile = "$(TARGET)-$(arch).def"
  2007. end
  2008. end
  2009. origdef ||= ''
  2010. if $extout and $INSTALLFILES
  2011. $cleanfiles.concat($INSTALLFILES.collect {|files, dir|File.join(dir, files.sub(/\A\.\//, ''))})
  2012. $distcleandirs.concat($INSTALLFILES.collect {|files, dir| dir})
  2013. end
  2014. if $extmk and $static
  2015. $defs << "-DRUBY_EXPORT=1"
  2016. end
  2017. if $extmk and not $extconf_h
  2018. create_header
  2019. end
  2020. libpath = libpathflag(libpath)
  2021. dllib = target ? "$(TARGET).#{CONFIG['DLEXT']}" : ""
  2022. staticlib = target ? "$(TARGET).#$LIBEXT" : ""
  2023. mfile = open("Makefile", "wb")
  2024. conf = configuration(srcprefix)
  2025. conf = yield(conf) if block_given?
  2026. mfile.puts(conf)
  2027. mfile.print "
  2028. libpath = #{($DEFLIBPATH|$LIBPATH).join(" ")}
  2029. LIBPATH = #{libpath}
  2030. DEFFILE = #{deffile}
  2031. CLEANFILES = #{$cleanfiles.join(' ')}
  2032. DISTCLEANFILES = #{$distcleanfiles.join(' ')}
  2033. DISTCLEANDIRS = #{$distcleandirs.join(' ')}
  2034. extout = #{$extout && $extout.quote}
  2035. extout_prefix = #{$extout_prefix}
  2036. target_prefix = #{target_prefix}
  2037. LOCAL_LIBS = #{$LOCAL_LIBS}
  2038. LIBS = #{$LIBRUBYARG} #{$libs} #{$LIBS}
  2039. ORIG_SRCS = #{orig_srcs.collect(&File.method(:basename)).join(' ')}
  2040. SRCS = $(ORIG_SRCS) #{(srcs - orig_srcs).collect(&File.method(:basename)).join(' ')}
  2041. OBJS = #{$objs.join(" ")}
  2042. HDRS = #{hdrs.map{|h| '$(srcdir)/' + File.basename(h)}.join(' ')}
  2043. TARGET = #{target}
  2044. TARGET_NAME = #{target && target[/\A\w+/]}
  2045. TARGET_ENTRY = #{EXPORT_PREFIX || ''}Init_$(TARGET_NAME)
  2046. DLLIB = #{dllib}
  2047. EXTSTATIC = #{$static || ""}
  2048. STATIC_LIB = #{staticlib unless $static.nil?}
  2049. #{!$extout && defined?($installed_list) ? "INSTALLED_LIST = #{$installed_list}\n" : ""}
  2050. TIMESTAMP_DIR = #{$extout ? '$(extout)/.timestamp' : '.'}
  2051. " #"
  2052. # TODO: fixme
  2053. install_dirs.each {|d| mfile.print("%-14s= %s\n" % d) if /^[[:upper:]]/ =~ d[0]}
  2054. n = ($extout ? '$(RUBYARCHDIR)/' : '') + '$(TARGET)'
  2055. mfile.print "
  2056. TARGET_SO = #{($extout ? '$(RUBYARCHDIR)/' : '')}$(DLLIB)
  2057. CLEANLIBS = #{n}.#{CONFIG['DLEXT']} #{config_string('cleanlibs') {|t| t.gsub(/\$\*/) {n}}}
  2058. CLEANOBJS = *.#{$OBJEXT} #{config_string('cleanobjs') {|t| t.gsub(/\$\*/, "$(TARGET)#{deffile ? '-$(arch)': ''}")} if target} *.bak
  2059. all: #{$extout ? "install" : target ? "$(DLLIB)" : "Makefile"}
  2060. static: $(STATIC_LIB)#{$extout ? " install-rb" : ""}
  2061. .PHONY: all install static install-so install-rb
  2062. .PHONY: clean clean-so clean-static clean-rb
  2063. "
  2064. mfile.print CLEANINGS
  2065. fsep = config_string('BUILD_FILE_SEPARATOR') {|s| s unless s == "/"}
  2066. if fsep
  2067. sep = ":/=#{fsep}"
  2068. fseprepl = proc {|s|
  2069. s = s.gsub("/", fsep)
  2070. s = s.gsub(/(\$\(\w+)(\))/) {$1+sep+$2}
  2071. s = s.gsub(/(\$\{\w+)(\})/) {$1+sep+$2}
  2072. }
  2073. rsep = ":#{fsep}=/"
  2074. else
  2075. fseprepl = proc {|s| s}
  2076. sep = ""
  2077. rsep = ""
  2078. end
  2079. dirs = []
  2080. mfile.print "install: install-so install-rb\n\n"
  2081. sodir = (dir = "$(RUBYARCHDIR)").dup
  2082. mfile.print("install-so: ")
  2083. if target
  2084. f = "$(DLLIB)"
  2085. dest = "#{dir}/#{f}"
  2086. if $extout
  2087. mfile.puts dest
  2088. mfile.print "clean-so::\n"
  2089. mfile.print "\t-$(Q)$(RM) #{fseprepl[dest]}\n"
  2090. mfile.print "\t-$(Q)$(RMDIRS) #{fseprepl[dir]}#{$ignore_error}\n"
  2091. else
  2092. mfile.print "#{f} #{timestamp_file(dir, target_prefix)}\n"
  2093. mfile.print "\t$(INSTALL_PROG) #{fseprepl[f]} #{dir}\n"
  2094. if defined?($installed_list)
  2095. mfile.print "\t@echo #{dir}/#{File.basename(f)}>>$(INSTALLED_LIST)\n"
  2096. end
  2097. end
  2098. mfile.print "clean-static::\n"
  2099. mfile.print "\t-$(Q)$(RM) $(STATIC_LIB)\n"
  2100. else
  2101. mfile.puts "Makefile"
  2102. end
  2103. mfile.print("install-rb: pre-install-rb install-rb-default\n")
  2104. mfile.print("install-rb-default: pre-install-rb-default\n")
  2105. mfile.print("pre-install-rb: Makefile\n")
  2106. mfile.print("pre-install-rb-default: Makefile\n")
  2107. for sfx, i in [["-default", [["lib/**/*.rb", "$(RUBYLIBDIR)", "lib"]]], ["", $INSTALLFILES]]
  2108. files = install_files(mfile, i, nil, srcprefix) or next
  2109. for dir, *files in files
  2110. unless dirs.include?(dir)
  2111. dirs << dir
  2112. mfile.print "pre-install-rb#{sfx}: #{timestamp_file(dir, target_prefix)}\n"
  2113. end
  2114. for f in files
  2115. dest = "#{dir}/#{File.basename(f)}"
  2116. mfile.print("install-rb#{sfx}: #{dest}\n")
  2117. mfile.print("#{dest}: #{f} #{timestamp_file(dir, target_prefix)}\n")
  2118. mfile.print("\t$(Q) $(#{$extout ? 'COPY' : 'INSTALL_DATA'}) #{f} $(@D#{sep})\n")
  2119. if defined?($installed_list) and !$extout
  2120. mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n")
  2121. end
  2122. if $extout
  2123. mfile.print("clean-rb#{sfx}::\n")
  2124. mfile.print("\t-$(Q)$(RM) #{fseprepl[dest]}\n")
  2125. end
  2126. end
  2127. end
  2128. mfile.print "pre-install-rb#{sfx}:\n"
  2129. mfile.print("\t$(ECHO) installing#{sfx.sub(/^-/, " ")} #{target} libraries\n")
  2130. if $extout
  2131. dirs.uniq!
  2132. unless dirs.empty?
  2133. mfile.print("clean-rb#{sfx}::\n")
  2134. for dir in dirs.sort_by {|d| -d.count('/')}
  2135. mfile.print("\t-$(Q)$(RMDIRS) #{fseprepl[dir]}#{$ignore_error}\n")
  2136. end
  2137. end
  2138. end
  2139. end
  2140. dirs.unshift(sodir) if target and !dirs.include?(sodir)
  2141. dirs.each do |d|
  2142. t = timestamp_file(d, target_prefix)
  2143. mfile.print "#{t}:\n\t$(Q) $(MAKEDIRS) $(@D) #{d}\n\t$(Q) $(TOUCH) $@\n"
  2144. end
  2145. mfile.print <<-SITEINSTALL
  2146. site-install: site-install-so site-install-rb
  2147. site-install-so: install-so
  2148. site-install-rb: install-rb
  2149. SITEINSTALL
  2150. return unless target
  2151. mfile.puts SRC_EXT.collect {|e| ".path.#{e} = $(VPATH)"} if $nmake == ?b
  2152. mfile.print ".SUFFIXES: .#{SRC_EXT.join(' .')} .#{$OBJEXT}\n"
  2153. mfile.print "\n"
  2154. compile_command = "\n\t$(ECHO) compiling $(<#{rsep})\n\t$(Q) %s\n\n"
  2155. CXX_EXT.each do |e|
  2156. each_compile_rules do |rule|
  2157. mfile.printf(rule, e, $OBJEXT)
  2158. mfile.printf(compile_command, COMPILE_CXX)
  2159. end
  2160. end
  2161. C_EXT.each do |e|
  2162. each_compile_rules do |rule|
  2163. mfile.printf(rule, e, $OBJEXT)
  2164. mfile.printf(compile_command, COMPILE_C)
  2165. end
  2166. end
  2167. mfile.print "$(RUBYARCHDIR)/" if $extout
  2168. mfile.print "$(DLLIB): "
  2169. mfile.print "$(DEFFILE) " if makedef
  2170. mfile.print "$(OBJS) Makefile"
  2171. mfile.print " #{timestamp_file('$(RUBYARCHDIR)', target_prefix)}" if $extout
  2172. mfile.print "\n"
  2173. mfile.print "\t$(ECHO) linking shared-object #{target_prefix.sub(/\A\/(.*)/, '\1/')}$(DLLIB)\n"
  2174. mfile.print "\t-$(Q)$(RM) $(@#{sep})\n"
  2175. link_so = LINK_SO.gsub(/^/, "\t$(Q) ")
  2176. if srcs.any?(&%r"\.(?:#{CXX_EXT.join('|')})\z".method(:===))
  2177. link_so = link_so.sub(/\bLDSHARED\b/, '\&XX')
  2178. end
  2179. mfile.print link_so, "\n\n"
  2180. unless $static.nil?
  2181. mfile.print "$(STATIC_LIB): $(OBJS)\n\t-$(Q)$(RM) $(@#{sep})\n\t"
  2182. mfile.print "$(ECHO) linking static-library $(@#{rsep})\n\t$(Q) "
  2183. mfile.print "$(AR) #{config_string('ARFLAGS') || 'cru '}$@ $(OBJS)"
  2184. config_string('RANLIB') do |ranlib|
  2185. mfile.print "\n\t-$(Q)#{ranlib} $(DLLIB) 2> /dev/null || true"
  2186. end
  2187. end
  2188. mfile.print "\n\n"
  2189. if makedef
  2190. mfile.print "$(DEFFILE): #{origdef}\n"
  2191. mfile.print "\t$(ECHO) generating $(@#{rsep})\n"
  2192. mfile.print "\t$(Q) $(RUBY) #{makedef} #{origdef} > $@\n\n"
  2193. end
  2194. depend = File.join(srcdir, "depend")
  2195. if File.exist?(depend)
  2196. mfile.print("###\n", *depend_rules(File.read(depend)))
  2197. else
  2198. mfile.print "$(OBJS): $(HDRS) $(ruby_headers)\n"
  2199. end
  2200. $makefile_created = true
  2201. ensure
  2202. mfile.close if mfile
  2203. end
  2204. # :stopdoc:
  2205. def init_mkmf(config = CONFIG, rbconfig = RbConfig::CONFIG)
  2206. $makefile_created = false
  2207. $arg_config = []
  2208. $enable_shared = config['ENABLE_SHARED'] == 'yes'
  2209. $defs = []
  2210. $extconf_h = nil
  2211. if $warnflags = CONFIG['warnflags'] and CONFIG['GCC'] == 'yes'
  2212. # turn warnings into errors only for bundled extensions.
  2213. config['warnflags'] = $warnflags.gsub(/(\A|\s)-Werror[-=]/, '\1-W')
  2214. RbConfig.expand(rbconfig['warnflags'] = config['warnflags'].dup)
  2215. config.each do |key, val|
  2216. RbConfig.expand(rbconfig[key] = val.dup) if /warnflags/ =~ val
  2217. end
  2218. $warnflags = config['warnflags'] unless $extmk
  2219. end
  2220. $CFLAGS = with_config("cflags", arg_config("CFLAGS", config["CFLAGS"])).dup
  2221. $ARCH_FLAG = with_config("arch_flag", arg_config("ARCH_FLAG", config["ARCH_FLAG"])).dup
  2222. $CPPFLAGS = with_config("cppflags", arg_config("CPPFLAGS", config["CPPFLAGS"])).dup
  2223. $LDFLAGS = with_config("ldflags", arg_config("LDFLAGS", config["LDFLAGS"])).dup
  2224. $INCFLAGS = "-I$(arch_hdrdir)"
  2225. $INCFLAGS << " -I$(hdrdir)/ruby/backward" unless $extmk
  2226. $INCFLAGS << " -I$(hdrdir) -I$(srcdir)"
  2227. $DLDFLAGS = with_config("dldflags", arg_config("DLDFLAGS", config["DLDFLAGS"])).dup
  2228. $LIBEXT = config['LIBEXT'].dup
  2229. $OBJEXT = config["OBJEXT"].dup
  2230. $EXEEXT = config["EXEEXT"].dup
  2231. $LIBS = "#{config['LIBS']} #{config['DLDLIBS']}"
  2232. $LIBRUBYARG = ""
  2233. $LIBRUBYARG_STATIC = config['LIBRUBYARG_STATIC']
  2234. $LIBRUBYARG_SHARED = config['LIBRUBYARG_SHARED']
  2235. $DEFLIBPATH = [$extmk ? "$(topdir)" : "$(#{config["libdirname"] || "libdir"})"]
  2236. $DEFLIBPATH.unshift(".")
  2237. $LIBPATH = []
  2238. $INSTALLFILES = []
  2239. $NONINSTALLFILES = [/~\z/, /\A#.*#\z/, /\A\.#/, /\.bak\z/i, /\.orig\z/, /\.rej\z/, /\.l[ao]\z/, /\.o\z/]
  2240. $VPATH = %w[$(srcdir) $(arch_hdrdir)/ruby $(hdrdir)/ruby]
  2241. $objs = nil
  2242. $srcs = nil
  2243. $libs = ""
  2244. if $enable_shared or RbConfig.expand(config["LIBRUBY"].dup) != RbConfig.expand(config["LIBRUBY_A"].dup)
  2245. $LIBRUBYARG = config['LIBRUBYARG']
  2246. end
  2247. $LOCAL_LIBS = ""
  2248. $cleanfiles = config_string('CLEANFILES') {|s| Shellwords.shellwords(s)} || []
  2249. $cleanfiles << "mkmf.log"
  2250. $distcleanfiles = config_string('DISTCLEANFILES') {|s| Shellwords.shellwords(s)} || []
  2251. $distcleandirs = config_string('DISTCLEANDIRS') {|s| Shellwords.shellwords(s)} || []
  2252. $extout ||= nil
  2253. $extout_prefix ||= nil
  2254. $arg_config.clear
  2255. dir_config("opt")
  2256. end
  2257. FailedMessage = <<MESSAGE
  2258. Could not create Makefile due to some reason, probably lack of necessary
  2259. libraries and/or headers. Check the mkmf.log file for more details. You may
  2260. need configuration options.
  2261. Provided configuration options:
  2262. MESSAGE
  2263. # Returns whether or not the Makefile was successfully generated. If not,
  2264. # the script will abort with an error message.
  2265. #
  2266. # Internal use only.
  2267. #
  2268. def mkmf_failed(path)
  2269. unless $makefile_created or File.exist?("Makefile")
  2270. opts = $arg_config.collect {|t, n| "\t#{t}#{n ? "=#{n}" : ""}\n"}
  2271. abort "*** #{path} failed ***\n" + FailedMessage + opts.join
  2272. end
  2273. end
  2274. private
  2275. def _libdir_basename
  2276. @libdir_basename ||= config_string("libdir") {|name| name[/\A\$\(exec_prefix\)\/(.*)/, 1]} || "lib"
  2277. end
  2278. def MAIN_DOES_NOTHING(*refs)
  2279. src = MAIN_DOES_NOTHING
  2280. unless refs.empty?
  2281. src = src.sub(/\{/) do
  2282. $& +
  2283. "\n if (argc > 1000000) {\n" +
  2284. refs.map {|n|" printf(\"%p\", &#{n});\n"}.join("") +
  2285. " }\n"
  2286. end
  2287. end
  2288. src
  2289. end
  2290. extend self
  2291. init_mkmf
  2292. $make = with_config("make-prog", ENV["MAKE"] || "make")
  2293. make, = Shellwords.shellwords($make)
  2294. $nmake = nil
  2295. case
  2296. when $mswin
  2297. $nmake = ?m if /nmake/i =~ make
  2298. when $bccwin
  2299. $nmake = ?b if /Borland/i =~ `#{make} -h`
  2300. end
  2301. $ignore_error = $nmake ? '' : ' 2> /dev/null || true'
  2302. RbConfig::CONFIG["srcdir"] = CONFIG["srcdir"] =
  2303. $srcdir = arg_config("--srcdir", File.dirname($0))
  2304. $configure_args["--topsrcdir"] ||= $srcdir
  2305. if $curdir = arg_config("--curdir")
  2306. RbConfig.expand(curdir = $curdir.dup)
  2307. else
  2308. curdir = $curdir = "."
  2309. end
  2310. unless File.expand_path(RbConfig::CONFIG["topdir"]) == File.expand_path(curdir)
  2311. CONFIG["topdir"] = $curdir
  2312. RbConfig::CONFIG["topdir"] = curdir
  2313. end
  2314. $configure_args["--topdir"] ||= $curdir
  2315. $ruby = arg_config("--ruby", File.join(RbConfig::CONFIG["bindir"], CONFIG["ruby_install_name"]))
  2316. # :startdoc:
  2317. split = Shellwords.method(:shellwords).to_proc
  2318. EXPORT_PREFIX = config_string('EXPORT_PREFIX') {|s| s.strip}
  2319. hdr = ['#include "ruby.h"' "\n"]
  2320. config_string('COMMON_MACROS') do |s|
  2321. Shellwords.shellwords(s).each do |w|
  2322. w, v = w.split(/=/, 2)
  2323. hdr << "#ifndef #{w}"
  2324. hdr << "#define #{[w, v].compact.join(" ")}"
  2325. hdr << "#endif /* #{w} */"
  2326. end
  2327. end
  2328. config_string('COMMON_HEADERS') do |s|
  2329. Shellwords.shellwords(s).each {|w| hdr << "#include <#{w}>"}
  2330. end
  2331. ##
  2332. # Common headers for ruby C extensions
  2333. COMMON_HEADERS = hdr.join("\n")
  2334. ##
  2335. # Common libraries for ruby C extensions
  2336. COMMON_LIBS = config_string('COMMON_LIBS', &split) || []
  2337. ##
  2338. # make compile rules
  2339. COMPILE_RULES = config_string('COMPILE_RULES', &split) || %w[.%s.%s:]
  2340. RULE_SUBST = config_string('RULE_SUBST')
  2341. ##
  2342. # Command which will compile C files in the generated Makefile
  2343. COMPILE_C = config_string('COMPILE_C') || '$(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<'
  2344. ##
  2345. # Command which will compile C++ files in the generated Makefile
  2346. COMPILE_CXX = config_string('COMPILE_CXX') || '$(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<'
  2347. ##
  2348. # Command which will compile a program in order to test linking a library
  2349. TRY_LINK = config_string('TRY_LINK') ||
  2350. "$(CC) #{OUTFLAG}conftest#{$EXEEXT} $(INCFLAGS) $(CPPFLAGS) " \
  2351. "$(CFLAGS) $(src) $(LIBPATH) $(LDFLAGS) $(ARCH_FLAG) $(LOCAL_LIBS) $(LIBS)"
  2352. ##
  2353. # Command which will link a shared library
  2354. LINK_SO = (config_string('LINK_SO') || "").sub(/^$/) do
  2355. if CONFIG["DLEXT"] == $OBJEXT
  2356. "ld $(DLDFLAGS) -r -o $@ $(OBJS)\n"
  2357. else
  2358. "$(LDSHARED) #{OUTFLAG}$@ $(OBJS) " \
  2359. "$(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)"
  2360. end
  2361. end
  2362. ##
  2363. # Argument which will add a library path to the linker
  2364. LIBPATHFLAG = config_string('LIBPATHFLAG') || ' -L%s'
  2365. RPATHFLAG = config_string('RPATHFLAG') || ''
  2366. ##
  2367. # Argument which will add a library to the linker
  2368. LIBARG = config_string('LIBARG') || '-l%s'
  2369. ##
  2370. # A C main function which does no work
  2371. MAIN_DOES_NOTHING = config_string('MAIN_DOES_NOTHING') || "int main(int argc, char **argv)\n{\n return 0;\n}"
  2372. UNIVERSAL_INTS = config_string('UNIVERSAL_INTS') {|s| Shellwords.shellwords(s)} ||
  2373. %w[int short long long\ long]
  2374. sep = config_string('BUILD_FILE_SEPARATOR') {|s| ":/=#{s}" if s != "/"} || ""
  2375. ##
  2376. # Makefile rules that will clean the extension build directory
  2377. CLEANINGS = "
  2378. clean-static::
  2379. clean-rb-default::
  2380. clean-rb::
  2381. clean-so::
  2382. clean: clean-so clean-static clean-rb-default clean-rb
  2383. \t\t-$(Q)$(RM) $(CLEANLIBS#{sep}) $(CLEANOBJS#{sep}) $(CLEANFILES#{sep}) .*.time
  2384. distclean-rb-default::
  2385. distclean-rb::
  2386. distclean-so::
  2387. distclean-static::
  2388. distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
  2389. \t\t-$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
  2390. \t\t-$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES#{sep})
  2391. \t\t-$(Q)$(RMDIRS) $(DISTCLEANDIRS#{sep})#{$ignore_error}
  2392. realclean: distclean
  2393. "
  2394. end
  2395. include MakeMakefile
  2396. if not $extmk and /\A(extconf|makefile).rb\z/ =~ File.basename($0)
  2397. END {mkmf_failed($0)}
  2398. end