# 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-2016 (20160515)
GNATPREPFLAGS = -c -r
TARGET=@host_alias@

ifneq ($(TARGET),)
TARGET_OPTION=--target=$(TARGET)
TARGET_PREFIX=$(TARGET)-
endif

ifeq ($(TARGET),powerpc-elinos-linux)
TARGET_RUNNER=run-cross --target=ppc-elinos,5.1,qemu
endif

# configure substitutions
ADAFLAGS                                := @ADAFLAGS@
CFLAGS                                  := @CFLAGS@
CPPFLAGS                                := @CPPFLAGS@
LDFLAGS                                 := @LDFLAGS@
LIBRARY_TYPE_OPTION                     := @LIBRARY_TYPE_OPTION@
CC = @CC@
LIBS = @LIBS@
SIGNALS_GENERATED = @SIGNALS_GENERATED@
PREFIX = @prefix@

PROJECT_FLAGS = $(LIBRARY_TYPE_OPTION) @BUILD_TYPE_OPTION@ @THREADS_OPTION@ @RTS_OPTION@ $(TARGET_OPTION)
# Scenario variables and RTS selection must be passed consistently to
# project-aware tools gprbuild, gprinstall, and gprclean.

GNATPREP = $(TARGET_PREFIX)gnatprep
GPRBUILD = gprbuild
GPRBUILD_FLAGS =

GENDIR = gensrc
GENDIR_ON_TARGET=$(GENDIR)

# files generated by "configure" script

CONFIG_GENERATED = \
 Makefile\
 confsrc/config.h\
 confsrc/pconfig.h\
 gnatprep.config\
 pconfig.h.in

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

# files generated by "c-posix" program
C-POSIX_GENERATED = \
 $(GENDIR)/posix.ads\
 $(GENDIR)/posix-limits.ads\
 $(GENDIR)/posix-options.ads\
 $(GENDIR)/posix-c.ads

# files generated by "c-posix-signals" program (if present)

ifneq "$(strip $(SIGNALS_GENERATED))" ""
C-POSIX-SIGNALS_GENERATED = $(GENDIR)/$(SIGNALS_GENERATED)
else
C-POSIX-SIGNALS_GENERATED =
endif

GNATPREP_SOURCES = $(GENDIR)/posix-implementation.adb $(GENDIR)/threads/posix-timers-extensions.adb

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

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

# 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: confsrc/config.h \
         confsrc/pconfig.h
c-posix: CPPFLAGS += -DVERSION="\"$(VERSION)\"" \
                     -DLIBS="\"$(LIBS)\"" \
                     -DGENDIR="\"$(GENDIR_ON_TARGET)\""
c-posix: LDLIBS += $(LIBS)
#
#
# Program c-posix-signals generates another Ada package spec.
#
c-posix-signals: CPPFLAGS += -DGENDIR="\"$(GENDIR_ON_TARGET)\""
c-posix-signals: LDLIBS += $(LIBS)
#
# generate Ada source files using "c-posix" program
#
$(C-POSIX_GENERATED): c-posix
	mkdir -p $(GENDIR) && cd $(GENDIR) && rm -f posix.ads posix-limits.ads posix-options.ads posix-c.ads
	# See file "c-posix.log" for results of this step.
	$(TARGET_RUNNER) ./c-posix > c-posix.log 2>&1
#
# generate Ada source files using "c-posix-signals" program
#
$(C-POSIX-SIGNALS_GENERATED): c-posix-signals
	mkdir -p $(GENDIR) && cd $(GENDIR) && rm -f posix-implementations-ok_signals.ads
	# See file "c-posix-signals.log" for results of this step.
	$(TARGET_RUNNER) ./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".
#
.SUFFIXES: .gps .gpb .ads .adb
$(GENDIR)/%.ads: libsrc/%.gps gnatprep.config
	mkdir -p `dirname $@` && $(GNATPREP) $< $@ gnatprep.config $(GNATPREPFLAGS)
$(GENDIR)/%.adb: libsrc/%.gpb gnatprep.config
	mkdir -p `dirname $@` && $(GNATPREP) $< $@ gnatprep.config $(GNATPREPFLAGS)

#
# ------------------------------------
# compilation of Florist packages
# ------------------------------------
#
force:

floristlib: $(GENERATED) force
	$(GPRBUILD) -P florist $(PROJECT_FLAGS) \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS LIBS,"-X$(v)=$($(v))") \
	  -p $(GPRBUILD_FLAGS)
#
# -----------------------------------
# Maintenance targets
# -----------------------------------
#
# remove all c-posix and gnatprep generated files
regen:
	rm -f $(GENERATED)

# remove editor and compiler generated files
clean: regen
	gprclean -Pflorist $(PROJECT_FLAGS)
	rm -rf obj lib
	rm -f *# *~ $(EXECUTABLES) b__* b~*

# remove all generated files, including configuration history
distclean: clean
	rm -f $(CONFIG_HISTORY) $(CONFIG_GENERATED)

# install floristlib
# make all files read-only to prevent recompilation
install:
	gprinstall -Pflorist $(PROJECT_FLAGS) -a -p -f --prefix=$(PREFIX) --sources-subdir=floristlib --lib-subdir=floristlib --link-lib-subdir=floristlib
	chmod a-w $(PREFIX)/floristlib/*

.PHONY: all install clean distclean regen floristlib force
