# Tools
CC =  cc
RANLIB = ranlib
AR = ar
STRIP = strip

# Configuration

jim_libtype := static
SH_CFLAGS ?= -fPIC
SH_LDFLAGS ?= -shared
SHOBJ_CFLAGS ?= -fPIC
SHOBJ_LDFLAGS ?= -shared -nostartfiles
CFLAGS = -g -O2
LDFLAGS = 
LDLIBS += -ldl
exec_prefix ?= ${prefix}
prefix ?= /usr/local
install_jim = 

CPPFLAGS += -D_GNU_SOURCE -Wall -Werror $(OPTIM) -I.
ifneq (.,.)
CPPFLAGS += -I.
VPATH := .
endif

ifeq ($(jim_libtype),static)
	LIBJIM := libjim.a
else
	LIBJIM := libjim.so
	SH_LIBJIM := $(LIBJIM)
	CPPFLAGS += $(SH_CFLAGS)
	DEF_LD_PATH := LD_LIBRARY_PATH=`pwd` 
endif

.EXPORT_ALL_VARIABLES:

OBJS := _load-static-exts.o jim-subcmd.o jim-interactive.o jim-format.o jim.o utf8.o jimregexp.o 

JIMSH := jimsh

JIM_EXTENSIONS := aio array clock eventloop exec file load nvp package posix readdir regexp signal syslog glob stdlib tclcompat
C_EXT_OBJS := $(patsubst %,jim-%.o,aio array clock eventloop exec file load nvp package posix readdir regexp signal syslog) $(patsubst %,_jim-%.o,glob stdlib tclcompat)
C_EXT_SHOBJS := $(patsubst %,%.so,)
EXTENSION_TCL := $(patsubst %,%.tcl,)

.PRECIOUS: jim-%.c

all: $(JIMSH) $(C_EXT_SHOBJS)

# Create C extensions from pure Tcl extensions
_jim-%.c: %.tcl
	/usr/bin/tclsh8.5 ./make-c-ext.tcl $< >$@ || ( rm $@; exit 1)

_initjimsh.c: initjimsh.tcl
	/usr/bin/tclsh8.5 ./make-c-ext.tcl $< >$@ || ( rm $@; exit 1)

docs: Tcl.html

$(JIMSH): $(LIBJIM) jimsh.o _initjimsh.o
	$(CC) $(CFLAGS) -rdynamic $(LDFLAGS) -o $@ jimsh.o _initjimsh.o $(LIBJIM) $(LDLIBS)

ifdef install_jim
install: all docs $(EXTENSION_TCL) install-exec
	mkdir -p $(DESTDIR)$(prefix)/lib/jim
	cp $(LIBJIM) $(DESTDIR)$(prefix)/lib
	cp ./README.extensions $(C_EXT_SHOBJS) $(EXTENSION_TCL) $(DESTDIR)$(prefix)/lib/jim
	mkdir -p $(DESTDIR)$(prefix)/include
	cp ./jim.h ./jim-eventloop.h ./jim-nvp.h ./jim-signal.h \
		./jim-subcmd.h ./jim-win32compat.h $(DESTDIR)$(prefix)/include
	cp jim-config.h $(DESTDIR)$(prefix)/include
	mkdir -p $(DESTDIR)$(prefix)/doc/jim
	cp Tcl.html $(DESTDIR)$(prefix)/doc/jim

install-exec: all
	mkdir -p $(DESTDIR)$(prefix)/bin
	cp $(JIMSH) $(DESTDIR)$(prefix)/bin

uninstall:
	rm -f $(DESTDIR)$(prefix)/bin/$(JIMSH)
	rm -f $(DESTDIR)$(prefix)/lib/$(LIBJIM)
	for i in README.extensions $(C_EXT_SHOBJS) $(EXTENSION_TCL); do rm -f $(DESTDIR)$(prefix)/lib/jim/$$i; done
	rm -f $(DESTDIR)$(prefix)/include/jim*.h
	rm -f $(DESTDIR)$(prefix)/doc/jim/Tcl.html
else
install install-exec uninstall:
endif

test: $(JIMSH)
	$(DEF_LD_PATH) $(MAKE) jimsh=`pwd`/jimsh -C ./tests

$(OBJS) $(C_EXT_OBJS): Makefile

ifeq (0,1)
# Generate the unicode case mapping
utf8.o: _unicode_mapping.c

_unicode_mapping.c: ./UnicodeData.txt ./parse-unidata.tcl
	/usr/bin/tclsh8.5 ./parse-unidata.tcl ./UnicodeData.txt >$@ || ( rm $@; exit 1)
endif

_load-static-exts.c: ./make-load-static-exts.tcl Makefile
	/usr/bin/tclsh8.5 ./make-load-static-exts.tcl $(JIM_EXTENSIONS) >$@ || ( rm $@; exit 1)

ifeq ($(jim_libtype),static)
$(LIBJIM): $(OBJS) $(C_EXT_OBJS)
	$(AR) cr $@ $^
	$(RANLIB) $@
else
$(LIBJIM): $(OBJS) $(C_EXT_OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(SH_LDFLAGS) -o $@ $^ $(LDLIBS)
endif

%.so: jim-%.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(SHOBJ_CFLAGS) -c -o $*.o $^
	$(CC) $(CFLAGS) $(LDFLAGS) $(SHOBJ_LDFLAGS) -o $@ $*.o $(SH_LIBJIM) $(LDLIBS)

Tcl.html: jim_tcl.txt
	/usr/bin/tclsh8.5 ./make-index $^ | asciidoc -o $@ -d manpage - || cp ./Tcl_shipped.html Tcl.html

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

distclean: clean
	rm -f jimautoconf.h jim-config.h Makefile config.log autosetup/jimsh0

ship: Tcl.html
	cp Tcl.html 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 compatibilty - install sources from the current dir to $(distdir)
distdir_full := $(shell cd $(distdir); pwd)
distdir:
	cd "."; git ls-files | cpio -pdmu $(distdir_full)

ifeq ($(findstring clean,$(MAKECMDGOALS)),)
Makefile: /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/autosetup/autosetup /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/auto.def /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/autosetup/system.tcl /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/autosetup/cc.tcl /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/autosetup/cc-shared.tcl ./Makefile.in
	@CC='cc' /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/configure --with-jim-ext=nvp --disable-lineedit --disable-install-jim --disable-option-checking --prefix=/usr/local --enable-ft2232_libftdi --cache-file=/dev/null --srcdir=.
endif

reconfig:
	CC='cc' /mnt/home/roman/work/Рабочие/openocd-0.5.0/jimtcl/configure --with-jim-ext=nvp --disable-lineedit --disable-install-jim --disable-option-checking --prefix=/usr/local --enable-ft2232_libftdi --cache-file=/dev/null --srcdir=.
