# -*- mode: makefile-gmake; indent-tabs-mode: t -*-

SHELL = /bin/sh
NULLCMD = @NULLCMD@
silence = no # yes/no
yes_silence = $(silence:no=)
no_silence = $(silence:yes=)
n=$(NULLCMD)
ECHO1 = $(V:1=$n)
RUNCMD = $(SHELL)
CDPATH = .
CHDIR = @CHDIR@
exec = exec
NULL = /dev/null
PATH_SEPARATOR = @PATH_SEPARATOR@

#### Start of system configuration section. ####

srcdir = @srcdir@
top_srcdir = $(srcdir)
hdrdir = $(srcdir)/include
tooldir = $(srcdir)/tool

PLATFORM_DIR = @PLATFORM_DIR@

CC_WRAPPER = @XCC_WRAPPER@
CC = @CC@
CPP = @CPP@
LD = @LD@
RUSTC = @RUSTC@
CARGO = @CARGO@
YACC = bison
PURIFY =
AUTOCONF = autoconf
CONFIGURE = @CONFIGURE@
@SET_MAKE@
MKFILES = @MAKEFILES@
BASERUBY = @BASERUBY@
HAVE_BASERUBY = @HAVE_BASERUBY@
TEST_RUNNABLE = @TEST_RUNNABLE@
CROSS_COMPILING = @CROSS_COMPILING@
DOXYGEN = @DOXYGEN@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
sbindir = @sbindir@
libdir = @libdir@
libexecdir = @libexecdir@
datarootdir = @datarootdir@
datadir = @datadir@
target_os = @target_os@
arch = @arch@
sitearch = @sitearch@
sitedir = @sitedir@
archlibdir = @archlibdir@
includedir = @includedir@
archincludedir = @archincludedir@
rubylibprefix = @rubylibprefix@
rubylibdir = @rubylibdir@
rubyarchprefix = @rubyarchprefix@
rubyarchdir = @rubyarchdir@
rubyhdrdir = @rubyhdrdir@
rubyarchhdrdir = @rubyarchhdrdir@
ruby_version = @ruby_version@
RUBY_VERSION_NAME = @RUBY_VERSION_NAME@
UNIVERSAL_ARCHNAMES = @UNIVERSAL_ARCHNAMES@

TESTUI = console
TESTS =
INSTALLDOC = @INSTALLDOC@
DOCTARGETS = @RDOCTARGET@ @CAPITARGET@

EXTOUT = @EXTOUT@
TIMESTAMPDIR  = $(EXTOUT)/.timestamp
arch_hdrdir = $(EXTOUT)/include/$(arch)
VPATH = $(arch_hdrdir)/ruby:$(hdrdir)/ruby:$(srcdir):$(srcdir)/missing

empty =
CC_VERSION = @CC_VERSION@
OUTFLAG = @OUTFLAG@$(empty)
COUTFLAG = @COUTFLAG@$(empty)
CPPOUTFLAG = >
ARCH_FLAG = @ARCH_FLAG@
CFLAGS_NO_ARCH = @CFLAGS@
CFLAGS = $(CFLAGS_NO_ARCH) $(ARCH_FLAG)
cflags = @cflags@
optflags = @optflags@
debugflags = @debugflags@
warnflags = @warnflags@ @strict_warnflags@
cppflags = @cppflags@
RUBY_DEVEL = @RUBY_DEVEL@ # "yes" or empty
_RUBY_DEVEL_enabled = $(RUBY_DEVEL:no=)
XCFLAGS = @XCFLAGS@ $(INCFLAGS) $(_RUBY_DEVEL_enabled:yes=-DRUBY_DEVEL=1)
USE_RUBYGEMS = @USE_RUBYGEMS@
USE_RUBYGEMS_ = $(USE_RUBYGEMS:yes=)
CPPFLAGS = @CPPFLAGS@ $(USE_RUBYGEMS_:no=-DDISABLE_RUBYGEMS=1)
MJIT_SUPPORT = @MJIT_SUPPORT@
MJIT_HEADER_FLAGS = @MJIT_HEADER_FLAGS@
MJIT_HEADER_SUFFIX =
MJIT_HEADER_ARCH =
MJIT_HEADER_INSTALL_DIR = @MJIT_HEADER_INSTALL_DIR@
MJIT_CC = @MJIT_CC@
MJIT_CFLAGS = @MJIT_CFLAGS@
MJIT_OPTFLAGS = @MJIT_OPTFLAGS@
MJIT_DEBUGFLAGS = @MJIT_DEBUGFLAGS@
MJIT_LDSHARED = @MJIT_LDSHARED@
MJIT_DLDFLAGS = $(XDLDFLAGS)
MJIT_HEADER   = rb_mjit_header.h
MJIT_MIN_HEADER_NAME = rb_mjit_min_header-$(RUBY_PROGRAM_VERSION).h
MJIT_MIN_HEADER = $(MJIT_HEADER_BUILD_DIR)/$(MJIT_MIN_HEADER_NAME)
MJIT_HEADER_BUILD_DIR = $(EXTOUT)/include/$(arch)
MJIT_TABS=@MJIT_TABS@
YJIT_SUPPORT=@YJIT_SUPPORT@
YJIT_LIBS=@YJIT_LIBS@
YJIT_OBJ=@YJIT_OBJ@
CARGO_TARGET_DIR=@abs_top_builddir@/yjit/target
CARGO_BUILD_ARGS=@CARGO_BUILD_ARGS@
LDFLAGS = @STATIC@ $(CFLAGS) @LDFLAGS@
EXE_LDFLAGS = $(LDFLAGS)
EXTLDFLAGS = @EXTLDFLAGS@
XLDFLAGS = @XLDFLAGS@ $(EXTLDFLAGS)
EXTLIBS =
LIBS = @LIBS@ $(EXTLIBS)
MISSING = @LIBOBJS@ @ALLOCA@
ENABLE_SHARED = @ENABLE_SHARED@
LDSHARED = @LIBRUBY_LDSHARED@
DLDSHARED = @DLDSHARED@
XDLDFLAGS = @DLDFLAGS@
DLDFLAGS = @LIBRUBY_DLDFLAGS@ $(XLDFLAGS) $(ARCH_FLAG)
SOLIBS = @SOLIBS@
ENABLE_DEBUG_ENV = @ENABLE_DEBUG_ENV@
MAINLIBS = $(YJIT_LIBS) @MAINLIBS@
ARCHMINIOBJS = @MINIOBJS@
DLNOBJ = @DLNOBJ@
ENCOBJS = @ENCOBJS@
EXTOBJS = @EXTOBJS@
BUILTIN_ENCOBJS = @BUILTIN_ENCOBJS@
BUILTIN_TRANSSRCS = @BUILTIN_TRANSSRCS@
BUILTIN_TRANSOBJS = @BUILTIN_TRANSOBJS@
POSTLINK = @POSTLINK@

LIBPATHENV = @LIBPATHENV@
PRELOADENV = @PRELOADENV@

RUBY_BASE_NAME=@RUBY_BASE_NAME@
RUBY_API_VERSION=@RUBY_API_VERSION@
RUBY_INSTALL_NAME=@RUBY_INSTALL_NAME@
RUBY_SO_NAME=@RUBY_SO_NAME@
EXEEXT = @EXEEXT@
LIBEXT = @LIBEXT@
PROGRAM=$(RUBY_INSTALL_NAME)$(EXEEXT)
RUBY = $(RUBY_INSTALL_NAME)
MINIRUBY = @MINIRUBY@\
	$(MINIRUBYOPT)
# RUNRUBY_COMMAND:: runruby.rb or baseruby. do not append options directly
RUNRUBY_COMMAND = @RUNRUBY_COMMAND@
# RUNRUBY:: run ruby with RUN_OPTS which is passed to ruby
RUNRUBY = @RUNRUBY@ $(RUN_OPTS)
# RUNRUBY_DEBUGGER:: debugging option for runruby.rb
RUNRUBY_DEBUGGER = --debugger='gdb -x run.gdb --quiet --args'
XRUBY = @XRUBY@
BTESTRUBY = @BTESTRUBY@ -r./$(arch)-fake
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
XRUBY_LIBDIR = @XRUBY_LIBDIR@
XRUBY_RUBYLIBDIR = @XRUBY_RUBYLIBDIR@
XRUBY_RUBYHDRDIR = @XRUBY_RUBYHDRDIR@

yes_baseruby = $(HAVE_BASERUBY:no=)
no_baseruby = $(HAVE_BASERUBY:yes=)
BOOTSTRAPRUBY = $(yes_baseruby:yes=$(BASERUBY)) $(no_baseruby:no=$(MINIRUBY))
BOOTSTRAPRUBY_OPT = $(yes_baseruby:yes=-r./$(arch)-fake)
BOOTSTRAPRUBY_FAKE = $(yes_baseruby:yes=$(arch)-fake.rb)

COROUTINE_H = @X_COROUTINE_H@
COROUTINE_OBJ = $(COROUTINE_H:.h=.$(OBJEXT))
COROUTINE_SRC = @X_COROUTINE_SRC@

#### End of system configuration section. ####

MAJOR=	@MAJOR@
MINOR=	@MINOR@
TEENY=	@TEENY@

# here for MJIT_MIN_HEADER_NAME, not in common.mk
RUBY_PROGRAM_VERSION = $(MAJOR).$(MINOR).$(TEENY)

LIBRUBY_A     = @LIBRUBY_A@
LIBRUBY_SO    = @LIBRUBY_SO@
LIBRUBY_SONAME= @LIBRUBY_SONAME@
LIBRUBY_ALIASES= @LIBRUBY_ALIASES@
LIBRUBY	      = @LIBRUBY@
LIBRUBYARG    = @LIBRUBYARG@
LIBRUBYARG_STATIC = @LIBRUBYARG_STATIC@
LIBRUBYARG_SHARED = @LIBRUBYARG_SHARED@
LIBRUBY_RELATIVE = @LIBRUBY_RELATIVE@
LIBRUBY_A_OBJS = @LIBRUBY_A_OBJS@

DTRACE_REBUILD_OBJS = $(DTRACE_REBUILD:yes=$(DTRACE_DEPENDENT_OBJS))

DTRACE_DEPENDENT_OBJS = array.$(OBJEXT) \
			eval.$(OBJEXT) \
			gc.$(OBJEXT) \
			hash.$(OBJEXT) \
			load.$(OBJEXT) \
			object.$(OBJEXT) \
			parse.$(OBJEXT) \
			string.$(OBJEXT) \
			symbol.$(OBJEXT) \
			vm.$(OBJEXT) \
			$(YJIT_OBJ)

THREAD_MODEL  = @THREAD_MODEL@

PREP          = @PREP@
ARCHFILE      = @ARCHFILE@
SETUP         =
EXTSTATIC     = @EXTSTATIC@
ENCSTATIC     = @ENCSTATIC@
SET_LC_MESSAGES = env LC_MESSAGES=C

MAKEDIRS      = @MKDIR_P@
CP            = cp
MV            = mv
RM            = rm -f
RM1           = $(RM)
RMDIR         = @RMDIR@
RMDIRS        = @RMDIRS@
RMALL         = @RMALL@
LN_S          = @LN_S@
TOUCH         = touch
NM            = @NM@
AR            = @AR@
ARFLAGS       = @ARFLAGS@$(empty)
RANLIB        = @RANLIB@
AS            = @AS@
ASFLAGS       = @ASFLAGS@ $(ARCH_FLAG) $(INCFLAGS)
IFCHANGE      = $(SHELL) $(tooldir)/ifchange
OBJDUMP       = @OBJDUMP@
OBJCOPY       = @OBJCOPY@
HAVE_GIT      = @HAVE_GIT@
GIT           = @GIT@
VCS           = @VCS@
VCSUP         = @VCSUP@
DTRACE        = @DTRACE@ @DTRACE_OPT@
DTRACE_EXT    = @DTRACE_EXT@
DTRACE_OBJ    = @DTRACE_OBJ@
DTRACE_REBUILD= @DTRACE_REBUILD@
DTRACE_GLOMMED_OBJ = $(DTRACE_REBUILD:yes=ruby-glommed.$(OBJEXT))

OBJEXT        = @OBJEXT@
ASMEXT        = @ASMEXT@
SOEXT         = @SOEXT@
DLEXT         = @DLEXT@
MANTYPE	      = @MANTYPE@
SYMBOL_PREFIX = @SYMBOL_PREFIX@

INSTALLED_LIST= .installed.list

NEWLINE_C     = enc/trans/newline.c
MINIPRELUDE_C = miniprelude.c
GOLF_PRELUDE_C= golf_prelude.c
RBCONFIG      = .rbconfig.time

MAINSRC = $(MAINOBJ:.$(OBJEXT)=.c)

SRC_FILE      = $<
OS_SRC_FILE   = $<
DEST_FILE     = $@
OS_DEST_FILE  = $@

MESSAGE_BEGIN = @for line in
MESSAGE_END = ; do echo "$$line"; done
ECHO_BEGIN = @sep=''; for word in
ECHO_END = ; do echo @ECHO_N@ "$$sep'$$word'@ECHO_C@"; sep=' '; done; echo

ACTIONS_GROUP = @$(NULLCMD) $(empty)
ACTIONS_ENDGROUP = @$(NULLCMD)

DESTDIR       = @DESTDIR@

configure_args = @configure_args@
#### End of variables

ABI_VERSION_HDR = $(hdrdir)/ruby/internal/abi.h

CAT_DEPEND = sed -e 's/{\$$([^(){}]*)[^{}]*}//g' -e /AUTOGENERATED/q

.SUFFIXES: .inc .h .c .y .i .$(ASMEXT) .$(DTRACE_EXT)

all:

# Prevent GNU make v3 from overflowing arg limit on SysV.
.NOEXPORT:

miniruby$(EXEEXT):
		@-if test -f $@; then $(MV) -f $@ $@.old; $(RM) $@.old; fi
		$(ECHO) linking $@
		$(Q) $(PURIFY) $(CC) $(EXE_LDFLAGS) $(XLDFLAGS) $(NORMALMAINOBJ) $(MINIOBJS) $(COMMONOBJS) $(MAINLIBS) $(LIBS) $(OUTFLAG)$@
		$(Q) $(POSTLINK)

$(PROGRAM):
		@$(RM) $@
		$(ECHO) linking $@
		$(Q) $(PURIFY) $(CC) $(EXE_LDFLAGS) $(XLDFLAGS) $(MAINOBJ) $(EXTOBJS) $(LIBRUBYARG) $(MAINLIBS) $(LIBS) $(EXTLIBS) $(OUTFLAG)$@
		$(Q) $(POSTLINK)

$(PROGRAM): @XRUBY_LIBPATHENV_WRAPPER@

PRE_LIBRUBY_UPDATE = [ -n "$(LIBRUBY_SO_UPDATE)" ] || $(gnumake:yes=exec) $(RM) $(LIBRUBY_EXTS)

# We must `rm' the library each time this rule is invoked because "updating" a
# MAB library on Apple/NeXT (see --enable-fat-binary in configure) is not
# supported.
#
# In YJIT builds, merge libyjit.a with libruby_static.a
$(LIBRUBY_A):
		@$(RM) $@
		@-[ -z "$(EXTSTATIC)" ] || $(PRE_LIBRUBY_UPDATE)
		$(ECHO) linking static-library $@
		$(Q) $(AR) $(ARFLAGS) $@ $(LIBRUBY_A_OBJS) $(INITOBJS)
		$(Q) if [ 'no' != '$(YJIT_SUPPORT)' ]; then \
		  set -eu$(V0:1=x) && \
		  $(ECHO0) 'merging $(YJIT_LIBS) into $@' && \
		  $(RMALL)    libyjit/ && \
		  $(MAKEDIRS) libyjit/ && \
		  trap "$(RMALL) libyjit/" 0 && \
		  (cd libyjit/ && $(AR) -x ../$(YJIT_LIBS)) && \
		  :  "$(AR) $(ARFLAGS) $@ libyjit/*.$(OBJEXT)" && \
		  find libyjit/ -name '*.$(OBJEXT)' -exec $(AR) $(ARFLAGS) $@ '{}' '+' ; \
		fi
		@-$(RANLIB) $@ 2> /dev/null || true

verify-static-library: $(LIBRUBY_A)
		$(ECHO) verifying static-library $@
		@$(PURIFY) $(CC) $(EXE_LDFLAGS) $(XLDFLAGS) $(MAINOBJ) $(LIBRUBY_A) $(MAINLIBS) $(EXTLIBS) $(LIBS) $(OUTFLAG)conftest$(EXEEXT)
		@$(RMALL) conftest$(EXEEXT) conftest.c conftest.dSYM

$(LIBRUBY_SO):
		@-[ -n "$(EXTSTATIC)" ] || $(PRE_LIBRUBY_UPDATE)
		$(ECHO) linking shared-library $@
		$(Q) $(LDSHARED) $(DLDFLAGS) $(OBJS) $(DLDOBJS) $(SOLIBS) $(EXTSOLIBS) $(OUTFLAG)$@
		-$(Q) $(OBJCOPY) -w -L '$(SYMBOL_PREFIX)Init_*' -L '$(SYMBOL_PREFIX)ruby_static_id_*' \
			-L '$(SYMBOL_PREFIX)*_threadptr_*' -L '$(SYMBOL_PREFIX)*_ec_*' $@
		$(Q) $(POSTLINK)
		@-$(MINIRUBY) -e 'so, *aliases = ARGV; aliases.uniq!; aliases.delete(File.basename(so)); \
				  aliases.each { |link| File.delete link rescue nil; File.symlink so, link }' \
				$(LIBRUBY_SO) $(LIBRUBY_ALIASES) || true

LIBRUBY_WITH_EXT = @LIBRUBY_WITH_EXT@
$(LIBRUBY_$(LIBRUBY_WITH_EXT)): $(LIBRUBY_SO_UPDATE)

PKG_CONFIG = @PKG_CONFIG@
ruby_pc = @ruby_pc@
$(ruby_pc): config.status Makefile
	$(Q)./config.status --file=-:$(srcdir)/template/ruby.pc.in | \
	sed -e 's/\$$(\([A-Za-z_][A-Za-z0-9_]*\))/$${\1}/g' \
	    -e 's|^prefix=.*|prefix=$(prefix)|' \
	    > ruby.tmp.pc
	$(Q)pkg_config=${PKG_CONFIG} && PKG_CONFIG_PATH=. $${pkg_config:-:} --print-errors ruby.tmp
	$(Q)$(MV) -f ruby.tmp.pc $(ruby_pc)

pre-install-local:: pkgconfig-data

ruby-runner.h: template/ruby-runner.h.in config.status
		@./config.status --file=$@:$(srcdir)/template/$(@F).in

install-cross: $(arch)-fake.rb $(RBCONFIG) rbconfig.rb $(arch_hdrdir)/ruby/config.h \
	$(LIBRUBY_A) $(LIBRUBY_SO) $(ARCHFILE) pkgconfig-data
	$(ECHO) installing cross-compiling stuff
	$(Q) $(MAKEDIRS) $(XRUBY_RUBYLIBDIR)/$(arch) $(XRUBY_RUBYHDRDIR)/$(arch)/ruby
	$(Q) sed '/^\$$:\.unshift/q' $(arch)-fake.rb > fake.rb
	$(Q) $(BASERUBY) -p \
	-e '~/^\s*CONFIG\["LDFLAGS"\]/ and' \
	-e '$$_[/(?=\s*"$$)/] = %q[ #{(CONFIG["LIBPATHFLAG"]%File.dirname(__FILE__)).strip}]' \
	rbconfig.rb > fake-rbconfig.rb
	$(INSTALL_SCRIPT) fake.rb $(XRUBY_RUBYLIBDIR)/$(arch)/fake.rb
	$(INSTALL_SCRIPT) fake-rbconfig.rb $(XRUBY_RUBYLIBDIR)/$(arch)/rbconfig.rb
	@$(RM) fake.rb fake-rbconfig.rb
	$(INSTALL_DATA) $(arch_hdrdir)/ruby/config.h $(XRUBY_RUBYHDRDIR)/$(arch)/ruby
	$(INSTALL_DATA) $(top_srcdir)/include/ruby/win32.h $(XRUBY_RUBYHDRDIR)/ruby
	$(INSTALL_DATA) $(LIBRUBY) $(LIBRUBY_A) $(XRUBY_RUBYLIBDIR)/$(arch)
	$(INSTALL_PROGRAM) $(LIBRUBY_SO) $(XRUBY_RUBYLIBDIR)/$(arch)

Makefile:	$(srcdir)/template/Makefile.in $(srcdir)/enc/Makefile.in

$(MKFILES): config.status $(srcdir)/version.h $(REVISION_H) $(ABI_VERSION_HDR)
		@[ -f $@ ] && mv $@ $@.old
		MAKE=$(MAKE) $(SHELL) ./config.status $@
		@cmp $@ $@.old > /dev/null 2>&1 && echo $@ unchanged && exit 0; \
		{ \
		    echo "all:; -@rm -f conftest.mk"; \
		    echo "conftest.mk: .force; @echo AUTO_REMAKE"; \
		    echo ".force:"; \
		} > conftest.mk || exit 1; \
		$(MAKE) -f conftest.mk | grep '^AUTO_REMAKE$$' >/dev/null 2>&1 || \
		{ echo "$@ updated, restart."; exit 1; }

uncommon.mk: $(srcdir)/common.mk
		sed 's/{\$$([^(){}]*)[^{}]*}//g' $< > $@

.PHONY: reconfig
reconfig-args = $(srcdir)/$(CONFIGURE) $(yes_silence:yes=--silent) $(configure_args)
config.status-args = ./config.status $(yes_silence:yes=--silent) --recheck
reconfig-exec-0 = test -t 1 && { : $${CONFIGURE_TTY=yes}; export CONFIGURE_TTY; }; exec 3>&1; exit `exec 4>&1; { "$$@" 3>&- 4>&-; echo $$? 1>&4; } | grep -F -v '(cached)' 1>&3 3>&- 4>&-`
reconfig-exec-1 = set -x; exec "$$@"
reconfig-exec-yes = $(reconfig-exec-1)

reconfig config.status:	$(srcdir)/$(CONFIGURE) $(srcdir)/enc/Makefile.in \
			$(hdrdir)/ruby/version.h $(ABI_VERSION_HDR)
	@PWD= MINIRUBY="$(MINIRUBY)"; export MINIRUBY; \
	set $(SHELL) $($@-args); $(reconfig-exec-$(silence:no=$(V)))

$(srcdir)/$(CONFIGURE): $(srcdir)/configure.ac
	$(CHDIR) $(srcdir) && exec $(AUTOCONF) -o $(@F)

prereq: $(srcdir)/$(CONFIGURE)

incs: id.h
all-incs: probes.h

# Things which should be considered:
# * with gperf v.s. without gperf
# * committers may have various versions of gperf
# * ./configure v.s. ../ruby/configure
# * svn checkout generate a file with mtime as current time
# * ext4 and XFS has a mtime with fractional part
lex.c: defs/keywords
	@\
	if cmp -s $(srcdir)/defs/lex.c.src $?; then \
	  [ $(Q) ] && echo copying $@ || set -x; \
	  $(CP) $(srcdir)/lex.c.blt $@; \
	else \
	  [ $(Q) ] && echo generating $@ || set -x; \
	  gperf -C -P -p -j1 -i 1 -g -o -t -N rb_reserved_word -k1,3,$$ $? \
	  | sed -f $(tooldir)/gperf.sed \
	  > $@.tmp && \
	  $(MV) $@.tmp $@ && \
	  $(CP) $? $(srcdir)/defs/lex.c.src && \
	  $(CP) $@ $(srcdir)/lex.c.blt; \
	fi

JIS_PROPS_OPTIONS = -k1,3 -7 -c -j1 -i1 -t -C -P -t --ignore-case -H onig_jis_property_hash -Q onig_jis_property_pool -N onig_jis_property

$(srcdir)/enc/jis/props.h: enc/jis/props.kwd
	$(MAKEDIRS) $(@D)
	@set +e; \
	if cmp -s $(?:.kwd=.src) $?; then \
	  set -x; \
	  $(CP) $(?:.kwd=.h.blt) $@; \
	else \
	  set -x; \
	  gperf $(JIS_PROPS_OPTIONS) $? | \
	  sed -f $(tooldir)/gperf.sed > $@ && \
	  $(CP) $? $(?:.kwd=.src) && \
	  $(CP) $@ $(?:.kwd=.h.blt); \
	fi

.c.$(OBJEXT):
	@$(ECHO) compiling $<
	$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c $<

.$(ASMEXT).$(OBJEXT):
	@$(ECHO) assembling $<
	$(Q) $(CC) $(ASFLAGS) -DSYMBOL_PREFIX=$(SYMBOL_PREFIX) -o $@ -c $<

.c.$(ASMEXT):
	@$(ECHO) translating $<
	$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -S $<

.c.i:
	@$(ECHO) preprocessing $<
	$(Q) $(CPP) $(warnflags) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -E $< > $@

.d.h:
	@$(ECHO) translating probes $<
	$(Q) $(DTRACE) -o $@.tmp -h -C $(INCFLAGS) -s $<
	$(Q) sed -e 's/RUBY_/RUBY_DTRACE_/g' -e 's/PROBES_H_TMP/RUBY_PROBES_H/' -e 's/(char \*/(const char */g' -e 's/, char \*/, const char */g' $@.tmp > $@
	$(Q) $(RM) $@.tmp

.dmyh.h:
	@$(ECHO) making dummy $(DEST_FILE)
	$(Q)echo '#include "$(*F).dmyh"' > $@

probes.stamp: $(DTRACE_REBUILD_OBJS)
	$(Q) if test -f $@ -o -f probes.$(OBJEXT); then \
	  $(RM) $(DTRACE_REBUILD_OBJS) $@; \
	  $(ECHO0) "rebuilding objects which were modified by \"dtrace -G\""; \
	  $(MAKE) $(DTRACE_REBUILD_OBJS); \
	fi
	$(Q) touch $@

probes.$(OBJEXT): $(srcdir)/probes.d $(DTRACE_REBUILD:yes=probes.stamp)
	@$(ECHO) processing probes in object files
	$(Q) $(RM) $@
	$(Q) $(DTRACE) -G -C $(INCFLAGS) -s $(srcdir)/probes.d -o $@ $(DTRACE_REBUILD_OBJS)

main: mjit-headers
yes-mjit-headers: $(MJIT_MIN_HEADER)
clean-local::
	$(Q)$(RM) \
	  $(MJIT_HEADER) $(MJIT_HEADER:.h=)$(MJIT_HEADER_SUFFIX:%=*).h \
	  $(MJIT_MIN_HEADER) $(MJIT_MIN_HEADER:.h=)$(MJIT_HEADER_SUFFIX:%=*).h \
	  $(MJIT_HEADER_INSTALL_DIR)/rb_mjit_min_header-*.h \
	  $(TIMESTAMPDIR)/$(MJIT_HEADER:.h=)$(MJIT_HEADER_SUFFIX).time mjit_config.h \
	|| $(NULLCMD)
	$(Q)$(RM) -r mjit_build_dir.*
	-$(Q) $(RMDIRS) $(MJIT_HEADER_INSTALL_DIR) $(MJIT_HEADER_BUILD_DIR) $(TIMESTAMPDIR) 2> $(NULL) || $(NULLCMD)

# DTrace static library hacks described here:
# https://marc.info/?l=opensolaris-dtrace-discuss&m=114761203110734&w=4
ruby-glommed.$(OBJEXT):
	@$(ECHO) generating a glommed object with DTrace probes for static library
	$(Q) $(LD) -r -o $@ $(OBJS)

clean-local::
	$(Q)$(RM) \
	  ext/extinit.c ext/extinit.$(OBJEXT) ext/ripper/y.output \
	  enc/encinit.c enc/encinit.$(OBJEXT) $(pkgconfig_DATA) \
	  ruby-runner.$(OBJEXT) ruby-runner.h \
	|| $(NULLCMD)
	$(Q)find . \( -name '*.bc' -o -name '*.[is]' \) -delete
	-$(Q)$(RMALL) exe/ *.dSYM

distclean-local::
	$(Q)$(RM) \
	  ext/config.cache $(RBCONFIG) Doxyfile run.gdb \
	  $(INSTALLED_LIST) $(arch_hdrdir)/ruby/config.h verconf.h \
	|| $(NULLCMD)
	-$(Q)$(RMDIRS) $(arch_hdrdir)/ruby 2> /dev/null || true

ext/clean.sub:: ext/clean.mk
ext/distclean.sub:: ext/distclean.mk
ext/realclean.sub:: ext/realclean.mk

ext/clean.mk ext/distclean.mk ext/realclean.mk::
	-$(Q) if [ -f $(EXTS_MK) ]; then exec $(MAKE) -f $(EXTS_MK) $(@F:.mk=); fi

ext/clean:: ext/clean.sub
ext/distclean:: ext/distclean.sub
ext/realclean:: ext/realclean.sub
.bundle/clean:: .bundle/clean.sub
.bundle/distclean:: .bundle/distclean.sub
.bundle/realclean:: .bundle/realclean.sub

ext/clean.sub .bundle/clean.sub:: ext/clean.mk
ext/distclean.sub .bundle/distclean.sub:: ext/distclean.mk
ext/realclean.sub .bundle/realclean.sub:: ext/realclean.mk

ext/clean.sub ext/distclean.sub ext/realclean.sub \
.bundle/clean.sub .bundle/distclean.sub .bundle/realclean.sub::
	$(Q) set dummy `echo "${EXTS}" | tr , ' '`; shift; \
	test "$$#" = 0 && set .; \
	set dummy `\
	    cd $(@D) 2>/dev/null && \
	    find "$$@" \( -name Makefile -o -name exts.mk \) -print | \
	    sed -n 's:^\./::;s:^:$(@D)/:;s:/[^/][^/]*$$::p' | sort -u; \
	`; shift; \
	for dir do \
	    $(RM) "$$dir/exts.mk"; \
	    if [ -f "$$dir/Makefile" ]; then \
		echo $(@F:.sub=)ing "$$dir"; \
		(cd "$$dir" && exec $(MAKE) $(mflags) $(@F:.sub=)); \
	    fi; \
	done || true

ext/distclean ext/realclean .bundle/distclean .bundle/realclean::
	$(Q) set dummy `echo "${EXTS}" | tr , ' '`; shift; \
	test "$$#" = 0 && set .; \
	cd $(@D) 2>/dev/null && \
	find "$$@" -type d -empty -exec $(RMDIRS) {} + 2> /dev/null || true
	$(Q) $(RMDIRS) $(@D) 2> /dev/null || true

clean-enc distclean-enc realclean-enc:
	@test -f "$(ENC_MK)" || exit 0; \
	echo $(@:-enc=ing) encodings; \
	exec $(MAKE) $(MAKE_ENC) $(@:-enc=)

ext/extinit.$(OBJEXT): ext/extinit.c $(SETUP)
	$(ECHO) compiling $@
	$(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c ext/extinit.c

enc/encinit.$(OBJEXT): enc/encinit.c $(SETUP)

cont.$(OBJEXT): $(COROUTINE_H)

update-src::
	@$(CHDIR) "$(srcdir)" && LC_TIME=C exec $(VCSUP)

update-download:: update-config_files

after-update:: prereq

gcov:
	$(Q) $(BASERUBY) $(tooldir)/run-gcov.rb

lcov:
	$(Q) $(BASERUBY) $(tooldir)/run-lcov.rb

update-benchmark-driver:
	$(Q) $(tooldir)/git-refresh -C $(srcdir)/benchmark $(Q1:0=-q) \
		--branch $(BENCHMARK_DRIVER_GIT_REF) \
		$(BENCHMARK_DRIVER_GIT_URL) benchmark-driver $(GIT_OPTS)

update-doclie:
	$(Q) $(tooldir)/git-refresh -C $(srcdir)/coverage $(Q1:0=-q) \
		--branch $(DOCLIE_GIT_REF) \
		$(DOCLIE_GIT_URL) doclie $(GIT_OPTS)

update-simplecov-html:
	$(Q) $(tooldir)/git-refresh -C $(srcdir)/coverage $(Q1:0=-q) \
		--branch $(SIMPLECOV_HTML_GIT_REF) \
		$(SIMPLECOV_HTML_GIT_URL) simplecov-html $(GIT_OPTS)

update-simplecov:
	$(Q) $(tooldir)/git-refresh -C $(srcdir)/coverage $(Q1:0=-q) \
		--branch $(SIMPLECOV_GIT_REF)  \
		$(SIMPLECOV_GIT_URL) simplecov $(GIT_OPTS)

update-coverage: update-simplecov update-simplecov-html update-doclie

update-known-errors:
	errno --list | cut -d' ' -f1 | sort -u - $(srcdir)/defs/known_errors.def | \
	$(IFCHANGE) $(srcdir)/defs/known_errors.def -

INSNS	= opt_sc.inc optinsn.inc optunifs.inc insns.inc insns_info.inc \
	  vmtc.inc vm.inc mjit_sp_inc.inc

$(INSNS): $(srcdir)/insns.def vm_opts.h \
	  $(srcdir)/defs/opt_operand.def $(srcdir)/defs/opt_insn_unif.def \
	  $(tooldir)/insns2vm.rb \
	  $(tooldir)/ruby_vm/controllers/application_controller.rb \
	  $(tooldir)/ruby_vm/helpers/c_escape.rb \
	  $(tooldir)/ruby_vm/helpers/dumper.rb \
	  $(tooldir)/ruby_vm/helpers/scanner.rb \
	  $(tooldir)/ruby_vm/loaders/insns_def.rb \
	  $(tooldir)/ruby_vm/loaders/opt_insn_unif_def.rb \
	  $(tooldir)/ruby_vm/loaders/opt_operand_def.rb \
	  $(tooldir)/ruby_vm/loaders/vm_opts_h.rb \
	  $(tooldir)/ruby_vm/models/attribute.rb \
	  $(tooldir)/ruby_vm/models/bare_instructions.rb \
	  $(tooldir)/ruby_vm/models/c_expr.rb \
	  $(tooldir)/ruby_vm/models/instructions.rb \
	  $(tooldir)/ruby_vm/models/instructions_unifications.rb \
	  $(tooldir)/ruby_vm/models/operands_unifications.rb \
	  $(tooldir)/ruby_vm/models/trace_instructions.rb \
	  $(tooldir)/ruby_vm/models/typemap.rb \
	  $(tooldir)/ruby_vm/scripts/converter.rb \
	  $(tooldir)/ruby_vm/scripts/insns2vm.rb \
	  $(tooldir)/ruby_vm/views/_attributes.erb \
	  $(tooldir)/ruby_vm/views/_c_expr.erb \
	  $(tooldir)/ruby_vm/views/_comptime_insn_stack_increase.erb \
	  $(tooldir)/ruby_vm/views/_copyright.erb \
	  $(tooldir)/ruby_vm/views/_insn_entry.erb \
	  $(tooldir)/ruby_vm/views/_insn_len_info.erb \
	  $(tooldir)/ruby_vm/views/_insn_name_info.erb \
	  $(tooldir)/ruby_vm/views/_insn_operand_info.erb \
	  $(tooldir)/ruby_vm/views/_insn_sp_pc_dependency.erb \
	  $(tooldir)/ruby_vm/views/_insn_type_chars.erb \
	  $(tooldir)/ruby_vm/views/_leaf_helpers.erb \
	  $(tooldir)/ruby_vm/views/_notice.erb \
	  $(tooldir)/ruby_vm/views/_sp_inc_helpers.erb \
	  $(tooldir)/ruby_vm/views/_trace_instruction.erb \
	  $(tooldir)/ruby_vm/views/insns.inc.erb \
	  $(tooldir)/ruby_vm/views/insns_info.inc.erb \
	  $(tooldir)/ruby_vm/views/mjit_sp_inc.inc.erb \
	  $(tooldir)/ruby_vm/views/opt_sc.inc.erb \
	  $(tooldir)/ruby_vm/views/optinsn.inc.erb \
	  $(tooldir)/ruby_vm/views/optunifs.inc.erb \
	  $(tooldir)/ruby_vm/views/vm.inc.erb \
	  $(tooldir)/ruby_vm/views/vmtc.inc.erb
	$(ECHO) generating $@
	$(Q) $(BASERUBY) -Ku $(tooldir)/insns2vm.rb $(INSNS2VMOPT) $@

loadpath: verconf.h
	@$(CPP) $(XCFLAGS) $(CPPFLAGS) $(srcdir)/loadpath.c | \
	sed -e '1,/^const char ruby_initial_load_paths/d;/;/,$$d' \
	    -e '/^ /!d;s/ *"\\0"$$//;s/" *"//g'

un-runnable:
	$(ECHO) cannot make runnable, configure with --enable-load-relative.
	$(Q) exit 1

mjit_config.h:
	$(ECHO) making $@
	@{ \
	. $(tooldir)/mjit_archflag.sh; \
	parse_arch_flags "$(UNIVERSAL_ARCHNAMES)" $(ARCH_FLAG); \
	test "$(Q)" = @ || set -x; \
	echo '#ifndef RUBY_MJIT_CONFIG_H'; \
	echo '#define RUBY_MJIT_CONFIG_H 1'; \
	echo; \
	sep=; \
	echo '#ifdef LOAD_RELATIVE'; \
	quote MJIT_HEADER_INSTALL_DIR "/$(MJIT_HEADER_INSTALL_DIR)"; \
	echo '#else'; \
	quote MJIT_HEADER_INSTALL_DIR "$(rubyarchhdrdir)"; \
	echo '#endif'; \
	quote MJIT_MIN_HEADER_NAME "$(MJIT_MIN_HEADER_NAME)"; \
	sep=,; \
	quote "MJIT_CC_COMMON  " $(MJIT_CC); \
	quote "MJIT_CFLAGS      MJIT_ARCHFLAG" $(MJIT_CFLAGS); \
	quote "MJIT_OPTFLAGS   " $(MJIT_OPTFLAGS); \
	quote "MJIT_DEBUGFLAGS " $(MJIT_DEBUGFLAGS); \
	quote "MJIT_LDSHARED   " $(MJIT_LDSHARED); \
	quote "MJIT_DLDFLAGS    MJIT_ARCHFLAG" $(MJIT_DLDFLAGS); \
	quote "MJIT_LIBS       " $(LIBRUBYARG_SHARED); \
	quote 'PRELOADENV       "@PRELOADENV@"'; \
	indent=$${archs:+'  '}; \
	define_arch_flags; \
	echo; \
	echo '#endif /* RUBY_MJIT_CONFIG_H */'; \
	} > $@

yes-test-almost yes-test-all programs: mjit_build_dir.$(SOEXT)
mjit_build_dir.$(SOEXT): $(MJIT_MIN_HEADER) $(srcdir)/ruby-runner.c ruby-runner.h
	$(ECHO) making $@
	$(Q) $(DLDSHARED) $(MJIT_DLDFLAGS) $(ARCH_FLAG) $(CFLAGS) $(INCFLAGS) $(CPPFLAGS) \
		-DMAKE_MJIT_BUILD_DIR=1 -DMJIT_MIN_HEADER='"$(MJIT_MIN_HEADER)"' \
		$(OUTFLAG)$@ $(srcdir)/ruby-runner.c

# yes-test-basic: leaked-globals
leaked-globals: $(COMMONOBJS) prog $(tooldir)/leaked-globals PHONY
	$(Q) $(XRUBY) $(tooldir)/leaked-globals NM=$(NM) SYMBOL_PREFIX=$(SYMBOL_PREFIX) PLATFORM=$(hdrdir)/ruby/$(PLATFORM_DIR).h $(srcdir)/configure.ac $(COMMONOBJS)
