# 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 CP), undefined)
CP=cp
endif

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

fsrcdir := $(shell cd $(srcdir);${PWD_COMMAND})

GEN_IL_INCLUDES = -I$(fsrcdir)/ada
GEN_IL_FLAGS = -gnata -gnat2012 -gnatw.g -gnatyg -gnatU $(GEN_IL_INCLUDES)

ada/seinfo_tables.ads ada/seinfo_tables.adb ada/sinfo.h ada/einfo.h ada/nmake.ads ada/nmake.adb ada/seinfo.ads ada/sinfo-nodes.ads ada/sinfo-nodes.adb ada/einfo-entities.ads ada/einfo-entities.adb: ada/stamp-gen_il ; @true
ada/stamp-gen_il: $(fsrcdir)/ada/gen_il*
	$(MKDIR) ada/gen_il
	cd ada/gen_il; gnatmake -q -g $(GEN_IL_FLAGS) gen_il-main
	# Ignore errors to work around finalization issues in older compilers
	- cd ada/gen_il; ./gen_il-main
	$(fsrcdir)/../move-if-change ada/gen_il/seinfo_tables.ads ada/seinfo_tables.ads
	$(fsrcdir)/../move-if-change ada/gen_il/seinfo_tables.adb ada/seinfo_tables.adb
	$(fsrcdir)/../move-if-change ada/gen_il/sinfo.h ada/sinfo.h
	$(fsrcdir)/../move-if-change ada/gen_il/einfo.h ada/einfo.h
	$(fsrcdir)/../move-if-change ada/gen_il/nmake.ads ada/nmake.ads
	$(fsrcdir)/../move-if-change ada/gen_il/nmake.adb ada/nmake.adb
	$(fsrcdir)/../move-if-change ada/gen_il/seinfo.ads ada/seinfo.ads
	$(fsrcdir)/../move-if-change ada/gen_il/sinfo-nodes.ads ada/sinfo-nodes.ads
	$(fsrcdir)/../move-if-change ada/gen_il/sinfo-nodes.adb ada/sinfo-nodes.adb
	$(fsrcdir)/../move-if-change ada/gen_il/einfo-entities.ads ada/einfo-entities.ads
	$(fsrcdir)/../move-if-change ada/gen_il/einfo-entities.adb ada/einfo-entities.adb
	touch ada/stamp-gen_il

# We need -gnatX to compile seinfo_tables, because it uses extensions. This
# target is not currently used when building gnat, because these extensions
# would cause bootstrapping with older compilers to fail. You can call it by
# hand, as a sanity check that these files are legal.
ada/seinfo_tables.o: ada/seinfo_tables.ads ada/seinfo_tables.adb
	cd ada ; gnatmake $(GEN_IL_INCLUDES) seinfo_tables.adb -gnatU -gnatX

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

ada/sdefault.adb: ada/stamp-sdefault ; @true
ada/stamp-sdefault : $(srcdir)/ada/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
	$(fsrcdir)/../move-if-change tmp-sdefault.adb ada/sdefault.adb
	touch ada/stamp-sdefault

ada/%: $(srcdir)/ada/libgnat/%
	$(CP) -f $< $@
