# This builds the "Florist" implementation of POSIX.5b
#
# If you need to modify the "configure" file, you will need
# autoconf version 2.12, or maybe a later version will also work.
#
.POSIX:
VERSION=Florist-gpl-2014 (20140405)
FLORIST_VERSION=$(VERSION)
GNATPREPFLAGS = -c -r
GCCFLAGS = -O2
GNATMAKE=gnatmake

# How to invoke ranlib.
RANLIB = ranlib

# The following will be set based on
# what the configure script discovers about the local system.
CC = @CC@
LIBS = @LIBS@
DEPS = @DEPS@
ADDRINFO_OBJECTS = @ADDRINFO_OBJECTS@
SIGNALS_GENERATED = @SIGNALS_GENERATED@
PREFIX = @prefix@

# files generated by "configure" script

CONFIG_GENERATED = \
 config.h\
 pconfig.h\
 gnatprep.config\
 pconfig.h.in

CONFIG_HISTORY = \
 config.cache\
 config.log\
 c-posix.log\
 configure.log\
 config.status\
 stamp-h

# files generated by "c-posix" program
C-POSIX_GENERATED = \
 posix.ads\
 posix-limits.ads\
 posix-options.ads\
 posix-c.ads

# files generated by "c-posix-signals" program
C-POSIX-SIGNALS_GENERATED = $(SIGNALS_GENERATED)

GNATPREP_SOURCES = posix-implementation.adb posix-timers-extensions.adb

GENERATED = $(CONFIG_GENERATED) $(C-POSIX_GENERATED) $(GNATPREP_SOURCES) \
 $(C-POSIX-SIGNALS_GENERATED)

# all the executable programs
EXECUTABLES = c-posix c-posix-signals

MACRO_OBJECTS = posix-macros.o posix-macros-sockets.o

# default target, get the gnat version and the system type and then
# set up files and call make again (in uname)

all: floristlib

#
# ------------------------------------
# configure-script
# ------------------------------------
#
# This first step should not need to be done during
# normal installation
#
configure: configure.in aclocal.m4
	autoconf
#
# ------------------------------------
# generation of OS dependent sources
# ------------------------------------
#
# Program c-posix generates some Ada package specs.
# It is supposed to be a "portable" POSIX C program.
# If it does not compile or does not link,
# it might be fixable by hand-editing config.h or pconfig.h.
# If you are unlucky, it will require fixes to c-posix.c.
#
c-posix: c-posix.c config.h pconfig.h
	$(CC) $(GCCFLAGS) -DVERSION="\"$(VERSION)\"" -DLIBS="\"$(LIBS)\"" -o c-posix c-posix.c $(LIBS)
#
# Program c-posix-signals generates another Ada package spec.
#
c-posix-signals: c-posix-signals.c
	$(CC) $(GCCFLAGS) -o c-posix-signals \
	    c-posix-signals.c $(LIBS)
#
# generate Ada source files using "c-posix" program
#
$(C-POSIX_GENERATED): c-posix
	rm -f posix.ads posix-limits.ads posix-options.ads posix-c.ads\
	tmpposix.ads
	# See file "c-posix.log" for results of this step.
	./c-posix > c-posix.log 2>&1
	rm -f tmpposix.ads
#
# generate Ada source files using "c-posix-signals" program
#
posix-implementation-ok_signals.ads: c-posix-signals
	rm -f posix-implementations-ok_signals.ads
	# See file "c-posix-signals.log" for results of this step.
	./c-posix-signals -nodefaults > c-posix-signals.log 2>&1

#
# Some other Ada source files are tailored to the
# particular OS using the "gnatprep" program.
# The sources for these end in ".gpb" and ".gps".
#
$(GNATPREP_SOURCES): gnatprep.config
.SUFFIXES: .gps .gpb .ads .adb .o .c
.gps.ads:
	gnatprep $< $@ gnatprep.config $(GNATPREPFLAGS)
.gpb.adb:
	gnatprep $< $@ gnatprep.config $(GNATPREPFLAGS)
.c.o:
	$(CC) -c $(GCCFLAGS) $<

.PHONY: deps deps_no_thread

#
#
# Some POSIX interfaces use macros.
# To make these usable from Ada, we have C subprograms.
#
$(MACRO_OBJECTS): pconfig.h
#
# Some POSIX interfaces use macros.
# To make these usable from Ada, we have C subprograms.
#
$(ADDRINFO_OBJECTS): pconfig.h
#
# ------------------------------------
# compilation of Florist packages
# ------------------------------------
#
# "deps.adb" is dummy main program, with dependences
# that should force compilation of all Florist packages;
# "deps5c.adb" is the same, but with POSIX.5c packages added.
#
deps deps_no_thread: $(GENERATED)
	$(GNATMAKE) -c -Pflorist_build $@
#
# -----------------------------------
# Create a florist library for objects
# -----------------------------------
# 
floristlib: $(MACRO_OBJECTS) $(DEPS) $(ADDRINFO_OBJECTS)
	@echo "Creating libflorist.a in directory floristlib"
	@if [ -d floristlib ]; then rm -rf floristlib; fi
	mkdir floristlib
	cp -p *.ads floristlib
	cp -p *.adb floristlib
	(tar cpf - *.o *.ali) | (cd floristlib; tar xpf -)
	rm -f floristlib/$(DEPS).o floristlib/$(DEPS).ali
	ar -r floristlib/libflorist.a floristlib/*.o
	-$(RANLIB) floristlib/libflorist.a
	chmod 444 floristlib/*.ali
	rm -f floristlib/*.o
#
# -----------------------------------
# Maintenance targets
# -----------------------------------
#
# remove all c-posix and gnatprep generated files
regen:
	rm -f $(C-POSIX_GENERATED)
# remove editor and compiler generated files
clean:
	rm -rf floristlib
	rm -f *.o *.ali a.out *# *~ $(EXECUTABLES) b_*.c b~* *.dSYM
	rm -f $(C-POSIX_GENERATED) $(GNATPREP_SOURCES)

# remove all generated files, including configuration history
distclean:
	rm -f  $(CONFIG_HISTORY)
	rm -rf floristlib
	rm -f *.o *.ali a.out *# *~ $(EXECUTABLES) b_*.c b~*
	rm -f $(GENERATED) $(GNATPREP_SOURCES)

# install floristlib
install:
	mkdir -p $(PREFIX)/lib/gnat
	cp -pr floristlib $(PREFIX)/floristlib
	cp -p florist.gpr $(PREFIX)/lib/gnat

.PHONY: install clean distclean regen
