## ----------------------------------------------------------------------------
##                                                                           --
##                      GNADE  : GNu Ada Database Environment                --
##                                                                           --
##  Description     : The top level Windows makefile for GNADE
##  Maintainer      : Stephen Leake <stephen_leake@acm.org> 
##                                                                           --
##  Copyright (C) 2000-2004 Stephen Leake
##
##  GNADE is copyrighted by the persons and institutions enumerated in the   --
##  AUTHORS file.
##                                                                           --
##  GNADE is free software;  you can redistribute it  and/or modify it under --
##  terms of the  GNU General Public License as published  by the Free Soft- --
##  ware  Foundation;  either version 2,  or (at your option) any later ver- --
##  sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
##  OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
##  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
##  for  more details.  You should have  received  a copy of the GNU General --
##  Public License  distributed with GNAT;  see file COPYING.  If not, write --
##  to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
##  MA 02111-1307, USA.                                                      --
##                                                                           --
##  As a special exception,  if other files  instantiate  generics from      --
##  GNADE Ada units, or you link GNADE Ada units or libraries with other     --
##  files  to produce an executable, these  units or libraries do not by     --
##  itself cause the resulting  executable  to  be covered  by the  GNU      --
##  General  Public  License.  This exception does not however invalidate    --
##  any other reasons why  the executable file  might be covered by the      --
##  GNU Public License.                                                      --
##                                                                           --
##  GNADE is implemented to work with GNAT, the GNU Ada compiler.            --
##                                                                           --
## ----------------------------------------------------------------------------
##
##  Functional Description
##  ======================
##
##  Makefile for gnade for Windows using native tools and GNU Make from
##  the GNAT distribution. It does the following:
##
##  - Copies all library source to ../adaodbc
##  - Preprocesses some files to ../adaodbc
##  - Compiles the sources and creates a library libadaodbc.a.
##  - Builds gesql.exe in the current directory
##  - Build simple.exe, dynamic.exe and demo.exe in the current directory 
##
##
##########################################################################
##
## Targets: all	(default)       create directory ../adaodbc and build gesql
##          mysql               build mysql stuff (not in 'all')
##          clean               removes created files and directories
##          mssqldb             Install test database in MS Sql Server
##          unsinstall_mssqldb  Uninstall test database in MS Sql Server
##
## Command line variables:
##
## DEBUG : [True | False] default False.
## 
##########################################################################

#### User customizable settings ####

## MySQL config
## version: MYSQL4 | MYSQL3
MYSQL_VERSION := MYSQL4
## library location (for -L link option)
MYSQL_LIB := c:/Apps/MySQL/lib/debug

#### end user customizable settings ####

all: prep build gesql.exe simple.exe dynamic.exe demo.exe

.PHONY: all clean mysql mssqldb uninstall_mssqldb build FORCE

include ../VERSION

VERSION=$(GNADE_MAJOR).$(GNADE_MINOR).$(GNADE_PATCHLEVEL)
DEBUG=False
#
# Directory to where to place all gnade/adaodbc files
#
ADAODBC_TARGETDIR = ../adaodbc
#
# Login information for gesql test programs
#
DBUSER   = gnade
DBPASSWD = gnade
DBSOURCE = DEMO_DB
#
# Source directories
#
SUPPORT = ../support
ODBC    = ../dbi/odbc
ESQL    = ../esql
SAMPLES = ../samples

# Figure out which flavor of Windows we're on.

ifneq ($(PROCESSOR_ARCHITECTURE),)
# NT Commands 
 CMD = cmd.exe /c
 COPY  = $(CMD) copy
 DEL   = $(CMD) del /q
 RMDIR = $(CMD) rmdir /q /s
 TO_NUL= > NUL:
else
# Windows 9X Commands
 CMD = command.com /c
 COPY  = $(CMD) copy /y
 DEL   = $(CMD) del
 RMDIR = $(CMD) deltree /y
 TO_NUL= 
endif

MKDIR = $(CMD) mkdir

#
# Compiler options etc
#

ADAPREP := gnatprep -r -b

ifeq ($(DEBUG),True)
AMFLAGS=-g -gnata -gnato -gnatf -gnatwa -gnatwL
else
AMFLAGS = -O2 -gnatf
endif
#
# Rules
#
define do-copy
$(COPY) $(subst /,\\,$<) $(subst /,\\,$@) $(TO_NUL)
endef

$(ADAODBC_TARGETDIR)/%: $(SUPPORT)/%
	$(do-copy)

$(ADAODBC_TARGETDIR)/%: $(ODBC)/%
	$(do-copy)

$(ADAODBC_TARGETDIR)/%.adb: $(ODBC)/%.gpb
	$(ADAPREP) -DCALLCONVENTION=Stdcall -DLINKOPT=\"-lodbc32\" \
	-DDEBUG=$(DEBUG) -DUNICODE=True $< $@

$(ADAODBC_TARGETDIR)/%: $(ESQL)/%
	$(do-copy)

$(ADAODBC_TARGETDIR)/%.adb: %.adb
	$(do-copy)

%.adb: %.adq
	.\\gesql -v $<

%.adq: %.gpq
	gnatprep -DDBUSER=\"$(DBUSER)\" -DDBPASSWD=\"$(DBPASSWD)\" \
	-DDBSOURCE=\"$(DBSOURCE)\" $< $@

%.exe: ../samples/esql/%.adb
	gnatmake $(AMFLAGS) -I$(ADAODBC_TARGETDIR) -o $@ $< 

#
# SQL and ODBC stuff
#
SRC := $(notdir $(wildcard $(SUPPORT)/*.ad?)) 
SRC += $(notdir $(wildcard $(ODBC)/*.ad?))
SRC += $(notdir $(wildcard $(ODBC)/*.gp?))
SRC := $(subst .gpb,.adb,$(SRC))
SRC += $(notdir $(wildcard $(ESQL)/gnu-db-*.ad?))
SRC += sql_standard.ads 
SRC := $(addprefix $(ADAODBC_TARGETDIR)/,$(SRC))

prep: $(ADAODBC_TARGETDIR) $(SRC) $(ADAODBC_TARGETDIR)/gnade.gpr 

$(ADAODBC_TARGETDIR):
	$(MKDIR) $(subst /,\\,$(ADAODBC_TARGETDIR))

$(ADAODBC_TARGETDIR)/gnade.gpr: $(ODBC)/gnade.gpr.gp
	gnatprep -DDEBUG=$(DEBUG) $< $@

build:	FORCE
	gnatmake -k -c -P$(ADAODBC_TARGETDIR)/gnade.gpr 

## MySQL stuff

mysql: prep ../dbi/mysql/gnu-db-mysql.adb testdb.out

# calling convention documented as __cdecl in c:/Apps/MySQL/lib/Readme
# GNAT doesn't support cdecl, so just use C
../dbi/mysql/gnu-db-mysql.adb: ../dbi/mysql/gnu-db-mysql.gpb
	$(ADAPREP) -D$(MYSQL_VERSION) -DCALLCONVENTION=C $< $@

../samples/mysql/testdb.adb: ../samples/mysql/testdb.gpb
	$(ADAPREP) -DDBUSER=\"$(DBUSER)\" -DDBPASSWD=\"$(DBPASSWD)\" -DDBNAME=\"$(DBSOURCE)\" $< $@

testdb.exe: ../samples/mysql/testdb.adb ../dbi/mysql/gnu-db-mysql.adb 
	gnatmake $(AMFLAGS) -I$(ADAODBC_TARGETDIR) -I../dbi/mysql -o $@ $< -largs -L$(MYSQL_LIB) -lmySQL

testdb.out : testdb.exe
	./testdb.exe > testdb.out

## gesql stuff
gesql.exe: FORCE gesql.adb
	gnatmake -o gesql -I$(ADAODBC_TARGETDIR) -I../esql gesql.adb 

gesql.adb: $(ESQL)/gesql.gpb ../VERSION
	$(ADAPREP) -DVersion=\"$(VERSION)\" $< $@

#
# Some sample programs
#
../samples/esql/simple.adq: ../samples/esql/simple.gpq

simple.exe: ../samples/esql/simple.adb gesql.exe FORCE 

../samples/esql/dynamic.adq: ../samples/esql/dynamic.gpq

dynamic.exe: ../samples/esql/dynamic.adb gesql.exe FORCE

../samples/odbc/demo.adb: ../samples/odbc/demo.gpb
	$(ADAPREP) -DDBUSER=\"$(DBUSER)\" -DDBPASSWD=\"$(DBPASSWD)\" -DDBSOURCE=\"$(DBSOURCE)\" $< $@

demo.exe: ../samples/odbc/demo.adb FORCE
	gnatmake -I$(ADAODBC_TARGETDIR) -o $@ $< 

#
# Install/uninstall test database in MS Sql Server
#
mssqldb:
	cscript //NoLogo //JOB:install gensql.wsf

uninstall_mssqldb:
	-$(DEL) mssql.qry
	-cscript //NoLogo //JOB:uninstall gensql.wsf
#
# Remove all
#
clean: clean-mysql
	-$(DEL) *.o
	-$(DEL) *.ali
	-$(DEL) b~*.ad?
	-$(DEL) gesql.adb
	-$(DEL) gesql.exe
	-$(DEL) simple.exe
	-$(DEL) dynamic.exe
	-$(DEL) demo.exe
	-$(DEL)   $(subst /,\\,../samples/esql/simple.adq)
	-$(DEL)   $(subst /,\\,../samples/esql/simple.adb)
	-$(DEL)   $(subst /,\\,../samples/esql/dynamic.adq)
	-$(DEL)   $(subst /,\\,../samples/esql/dynamic.adb)
	-$(DEL)   $(subst /,\\,../samples/odbc/demo.adb)
	-$(RMDIR) $(subst /,\\,$(ADAODBC_TARGETDIR))

clean-mysql :
	-$(DEL) testdb.exe
	-$(DEL) testdb.out
	-$(DEL)   $(subst /,\\,../dbi/mysql/gnu-db-mysql.adb)
	-$(DEL)   $(subst /,\\,../samples/mysql/testdb.adb)

#Local Variables:
#eval: (ada-parse-prj-file "../gnade.adp")
#mode: makefile
#End:
