# Dependencies for compiler sources that are generated at build time

# Note: can't use ?= here, not supported by older versions of GNU Make

ifeq ($(origin ADA_GEN_SUBDIR), undefined)
ADA_GEN_SUBDIR=ada
endif

ifeq ($(origin CP), undefined)
CP=cp
endif

ifeq ($(origin MKDIR), undefined)
MKDIR=mkdir -p
endif

ifeq ($(origin MOVE_IF_CHANGE), undefined)
MOVE_IF_CHANGE=mv -f
endif

.PHONY: ada_extra_files
ada_extra_files : $(ADA_GEN_SUBDIR)/treeprs.ads $(ADA_GEN_SUBDIR)/einfo.h $(ADA_GEN_SUBDIR)/sinfo.h $(ADA_GEN_SUBDIR)/nmake.adb \
	$(ADA_GEN_SUBDIR)/nmake.ads $(ADA_GEN_SUBDIR)/snames.ads $(ADA_GEN_SUBDIR)/snames.adb $(ADA_GEN_SUBDIR)/snames.h

# We delete the files before copying, below, in case they are read-only.

$(ADA_GEN_SUBDIR)/treeprs.ads : $(ADA_GEN_SUBDIR)/treeprs.adt $(ADA_GEN_SUBDIR)/sinfo.ads $(ADA_GEN_SUBDIR)/xtreeprs.adb
	-$(MKDIR) $(ADA_GEN_SUBDIR)/bldtools/treeprs
	$(RM) $(addprefix $(ADA_GEN_SUBDIR)/bldtools/treeprs/,$(notdir $^))
	$(CP) $^ $(ADA_GEN_SUBDIR)/bldtools/treeprs
	(cd $(ADA_GEN_SUBDIR)/bldtools/treeprs; gnatmake -q xtreeprs ; ./xtreeprs treeprs.ads )
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/treeprs/treeprs.ads $(ADA_GEN_SUBDIR)/treeprs.ads

$(ADA_GEN_SUBDIR)/einfo.h : $(ADA_GEN_SUBDIR)/einfo.ads $(ADA_GEN_SUBDIR)/einfo.adb $(ADA_GEN_SUBDIR)/xeinfo.adb $(ADA_GEN_SUBDIR)/ceinfo.adb
	-$(MKDIR) $(ADA_GEN_SUBDIR)/bldtools/einfo
	$(RM) $(addprefix $(ADA_GEN_SUBDIR)/bldtools/einfo/,$(notdir $^))
	$(CP) $^ $(ADA_GEN_SUBDIR)/bldtools/einfo
	(cd $(ADA_GEN_SUBDIR)/bldtools/einfo; gnatmake -q xeinfo ; ./xeinfo einfo.h )
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/einfo/einfo.h $(ADA_GEN_SUBDIR)/einfo.h

$(ADA_GEN_SUBDIR)/sinfo.h : $(ADA_GEN_SUBDIR)/sinfo.ads $(ADA_GEN_SUBDIR)/sinfo.adb $(ADA_GEN_SUBDIR)/xsinfo.adb $(ADA_GEN_SUBDIR)/csinfo.adb
	-$(MKDIR) $(ADA_GEN_SUBDIR)/bldtools/sinfo
	$(RM) $(addprefix $(ADA_GEN_SUBDIR)/bldtools/sinfo/,$(notdir $^))
	$(CP) $^ $(ADA_GEN_SUBDIR)/bldtools/sinfo
	(cd $(ADA_GEN_SUBDIR)/bldtools/sinfo; gnatmake -q xsinfo ; ./xsinfo sinfo.h )
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/sinfo/sinfo.h $(ADA_GEN_SUBDIR)/sinfo.h

$(ADA_GEN_SUBDIR)/snames.h $(ADA_GEN_SUBDIR)/snames.ads $(ADA_GEN_SUBDIR)/snames.adb : $(ADA_GEN_SUBDIR)/stamp-snames ; @true
$(ADA_GEN_SUBDIR)/stamp-snames : $(ADA_GEN_SUBDIR)/snames.ads-tmpl $(ADA_GEN_SUBDIR)/snames.adb-tmpl $(ADA_GEN_SUBDIR)/snames.h-tmpl $(ADA_GEN_SUBDIR)/xsnamest.adb $(ADA_GEN_SUBDIR)/xutil.ads $(ADA_GEN_SUBDIR)/xutil.adb
	-$(MKDIR) $(ADA_GEN_SUBDIR)/bldtools/snamest
	$(RM) $(addprefix $(ADA_GEN_SUBDIR)/bldtools/snamest/,$(notdir $^))
	$(CP) $^ $(ADA_GEN_SUBDIR)/bldtools/snamest
	(cd $(ADA_GEN_SUBDIR)/bldtools/snamest; gnatmake -q xsnamest ; ./xsnamest )
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/snamest/snames.ns $(ADA_GEN_SUBDIR)/snames.ads
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/snamest/snames.nb $(ADA_GEN_SUBDIR)/snames.adb
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/snamest/snames.nh $(ADA_GEN_SUBDIR)/snames.h
	touch $(ADA_GEN_SUBDIR)/stamp-snames

$(ADA_GEN_SUBDIR)/nmake.adb $(ADA_GEN_SUBDIR)/nmake.ads: $(ADA_GEN_SUBDIR)/stamp-nmake ; @true
$(ADA_GEN_SUBDIR)/stamp-nmake: $(ADA_GEN_SUBDIR)/sinfo.ads $(ADA_GEN_SUBDIR)/nmake.adt $(ADA_GEN_SUBDIR)/xnmake.adb $(ADA_GEN_SUBDIR)/xutil.ads $(ADA_GEN_SUBDIR)/xutil.adb
	-$(MKDIR) $(ADA_GEN_SUBDIR)/bldtools/nmake
	$(RM) $(addprefix $(ADA_GEN_SUBDIR)/bldtools/nmake/,$(notdir $^))
	$(CP) $^ $(ADA_GEN_SUBDIR)/bldtools/nmake
	(cd $(ADA_GEN_SUBDIR)/bldtools/nmake; gnatmake -q xnmake ; ./xnmake -b nmake.adb ; ./xnmake -s nmake.ads)
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/nmake/nmake.ads $(ADA_GEN_SUBDIR)/nmake.ads
	$(MOVE_IF_CHANGE) $(ADA_GEN_SUBDIR)/bldtools/nmake/nmake.adb $(ADA_GEN_SUBDIR)/nmake.adb
	touch $(ADA_GEN_SUBDIR)/stamp-nmake

$(ADA_GEN_SUBDIR)/sdefault.adb: $(ADA_GEN_SUBDIR)/stamp-sdefault ; @true
$(ADA_GEN_SUBDIR)/stamp-sdefault : $(srcdir)/version.c Makefile
	$(ECHO) "pragma Style_Checks (Off);" >tmp-sdefault.adb
	$(ECHO) "with Osint; use Osint;" >>tmp-sdefault.adb
	$(ECHO) "package body Sdefault is" >>tmp-sdefault.adb
	$(ECHO) "   S0 : constant String := \"$(prefix)/\";" >>tmp-sdefault.adb
	$(ECHO) "   S1 : constant String := \"$(ADA_INCLUDE_DIR)/\";" >>tmp-sdefault.adb
	$(ECHO) "   S2 : constant String := \"$(ADA_RTL_OBJ_DIR)/\";" >>tmp-sdefault.adb
	$(ECHO) "   S3 : constant String := \"$(target_noncanonical)/\";" >>tmp-sdefault.adb
	$(ECHO) "   S4 : constant String := \"$(libsubdir)/\";" >>tmp-sdefault.adb
	$(ECHO) "   function Include_Dir_Default_Name return String_Ptr is" >>tmp-sdefault.adb
	$(ECHO) "   begin" >>tmp-sdefault.adb
	$(ECHO) "      return Relocate_Path (S0, S1);" >>tmp-sdefault.adb
	$(ECHO) "   end Include_Dir_Default_Name;" >>tmp-sdefault.adb
	$(ECHO) "   function Object_Dir_Default_Name return String_Ptr is" >>tmp-sdefault.adb
	$(ECHO) "   begin" >>tmp-sdefault.adb
	$(ECHO) "      return Relocate_Path (S0, S2);" >>tmp-sdefault.adb
	$(ECHO) "   end Object_Dir_Default_Name;" >>tmp-sdefault.adb
	$(ECHO) "   function Target_Name return String_Ptr is" >>tmp-sdefault.adb
	$(ECHO) "   begin" >>tmp-sdefault.adb
	$(ECHO) "      return new String'(S3);" >>tmp-sdefault.adb
	$(ECHO) "   end Target_Name;" >>tmp-sdefault.adb
	$(ECHO) "   function Search_Dir_Prefix return String_Ptr is" >>tmp-sdefault.adb
	$(ECHO) "   begin" >>tmp-sdefault.adb
	$(ECHO) "      return Relocate_Path (S0, S4);" >>tmp-sdefault.adb
	$(ECHO) "   end Search_Dir_Prefix;" >>tmp-sdefault.adb
	$(ECHO) "end Sdefault;" >> tmp-sdefault.adb
	$(MOVE_IF_CHANGE) tmp-sdefault.adb $(ADA_GEN_SUBDIR)/sdefault.adb
	touch $(ADA_GEN_SUBDIR)/stamp-sdefault
