#-------------------------------------------------------------------------
#   rxtx is a native interface to serial ports in java.
#   Copyright 1997-2002 by Trent Jarvi taj@www.linux.org.uk.
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Library General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Library General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License along with this library; if not, write to the Free
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#-------------------------------------------------------------------------
#  This Makefile works on windows 98/NT with mingw32 in a DOS shell
#  One catch.  We cant figure out how to generate .def files with default
#  DOS tools and mingw32.  Install Cygwin if you are adding/removing/chaninging
#  C functions.
#  mingw build tools are used
#  PATH=c:\mingw\bin;c:\jdk118\bin;%PATH%
#  mingw32 version 1.0.1-20010726
#  jdk was 1.1.8
#  java.sun.com

######################
#  user defined variables
######################

# path to the source code (directory with SerialImp.c) Unix style path
SRC=../src
# and the dos path
DOSSRC=..\\\src

# path to the jdk directory that has include, bin, lib, ... Unix style path
JDKHOME=C:/jdk13

#path to mingw32
MINGHOME="D:/mingw"

# path to install RXTXcomm.jar DOS style path
COMMINSTALL="C:\jdk13\lib"

# path to install the shared libraries DOS style path
LIBINSTALL="C:\jdk13\bin"

# path to the mingw32 libraries (directory with libmingw32.a) DOS style path
LIBDIR="c:\cygwin\lib\mingw"

######################
#  End of user defined variables
######################


######################
#  Tools
######################

AS=as
CC=gcc
LD=ld
DLLTOOL=dlltool
# this looks like a nice tool but I was not able to get symbols in the dll.
DLLWRAP=dllwrap

CLASSPATH=-classpath ".;C:\jdk13\lib\RXTXcomm.jar;c:\BlackBox.jar;c:\jdk13\lib\classes.zip"
JAVAH=javah $(CLASSPATH)
JAR=jar 
JAVAC=javac $(CLASSPATH)

######################
#  Tool Flags
######################

CFLAGS_CYGWIN= -O2 $(INCLUDE) -DWIN32 -D __int64="long long" -mno-fp-ret-in-387  -Wall 
CFLAGS= -O2 $(INCLUDE) -mno-cygwin -DWIN32 -D __int64="long long" -mno-fp-ret-in-387  -Wall
#CFLAGS= -O2 $(INCLUDE) -mno-cygwin -DWIN32 -D __int64="long long" -mno-fp-ret-in-387 -Wall -D TRENT_IS_HERE_DEBUGGING_ENUMERATION -DTRENT_IS_HERE_DEBUGGING_THREADS
INCLUDE= -I c:\matlab6p5/extern/include/ -I . -I $(JDKINCLUDE) -I $(JDKINCLUDE)/win32 -I $(SRC) -I include -I $(MINGINCLUDE)
JAVAHFLAGS= -jni -d include
LIBS=-L . -L $(LIBDIR) -LC:/tmp/rxtx5/build -luser32 -lgdi32 -lcomdlg32 -lkernel32 -ladvapi32 -lmoldname -lcrtdll
# -lmex

# path to the java native interface headers (directory with jni.h)
JDKINCLUDE=$(JDKHOME)/include
MINGINCLUDE=$(MINGHOME)/include
JAVAFILES = $(wildcard $(SRC)/*.java)
CFILES=$(wildcard $(SRC)/*.c) $(wildcard $(SRC)/*.cc)
TARGETLIBS= rxtxSerial.dll rxtxParallel.dll
#TARGETLIBS= rxtxSerial.dll rxtxParallel.dll rxtxZystem.dll
DLLOBJECTS= fixup.o SerialImp.o termios.o init.o fuserImp.o
PARALLELOBJECTS= ParallelImp.o termios.o init.o

all:  $(TARGETLIBS) # rebuild  rebuild will force everything to be built.

rebuild:
	rm -rf gnu include RXTXcomm.jar Serial.* rxtxSerial.* * *.O.o *.O gnutimestamp 

include:
	mkdir include

gnu:
	mkdir gnu
	mkdir gnu\\\io

# yayaya We should have put the files in gnu.io to start with
gnutimestamp:	$(JAVAFILES) $(CFILES) include gnu
	xcopy /Y $(DOSSRC)\\*.* gnu\\io\\
	echo > gnutimestamp

# FIXME make 3.79.1 behaves really strage if we use %.o rules.

init.o:	
	$(CC) $(CFLAGS) -c $(SRC)/init.cc -o init.o

fixup.o:
	$(CC) $(CFLAGS) -c $(SRC)/$*.c -o fixup.o

#ZystemImp.o:  include/config.h
#	$(CC) $(CFLAGS_CYGWIN) -c $(SRC)/ZystemImp.c -o ZystemImp.o

SerialImp.o:  include/config.h
	$(CC) $(CFLAGS) -c $(SRC)/SerialImp.c -o SerialImp.o

fuserImp.o:
	$(CC) $(CFLAGS) -c $(SRC)/fuserImp.c -o fuserImp.o

termios.o:
	$(CC) $(CFLAGS) -c $(SRC)/$*.c -o termios.o

ParallelImp.o:  include/config.h
	$(CC) $(CFLAGS) -c $(SRC)/$*.c -o ParallelImp.o

#  This file is a pain in the rear to generate.  If your looking at this you
#  need to install cygwin.

#$(SRC)/Zystem.def:	$(DLLOBJECTS) ZystemImp.o
	#$(LD) --base-file Zystem.base --dll -o rxtxZystem.dll $(DLLOBJECTS) ZystemImp.o _divdi3.o $(LIBS) -lcygwin  -e _dll_entry@12
	#xcopy /Y  $(DOSSRC)\\Zystem.def gnu\\io\\


$(SRC)/Serial.def:	$(DLLOBJECTS)
	$(LD) --base-file Serial.base --dll -o rxtxSerial.dll $(DLLOBJECTS) $(LIBS) -lmingw32 -e _dll_entry@12
	xcopy /Y  $(DOSSRC)\\Serial.def gnu\\io\\

#	echo EXPORTS >$(SRC)/Serial.def;for i in `nm rxtxSerial.dll | grep "T _Java"|cut -b 13-`;do echo -n $$i|sed s#@.*##;echo "="$$i;done >> $(SRC)/Serial.def


$(SRC)/Parallel.def:	$(PARALLELOBJECTS)
	$(LD) --base-file Parallel.base --dll -o rxtxParallel.dll $(PARALLELOBJECTS) $(LIBS) -lmingw32 -e _dll_entry@12
	xcopy  $(DOSSRC)\\Parallel.def gnu\\io\\
#	echo EXPORTS >$(SRC)/Parallel.def;for i in `nm rxtxParallel.dll | grep "T _Java"|cut -b 13-`;do echo -n $$i|sed s#@.*##;echo "="$$i;done >> $(SRC)/Parallel.def


rxtxZystem.dll:	RXTXcomm.jar $(DLLOBJECTS) #ZystemImp.o $(SRC)/Zystem.def
	gcc  -c -DBUILDING_DLL=1 -D_DLL=1  -I. -Ic:/jdk13/include -Ic:/jdk13/include/win32  -Wall -O2 -D __int64="long long" -o ZystemImp.o $(SRC)/ZystemImp.c
	dllwrap --verbose --no-default-excludes --export-all-symbols --output-exp Zystem.exp --output-def Zystem.def --add-stdcall-alias --driver-name gcc  -o rxtxZystem.dll ZystemImp.o -Wl,-e,__cygwin_noncygwin_dll_entry@12 -s

#rxtxZystem.dll:	RXTXcomm.jar $(DLLOBJECTS) ZystemImp.o $(SRC)/Zystem.def
	#$(LD) --base-file Zystem.base --dll -o rxtxZystem.dll $(DLLOBJECTS) \
		#ZystemImp.o _divdi3.o $(LIBS) -lcygwin -e _dll_entry@12
	#$(DLLTOOL) --as=$(AS) --dllname rxtxZystem.dll --def $(SRC)/Zystem.def \
		#--base-file Zystem.base --output-exp Zystem.exp
	#$(LD) --base-file Zystem.base Zystem.exp -dll -o rxtxZystem.dll \
		#$(DLLOBJECTS) ZystemImp.o _divdi3.o $(LIBS) -lcygwin -e _dll_entry@12
	#$(DLLTOOL) --as=$(AS) --dllname rxtxZystem.dll --def $(SRC)/Zystem.def \
		#--base-file Zystem.base --output-exp Zystem.exp
	#$(LD) --base-file Zystem.base Zystem.exp -dll -o rxtxZystem.dll \
		#$(DLLOBJECTS) ZystemImp.o _divdi3.o $(LIBS) -lcygwin -e _dll_entry@12

rxtxSerial.dll:	RXTXcomm.jar $(DLLOBJECTS)  $(SRC)/Serial.def
	$(LD) --base-file Serial.base --dll -o rxtxSerial.dll $(DLLOBJECTS) \
		$(LIBS) -lmingw32 -e _dll_entry@12
	$(DLLTOOL) --as=$(AS) --dllname rxtxSerial.dll --def $(SRC)/Serial.def \
		--base-file Serial.base --output-exp Serial.exp
	$(LD) --base-file Serial.base Serial.exp -dll -o rxtxSerial.dll \
		$(DLLOBJECTS) $(LIBS) -lmingw32 -e _dll_entry@12
	$(DLLTOOL) --as=$(AS) --dllname rxtxSerial.dll --def $(SRC)/Serial.def \
		--base-file Serial.base --output-exp Serial.exp
	$(LD) --base-file Serial.base Serial.exp -dll -o rxtxSerial.dll \
		$(DLLOBJECTS) $(LIBS) -lmingw32 -e _dll_entry@12
#
# This should replace the mess above if it worked.
# nm shows no symbols in the dll produced.  The old stuff above works ok.
#	$(DLLWRAP) --output-def $*.def --output-exp $*.exp \
#		--add-stdcall-alias --driver-name gcc -mwindows \
#		--target=i386-mingw32 -o $*.dll $(DLLOBJECTS) $(LIBS) -s
#		-mno-cygwin

rxtxParallel.dll: $(PARALLELOBJECTS)
	$(LD) --base-file Parallel.base --dll -o rxtxParallel.dll $(PARALLELOBJECTS) \
		$(LIBS) -lmingw32 -e _dll_entry@12
	$(DLLTOOL) --as=$(AS) --dllname rxtxParallel.dll --def $(SRC)/Parallel.def \
		--base-file Parallel.base --output-exp Parallel.exp
	$(LD) --base-file Parallel.base Parallel.exp -dll -o rxtxParallel.dll \
		$(PARALLELOBJECTS) $(LIBS) -lmingw32 -e _dll_entry@12
	$(DLLTOOL) --as=$(AS) --dllname rxtxParallel.dll --def $(SRC)/Parallel.def \
		--base-file Parallel.base --output-exp Parallel.exp
	$(LD) --base-file Parallel.base Parallel.exp -dll -o rxtxParallel.dll \
		$(PARALLELOBJECTS) $(LIBS) -lmingw32 -e _dll_entry@12

RXTXcomm.jar:	gnutimestamp
	$(JAVAC) gnu\\io\\*.java
	$(JAR) -cf RXTXcomm.jar gnu\\io\\*.class
	$(JAVAH) $(JAVAHFLAGS) $(patsubst gnu/io/%.java,gnu.io.%,$(wildcard gnu/io/*.java))

include/config.h:  gnutimestamp
	echo "#define HAVE_FCNTL_H 1" > include\\\config.h
	echo "#define HAVE_SIGNAL_H 1" >> include\\\config.h
	echo "#define HAVE_SYS_FCNTL_H 1" >> include\\\config.h
	echo "#define HAVE_SYS_FILE_H 1" >> include\\\config.h
	echo "#undef HAVE_SYS_SIGNAL_H" >> include\\\config.h
	echo "#undef HAVE_TERMIOS_H" >> include\\\config.h

install: all
	xcopy RXTXcomm.jar $(COMMINSTALL)
	xcopy RXTXcomm.jar "e:\matlab~1\java\jarext\commapi\win32"
	xcopy $(TARGETLIBS) $(LIBINSTALL)
	xcopy $(TARGETLIBS) "e:\matlab~1\bin\win32"

uninstall:
	del $(COMMINSTALL)\\\RXTXcomm.jar
	del $(LIBINSTALL)\\\$(TARGETLIBS)

clean:
	deltree gnu\\\io\\\*.*
	deltree include
	del Serial.* gnutimestamp *.o *.O RXTXcomm.jar rxtxSerial.*