setup.rb 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. #
  2. # setup.rb
  3. #
  4. # Copyright (c) 2000-2004 Minero Aoki
  5. #
  6. # This program is free software.
  7. # You can distribute/modify this program under the terms of
  8. # the GNU LGPL, Lesser General Public License version 2.1.
  9. #
  10. #
  11. # For backward compatibility
  12. #
  13. unless Enumerable.method_defined?(:map)
  14. module Enumerable
  15. alias map collect
  16. end
  17. end
  18. unless Enumerable.method_defined?(:detect)
  19. module Enumerable
  20. alias detect find
  21. end
  22. end
  23. unless Enumerable.method_defined?(:select)
  24. module Enumerable
  25. alias select find_all
  26. end
  27. end
  28. unless Enumerable.method_defined?(:reject)
  29. module Enumerable
  30. def reject
  31. select {|i| not yield(i) }
  32. end
  33. end
  34. end
  35. unless Enumerable.method_defined?(:inject)
  36. module Enumerable
  37. def inject(result)
  38. each do |i|
  39. result = yield(result, i)
  40. end
  41. result
  42. end
  43. end
  44. end
  45. unless Enumerable.method_defined?(:any?)
  46. module Enumerable
  47. def any?
  48. each do |i|
  49. return true if yield(i)
  50. end
  51. false
  52. end
  53. end
  54. end
  55. unless File.respond_to?(:read)
  56. def File.read(fname)
  57. open(fname) {|f|
  58. return f.read
  59. }
  60. end
  61. end
  62. #
  63. # Application independent utilities
  64. #
  65. def File.binread(fname)
  66. open(fname, 'rb') {|f|
  67. return f.read
  68. }
  69. end
  70. # for corrupted windows stat(2)
  71. def File.dir?(path)
  72. File.directory?((path[-1,1] == '/') ? path : path + '/')
  73. end
  74. #
  75. # Config
  76. #
  77. if arg = ARGV.detect {|arg| /\A--rbconfig=/ =~ arg }
  78. ARGV.delete(arg)
  79. require arg.split(/=/, 2)[1]
  80. $".push 'rbconfig.rb'
  81. else
  82. require 'rbconfig'
  83. end
  84. def multipackage_install?
  85. FileTest.directory?(File.dirname($0) + '/packages')
  86. end
  87. class ConfigTable
  88. c = ::Config::CONFIG
  89. rubypath = c['bindir'] + '/' + c['ruby_install_name']
  90. major = c['MAJOR'].to_i
  91. minor = c['MINOR'].to_i
  92. teeny = c['TEENY'].to_i
  93. version = "#{major}.#{minor}"
  94. # ruby ver. >= 1.4.4?
  95. newpath_p = ((major >= 2) or
  96. ((major == 1) and
  97. ((minor >= 5) or
  98. ((minor == 4) and (teeny >= 4)))))
  99. subprefix = lambda {|path|
  100. path.sub(/\A#{Regexp.quote(c['prefix'])}/o, '$prefix')
  101. }
  102. if c['rubylibdir']
  103. # V < 1.6.3
  104. stdruby = subprefix.call(c['rubylibdir'])
  105. siteruby = subprefix.call(c['sitedir'])
  106. versite = subprefix.call(c['sitelibdir'])
  107. sodir = subprefix.call(c['sitearchdir'])
  108. elsif newpath_p
  109. # 1.4.4 <= V <= 1.6.3
  110. stdruby = "$prefix/lib/ruby/#{version}"
  111. siteruby = subprefix.call(c['sitedir'])
  112. versite = siteruby + '/' + version
  113. sodir = "$site-ruby/#{c['arch']}"
  114. else
  115. # V < 1.4.4
  116. stdruby = "$prefix/lib/ruby/#{version}"
  117. siteruby = "$prefix/lib/ruby/#{version}/site_ruby"
  118. versite = siteruby
  119. sodir = "$site-ruby/#{c['arch']}"
  120. end
  121. if arg = c['configure_args'].split.detect {|arg| /--with-make-prog=/ =~ arg }
  122. makeprog = arg.sub(/'/, '').split(/=/, 2)[1]
  123. else
  124. makeprog = 'make'
  125. end
  126. common_descripters = [
  127. [ 'prefix', [ c['prefix'],
  128. 'path',
  129. 'path prefix of target environment' ] ],
  130. [ 'std-ruby', [ stdruby,
  131. 'path',
  132. 'the directory for standard ruby libraries' ] ],
  133. [ 'site-ruby-common', [ siteruby,
  134. 'path',
  135. 'the directory for version-independent non-standard ruby libraries' ] ],
  136. [ 'site-ruby', [ versite,
  137. 'path',
  138. 'the directory for non-standard ruby libraries' ] ],
  139. [ 'bin-dir', [ '$prefix/bin',
  140. 'path',
  141. 'the directory for commands' ] ],
  142. [ 'rb-dir', [ '$site-ruby',
  143. 'path',
  144. 'the directory for ruby scripts' ] ],
  145. [ 'so-dir', [ sodir,
  146. 'path',
  147. 'the directory for ruby extentions' ] ],
  148. [ 'data-dir', [ '$prefix/share',
  149. 'path',
  150. 'the directory for shared data' ] ],
  151. [ 'ruby-path', [ rubypath,
  152. 'path',
  153. 'path to set to #! line' ] ],
  154. [ 'ruby-prog', [ rubypath,
  155. 'name',
  156. 'the ruby program using for installation' ] ],
  157. [ 'make-prog', [ makeprog,
  158. 'name',
  159. 'the make program to compile ruby extentions' ] ],
  160. [ 'without-ext', [ 'no',
  161. 'yes/no',
  162. 'does not compile/install ruby extentions' ] ]
  163. ]
  164. multipackage_descripters = [
  165. [ 'with', [ '',
  166. 'name,name...',
  167. 'package names that you want to install',
  168. 'ALL' ] ],
  169. [ 'without', [ '',
  170. 'name,name...',
  171. 'package names that you do not want to install',
  172. 'NONE' ] ]
  173. ]
  174. if multipackage_install?
  175. DESCRIPTER = common_descripters + multipackage_descripters
  176. else
  177. DESCRIPTER = common_descripters
  178. end
  179. SAVE_FILE = '.config'
  180. def ConfigTable.each_name(&block)
  181. keys().each(&block)
  182. end
  183. def ConfigTable.keys
  184. DESCRIPTER.map {|name, *dummy| name }
  185. end
  186. def ConfigTable.each_definition(&block)
  187. DESCRIPTER.each(&block)
  188. end
  189. def ConfigTable.get_entry(name)
  190. name, ent = DESCRIPTER.assoc(name)
  191. ent
  192. end
  193. def ConfigTable.get_entry!(name)
  194. get_entry(name) or raise ArgumentError, "no such config: #{name}"
  195. end
  196. def ConfigTable.add_entry(name, vals)
  197. ConfigTable::DESCRIPTER.push [name,vals]
  198. end
  199. def ConfigTable.remove_entry(name)
  200. get_entry(name) or raise ArgumentError, "no such config: #{name}"
  201. DESCRIPTER.delete_if {|n, arr| n == name }
  202. end
  203. def ConfigTable.config_key?(name)
  204. get_entry(name) ? true : false
  205. end
  206. def ConfigTable.bool_config?(name)
  207. ent = get_entry(name) or return false
  208. ent[1] == 'yes/no'
  209. end
  210. def ConfigTable.value_config?(name)
  211. ent = get_entry(name) or return false
  212. ent[1] != 'yes/no'
  213. end
  214. def ConfigTable.path_config?(name)
  215. ent = get_entry(name) or return false
  216. ent[1] == 'path'
  217. end
  218. class << self
  219. alias newobj new
  220. end
  221. def ConfigTable.new
  222. c = newobj()
  223. c.initialize_from_table
  224. c
  225. end
  226. def ConfigTable.load
  227. c = newobj()
  228. c.initialize_from_file
  229. c
  230. end
  231. def initialize_from_table
  232. @table = {}
  233. DESCRIPTER.each do |k, (default, vname, desc, default2)|
  234. @table[k] = default
  235. end
  236. end
  237. def initialize_from_file
  238. raise InstallError, "#{File.basename $0} config first"\
  239. unless File.file?(SAVE_FILE)
  240. @table = {}
  241. File.foreach(SAVE_FILE) do |line|
  242. k, v = line.split(/=/, 2)
  243. @table[k] = v.strip
  244. end
  245. end
  246. def save
  247. File.open(SAVE_FILE, 'w') {|f|
  248. @table.each do |k, v|
  249. f.printf "%s=%s\n", k, v if v
  250. end
  251. }
  252. end
  253. def []=(k, v)
  254. raise InstallError, "unknown config option #{k}"\
  255. unless ConfigTable.config_key?(k)
  256. @table[k] = v
  257. end
  258. def [](key)
  259. return nil unless @table[key]
  260. @table[key].gsub(%r<\$([^/]+)>) { self[$1] }
  261. end
  262. def set_raw(key, val)
  263. @table[key] = val
  264. end
  265. def get_raw(key)
  266. @table[key]
  267. end
  268. end
  269. module MetaConfigAPI
  270. def eval_file_ifexist(fname)
  271. instance_eval File.read(fname), fname, 1 if File.file?(fname)
  272. end
  273. def config_names
  274. ConfigTable.keys
  275. end
  276. def config?(name)
  277. ConfigTable.config_key?(name)
  278. end
  279. def bool_config?(name)
  280. ConfigTable.bool_config?(name)
  281. end
  282. def value_config?(name)
  283. ConfigTable.value_config?(name)
  284. end
  285. def path_config?(name)
  286. ConfigTable.path_config?(name)
  287. end
  288. def add_config(name, argname, default, desc)
  289. ConfigTable.add_entry name,[default,argname,desc]
  290. end
  291. def add_path_config(name, default, desc)
  292. add_config name, 'path', default, desc
  293. end
  294. def add_bool_config(name, default, desc)
  295. add_config name, 'yes/no', default ? 'yes' : 'no', desc
  296. end
  297. def set_config_default(name, default)
  298. if bool_config?(name)
  299. ConfigTable.get_entry!(name)[0] = (default ? 'yes' : 'no')
  300. else
  301. ConfigTable.get_entry!(name)[0] = default
  302. end
  303. end
  304. def remove_config(name)
  305. ent = ConfigTable.get_entry(name)
  306. ConfigTable.remove_entry name
  307. ent
  308. end
  309. end
  310. #
  311. # File Operations
  312. #
  313. module FileOperations
  314. def mkdir_p(dirname, prefix = nil)
  315. dirname = prefix + dirname if prefix
  316. $stderr.puts "mkdir -p #{dirname}" if verbose?
  317. return if no_harm?
  318. # does not check '/'... it's too abnormal case
  319. dirs = dirname.split(%r<(?=/)>)
  320. if /\A[a-z]:\z/i =~ dirs[0]
  321. disk = dirs.shift
  322. dirs[0] = disk + dirs[0]
  323. end
  324. dirs.each_index do |idx|
  325. path = dirs[0..idx].join('')
  326. Dir.mkdir path unless File.dir?(path)
  327. end
  328. end
  329. def rm_f(fname)
  330. $stderr.puts "rm -f #{fname}" if verbose?
  331. return if no_harm?
  332. if File.exist?(fname) or File.symlink?(fname)
  333. File.chmod 0777, fname
  334. File.unlink fname
  335. end
  336. end
  337. def rm_rf(dn)
  338. $stderr.puts "rm -rf #{dn}" if verbose?
  339. return if no_harm?
  340. Dir.chdir dn
  341. Dir.foreach('.') do |fn|
  342. next if fn == '.'
  343. next if fn == '..'
  344. if File.dir?(fn)
  345. verbose_off {
  346. rm_rf fn
  347. }
  348. else
  349. verbose_off {
  350. rm_f fn
  351. }
  352. end
  353. end
  354. Dir.chdir '..'
  355. Dir.rmdir dn
  356. end
  357. def move_file(src, dest)
  358. File.unlink dest if File.exist?(dest)
  359. begin
  360. File.rename src, dest
  361. rescue
  362. File.open(dest, 'wb') {|f| f.write File.binread(src) }
  363. File.chmod File.stat(src).mode, dest
  364. File.unlink src
  365. end
  366. end
  367. def install(from, dest, mode, prefix = nil)
  368. $stderr.puts "install #{from} #{dest}" if verbose?
  369. return if no_harm?
  370. realdest = prefix ? prefix + dest : dest
  371. realdest = File.join(realdest, File.basename(from)) if File.dir?(realdest)
  372. str = File.binread(from)
  373. if diff?(str, realdest)
  374. verbose_off {
  375. rm_f realdest if File.exist?(realdest)
  376. }
  377. File.open(realdest, 'wb') {|f|
  378. f.write str
  379. }
  380. File.chmod mode, realdest
  381. File.open("#{objdir_root()}/InstalledFiles", 'a') {|f|
  382. if prefix
  383. f.puts realdest.sub(prefix, '')
  384. else
  385. f.puts realdest
  386. end
  387. }
  388. end
  389. end
  390. def diff?(new_content, path)
  391. return true unless File.exist?(path)
  392. new_content != File.binread(path)
  393. end
  394. def command(str)
  395. $stderr.puts str if verbose?
  396. system str or raise RuntimeError, "'system #{str}' failed"
  397. end
  398. def ruby(str)
  399. command config('ruby-prog') + ' ' + str
  400. end
  401. def make(task = '')
  402. command config('make-prog') + ' ' + task
  403. end
  404. def extdir?(dir)
  405. File.exist?(dir + '/MANIFEST') or File.exist?("#{dir}/extconf.rb")
  406. end
  407. def all_files_in(dirname)
  408. Dir.open(dirname) {|d|
  409. return d.select {|ent| File.file?("#{dirname}/#{ent}") }
  410. }
  411. end
  412. REJECT_DIRS = %w(
  413. CVS SCCS RCS CVS.adm .svn
  414. )
  415. def all_dirs_in(dirname)
  416. Dir.open(dirname) {|d|
  417. return d.select {|n| File.dir?("#{dirname}/#{n}") } - %w(. ..) - REJECT_DIRS
  418. }
  419. end
  420. end
  421. #
  422. # Main Installer
  423. #
  424. class InstallError < StandardError; end
  425. module HookUtils
  426. def run_hook(name)
  427. try_run_hook "#{curr_srcdir()}/#{name}" or
  428. try_run_hook "#{curr_srcdir()}/#{name}.rb"
  429. end
  430. def try_run_hook(fname)
  431. return false unless File.file?(fname)
  432. begin
  433. instance_eval File.read(fname), fname, 1
  434. rescue
  435. raise InstallError, "hook #{fname} failed:\n" + $!.message
  436. end
  437. true
  438. end
  439. end
  440. module HookScriptAPI
  441. def get_config(key)
  442. @config[key]
  443. end
  444. alias config get_config
  445. def set_config(key, val)
  446. @config[key] = val
  447. end
  448. #
  449. # srcdir/objdir (works only in the package directory)
  450. #
  451. #abstract srcdir_root
  452. #abstract objdir_root
  453. #abstract relpath
  454. def curr_srcdir
  455. "#{srcdir_root()}/#{relpath()}"
  456. end
  457. def curr_objdir
  458. "#{objdir_root()}/#{relpath()}"
  459. end
  460. def srcfile(path)
  461. "#{curr_srcdir()}/#{path}"
  462. end
  463. def srcexist?(path)
  464. File.exist?(srcfile(path))
  465. end
  466. def srcdirectory?(path)
  467. File.dir?(srcfile(path))
  468. end
  469. def srcfile?(path)
  470. File.file? srcfile(path)
  471. end
  472. def srcentries(path = '.')
  473. Dir.open("#{curr_srcdir()}/#{path}") {|d|
  474. return d.to_a - %w(. ..)
  475. }
  476. end
  477. def srcfiles(path = '.')
  478. srcentries(path).select {|fname|
  479. File.file?(File.join(curr_srcdir(), path, fname))
  480. }
  481. end
  482. def srcdirectories(path = '.')
  483. srcentries(path).select {|fname|
  484. File.dir?(File.join(curr_srcdir(), path, fname))
  485. }
  486. end
  487. end
  488. class ToplevelInstaller
  489. Version = '3.3.0'
  490. Copyright = 'Copyright (c) 2000-2004 Minero Aoki'
  491. TASKS = [
  492. [ 'all', 'do config, setup, then install' ],
  493. [ 'config', 'saves your configurations' ],
  494. [ 'show', 'shows current configuration' ],
  495. [ 'setup', 'compiles ruby extentions and others' ],
  496. [ 'install', 'installs files' ],
  497. [ 'clean', "does `make clean' for each extention" ],
  498. [ 'distclean',"does `make distclean' for each extention" ]
  499. ]
  500. def ToplevelInstaller.invoke
  501. instance().invoke
  502. end
  503. @singleton = nil
  504. def ToplevelInstaller.instance
  505. @singleton ||= new(File.dirname($0))
  506. @singleton
  507. end
  508. include MetaConfigAPI
  509. def initialize(ardir_root)
  510. @config = nil
  511. @options = { 'verbose' => true }
  512. @ardir = File.expand_path(ardir_root)
  513. end
  514. def inspect
  515. "#<#{self.class} #{__id__()}>"
  516. end
  517. def invoke
  518. run_metaconfigs
  519. case task = parsearg_global()
  520. when nil, 'all'
  521. @config = load_config('config')
  522. parsearg_config
  523. init_installers
  524. exec_config
  525. exec_setup
  526. exec_install
  527. else
  528. @config = load_config(task)
  529. __send__ "parsearg_#{task}"
  530. init_installers
  531. __send__ "exec_#{task}"
  532. end
  533. end
  534. def run_metaconfigs
  535. eval_file_ifexist "#{@ardir}/metaconfig"
  536. end
  537. def load_config(task)
  538. case task
  539. when 'config'
  540. ConfigTable.new
  541. when 'clean', 'distclean'
  542. if File.exist?(ConfigTable::SAVE_FILE)
  543. then ConfigTable.load
  544. else ConfigTable.new
  545. end
  546. else
  547. ConfigTable.load
  548. end
  549. end
  550. def init_installers
  551. @installer = Installer.new(@config, @options, @ardir, File.expand_path('.'))
  552. end
  553. #
  554. # Hook Script API bases
  555. #
  556. def srcdir_root
  557. @ardir
  558. end
  559. def objdir_root
  560. '.'
  561. end
  562. def relpath
  563. '.'
  564. end
  565. #
  566. # Option Parsing
  567. #
  568. def parsearg_global
  569. valid_task = /\A(?:#{TASKS.map {|task,desc| task }.join '|'})\z/
  570. while arg = ARGV.shift
  571. case arg
  572. when /\A\w+\z/
  573. raise InstallError, "invalid task: #{arg}" unless valid_task =~ arg
  574. return arg
  575. when '-q', '--quiet'
  576. @options['verbose'] = false
  577. when '--verbose'
  578. @options['verbose'] = true
  579. when '-h', '--help'
  580. print_usage $stdout
  581. exit 0
  582. when '-v', '--version'
  583. puts "#{File.basename($0)} version #{Version}"
  584. exit 0
  585. when '--copyright'
  586. puts Copyright
  587. exit 0
  588. else
  589. raise InstallError, "unknown global option '#{arg}'"
  590. end
  591. end
  592. nil
  593. end
  594. def parsearg_no_options
  595. raise InstallError, "#{task}: unknown options: #{ARGV.join ' '}"\
  596. unless ARGV.empty?
  597. end
  598. alias parsearg_show parsearg_no_options
  599. alias parsearg_setup parsearg_no_options
  600. alias parsearg_clean parsearg_no_options
  601. alias parsearg_distclean parsearg_no_options
  602. def parsearg_config
  603. re = /\A--(#{ConfigTable.keys.join '|'})(?:=(.*))?\z/
  604. @options['config-opt'] = []
  605. while i = ARGV.shift
  606. if /\A--?\z/ =~ i
  607. @options['config-opt'] = ARGV.dup
  608. break
  609. end
  610. m = re.match(i) or raise InstallError, "config: unknown option #{i}"
  611. name, value = m.to_a[1,2]
  612. if value
  613. if ConfigTable.bool_config?(name)
  614. raise InstallError, "config: --#{name} allows only yes/no for argument"\
  615. unless /\A(y(es)?|n(o)?|t(rue)?|f(alse))\z/i =~ value
  616. value = (/\Ay(es)?|\At(rue)/i =~ value) ? 'yes' : 'no'
  617. end
  618. else
  619. raise InstallError, "config: --#{name} requires argument"\
  620. unless ConfigTable.bool_config?(name)
  621. value = 'yes'
  622. end
  623. @config[name] = value
  624. end
  625. end
  626. def parsearg_install
  627. @options['no-harm'] = false
  628. @options['install-prefix'] = ''
  629. while a = ARGV.shift
  630. case a
  631. when /\A--no-harm\z/
  632. @options['no-harm'] = true
  633. when /\A--prefix=(.*)\z/
  634. path = $1
  635. path = File.expand_path(path) unless path[0,1] == '/'
  636. @options['install-prefix'] = path
  637. else
  638. raise InstallError, "install: unknown option #{a}"
  639. end
  640. end
  641. end
  642. def print_usage(out)
  643. out.puts 'Typical Installation Procedure:'
  644. out.puts " $ ruby #{File.basename $0} config"
  645. out.puts " $ ruby #{File.basename $0} setup"
  646. out.puts " # ruby #{File.basename $0} install (may require root privilege)"
  647. out.puts
  648. out.puts 'Detailed Usage:'
  649. out.puts " ruby #{File.basename $0} <global option>"
  650. out.puts " ruby #{File.basename $0} [<global options>] <task> [<task options>]"
  651. fmt = " %-20s %s\n"
  652. out.puts
  653. out.puts 'Global options:'
  654. out.printf fmt, '-q,--quiet', 'suppress message outputs'
  655. out.printf fmt, ' --verbose', 'output messages verbosely'
  656. out.printf fmt, '-h,--help', 'print this message'
  657. out.printf fmt, '-v,--version', 'print version and quit'
  658. out.printf fmt, ' --copyright', 'print copyright and quit'
  659. out.puts
  660. out.puts 'Tasks:'
  661. TASKS.each do |name, desc|
  662. out.printf " %-10s %s\n", name, desc
  663. end
  664. out.puts
  665. out.puts 'Options for CONFIG or ALL:'
  666. ConfigTable.each_definition do |name, (default, arg, desc, default2)|
  667. out.printf " %-20s %s [%s]\n",
  668. '--'+ name + (ConfigTable.bool_config?(name) ? '' : '='+arg),
  669. desc,
  670. default2 || default
  671. end
  672. out.printf " %-20s %s [%s]\n",
  673. '--rbconfig=path', 'your rbconfig.rb to load', "running ruby's"
  674. out.puts
  675. out.puts 'Options for INSTALL:'
  676. out.printf " %-20s %s [%s]\n",
  677. '--no-harm', 'only display what to do if given', 'off'
  678. out.printf " %-20s %s [%s]\n",
  679. '--prefix', 'install path prefix', '$prefix'
  680. out.puts
  681. end
  682. #
  683. # Task Handlers
  684. #
  685. def exec_config
  686. @installer.exec_config
  687. @config.save # must be final
  688. end
  689. def exec_setup
  690. @installer.exec_setup
  691. end
  692. def exec_install
  693. @installer.exec_install
  694. end
  695. def exec_show
  696. ConfigTable.each_name do |k|
  697. v = @config.get_raw(k)
  698. if not v or v.empty?
  699. v = '(not specified)'
  700. end
  701. printf "%-10s %s\n", k, v
  702. end
  703. end
  704. def exec_clean
  705. @installer.exec_clean
  706. end
  707. def exec_distclean
  708. @installer.exec_distclean
  709. end
  710. end
  711. class ToplevelInstallerMulti < ToplevelInstaller
  712. include HookUtils
  713. include HookScriptAPI
  714. include FileOperations
  715. def initialize(ardir)
  716. super
  717. @packages = all_dirs_in("#{@ardir}/packages")
  718. raise 'no package exists' if @packages.empty?
  719. end
  720. def run_metaconfigs
  721. eval_file_ifexist "#{@ardir}/metaconfig"
  722. @packages.each do |name|
  723. eval_file_ifexist "#{@ardir}/packages/#{name}/metaconfig"
  724. end
  725. end
  726. def init_installers
  727. @installers = {}
  728. @packages.each do |pack|
  729. @installers[pack] = Installer.new(@config, @options,
  730. "#{@ardir}/packages/#{pack}",
  731. "packages/#{pack}")
  732. end
  733. with = extract_selection(config('with'))
  734. without = extract_selection(config('without'))
  735. @selected = @installers.keys.select {|name|
  736. (with.empty? or with.include?(name)) \
  737. and not without.include?(name)
  738. }
  739. end
  740. def extract_selection(list)
  741. a = list.split(/,/)
  742. a.each do |name|
  743. raise InstallError, "no such package: #{name}" \
  744. unless @installers.key?(name)
  745. end
  746. a
  747. end
  748. def print_usage(f)
  749. super
  750. f.puts 'Inluded packages:'
  751. f.puts ' ' + @packages.sort.join(' ')
  752. f.puts
  753. end
  754. #
  755. # multi-package metaconfig API
  756. #
  757. attr_reader :packages
  758. def declare_packages(list)
  759. raise 'package list is empty' if list.empty?
  760. list.each do |name|
  761. raise "directory packages/#{name} does not exist"\
  762. unless File.dir?("#{@ardir}/packages/#{name}")
  763. end
  764. @packages = list
  765. end
  766. #
  767. # Task Handlers
  768. #
  769. def exec_config
  770. run_hook 'pre-config'
  771. each_selected_installers {|inst| inst.exec_config }
  772. run_hook 'post-config'
  773. @config.save # must be final
  774. end
  775. def exec_setup
  776. run_hook 'pre-setup'
  777. each_selected_installers {|inst| inst.exec_setup }
  778. run_hook 'post-setup'
  779. end
  780. def exec_install
  781. run_hook 'pre-install'
  782. each_selected_installers {|inst| inst.exec_install }
  783. run_hook 'post-install'
  784. end
  785. def exec_clean
  786. rm_f ConfigTable::SAVE_FILE
  787. run_hook 'pre-clean'
  788. each_selected_installers {|inst| inst.exec_clean }
  789. run_hook 'post-clean'
  790. end
  791. def exec_distclean
  792. rm_f ConfigTable::SAVE_FILE
  793. run_hook 'pre-distclean'
  794. each_selected_installers {|inst| inst.exec_distclean }
  795. run_hook 'post-distclean'
  796. end
  797. #
  798. # lib
  799. #
  800. def each_selected_installers
  801. Dir.mkdir 'packages' unless File.dir?('packages')
  802. @selected.each do |pack|
  803. $stderr.puts "Processing the package `#{pack}' ..." if @options['verbose']
  804. Dir.mkdir "packages/#{pack}" unless File.dir?("packages/#{pack}")
  805. Dir.chdir "packages/#{pack}"
  806. yield @installers[pack]
  807. Dir.chdir '../..'
  808. end
  809. end
  810. def verbose?
  811. @options['verbose']
  812. end
  813. def no_harm?
  814. @options['no-harm']
  815. end
  816. end
  817. class Installer
  818. FILETYPES = %w( bin lib ext data )
  819. include HookScriptAPI
  820. include HookUtils
  821. include FileOperations
  822. def initialize(config, opt, srcroot, objroot)
  823. @config = config
  824. @options = opt
  825. @srcdir = File.expand_path(srcroot)
  826. @objdir = File.expand_path(objroot)
  827. @currdir = '.'
  828. end
  829. def inspect
  830. "#<#{self.class} #{File.basename(@srcdir)}>"
  831. end
  832. #
  833. # Hook Script API bases
  834. #
  835. def srcdir_root
  836. @srcdir
  837. end
  838. def objdir_root
  839. @objdir
  840. end
  841. def relpath
  842. @currdir
  843. end
  844. #
  845. # configs/options
  846. #
  847. def no_harm?
  848. @options['no-harm']
  849. end
  850. def verbose?
  851. @options['verbose']
  852. end
  853. def verbose_off
  854. begin
  855. save, @options['verbose'] = @options['verbose'], false
  856. yield
  857. ensure
  858. @options['verbose'] = save
  859. end
  860. end
  861. #
  862. # TASK config
  863. #
  864. def exec_config
  865. exec_task_traverse 'config'
  866. end
  867. def config_dir_bin(rel)
  868. end
  869. def config_dir_lib(rel)
  870. end
  871. def config_dir_ext(rel)
  872. extconf if extdir?(curr_srcdir())
  873. end
  874. def extconf
  875. opt = @options['config-opt'].join(' ')
  876. command "#{config('ruby-prog')} #{curr_srcdir()}/extconf.rb #{opt}"
  877. end
  878. def config_dir_data(rel)
  879. end
  880. #
  881. # TASK setup
  882. #
  883. def exec_setup
  884. exec_task_traverse 'setup'
  885. end
  886. def setup_dir_bin(rel)
  887. all_files_in(curr_srcdir()).each do |fname|
  888. adjust_shebang "#{curr_srcdir()}/#{fname}"
  889. end
  890. end
  891. def adjust_shebang(path)
  892. return if no_harm?
  893. tmpfile = File.basename(path) + '.tmp'
  894. begin
  895. File.open(path, 'rb') {|r|
  896. File.open(tmpfile, 'wb') {|w|
  897. first = r.gets
  898. return unless should_modify_shebang?(first)
  899. $stderr.puts "adjusting shebang: #{File.basename(path)}" if verbose?
  900. w.print first.sub(SHEBANG_RE, '#!' + config('ruby-path'))
  901. w.write r.read
  902. }
  903. }
  904. move_file tmpfile, File.basename(path)
  905. ensure
  906. File.unlink tmpfile if File.exist?(tmpfile)
  907. end
  908. end
  909. def should_modify_shebang?(line)
  910. File.basename(config('ruby-path')) == 'ruby' or
  911. shebang_command(line) == 'ruby'
  912. end
  913. def shebang_command(line)
  914. cmd, arg = *line.sub(/\A\#!/, '').strip.split(/\s+/, 2)
  915. cmd
  916. end
  917. def setup_dir_lib(rel)
  918. end
  919. def setup_dir_ext(rel)
  920. make if extdir?(curr_srcdir())
  921. end
  922. def setup_dir_data(rel)
  923. end
  924. #
  925. # TASK install
  926. #
  927. def exec_install
  928. exec_task_traverse 'install'
  929. end
  930. def install_dir_bin(rel)
  931. install_files collect_filenames_auto(), "#{config('bin-dir')}/#{rel}", 0755
  932. end
  933. def install_dir_lib(rel)
  934. install_files ruby_scripts(), "#{config('rb-dir')}/#{rel}", 0644
  935. end
  936. def install_dir_ext(rel)
  937. return unless extdir?(curr_srcdir())
  938. install_files ruby_extentions('.'),
  939. "#{config('so-dir')}/#{rel}",
  940. 0555
  941. end
  942. def install_dir_data(rel)
  943. install_files collect_filenames_auto(), "#{config('data-dir')}/#{rel}", 0644
  944. end
  945. def install_files(list, dest, mode)
  946. mkdir_p dest, @options['install-prefix']
  947. list.each do |fname|
  948. install fname, dest, mode, @options['install-prefix']
  949. end
  950. end
  951. def ruby_scripts
  952. collect_filenames_auto().select {|n| /\.rb\z/ =~ n }
  953. end
  954. # picked up many entries from cvs-1.11.1/src/ignore.c
  955. reject_patterns = %w(
  956. core RCSLOG tags TAGS .make.state
  957. .nse_depinfo #* .#* cvslog.* ,* .del-* *.olb
  958. *~ *.old *.bak *.BAK *.orig *.rej _$* *$
  959. *.org *.in .*
  960. )
  961. mapping = {
  962. '.' => '\.',
  963. '$' => '\$',
  964. '#' => '\#',
  965. '*' => '.*'
  966. }
  967. REJECT_PATTERNS = Regexp.new('\A(?:' +
  968. reject_patterns.map {|pat|
  969. pat.gsub(/[\.\$\#\*]/) {|ch| mapping[ch] }
  970. }.join('|') +
  971. ')\z')
  972. def collect_filenames_auto
  973. mapdir((existfiles() - hookfiles()).reject {|fname|
  974. REJECT_PATTERNS =~ fname
  975. })
  976. end
  977. def existfiles
  978. all_files_in(curr_srcdir()) | all_files_in('.')
  979. end
  980. def hookfiles
  981. %w( pre-%s post-%s pre-%s.rb post-%s.rb ).map {|fmt|
  982. %w( config setup install clean ).map {|t| sprintf(fmt, t) }
  983. }.flatten
  984. end
  985. def mapdir(filelist)
  986. filelist.map {|fname|
  987. if File.exist?(fname) # objdir
  988. fname
  989. else # srcdir
  990. File.join(curr_srcdir(), fname)
  991. end
  992. }
  993. end
  994. def ruby_extentions(dir)
  995. _ruby_extentions(dir) or
  996. raise InstallError, "no ruby extention exists: 'ruby #{$0} setup' first"
  997. end
  998. DLEXT = /\.#{ ::Config::CONFIG['DLEXT'] }\z/
  999. def _ruby_extentions(dir)
  1000. Dir.open(dir) {|d|
  1001. return d.select {|fname| DLEXT =~ fname }
  1002. }
  1003. end
  1004. #
  1005. # TASK clean
  1006. #
  1007. def exec_clean
  1008. exec_task_traverse 'clean'
  1009. rm_f ConfigTable::SAVE_FILE
  1010. rm_f 'InstalledFiles'
  1011. end
  1012. def clean_dir_bin(rel)
  1013. end
  1014. def clean_dir_lib(rel)
  1015. end
  1016. def clean_dir_ext(rel)
  1017. return unless extdir?(curr_srcdir())
  1018. make 'clean' if File.file?('Makefile')
  1019. end
  1020. def clean_dir_data(rel)
  1021. end
  1022. #
  1023. # TASK distclean
  1024. #
  1025. def exec_distclean
  1026. exec_task_traverse 'distclean'
  1027. rm_f ConfigTable::SAVE_FILE
  1028. rm_f 'InstalledFiles'
  1029. end
  1030. def distclean_dir_bin(rel)
  1031. end
  1032. def distclean_dir_lib(rel)
  1033. end
  1034. def distclean_dir_ext(rel)
  1035. return unless extdir?(curr_srcdir())
  1036. make 'distclean' if File.file?('Makefile')
  1037. end
  1038. #
  1039. # lib
  1040. #
  1041. def exec_task_traverse(task)
  1042. run_hook "pre-#{task}"
  1043. FILETYPES.each do |type|
  1044. if config('without-ext') == 'yes' and type == 'ext'
  1045. $stderr.puts 'skipping ext/* by user option' if verbose?
  1046. next
  1047. end
  1048. traverse task, type, "#{task}_dir_#{type}"
  1049. end
  1050. run_hook "post-#{task}"
  1051. end
  1052. def traverse(task, rel, mid)
  1053. dive_into(rel) {
  1054. run_hook "pre-#{task}"
  1055. __send__ mid, rel.sub(%r[\A.*?(?:/|\z)], '')
  1056. all_dirs_in(curr_srcdir()).each do |d|
  1057. traverse task, "#{rel}/#{d}", mid
  1058. end
  1059. run_hook "post-#{task}"
  1060. }
  1061. end
  1062. def dive_into(rel)
  1063. return unless File.dir?("#{@srcdir}/#{rel}")
  1064. dir = File.basename(rel)
  1065. Dir.mkdir dir unless File.dir?(dir)
  1066. prevdir = Dir.pwd
  1067. Dir.chdir dir
  1068. $stderr.puts '---> ' + rel if verbose?
  1069. @currdir = rel
  1070. yield
  1071. Dir.chdir prevdir
  1072. $stderr.puts '<--- ' + rel if verbose?
  1073. @currdir = File.dirname(rel)
  1074. end
  1075. end
  1076. if $0 == __FILE__
  1077. begin
  1078. if multipackage_install?
  1079. ToplevelInstallerMulti.invoke
  1080. else
  1081. ToplevelInstaller.invoke
  1082. end
  1083. rescue
  1084. raise if $DEBUG
  1085. $stderr.puts $!.message
  1086. $stderr.puts "Try 'ruby #{$0} --help' for detailed usage."
  1087. exit 1
  1088. end
  1089. end