@if NO_SILENT_RULES
Q :=
ECHO := @:
@else
# make V=1 for verbose make output
Q := @
ECHO := @echo
ifeq ($(V),1)
ifeq ("$(origin V)", "command line")
Q :=
ECHO := @:
endif
endif
@endif

# Tools
CC = @CCACHE@ @CC@
CXX = @CCACHE@ @CXX@
RANLIB = @RANLIB@
AR = @AR@
STRIP = @STRIP@

# Configuration

SH_CFLAGS ?= @SH_CFLAGS@
SH_LDFLAGS ?= @SH_LDFLAGS@
@if SH_SOPREFIX
SH_LDFLAGS += @SH_SOPREFIX@libjim@LIBSOEXT@
@endif
SHOBJ_CFLAGS ?= @SHOBJ_CFLAGS@
@if JIM_STATICLIB
SHOBJ_LDFLAGS ?= @SHOBJ_LDFLAGS@
@else
SHOBJ_LDFLAGS ?= @SHOBJ_LDFLAGS_R@
@endif
CFLAGS = @CFLAGS@ @CCOPTS@ @CPPFLAGS@
CXXFLAGS = @CXXFLAGS@ @CXXOPTS@
LDFLAGS = @LDFLAGS@
LDLIBS += @LDLIBS@
LIBS += @LIBS@
exec_prefix ?= @exec_prefix@
prefix ?= @prefix@
docdir = @docdir@

CC += -Wall $(OPTIM) -I.
CXX += -Wall $(OPTIM) -I.
@if [get-define srcdir] ne "."
CFLAGS += -I@srcdir@
CXXFLAGS += -I@srcdir@
VPATH := @srcdir@
@endif

@if JIM_STATICLIB
LIBJIM := libjim.a
@else
LIBJIM := libjim@LIBSOEXT@
SH_LIBJIM := $(LIBJIM)
CC += $(SH_CFLAGS)
CXX += $(SH_CFLAGS)
DEF_LD_PATH := @LD_LIBRARY_PATH@="@builddir@:$(@LD_LIBRARY_PATH@)"
@endif

@if HAVE_CXX_EXTENSIONS
JIMSH_CC := $(CXX) $(CXXFLAGS)
@else
JIMSH_CC := $(CC) $(CFLAGS)
@endif

OBJS := _load-static-exts.o jim-subcmd.o jim-interactive.o jim-format.o jim.o utf8.o jimregexp.o jimiocompat.o \
    @EXTRA_OBJS@ @C_EXT_OBJS@ @TCL_EXT_OBJS@

JIMSH := jimsh@EXEEXT@

@if JIM_INSTALL
INSTALL_DATA_DIR ?= mkdir -p
INSTALL_DATA ?= cp
INSTALL_PROGRAM ?= cp
@endif

all: $(JIMSH) @C_EXT_SHOBJS@

# Create C extensions from pure Tcl extensions
.SUFFIXES: .tcl
.tcl.o:
	$(ECHO) " 	TCLEXT	_$*.c"
	$(Q)@tclsh@ @srcdir@/make-c-ext.tcl $< >_$*.c
	$(ECHO) " 	CC	$@"
	$(Q)$(CC) $(CFLAGS) -c -o $@ _$*.c

.c.o:
	$(ECHO) "	CC	$@"
	$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@

$(JIMSH): $(LIBJIM) jimsh.o initjimsh.o
	$(ECHO) "	LINK	$@"
	$(Q)$(JIMSH_CC) @SH_LINKRPATH_FLAGS@ @SH_LINKFLAGS@ $(LDFLAGS) -o $@ jimsh.o initjimsh.o $(LIBJIM) $(LDLIBS) $(LIBS)

@if JIM_INSTALL
install: all @TCL_EXTS@ install-exec install-docs
	$(INSTALL_DATA_DIR) $(DESTDIR)@libdir@/jim
	$(INSTALL_DATA) $(LIBJIM) $(DESTDIR)@libdir@
	$(INSTALL_DATA) @srcdir@/README.extensions @C_EXT_SHOBJS@ $(DESTDIR)@libdir@/jim
	for i in tcltest.tcl @TCL_EXTS@; do $(INSTALL_DATA) @srcdir@/$$i $(DESTDIR)@libdir@/jim; done
	$(INSTALL_DATA_DIR) $(DESTDIR)@includedir@
	$(INSTALL_DATA) @srcdir@/jim.h @srcdir@/jim-eventloop.h @srcdir@/jim-signal.h \
		@srcdir@/jim-subcmd.h @srcdir@/jim-win32compat.h $(DESTDIR)@includedir@
	$(INSTALL_DATA) jim-config.h $(DESTDIR)@includedir@
	$(INSTALL_DATA_DIR) $(DESTDIR)@bindir@
	$(INSTALL_DATA) build-jim-ext $(DESTDIR)@bindir@
	$(INSTALL_DATA_DIR) $(DESTDIR)@libdir@/pkgconfig
	$(INSTALL_DATA) jimtcl.pc $(DESTDIR)@libdir@/pkgconfig

install-exec: all
	$(INSTALL_DATA_DIR) $(DESTDIR)@bindir@
	$(INSTALL_PROGRAM) $(JIMSH) $(DESTDIR)@bindir@

uninstall:
	rm -f $(DESTDIR)@bindir@/$(JIMSH)
	rm -f $(DESTDIR)@bindir@/build-jim-ext
	rm -f $(DESTDIR)@libdir@/$(LIBJIM)
	for i in README.extensions @C_EXT_SHOBJS@ @TCL_EXTS@; do rm -f $(DESTDIR)@libdir@/jim/$$i; done
	rm -f $(DESTDIR)@includedir@/jim*.h
	rm -f $(DESTDIR)@datadir@/doc/jim/Tcl.html
@else
install install-exec: all
uninstall:
@endif

test check: $(JIMSH)
	cd @srcdir@/tests; $(DEF_LD_PATH) $(MAKE) jimsh=@builddir@/jimsh TOPSRCDIR=..

$(OBJS) jimsh.o initjimsh.o: Makefile $(wildcard *.h)

@if JIM_UTF8
# Generate the unicode case mapping
utf8.o: _unicode_mapping.c

_unicode_mapping.c: @srcdir@/UnicodeData.txt @srcdir@/parse-unidata.tcl
	$(ECHO) "	UNIDATA	$@"
	$(Q)@tclsh@ @srcdir@/parse-unidata.tcl @PARSE_UNIDATA_FLAGS@ @srcdir@/UnicodeData.txt @srcdir@/EastAsianWidth.txt >$@ || ( rm $@; exit 1)
@endif

_load-static-exts.c: @srcdir@/make-load-static-exts.tcl Makefile
	$(ECHO) "	MKLDEXT	$@"
	$(Q)@tclsh@ @srcdir@/make-load-static-exts.tcl @STATIC_EXTS@ >$@ || ( rm $@; exit 1)

$(LIBJIM): $(OBJS)
@if JIM_STATICLIB
	$(ECHO) "	AR	$@"
	$(Q)$(AR) cr $@ $(OBJS)
	$(Q)$(RANLIB) $@
@else
	$(ECHO) "	LDSO	$@"
	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(SH_LDFLAGS) -o $@ $(OBJS) $(LDLIBS) $(LIBS)
@endif

@BUILD_SHOBJS@

docs: Tcl.html

@if INSTALL_DOCS eq "nodocs"
install-docs:
@endif

@if INSTALL_DOCS eq "docs"
install-docs: docs
	$(INSTALL_DATA_DIR) $(DESTDIR)$(docdir)
	$(INSTALL_DATA) Tcl.html $(DESTDIR)$(docdir)
@endif

@if INSTALL_DOCS eq "shipped"
install-docs:
	$(INSTALL_DATA_DIR) $(DESTDIR)$(docdir)
	@echo "Warning: asciidoc not available - installing Tcl_shipped.html"
	$(INSTALL_DATA) Tcl_shipped.html $(DESTDIR)$(docdir)/Tcl.html
@endif

Tcl.html: jim_tcl.txt @srcdir@/make-index
@if HAVE_ASCIIDOC
	@tclsh@ @srcdir@/make-index $> $^ | @ASCIIDOC@ -d manpage - | @SED@ -e '/^<div.*id="footer-text"/,/<\/div>/d' >$@
@else
	@echo "asciidoc is not available"; false
@endif

clean:
	rm -f *.o *.so *.dll *.exe lib*.a $(JIMSH) $(LIBJIM) Tcl.html _*.c

distclean: clean
	rm -f jimautoconf.h jim-config.h Makefile config.log @srcdir@/autosetup/jimsh0@EXEEXT@ build-jim-ext
	rm -f jimtcl.pc tests/Makefile

ship: Tcl.html
	cp $< Tcl_shipped.html

# automake compatibility. do nothing for all these targets
EMPTY_AUTOMAKE_TARGETS := dvi pdf ps info html tags ctags mostlyclean maintainer-clean check installcheck installdirs \
 install-pdf install-ps install-info install-html -install-dvi uninstall install-data
.PHONY: $(EMPTY_AUTOMAKE_TARGETS)
$(EMPTY_AUTOMAKE_TARGETS):

# automake compatibility - install sources from the current dir to $(distdir)
distdir_full := $(shell cd $(distdir); pwd)
distdir:
	cd "@srcdir@"; git ls-files | cpio -pdmu $(distdir_full)

reconfig:
	CC='@CC@' @AUTOREMAKE@
