## ----------------------------------------------------------------------------
##                                                                           --
##                      GNADE  : GNu Ada Database Environment                --
##                                                                           --
##  Filename        : $Source: /var/cvs/gnuada/gnade/win32/GNUmakefile,v $
##  Description     : The top level makefile of GNADE
##  Author          : Sune Falck  <sunef@hem.passagen.se>  
##  Created On      : 28-3-2001 
##  Last Modified By: Sune Falck
##  Last Modified On: 2001-05-29 19:10
##  Status          : $State: Exp $
##                                                                           --
##  Copyright (C) 2000-2001
##
##  GNADE is copyrighted by the persons and institutions enumerated in the   --
##  AUTHORS file. This file is located in the root directory of the          --
##  GNADE distribution.                                                      --
##                                                                           --
##  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 NT 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 ./win32
##  - Build simple.exe, dynamic.exe and odbctest.exe in the current directory 
##
##
##########################################################################
##
## Targets: all	(default)       create directory ../adaodbc and build gesql
##          clean               removes created files and directories
##          mssqldb             Install test database in MS Sql Server
##          unsinstall_mssqldb  Uninstall test database in MS Sql Server
##
##########################################################################

.PHONY: all clean mssqldb uninstall_mssqldb build FORCE

include ../VERSION

VERSION=$(GNADE_MAJOR).$(GNADE_MINOR).$(GNADE_PATCHLEVEL)
DEBUG=True
#
# Directory to where to place all gnade/adaodbc files
#
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
#
# Use the environment variable windir to select between Windows NT and
# Unix, it is always set in Windows NT
#
ifneq ($(windir),)
# NT Commands 
ifneq ($(PROCESSOR_ARCHITECTURE),)
 CMD = cmd /c
 COPY  = $(CMD) copy
 DEL   = $(CMD) del /q
 RMDIR = $(CMD) rmdir /q /s
 TO_NUL= > NUL:
# Windows 9X Commands
else
 CMD = command /c
 COPY  = $(CMD) copy /y
 DEL   = $(CMD) del
 RMDIR = $(CMD) deltree /y
 TO_NUL= 
 SEP   = "\"
endif
 MKDIR = $(CMD) mkdir
 SEP   = \\
 EXE   = .exe

 CONVENTION=Stdcall
 LIBID=odbc32
else
# unix
 CMD   = sh
 CD    = cd
 MKDIR = mkdir
 DEL   = rm -f
 RMDIR = rm -rf
 COPY  = cp
 TO_NUL=
 SEP   = /
 EXE   =
 CONVENTION=C
 LIBID=odbc
endif
#
# Compiler options etc
#
ADAPREP = gnatprep
ADAMAKE = gnatmake
ifneq ($(DEBUG),True)
AMFLAGS = -O2 -gnatf -gnatwl
AFLAGS = $(AMFLAGS) -i
LARGS  = -largs -Wl,-s
else
AMFLAGS=-g -gnata -gnatf -gnatwl
AFLAGS =$(AMFLAGS) -i
LARGS  =
endif
#
# Rules
#
define do-copy
@$(COPY) $(subst /,$(SEP),$<) $(subst /,$(SEP),$@) $(TO_NUL)
endef

define do-delete
ifneq ($(windir),)
-@$(CMD) /C  IF EXIST $< DEL $<
else
$(DEL) $<
endif
endef

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

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

$(TARGETDIR)/%.adb: $(ODBC)/%.gpb
	@$(ADAPREP) -DCALLCONVENTION=$(CONVENTION) -DLIBID=\"-l$(LIBID)\" \
	-DDEBUG=$(DEBUG) -DUNICODE=True $< $@

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

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

%.adb: %.adq
	.$(SEP)gesql -v $<

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

%$(EXE): $(SAMPLES)/esql/%.adb
	gnatmake -I$(TARGETDIR) -o $@ $< $(LARGS)

#
# Files
#
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 all_modules.adb
SRC := $(addprefix $(TARGETDIR)/,$(SRC))
#
# Dependencies
#
all: 	$(TARGETDIR) $(SRC) $(TARGETDIR)/all_modules.ali gesql$(EXE) \
	simple$(EXE) dynamic$(EXE) demo$(EXE)

$(TARGETDIR):
	@$(MKDIR) $(subst /,$(SEP),$(TARGETDIR))

#
# Recursive make to eliminate the need for cd && ... wich does not
# work with Windows 9X
#
$(TARGETDIR)/all_modules.ali: FORCE
	@$(MAKE) -C $(TARGETDIR) -f $(CURDIR)/GNUmakefile build
build:
	$(ADAMAKE) -c $(AFLAGS) all_modules

gesql$(EXE): FORCE gesql.adb
	gnatmake -o gesql -I$(TARGETDIR) -I../esql gesql.adb $(LARGS)

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
	$(ADAMAKE) $(AMFLAGS) -I$(TARGETDIR) -o $@ $< $(LARGS)
#
# 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:
	@-$(DEL) *.o
	@-$(DEL) *.ali
	@-$(DEL) b~*.ad?
	@-$(DEL) gesql.adb
	@-$(DEL) gesql$(EXE)
	@-$(DEL) simple$(EXE)
	@-$(DEL) dynamic$(EXE)
	@-$(DEL) demo$(EXE)
	@-$(DEL)   $(subst /,$(SEP),$(SAMPLES)/esql/simple.adq)
	@-$(DEL)   $(subst /,$(SEP),$(SAMPLES)/esql/simple.adb)
	@-$(DEL)   $(subst /,$(SEP),$(SAMPLES)/esql/dynamic.adq)
	@-$(DEL)   $(subst /,$(SEP),$(SAMPLES)/esql/dynamic.adb)
	@-$(DEL)   $(subst /,$(SEP),$(SAMPLES)/odbc/demo.adb)
	@-$(RMDIR) $(subst /,$(SEP),$(TARGETDIR))
