@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@
@if COVERAGE
export CCACHE_DISABLE := 1
@endif

# Configuration

SH_CFLAGS ?= @SH_CFLAGS@
SH_LDFLAGS ?= @SH_LDFLAGS@
@if SH_SOPREFIX
@if SH_SOFULLPATH
SH_LDFLAGS += @SH_SOPREFIX@$(DESTDIR)@libdir@/libjim@LIBSOEXT@
@else
SH_LDFLAGS += @SH_SOPREFIX@libjim@LIBSOEXT@
@endif
@endif
SHOBJ_CFLAGS ?= @SHOBJ_CFLAGS@
@if JIM_STATICLIB
SHOBJ_LDFLAGS ?= @SHOBJ_LDFLAGS@
@else
SHOBJ_LDFLAGS ?= @SHOBJ_LDFLAGS_R@
@endif
@if [get-define srcdir] ne "."
VPATH := @srcdir@
AS_CFLAGS = -I@srcdir@
AS_CXXFLAGS = -I@srcdir@
@endif

AS_CFLAGS += @AS_CFLAGS@
AS_CXXFLAGS += @AS_CFLAGS@
AS_CPPFLAGS += @AS_CPPFLAGS@
CFLAGS ?= @CFLAGS@
CXXFLAGS ?= @CXXFLAGS@
LDFLAGS = @LDFLAGS@
LDLIBS += @LDLIBS@
LIBS += @LIBS@
exec_prefix ?= @exec_prefix@
prefix ?= @prefix@
docdir = @docdir@
srcdir := @srcdir@

@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) $(AS_CXXFLAGS) $(CXXFLAGS)
@else
JIMSH_CC := $(CC) $(AS_FLAGS) $(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) $(AS_CFLAGS) $(AS_CPPFLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@ _$*.c

.c.o:
	$(ECHO) "	CC	$@"
	$(Q)$(CC) $(AS_CFLAGS) $(AS_CPPFLAGS) $(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@
@if !JIM_STATICLIB
	ln -s -f $(LIBJIM) $(DESTDIR)@libdir@/libjim@SH_SOEXT@
@endif
	$(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@
	$(INSTALL_PROGRAM) @srcdir@/jimdb $(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)
	$(DEF_LD_PATH) $(MAKE) -C tests jimsh="@builddir@/jimsh"

$(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	$@ libjim@SH_SOEXT@"
	$(Q)$(CC) $(AS_CFLAGS) $(CFLAGS) $(LDFLAGS) $(SH_LDFLAGS) -o $@ $(OBJS) $(LDLIBS) $(LIBS)
@if SH_SOEXT ne LIBSOEXT
	$(Q)ln -s -f $@ libjim@SH_SOEXT@
@endif
@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) @srcdir@/Tcl_shipped.html $(DESTDIR)$(docdir)/Tcl.html
@endif

Tcl.html: jim_tcl.txt @srcdir@/make-index
@if HAVE_ASCIIDOC
	@tclsh@ @srcdir@/make-index $> $^ | @ASCIIDOC@ --attribute footer-style=none -d manpage - >$@
@else
	@echo "asciidoc is not available"; false
@endif

coverage:
@if COVERAGE
@if COVERAGE_TOOL eq "lcov"
	@LCOV@ -c --rc genhtml_branch_coverage=1 -d . -o lcov.txt
	@GENHTML@ --rc genhtml_branch_coverage=1 -o coverage_html lcov.txt > genhtml_output.txt
	@LCOV@ --summary lcov.txt
@endif
@if COVERAGE_TOOL eq "gcovr"
	@GCOVR@ -s -e 'conftest*' --html --html-details --output coverage.html
@endif
@if COVERAGE_TOOL eq "gcov"
	gcov @srcdir@/*.c
@endif
@else
	@echo "Use ./configure --coverage to enable code coverage"
@endif

clean:
	rm -f *.o *.so *.dll *.exe lib*.a $(JIMSH) $(LIBJIM) Tcl.html _*.c libjim@SH_SOEXT@
	rm -f jsmn/*.o
@if COVERAGE
	rm -f *.gcno *.gcov *.gcda */*.gcno */*.gcda */*.gcov coverage*.html
	rm -rf coverage_html lcov.txt genhtml_output.txt
@endif

distclean: clean
	rm -f jimautoconf.h jim-config.h Makefile config.log jimsh0@EXEEXT@ build-jim-ext
	rm -f jimtcl.pc tests/Makefile examples.api/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@
