## 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

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

## Set several macro depending on OS, ARCH ...
##  PATH_SEP:  character used in PATH (or CLASSPATH) to separate paths
##  DLL     :  the full name of the final shared library


ifeq ($(OS), win)
PATH_SEP=;
DLL_TCL=tclMuPDF.dll
DLL_TK=tkMuPDF.dll

else ifeq ($(OS), linux)
PATH_SEP=:
DLL_TCL=libtclMuPDF.so
DLL_TK=libtkMuPDF.so

else ifeq ($(OS), darwin)
PATH_SEP=:
DLL_TCL=libtclMuPDF.dylib
DLL_TK=libtkMuPDF.dylib

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

endif


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

ifndef BUILDDIR
BUILDDIR=XBUILD
endif

PKGDIR=$(BUILDDIR)/tclMuPDF
CC=cc  $(CC_TARGET)

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

## 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

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

# Inference Rule: from  any  to *.h :
$(DIR_CSRC)/%.h :
	touch $@
    
# Inference Rule: from *.h to *.c
$(DIR_CSRC)/%.c : $(DIR_CSRC)/%.h
	touch $@


DIR_OBJ=$(BUILDDIR)/$(TARGET)

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

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

$(DIR_OBJ)/%.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 
LIB_OPTIONS=\
	-LBuildSupport/tcltk/$(TARGET)  -ltclstub8.6 \
	-LBuildSupport/mupdf/$(TARGET)  -lmupdf  \
    -LBuildSupport/mupdf/$(TARGET)  -lmupdf-third \
    -lm

TCL_LIB_OPTIONS=$(LIB_OPTIONS)
TK_LIB_OPTIONS=$(LIB_OPTIONS) -LBuildSupport/tcltk/$(TARGET) -ltkstub8.6
    
include cross-makefile
