Skip to content

Instantly share code, notes, and snippets.

@nobu
Created November 5, 2009 15:12
Show Gist options
  • Save nobu/227100 to your computer and use it in GitHub Desktop.
Save nobu/227100 to your computer and use it in GitHub Desktop.
ruby-build files
inside-work-tree := $(git rev-parse --is-inside-work-tree)
ifeq ($(inside-work-tree),true)
branches := $(shell git branch -l | sort | sed 's/\*//')
gitdir = .git
backupdir = $(gitdir)
backup = git.tar.bz2
backuparg =
else
gitdir = .
backupdir := $(notdir $(shell pwd))
backup = ../$(backupdir).tar.bz2
backuparg = -C ..
endif
backupopt = --dereference
all: up backup
up: up-remote up-local .force
up-remote: fetch rebase .force
up-local: .force
fetch: .force
git fetch
$(if $(wildcard $(gitdir)/svn),git svn fetch)
rebase: $(addprefix rebase-,$(branches)) .force
define rebasecmd
.PHONY: rebase-$(1)
rebase-$(1): .force
git rebase origin$(if $(filter-out master,$(1)),/$(1)) $(1)
endef
$(foreach branch,$(branches),$(eval $(call rebasecmd,$(branch))))
branches: .force
echo $(branches)
backup: $(backup) .force
$(backup): $(gitdir) gc
tar $(backuparg) $(backupopt) -cjf $@ $(backupdir)
gc: .force
du -s $(gitdir)
git gc --prune
du -s $(gitdir)
.PHONY: .force fetch rebase branch backup gc
.force:
versions := *
override versions := $(patsubst %/,%,$(dir $(wildcard $(addsuffix /GNUmakefile,$(versions)) $(addsuffix /src/GNUmakefile,$(versions)))))
goals := all $(filter-out %/all $(shell sed '/^[a-zA-Z_][-a-zA-Z0-9_]*:/!d;s/:.*//;s:.*:& %/&:' $(MAKEFILE_LIST)),$(MAKECMDGOALS))
unexport versions goals
define dive
$(MAKE) -C $(@D) $(if $(filter --,$(MAKEFLAGS))$(filter $(firstword $(MAKEFLAGS)),$(subst =,,$(firstword $(MAKEFLAGS)))),-)$(MAKEFLAGS) $(@F)
endef
define no-need-install
! { [ -e $(@D)/miniruby ] && $(@D)/miniruby -e 'exit RUBY_REVISION > `ruby-#{RUBY_VERSION} -e "p RUBY_REVISION"`.to_i'; }
endef
$(foreach goal,$(goals),$(eval $(value goal): $$(versions:=/$(value goal))))
$(foreach subdir,$(versions),$(eval $(value subdir)/%:; $$(call dive)))
up: up-remote up-local
$(if $(wildcard .svn),svn up)
#$(foreach subdir,$(versions),$(eval $(value subdir)/up:; svn up --accept postpone $$(@D)))
$(foreach target,remote local,\
$(eval up-$(value target): $(addsuffix /up-$(value target),$(versions)))\
)
$(foreach target,remote local,\
$(foreach subdir,$(versions),\
$(eval $(value subdir)/up-$(value target):; +$(MAKE) -$(MAKEFLAGS) -C $$(@D) UPDATE_PREREQ= up-$(value target))\
))
resolved:
@PWD= resolve-conflict $(versions)
stat:
! svn $@ $(versions) | grep '^C'
edit:
$(EDITOR) `svn stat $(versions) | sed -n 's/^C//p'`
sudo-install: stable/sudo-install trunk/sudo-install
stable/sudo-install trunk/sudo-install:
@$(call no-need-install) || $(call dive)
RUBYOPT =
PWD := $(shell pwd)
define cvs_srcs
$(addprefix $(1),$(shell cut -d/ -f2 $(1)CVS/Entries | grep -e '\.[chy]$$' | sort))
endef
define svn_srcs
$(subst .svn/text-base/,,$(patsubst %.svn-base,%,$(wildcard $(filter-out ./,$(dir $(1))).svn/text-base/$(call or,$(notdir $(1)),*.[chy]).svn-base)))
endef
define git_srcs
$(shell $(GIT) ls-files $(1) $(2) $(3) | grep -v '^ext/')
endef
or = $(if $(1),$(1),$(2))
CVS = cvs -f
SVN = svn
GIT = git
GIT_SVN = $(GIT) svn
svn-up = update
svn-up-options = --accept postpone
GITSVN = git svn
ifneq ($(wildcard .svn/entries),)
UPDATE_REVISION = $(VCS) info $(@D) | \
sed -n \
-e 's,^URL:.*/branches/\([^/]*\)$$,\#define RUBY_BRANCH_NAME "\1",p' \
-e 's/.*Rev:/\#define RUBY_REVISION/p'
VCS = $(SVN)
SRCS := $(call svn_srcs,include/ruby/) $(call svn_srcs,*.[chy]) \
$(call svn_srcs,*.ci) $(call svn_srcs,insns.def) \
$(call svn_srcs,*.def) $(call svn_srcs,missing/) \
$(call svn_srcs,enc/) $(call svn_srcs,win32/)
SRCS := $(wildcard $(SRCS))
else ifneq ($(wildcard .git),)
UPDATE_REVISION = git log -n 1 --grep='^ *git-svn-id:' $(@D) | \
sed -e '$$!d' -e '/ *git-svn-id: */!d' -e 's///' \
-e 's,.*/branches/\([^/]*\)@\([0-9][0-9]*\) .*,\#define RUBY_BRANCH_NAME "\1"/\#define RUBY_REVISION \2,' \
-e 's,.*/trunk@\([0-9][0-9]*\) .*,\#define RUBY_REVISION \1,' | tr / '\012'
ORIGIN_URL := $(shell git config remote.origin.url)
ifeq ($(patsubst /%,/,$(patsubst file:%,%,$(ORIGIN_URL))),/)
UPDATE_PREREQ_LOCAL := update-prereq-local
UPDATE_PREREQ := update-prereq
endif
before-up := $(shell git status --porcelain | sed '/^?/d;s/.*/stash-save/;q')
after-up := $(before-up:-save=-pop)
SRCS := $(call git_srcs,include/ruby/) $(call git_srcs,*.[chy])\
$(call git_srcs,*.ci) $(call git_srcs,insns.def) \
$(call git_srcs,*.def) $(call git_srcs,missing/) \
$(call git_srcs,enc/) $(call git_srcs,win32/)
SRCS := $(wildcard $(SRCS))
ifneq ($(wildcard .git/svn),)
VCS = $(GIT_SVN)
VCSUP = $(VCS) rebase $(gitsvnup-options)
# else ifeq ($(patsubst +%,+,$(shell git config remote.origin.fetch)),+)
#GIT_ORIGIN = $(shell git config remote.origin.url)
#VCS = $(GIT)
#VCSUP = $(MAKE) -C "$(GIT_ORIGIN)" gitup-options=$(gitup-options) up
else
VCS = $(GIT)
VCSUP = $(VCS) pull
endif
else ifneq ($(wildcard CVS/Entries),)
VCS = $(CVS)
SRCS := $(call cvs_srcs) $(call cvs_srcs,missing/) $(call cvs_srcs,win32/)
else
SRCS := $(wildcard *.h $(filter-out parse.c,*.c) parse.y missing/*.[ch] win32/win32.[ch] win32/dir.h)
endif
TESTS ?= $(if $(wildcard .tests),$(shell cat .tests),$(EXTS))
VCSUP ?= $(VCS) $(call or,$(value $(subst ,-,$(VCS))up),up) $(value $(subst ,-,$(VCS))up-options)
nonexec :=
print-database :=
keep-going :=
$(foreach mflags,$(filter-out --%,$(filter -%,$(MFLAGS) $(MAKEFLAGS))),\
$(if $(filter $(subst n,,$(mflags)),$(mflags)),,$(eval nonexec := t))\
$(if $(filter $(subst p,,$(mflags)),$(mflags)),,$(eval print-database := t))\
$(if $(filter $(subst k,,$(mflags)),$(mflags)),,$(eval keep-going := t))\
)
ifeq ($(TERM),dumb)
tty :=
else
tty := $(shell sh -c "test -t 2 && echo tty")
endif
SETTITLE := $(call or,$(if $(tty),$(shell sh -c "type -p settitle"),$(shell echo ': -*- compilation -*-' 1>&2)),:)
define MESSAGE
$(if $(nonexec),,@$(SETTITLE) making $(if $(2),$(2),$@))
$(if $(nonexec),,@echo ")<=== $(1) $(if $(2),$(2),$@) ===>$(if $(nonexec),,")
endef
STARTING = $(call MESSAGE,{{{starting,$(1))
FINISHED = $(call MESSAGE,}}}finished,$(1))
CMDSTARTING = $(if $(filter $@,$(MAKECMDGOALS)),,$(call STARTING,$(1)))
CMDFINISHED = $(if $(filter $@,$(MAKECMDGOALS)),$(call FINISHED,$(1)))
ifeq ($(print-database),)
TOPMAKE = $(MAKE)
else
TOPMAKE =
endif
CMDVARS := $(filter-out subdirs=%,$(-*-command-variables-*- :=))
MFLAGS := $(if $(EXTOUT),EXTOUT=$(EXTOUT)) $(if $(RDOCOUT),RDOCOUT=$(RDOCOUT))
makeflags := $(patsubst -w%,-%,-$(filter-out --%,$(MAKEFLAGS)))
make-default = $(if $(nonexec),,$(make-precommand) $(if $(wildcard $(1)/.env),$(1)/.env -C $(1))) \
$(MAKE) $(if $(if $(nonexec),,$(wildcard $(1)/.env)),,-C $(1))
config-default = cd $(@D); \
sh $(if $(wildcard $@), $(shell sed -n 's/^srcdir=//p' $@),$(PWD))/$(CONFIGURE) \
$(if $(wildcard $@), $(shell sed -n 's/^s,@configure_args@,\(.*\),;t t$$/\1/p' $@))
nmake := $(shell which nmake 2>&1)
bcc32 := $(shell which bcc32 2>&1)
ifneq ($(nmake),)
make-mswin32 = $(if $(nonexec),,@echo $(call make-default,$(1));) cd $(1); unset MAKEFLAGS; \
$(strip exec $(if $(nonexec),,$(make-precommand)) ./.env nmake -l $(makeflags) $(MFLAGS))
#make-mswin32 = nmake -C"$(1)" -l $(filter-out --%,$(MAKEFLAGS)) $(MFLAGS)
configure-mswin32 = win32/Makefile.sub
config-mswin32 = cd $(@D); \
$(if $(wildcard $@), $(shell sed -n 's/^srcdir=//p' $@),$(PWD))/win32/configure.bat
endif
ifneq ($(bcc32),)
make-bccwin32 = $(if $(nonexec),,@echo $(call make-default,$(1));) cd $(1); unset MAKEFLAGS; \
$(strip exec $(if $(nonexec),,$(make-precommand)) ./.env make $(subst k,,$(makeflags)) $(MFLAGS))
#make-bccwin32 = bmake -C"$(1)" $(filter-out --%,$(MAKEFLAGS)) $(MFLAGS)
configure-bccwin32 = bcc32/Makefile.sub
config-bccwin32 = cd $(@D); \
$(if $(wildcard $@), $(shell sed -n 's/^srcdir=//p' $@),$(PWD))/bcc32/configure.bat
endif
common.mk := $(wildcard common.mk)
configure-default = Makefile.in $(common.mk) $(subdir)/config.status
submake = $(strip $(call $(if $(make-$(target)),make-$(target),make-default),$(@D)) $(CMDVARS))
AUTOCONF = autoconf
RM = rm -f
ifndef subdirs
subdirs := $(wildcard */Makefile .[^.]*/Makefile)
subdirs += $(filter-out djgpp/config.status,$(wildcard */config.status .[^.]*/config.status))
else
subdirs := $(wildcard $(subdirs))
endif
subdirs := $(sort $(patsubst %/,%,$(dir $(subdirs))))
ostype = $(word 2,$(subst -, ,$1))
target = $(call ostype,$(@D))
BISON = bison
CONFIGURE_IN := $(wildcard configure.in)
CONFIGURE = $(CONFIGURE_IN:.in=)
PARSE_Y := $(wildcard parse.y)
KEYWORDS := $(call or,$(wildcard defs/keywords),$(wildcard keywords))
LEX_C := $(if $(KEYWORDS),lex.c)
RIPPER := $(if $(wildcard ext/ripper/depend),ripper)
PREREQ = .force $(CONFIGURE) $(PARSE_Y:.y=.c) $(LEX_C) revision.h .revision.time
ifndef RUBY
NATIVEARCH := $(patsubst %/Makefile,%,$(shell grep -l '^PREP *= *miniruby' $(subdirs:=/Makefile) /dev/null))
DEFAULTARCH := $(word 1, $(filter $(ARCH) .$(ARCH),$(NATIVEARCH)) $(NATIVEARCH))
MINIRUBY := $(DEFAULTARCH)/miniruby
export BASERUBY ?= /usr/bin/ruby
export RUBYLIB = .$(if $(EXTOUT),:$(EXTOUT)/common:$(EXTOUT)/$(DEFAULTARCH)):$(PWD)/lib
export RUBY := $(if $(BASERUBY),$(BASERUBY),$(if $(patsubst /%,,$(MINIRUBY)),$(PWD)/$(MINIRUBY) -I $(RUBYLIB),$(MINIRUBY)))
endif
#subdirs := $(filter-out $(DEFAULTARCH),$(subdirs)) $(DEFAULTARCH)
PAGER ?= less
ifeq ($(origin MAKEFILE_LIST),undefined)
MAKEFILE_LIST := $(wildcard GNUmakefile Makefile makefile Makefile.in common.mk $(MAKEFILES))
endif
EXTOUT ?= ../ext
RDOCOUT ?= $(EXTOUT)/rdoc
RBCONFIG ?= ./.rbconfig.time
export EXTOUT RDOCOUT RBCONFIG
INSTALLED_LIST ?= $(EXTOUT)/.installed.list
localgoals := $(shell sed -n '/^[A-Za-z0-9_][-A-Za-z0-9_./]*:[^=]/s/:.*//p' $(MAKEFILE_LIST))
localgoals := $(localgoals) $(shell sed -n '/^[A-Za-z0-9_][-A-Za-z0-9_./]*:$$/s/:.*//p' $(MAKEFILE_LIST))
localgoals := $(sort $(localgoals))
localgoals := $(filter-out all,$(localgoals))
goals := $(filter-out $(localgoals) $(PREREQ),$(call or,$(MAKECMDGOALS),all))
cmdgoals := $(filter-out $(subdirs:=/%),$(goals))
subdir-goals := $(filter $(subdirs:=/%),$(goals))
.pre-%:
$(call STARTING,$*)
.post-%:
$(call FINISHED,$*)
all:
debug:
+$(MAKE) optflags=-O0
$(MINIRUBY): $(PREREQ) $(dir $(MINIRUBY))Makefile .pre-host-miniruby
@$(call SETTITLE,making $(@F) in $(@D))
+$(submake) $(if $(TOPMAKE),,TOPMAKE=$(MAKE)) $(@F)
$(call FINISHED,host-miniruby)
#$(filter-out $(MINIRUBY),$(subdir-goals)): prereq
# @$(call SETTITLE,making $(@F) in $(@D))
# $(submake) $(@F)
#$(addprefix %/,$(cmdgoals)): prereq
# @$(call SETTITLE,making $(@F) in $(@D))
# $(submake) $(filter $(MAKECMDGOALS),$(cmdgoals))
define subdircmd
$(if $(configure-$(call ostype,$(1))),
$(1)/config.status: $(value configure-$(call ostype,$(1))) $(common.mk) $(1)/Makefile
$(1)/Makefile:;
,
$(1)/config.status: $(CONFIGURE)
$(1)/Makefile: $$(configure-default)
cd $$(@D); $(if $(wildcard $(1)/.env),./.env) sh config.status
)
$(1)/config.status: make-precommand += time
$(1)/config.status:
@$$(SETTITLE) making $$(@F) in $$(@D)
-$$(submake) TOPMAKE=$(value TOPMAKE) $$(@F)
$(1)/%: prereq .force
@$$(SETTITLE) making $$(@F) in $$(@D)
+$$(submake) TOPMAKE=$(value TOPMAKE) $$(mflags) $$(@F)
endef
$(foreach subdir,$(subdirs),$(eval $(call subdircmd,$(subdir))))
phony-filter := TAGS builtpack% $(shell grep -e ^incs: -e ^srcs: -e ^change: common.mk | sed s/:.*$$//)
phony-filter += $(shell sed '/\.force$$/!d;/^[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]:/!d;s/:.*//' $(MAKEFILE_LIST))
prereq-filter = prereq .pre-prereq $(PREREQ) $(RIPPER) config Makefile $(MINIRUBY) $(phony-filter)
subdir-filter = $(subdirs:=/%) $(localgoals) $(PREREQ)
$(foreach goal,all $(filter-out $(prereq-filter),$(MAKECMDGOALS)),$(eval $(value goal): prereq))
$(foreach goal,all $(filter-out $(subdirs:=/%) $(phony-filter),$(MAKECMDGOALS)),$(eval $(value goal): .pre-$(value goal)))
$(foreach goal,all $(filter-out $(subdir-filter) $(phony-filter),$(MAKECMDGOALS)),$(eval $(value goal): $$(subdirs:=/$(value goal))))
$(foreach goal,$(filter-out $(phony-filter),$(cmdgoals)),$(eval $(value goal):\; $$(FINISHED)))
prereq: .pre-prereq .do-prereq $(PREREQ) config Makefile $(RIPPER) .post-prereq
@-sync
prereq: MAKEFLAGS += --no-print-directory
resolved:
@PWD= resolve-conflict
#miniruby:;
Makefiles: Makefile
Makefile: .pre-Makefile $(subdirs:=/Makefile) .post-Makefile
config: .pre-config $(subdirs:=/config.status) .post-config
rbconfig: prereq .pre-rbconfig $(subdirs:=/$(RBCONFIG:./%=%)) .post-rbconfig
%.c: %.y
+{ sed '/^@/d' Makefile.in; sed 's/{[.;]*$$([a-zA-Z0-9_]*)}//g' common.mk; } | \
$(MAKE) -f - srcdir=. CHDIR=cd VPATH=include/ruby YACC="$(BISON) -y" YFLAGS="$(YFLAGS)" $@
$(CMDFINISHED)
configure: configure.in
+$(AUTOCONF)
prereq-targets := $(shell grep -e '^prereq:' -e '/revision\.h:' -e '^change:' common.mk | \
sed -e 's/:.*//;s/^/.do-/;s,.*/,,')
ifneq ($(prereq-targets),)
$(prereq-targets):
@{ sed 's/@[A-Z][A-Z_0-9]*@//g' Makefile.in; sed 's/{[.;]*$$([a-zA-Z0-9_]*)}//g' common.mk; } | \
$(MAKE) -f - srcdir=. VPATH=include/ruby MKFILES="" PREP="" WORKDIRS="" \
CHDIR=cd MAKEDIRS='mkdir -p' BASERUBY="$(RUBY)" MINIRUBY="$(RUBY)" RUBY="$(RUBY)" RBCONFIG="" \
ENC_MK=.top-enc.mk REVISION_FORCE=PHONY PROGRAM="" VCSUP="$(VCSUP)" VCS="$(VCS)" \
$(filter-out prereq,$(patsubst .do-%,%,$@)) \
$(if $(filter-out revision.h,$@),prereq)
endif
.do-up:
env LC_TIME=C $(VCSUP)
$(if $(filter revision.h,$(prereq-targets)),,-@$(RM) revision.h)
stash-save:
$(GIT) stash save
stash-pop:
$(GIT) stash pop
up: up-remote up-local .force
.do-up-remote: $(before-up) .do-up $(after-up) .force
up-remote: $(UPDATE_PREREQ_LOCAL) .do-up-remote .force
up-local: prereq .force
.do-prereq:
host-miniruby: $(MINIRUBY)
lex.c: $(KEYWORDS)
ripper_hdrdir = $(if $(wildcard include/ruby/ruby.h),top_srcdir,hdrdir)
ripper: .force
$(CMDSTARTING)
$(if $(TOPMAKE),$(MAKE),$(MAKE)) -C ext/ripper -f depend $(ripper_hdrdir)=../.. srcdir=. RUBY="$(RUBY)"
$(FINISHED)
revision.h: .force
.revision.time: .force
ifeq ($(filter revision.h,$(prereq-targets)),)
revision.h:
@LC_MESSAGES=C $(UPDATE_REVISION) > "[email protected]"
@if test -f "$@" -a -s "[email protected]" && diff -u "$@" "[email protected]" > /dev/null 2>&1; then \
rm -f "[email protected]"; \
else \
mv -f "[email protected]" "$@"; \
fi
touch .revision.time
# @! fgrep revision.h version.h > /dev/null || $(BASERUBY) tool/revup.rb
endif
help: .force
@$(MAKE) -f common.mk $@
update-prereq: .force
$(MAKE) -C $(patsubst file:%,%,$(ORIGIN_URL)) up
update-prereq-local: $(UPDATE_PREREQ) .force
up: revision.h .force
UP: .force
@echo $(VCSUP) $(UPS); \
while $(VCSUP) $(UPS) | tee makeup.log | $(PAGER) +/^C; \
grep ^C makeup.log; do \
sleep 1; \
done; \
rm -f makeup.log
tags: TAGS .force
TAGS: $(SRCS)
etags -lc $(wildcard $(patsubst template/%.tmpl,%,$(SRCS)))
sudo-install:
ifneq ($(wildcard $(EXTOUT)),)
sudo $(MAKE) prereq-targets= install
endif
install-prereq: .force
@exit > $(INSTALLED_LIST)
# @MAKE=$(MAKE) touch install-prereq
# @rm -f install-prereq
install: install-nodoc
install-nodoc: install-comm install-arch
install-arch install-comm: mflags := \
INSTALLED_LIST='$(EXTOUT)/.installed.list' \
CLEAR_INSTALLED_LIST=PHONY
install-arch install-comm:
install-arch: install-prereq $(subdirs:=/install-arch)
install-comm: install-prereq $(DEFAULTARCH)/install-comm
install-local: install-bin install-lib install-man
install-bin: install-prereq $(subdirs:=/install-bin)
install-lib: install-prereq $(DEFAULTARCH)/install-lib
install-man: install-prereq $(DEFAULTARCH)/install-man
install-ext: install-ext-arch install-ext-comm
install-ext-arch: install-prereq $(subdirs:=/install-ext-arch)
install-ext-comm: install-prereq $(DEFAULTARCH)/install-lib
install-doc: $(DEFAULTARCH)/install-doc
install-rdoc: $(DEFAULTARCH)/install-rdoc
pre-install-local: $(subdirs:=/pre-install-local)
post-install-local: $(subdirs:=/post-install-local)
pre-install-ext: $(subdirs:=/pre-install-ext)
post-install-ext: $(subdirs:=/post-install-ext)
check: prereq .pre-check test test-all .post-check .force
test: prereq .pre-test $(subdirs:=/test) .post-test .force
test-all: prereq .pre-test-all $(subdirs:=/test-all) .post-test-all .force
test test-all:; sync
try: $(DEFAULTARCH)/miniruby try.rb
$(DEFAULTARCH)/miniruby try.rb
ifeq ($(tty),)
oneline = echo "$(2)"; exec $(1)
else
oneline = exec $(1) 2>&1 | oneline -p"$(2): " -e"$(3)"
endif
transsrcs := $(addprefix src/,$(filter-out enc/trans/transdb.c,$(wildcard enc/trans/*.c)))
builttargets := $(addprefix builtpack-,$(subdirs))
builtpack: .pre-builtpack builtpack-common $(builttargets) .post-builtpack
#builtdirs := $(notdir $(foreach d,$(subdirs),$(shell readlink $(value d))))
#$(addprefix builtpack-,common $(builtdirs))::; @echo $(patsubst builtpack-%,packing %,$@)
builtpack-%: dir = $(patsubst builtpack-.%,.%,$@)
builtpack-%: real = $(notdir $(shell readlink $(dir)))
builtpack-%: arch = $(shell sed -n '/CONFIG\["arch"\]/s/.*= "\(.*\)"$$/\1/p' "$(dir)/rbconfig.rb")
builtpack-common:
@$(call oneline,tar -C .. -c$(if $(tty),v)jf ../built-common.tar.bz2 \
$(transsrcs) ext/common/ $(patsubst ../%,%,$(wildcard $(RDOCOUT))) \
ext/include/ruby,packing common,done)
$(builttargets):
@$(if $(arch),\
$(call oneline,tar -C .. -c$(if $(tty),v)jf ../built-$(real).tar.bz2 ext/$(arch) ext/include/$(arch) \
$(real),packing $(dir),done),\
echo '$(dir) is not built')
.PHONY: .force
.force:
#!/usr/bin/ruby
command = []
while arg = ARGV[0]
break ARGV.shift if arg == '--'
/\A--([-\w]+)(?:=(.*))?\z/ =~ arg or break
arg, value = $1, $2
re = Regexp.new('\A'+arg.gsub(/\w+\b/, '\&\\w*')+'\z', "i")
case
when re =~ "debug"
debug = true
when re =~ "srcdir"
srcdir = value
when re =~ "archdir"
archdir = value
when re =~ "cpu"
command.concat(%w[arch -arch]) << value
when re =~ "extout"
extout = value
when re =~ "precommand"
require 'shellwords'
command.concat(Shellwords.shellwords(value))
when re =~ "gdb"
command.unshift("gdb", "--args")
when re =~ "rubyopt"
rubyopt = value
when re =~ "print-libraries"
print_libraries = true
when re =~ "version" && value
unless File.directory?(dir = File.join(srcdir || File.dirname(__FILE__), value))
abort "#{$0}: version #{value} not found"
end
version = value
srcdir = dir
else
break
end
ARGV.shift
end
srcdir ||= File.dirname(__FILE__)
rbconf = "rbconfig.rb"
arch = ENV["ARCH"] || RUBY_PLATFORM.sub(/^universal\./, '')
i386 = ("i3#{$1}" if /^i[4-9](86-.*)/ =~ arch)
universal = ("universal-darwin" if /darwin/ =~ arch)
begin
abs_archdir = (File.expand_path(archdir, srcdir) if archdir)
if abs_archdir and File.file?(conffile = File.join(abs_archdir, rbconf))
config = File.read(conffile)
elsif File.file?(conffile = File.join(abs_archdir = File.join(srcdir, arch), rbconf))
config = File.read(conffile)
elsif i386 and File.file?(conffile = File.join(abs_archdir = File.join(srcdir, i386), rbconf))
config = File.read(conffile)
elsif universal and File.file?(conffile = File.join(abs_archdir = File.join(srcdir, universal), rbconf))
config = File.read(conffile)
elsif File.file?(conffile = File.join(abs_archdir = File.join(srcdir, "."+arch), rbconf))
config = File.read(conffile)
elsif i386 and File.file?(conffile = File.join(abs_archdir = File.join(srcdir, "."+i386), rbconf))
config = File.read(conffile)
elsif universal and File.file?(conffile = File.join(abs_archdir = File.join(srcdir, "."+universal), rbconf))
config = File.read(conffile)
elsif !version
srcdir = File.join(srcdir, version = "trunk")
redo
else
abort "archdir not defined"
end
end while false
config.sub!(/^(\s*)RUBY_VERSION\s*==.*(\sor\s*)$/, '\1true\2')
config = Module.new {module_eval(config, conffile)}::Config::CONFIG
if /cygwin/ =~ RUBY_PLATFORM and /cygwin/ !~ config["target_os"]
def File.extern_path(path)
path = IO.popen("-") {|f| f || exec("realpath", path); f.read}.chomp
IO.popen("-") {|f| f || exec("cygpath", "-ma", path); f.read}.chomp
end
else
class << File
alias extern_path expand_path
end
end
begin
require 'pathname'
abs_archdir = Pathname.new(abs_archdir).realpath.to_s
rescue Errno::EINVAL
abs_archdir = File.extern_path(abs_archdir)
end
ruby = File.basename(__FILE__).sub(/ruby/, config['ruby_install_name'])
ruby = File.expand_path(ruby+config['EXEEXT'], abs_archdir)
File.exist?(ruby) or abort "#{ruby} is not found."
libs = [abs_archdir]
if extout
abs_extout = File.extern_path(extout)
elsif extout = config["EXTOUT"]
abs_extout = File.join(abs_archdir, extout)
end
if abs_extout
if File.directory?(common = File.join(abs_extout, "common"))
libs << common
end
libs << File.join(abs_extout, config["arch"])
end
if e = ENV["RUBYOPT"]
dlext = "." + config["DLEXT"]
e.scan(/(?:\A|\s)r(\S+)/) do |lib,|
libs << lib if lib = $:.find do |d|
d = File.join(d, lib)
File.file?(d + ".rb") or File.file?(d + dlext)
end
end
end
if File.directory?(lib = File.expand_path("lib", srcdir)) or
File.directory?(lib = File.expand_path("src/lib", srcdir))
libs << lib
end
ENV["RUBY"] = File.extern_path(ruby)
ENV["PATH"] = [abs_archdir, ENV["PATH"]].compact.join(File::PATH_SEPARATOR)
ENV["RUBYOPT"] = rubyopt if rubyopt
libs.collect!(&File.method(:extern_path))
libs.uniq!
if e = ENV["RUBYLIB"]
libs << e
end
case config["arch"]
when /mswin|bccwin|mingw|msdosdjgpp|human|os2/
pathsep = ";"
when /riscos/
pathsep = ","
else
pathsep = ":"
end
ENV["RUBYLIB"] = libs.join(pathsep)
libruby_so = File.join(abs_archdir, config['LIBRUBY_SO'])
if File.file?(libruby_so)
if e = config['LIBPATHENV'] and !e.empty?
ENV[e] = [abs_archdir, ENV[e]].compact.join(pathsep)
end
if /linux/ =~ RUBY_PLATFORM
ENV["LD_PRELOAD"] = [libruby_so, ENV["LD_PRELOAD"]].compact.join(' ')
end
end
ENV["DYLD_PRINT_LIBRARIES"] = "1" if print_libraries
command << ruby
exec *command.concat(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment