## This is just the *preliminary* part of the general 'cross-makefile'.
## It's designed for different OS (linux,darwin and mingw)

## Expected:
##   OS : linux, darwin, win
##   ARCH: x64
##   TCL_MAJOR_VERSION:  tcl-8 or tcl-9

TARGET:=$(OS)-$(ARCH)

ifndef BUILD_DIR
BUILD_DIR=XBUILD
endif

## set some macros for OS-Dependent commands.
## -- Macros for Unix commands --------------

X_COMMENT=\#
X_RMDIR=rm -rf
X_MKDIR=mkdir -p
X_COPY=cp
X_RM=rm
ifeq ($(OS), win)
X_COPYTREE=cp --verbose -uRf -T
else ifeq ($(OS), linux)
X_COPYTREE=cp --verbose -uRf -T
else ifeq ($(OS), darwin)
X_COPYTREE=cp -v -Rf
endif

STUB_DIR=BuildSupport/$(TCL_MAJOR_VER)/$(TARGET)

#
# pattern Rule: from *.c to *.obj :
#
.SUFFIXES: .c .h

# Inference Rule: from  any  to *.h :
src/%.h :
	touch $@

# Inference Rule: from *.h to *.c
src/%.c : src/%.h
	touch $@


## Set several macro depending on OS, ARCH ...
##  DLL     :  the full name of the final shared library

ifeq ($(TCL_MAJOR_VER), tcl-8)
  VTAG=86
  VSTUB=8.6
else ifeq ($(TCL_MAJOR_VER), tcl-9)
  VTAG=90
  VSTUB=9.0
else
  $(error ERROR: Unrecognized TCL_MAJOR_VER "$(TCL_MAJOR_VER)" )
endif


LIB_OPTIONS=\
	-L$(STUB_DIR)  -ltclstub$(VSTUB) \
	-LBuildSupport/mupdf/$(TARGET) -lmupdf  \
    -LBuildSupport/mupdf/$(TARGET) -lmupdf-third \
    -lm

TCL_LIB_OPTIONS=$(LIB_OPTIONS)
TK_LIB_OPTIONS=$(LIB_OPTIONS) -L$(STUB_DIR) -ltkstub$(VSTUB)



ifeq ($(OS), win)


DLL_TCL=tclMuPDF$(VTAG).dll
DLL_TK=tkMuPDF$(VTAG).dll

else ifeq ($(OS), linux)
DLL_TCL=tclMuPDF$(VTAG).so
DLL_TK=tkMuPDF$(VTAG).so

else ifeq ($(OS), darwin)
DLL_TCL=tclMuPDF$(VTAG).dylib
DLL_TK=tkMuPDF$(VTAG).dylib

else
$(error ERROR: Unrecognized OS "$(OS)" )

endif


ifeq ($(ARCH), x64)
CC_TARGET=-m64
else
$(error ERROR: Unrecognized ARCH "$(ARCH)" )
endif


CC=cc  $(CC_TARGET)

## Options for CC :
 ##   -O3 - (optional) - maximum optimizations
 ##  -fPIC ...  ??
override CFLAGS += -O3 -fPIC



OBJ_DIR=$(BUILD_DIR)/$(TCL_MAJOR_VER)/$(TARGET)

# NOTE: the following macros, common for all platforms,
#   CC_TCL_OPTIONS
#   CC_TK_OPTIONS
# will be defined later in cross-makefile

$(OBJ_DIR)/%.obj :  src/%.c
	$(CC) -c $(CC_TCL_OPTIONS) $< -o $@ 

$(OBJ_DIR)/%.tkobj :  src/%.c
	$(CC) -c $(CC_TK_OPTIONS) $< -o $@ 


## Commands and options for linking

ifeq ($(OS), win)
LINK=$(CC) -shared -Wl,--no-undefined,--add-stdcall-alias

else ifeq ($(OS), linux)
LINK=$(CC) -shared -Wl,--no-undefined

else ifeq ($(OS), darwin)
LINK=$(CC) -shared

endif

LINK_OUT_OPTION=-o 

    
include cross-makefile
