# Top level -*- makefile -*- fragment for GNU M2.

# Copyright (C) 2000-2025 Free Software Foundation, Inc.

#This file is part of GCC.

#GCC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3, or (at your option)
#any later version.

#GCC is distributed in the hope that it will be useful,
#but WITHOUT 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
#along with GCC; see the file COPYING3.  If not see
#<http://www.gnu.org/licenses/>.

# Actual names to use when installing a native compiler.
GM2_INSTALL_NAME = $(shell echo gm2|sed '$(program_transform_name)')
GM2_TARGET_INSTALL_NAME = $(target_noncanonical)-$(shell echo gm2|sed '$(program_transform_name)')

# Actual names to use when installing a cross-compiler.
GM2_CROSS_NAME = `echo gm2|sed '$(program_transform_cross_name)'`

M2_MAINTAINER = no

# CM2DEP must match the COMPILE and POSTCOMPILE defines in gcc/Make-lang.in
ifeq ($(CXXDEPMODE),depmode=gcc3)
CM2DEP=-MT $@ -MMD -MP -MF $(@D)/$(DEPDIR)/$(*F).TPo
else
CM2DEP=
endif

GM2_1 = ./gm2 -B./m2/stage1 -g -fm2-g $(CM2DEP)

GM2_FOR_TARGET = $(STAGE_CC_WRAPPER) ./gm2 -B./ -B$(build_tooldir)/bin/ -L$(objdir)/../ld $(TFLAGS)

# FIXME: Get from gcc-plugin.m4 instead of hardcoding.
ifeq (,$(findstring darwin,$(host)))
  soext=.so
else
  soext=.dylib
  PLUGINLDFLAGS = -Wl,-undefined,dynamic_lookup
  PLUGINLDFLAGS += -Wl,-install_name,m2rte$(soext)
  PLUGINLDFLAGS += -nodefaultlibs
  PLUGINLDFLAGS += -lc
endif

TEXISRC = $(srcdir)/doc/gm2.texi \
          m2/gm2-libs.texi \
          m2/gm2-ebnf.texi \
          m2/SYSTEM-pim.texi \
          m2/SYSTEM-iso.texi \
          m2/Builtins.texi \
          m2/gpl_v3_without_node.texi

RSTSRC =  $(srcdir)/doc/gm2.texi \
          m2/gm2-libs.rst \
          m2/gm2-ebnf.rst \
          m2/SYSTEM-pim.rst \
          m2/SYSTEM-iso.rst \
          m2/Builtins.rst

# Define the names for selecting modula-2 in LANGUAGES.
m2 modula-2 modula2: gm2$(exeext) xgcc$(exeext) cc1gm2$(exeext) $(GCC_PASSES)
m2.serial = cc1gm2$(exeext)

m2.tags: force
	cd $(srcdir)/m2; $(ETAGS) -o TAGS.sub \
        *.cc *.h gm2-gcc/*.cc gm2-gcc/*.h \
        gm2-libs-ch/*.h gm2-libs-ch/*.c gm2-libs-ch/*.cc; \
	$(ETAGS) --include TAGS.sub --include ../TAGS.sub

m2.srcinfo: doc/m2.info
	-cp -p $^ $(srcdir)/doc

ifeq ($(HAVE_PYTHON),yes)
m2.srcextra: m2/SYSTEM-pim.texi m2/SYSTEM-iso.texi m2/gm2-libs.texi m2/gm2-ebnf.texi
	-cp -p m2/SYSTEM-pim.texi $(srcdir)/m2
	-cp -p m2/SYSTEM-iso.texi $(srcdir)/m2
	-cp -p m2/gm2-libs.texi $(srcdir)/m2
	-cp -p m2/gm2-ebnf.texi $(srcdir)/m2
else
m2.srcextra:
endif

m2.srcman: doc/gm2.1
	-cp -p $^ $(srcdir)/doc

# Tell GNU make to ignore these if they exist.
.PHONY: m2 modula-2 modula2

GM2_PROG_DEP=gm2$(exeext) xgcc$(exeext) cc1gm2$(exeext)

include m2/config-make

LIBSTDCXX=../$(TARGET_SUBDIR)/libstdc++-v3/src/.libs/libstdc++.a

PGE=m2/pge$(exeext)

SRC_PREFIX=G

ifeq ($(enable_plugin),yes)
M2RTE_PLUGIN_SO=plugin/m2rte$(soext)
endif

m2/gm2spec.o: $(srcdir)/m2/gm2spec.cc $(SYSTEM_H) $(GCC_H) $(CONFIG_H) \
               m2/gm2config.h $(TARGET_H) $(PLUGIN_HEADERS) \
               $(generated_files) $(C_TREE_H) insn-attr-common.h
	(SHLIB_LINK='$(SHLIB_LINK)' \
	SHLIB_MULTILIB='$(SHLIB_MULTILIB)'; \
	$(COMPILER) $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
         $(DRIVER_DEFINES) \
		-DLIBSUBDIR=\"$(libsubdir)\" \
                -DPREFIX=\"$(prefix)\" \
                -c $(srcdir)/m2/gm2spec.cc $(OUTPUT_OPTION))

# Create the compiler driver for M2.
CFLAGS-m2/m2/gm2spec.o += $(DRIVER_DEFINES)

GM2_OBJS = $(GCC_OBJS) prefix.o intl.o m2/gm2spec.o

# Create the compiler driver for gm2.
gm2$(exeext): $(GM2_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a $(LIBDEPS) \
    m2/gm2config.h
	+$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \
	  $(GM2_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a \
	  $(EXTRA_GCC_LIBS) $(LIBS)

# Create a version of the gm2 driver which calls the cross-compiler.
gm2-cross$(exeext): gm2$(exeext)
	-rm -f gm2-cross$(exeext)
	cp gm2$(exeext) gm2-cross$(exeext)

po-generated:

# Build hooks:

m2.all.cross: gm2-cross$(exeext) $(M2RTE_PLUGIN_SO)

m2.start.encap: gm2$(exeext) $(M2RTE_PLUGIN_SO)
m2.rest.encap:


m2.info: doc/m2.info

m2.man: doc/m2.1

m2.install-man: $(DESTDIR)$(man1dir)/$(GM2_INSTALL_NAME)$(man1ext)

$(DESTDIR)$(man1dir)/$(GM2_INSTALL_NAME)$(man1ext): doc/m2.1 installdirs
	-rm -f $@
	-$(INSTALL_DATA) $< $@
	-chmod a-x $@

m2.dvi: doc/m2.dvi

doc/m2.dvi: $(TEXISRC) $(objdir)/m2/images/gnu.eps
	$(TEXI2DVI) -c -I $(objdir)/m2 -I $(srcdir)/doc/include -o $@ $(srcdir)/doc/gm2.texi

M2_DVIFILES = doc/m2.dvi

m2.install-dvi: $(M2_DVIFILES)
	@$(NORMAL_INSTALL)
	test -z "$(dvidir)/gcc" || $(mkinstalldirs) "$(DESTDIR)$(dvidir)/gcc"
	@list='$(M2_DVIFILES)'; for p in $$list; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(dvi__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(dvidir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(dvidir)/gcc/$$f"; \
	done

doc/m2.ps: doc/m2.dvi
	dvips -o $@ $<

m2.pdf: doc/m2.pdf

doc/m2.pdf: $(TEXISRC) $(objdir)/m2/images/gnu.eps
	$(TEXI2PDF) -I $(objdir)/m2 -I $(srcdir)/doc/include \
          $(srcdir)/doc/gm2.texi -o $@

M2_PDFFILES = doc/m2.pdf

m2.install-pdf: $(M2_PDFFILES)
	@$(NORMAL_INSTALL)
	test -z "$(pdfdir)/gcc" || $(mkinstalldirs) "$(DESTDIR)$(pdfdir)/gcc"
	@list='$(M2_PDFFILES)'; for p in $$list; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(pdf__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(pdfdir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(pdfdir)/gcc/$$f"; \
	done

.INTERMEDIATE: m2.pod

m2.pod: $(TEXISRC) $(objdir)/m2/images/gnu.eps
	-$(TEXI2POD) -I $(objdir)/m2 -D m2 < $< > $@

doc/m2.info: $(TEXISRC) $(objdir)/m2/images/gnu.eps
	if test "x$(BUILD_INFO)" = xinfo; then \
	  rm -f doc/m2.info*; \
          $(MAKEINFO) -I$(objdir)/m2 -I$(srcdir)/doc/include \
                      -o $@ $(srcdir)/doc/gm2.texi ; \
	else true; fi

$(objdir)/m2/images/gnu.eps: $(srcdir)/m2/images/gnupng
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	cp $(srcdir)/m2/images/gnu.eps $@

m2.html: $(build_htmldir)/m2/index.html

$(build_htmldir)/m2/index.html: $(TEXISRC) $(objdir)/m2/images/gnu.eps
	$(mkinstalldirs) $(@D)
	rm -f $(@D)/*
	$(TEXI2HTML) -I $(objdir)/m2 -I $(srcdir)/m2 -I $(gcc_docdir)/include -o $(@D) $<

M2_HTMLFILES = $(build_htmldir)/m2

m2.install-html: $(M2_HTMLFILES)
	@$(NORMAL_INSTALL)
	test -z "$(htmldir)" || $(mkinstalldirs) "$(DESTDIR)$(htmldir)"
	@list='$(M2_HTMLFILES)'; for p in $$list; do \
	  if test -f "$$p" || test -d "$$p"; then d=""; else d="$(srcdir)/"; fi; \
	  f=$(html__strip_dir) \
	  if test -d "$$d$$p"; then \
	    echo " $(mkinstalldirs) '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(mkinstalldirs) "$(DESTDIR)$(htmldir)/$$f" || exit 1; \
	    echo " $(INSTALL_DATA) '$$d$$p'/* '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(INSTALL_DATA) "$$d$$p"/* "$(DESTDIR)$(htmldir)/$$f"; \
	  else \
	    echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(htmldir)/$$f"; \
	  fi; \
	done

# gm2-libs.texi

m2/gm2-libs.texi: gm2-libs.texi-check; @true

ifeq ($(HAVE_PYTHON),yes)
gm2-libs.texi-check: m2/SYSTEM-pim.texi m2/SYSTEM-iso.texi m2/Builtins.texi \
                     $(objdir)/m2/gm2-libs-coroutines/SYSTEM.def
	-test -d m2/gm2-libs-log || $(mkinstalldirs) m2/gm2-libs-log
	-test -d m2/gm2-libs-iso || $(mkinstalldirs) m2/gm2-libs-iso
	-test -d m2/gm2-libs || $(mkinstalldirs) m2/gm2-libs
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -t -uLibraries -s$(srcdir)/m2 -b$(objdir)/m2 -o $(objdir)/m2/gm2-libs.texi
else
gm2-libs.texi-check:
	cp $(srcdir)/m2/target-independent/m2/gm2-libs.texi $(objdir)/m2/gm2-libs.texi
endif
	$(STAMP) gm2-libs.texi-check

# gm2-libs.rst

m2/gm2-libs.rst: gm2-libs.rst-check; @true

ifeq ($(HAVE_PYTHON),yes)
gm2-libs.rst-check: m2/SYSTEM-pim.texi m2/SYSTEM-iso.texi m2/Builtins.texi \
                     $(objdir)/m2/gm2-libs-coroutines/SYSTEM.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -x -uLibraries -s$(srcdir)/m2 -b$(objdir)/m2 -o $(objdir)/m2/gm2-libs.rst
else
gm2-libs.rst-check:
	cp $(srcdir)/m2/target-independent/m2/gm2-libs.rst $(objdir)/m2/gm2-libs.rst
endif
	$(STAMP) gm2-libs.rst-check

# gm2-ebnf.texi

m2/gm2-ebnf.texi: gm2-ebnf.texi-check; @true

gm2-ebnf.texi-check: $(PGE) $(srcdir)/m2/gm2-compiler/P0SyntaxCheck.bnf
	$(PGE) -c -p -t -f $(srcdir)/m2/gm2-compiler/P0SyntaxCheck.bnf -o m2/gm2-ebnf.texi
	$(STAMP) gm2-ebnf.texi-check

# gm2-ebnf.rst

m2/gm2-ebnf.rst: gm2-ebnf.rst-check; @true

gm2-ebnf.rst-check: $(PGE) $(srcdir)/m2/gm2-compiler/P0SyntaxCheck.bnf
	$(PGE) -c -p -t -f $(srcdir)/m2/gm2-compiler/P0SyntaxCheck.bnf -o m2/gm2-ebnf.rst
	$(STAMP) gm2-ebnf.rst-check

# SYSTEM-pim.texi

m2/SYSTEM-pim.texi: SYSTEM-pim-texi-check; @true

ifeq ($(HAVE_PYTHON),yes)
SYSTEM-pim-texi-check: $(objdir)/m2/gm2-libs/SYSTEM.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -t -b$(objdir)/m2 -f$(objdir)/m2/gm2-libs/SYSTEM.def -o $(objdir)/m2/SYSTEM-pim.texi
else
SYSTEM-pim-texi-check: $(objdir)/m2/gm2-libs/SYSTEM.def
	cp $(srcdir)/m2/target-independent/m2/SYSTEM-pim.texi $(objdir)/m2/SYSTEM-pim.texi
endif
	$(STAMP) SYSTEM-pim-texi-check

# SYSTEM-pim.rst

m2/SYSTEM-pim.rst: SYSTEM-pim-rst-check; @true

ifeq ($(HAVE_PYTHON),yes)
SYSTEM-pim-rst-check: $(objdir)/m2/gm2-libs/SYSTEM.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -x -b$(objdir)/m2 -f$(objdir)/m2/gm2-libs/SYSTEM.def -o $(objdir)/m2/SYSTEM-pim.rst
else
SYSTEM-pim-rst-check: $(objdir)/m2/gm2-libs/SYSTEM.def
	cp $(srcdir)/m2/target-independent/m2/SYSTEM-pim.rst $(objdir)/m2/SYSTEM-pim.rst
endif
	$(STAMP) SYSTEM-pim-rst-check

# SYSTEM-pim.texi

m2/SYSTEM-iso.texi:  SYSTEM-iso.texi-check; @true

ifeq ($(HAVE_PYTHON),yes)
SYSTEM-iso.texi-check:  $(objdir)/m2/gm2-libs-iso/SYSTEM.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -t -b$(objdir)/m2 -f$(objdir)/m2/gm2-libs-iso/SYSTEM.def -o $(objdir)/m2/SYSTEM-iso.texi
else
SYSTEM-iso.texi-check:  $(objdir)/m2/gm2-libs-iso/SYSTEM.def
	cp $(srcdir)/m2/target-independent/m2/SYSTEM-iso.texi $(objdir)/m2/SYSTEM-iso.texi
endif
	$(STAMP) SYSTEM-iso.texi-check

# SYSTEM-pim.rst

m2/SYSTEM-iso.rst:  SYSTEM-iso.rst-check; @true

ifeq ($(HAVE_PYTHON),yes)
SYSTEM-iso.rst-check:  $(objdir)/m2/gm2-libs-iso/SYSTEM.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -x -b$(objdir)/m2 -f$(objdir)/m2/gm2-libs-iso/SYSTEM.def -o $(objdir)/m2/SYSTEM-iso.rst
else
SYSTEM-iso.rst-check:  $(objdir)/m2/gm2-libs-iso/SYSTEM.def
	cp $(srcdir)/m2/target-independent/m2/SYSTEM-iso.rst $(objdir)/m2/SYSTEM-iso.rst
endif
	$(STAMP) SYSTEM-iso.rst-check


# m2/Builtins.texi

m2/Builtins.texi:  Builtins.texi-check; @true

ifeq ($(HAVE_PYTHON),yes)
Builtins.texi-check:  m2/gm2-libs/Builtins.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -t -b./ -f$(srcdir)/m2/gm2-libs/Builtins.def -o $(objdir)/m2/Builtins.texi
else
Builtins.texi-check:  m2/gm2-libs/Builtins.def
	cp $(srcdir)/m2/target-independent/m2/Builtins.texi $(objdir)/m2/Builtins.texi
endif
	$(STAMP) Builtins.texi-check

# m2/Builtins.rst

m2/Builtins.rst:  Builtins.rst-check; @true

ifeq ($(HAVE_PYTHON),yes)
Builtins.rst-check:  m2/gm2-libs/Builtins.def
	$(PYTHON) $(srcdir)/m2/tools-src/def2doc.py -x -b./ -f$(srcdir)/m2/gm2-libs/Builtins.def -o $(objdir)/m2/Builtins.rst
else
Builtins.rst-check:  m2/gm2-libs/Builtins.def
	cp $(srcdir)/m2/target-independent/m2/Builtins.rst $(objdir)/m2/Builtins.rst
endif
	$(STAMP) Builtins.rst-check

m2/gpl_v3_without_node.texi: doc/include/gpl_v3.texi
	grep -v "^@node" $< > $@

# Stage hooks:
# The toplevel makefile has already created stage?/m2 at this point.

m2.stage1: stage1-start
	-mv m2/*$(objext) stage1/m2
m2.stage2: stage2-start
	-mv m2/*$(objext) stage2/m2
m2.stage3: stage3-start
	-mv m2/*$(objext) stage3/m2
m2.stage4: stage4-start
	-mv m2/*$(objext) stage4/m2
m2.stageprofile: stageprofile-start
	-mv m2/*$(objext) stageprofile/m2
m2.stagefeedback: stageprofile-start
	-mv m2/*$(objext) stagefeedback/m2

# No gm2-specific selftests
selftest-m2:

# Install hooks:
# cc1gm2 is installed elsewhere as part of $(COMPILERS).
# $(COMPILERS) is defined in `config-lang.in'

m2.install-common: installdirs
	-rm -f $(DESTDIR)$(bindir)/$(GM2_INSTALL_NAME)$(exeext)
	$(INSTALL_PROGRAM) gm2$(exeext) $(DESTDIR)$(bindir)/$(GM2_INSTALL_NAME)$(exeext)
	-if test -f cc1gm2$(exeext); then \
	  if test -f gm2-cross$(exeext); then \
	    :; \
	  else \
	    rm -f $(DESTDIR)$(bindir)/$(GM2_TARGET_INSTALL_NAME)$(exeext); \
	    ( cd $(DESTDIR)$(bindir) && \
	      $(LN) $(GM2_INSTALL_NAME)$(exeext) $(GM2_TARGET_INSTALL_NAME)$(exeext) ); \
	  fi; \
	fi
	-for tool in cc1gm2$(exeext); do \
             if [ -f $$tool ]; then \
               rm -f $(DESTDIR)$(libexecsubdir)/$$tool; \
               $(INSTALL_PROGRAM) $$tool $(DESTDIR)$(libexecsubdir)/$$tool; \
               chmod a+x $(DESTDIR)$(libexecsubdir)/$$tool; \
             else \
               echo "cannot find $$tool" ; \
             fi ; \
         done

m2.install-info: installdirs
	if [ -f gm2$(exeext) ] ; then \
	  if [ -f $(objdir)/doc/m2.info ]; then \
	    rm -f $(DESTDIR)$(infodir)/m2.info*; \
	    for f in $(objdir)/doc/m2.info*; do \
	      realfile=`echo $$f | sed -e 's|.*/\([^/]*\)$$|\1|'`; \
              rm -f $(DESTDIR)$(infodir)/`basename $$realfile`; \
	      $(INSTALL_DATA) $$f $(DESTDIR)$(infodir)/`basename $$realfile`; \
	    done; \
	    chmod a-x $(DESTDIR)$(infodir)/m2.info*; \
	  else true; fi; \
	else true; fi
	-if [ -f gm2$(exeext) ] && [ -f $(DESTDIR)$(infodir)/m2.info ]; then \
	  if $(SHELL) -c 'install-info --version' >/dev/null 2>&1; then \
	    install-info --dir-file=$(DESTDIR)$(infodir)/dir $(DESTDIR)$(infodir)/m2.info; \
	  else true; fi; \
	else true; fi

m2.install-normal: m2.install-common m2.install-info m2.install-man

# This target will install GM2 into an existing GCC installation,
# without overwriting existing files.
# The semicolon is to prevent the install.sh -> install default rule
# from doing anything.  Having it run true helps avoid problems and
# noise from versions of make which don't like to have null commands.
m2.install: m2.install-normal; @true

gm2.install-with-gcc: $(INSTALL_HEADERS) gm2.install $(INSTALL_LIBGCC)
	for file in $(GCC_PASSES); do \
          if [ x"$$file" != x"xgcc$(exeext)" ]; then \
            rm -f $(DESTDIR)$(libsubdir)/$$file; \
            $(INSTALL_PROGRAM) $$file $(DESTDIR)$(libsubdir)/$$file || exit 1; \
          fi; \
        done; exit 0

m2.uninstall:
	-rm -rf $(bindir)/$(GM2_INSTALL_NAME)
	-rm -rf $(bindir)/$(GM2_CROSS_NAME)

ifeq ($(enable_plugin),yes)
m2.install-plugin: installdirs
	$(mkinstalldirs) $(DESTDIR)$(plugin_resourcesdir)
	$(INSTALL_PROGRAM) $(M2RTE_PLUGIN_SO) $(DESTDIR)$(plugin_resourcesdir)/m2rte$(soext)
	chmod a+x $(DESTDIR)$(plugin_resourcesdir)/m2rte$(soext)

override PLUGINCFLAGS := $(filter-out -mdynamic-no-pic,$(PLUGINCFLAGS))

plugin/m2rte$(soext): $(srcdir)/m2/plugin/m2rte.cc $(GCC_HEADER_DEPENDENCIES_FOR_M2) \
        insn-attr-common.h insn-flags.h $(generated_files)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(PLUGINCC) $(PLUGINCFLAGS) -fno-rtti -I. -I$(srcdir) $(INCINTL) -I$(srcdir)/m2 -I$(srcdir)/m2/gm2-gcc -I$(srcdir)/../include -I$(srcdir)/../libcpp/include -Wall $(GMPINC) -Wno-literal-suffix -fPIC -c -o plugin/m2rte.o $(srcdir)/m2/plugin/m2rte.cc
	$(PLUGINCC) $(PLUGINCFLAGS) $(PLUGINLDFLAGS) $(PLUGINLIBS) $(LIBINTL) -fno-rtti plugin/m2rte.o -shared -o $@
else
m2.install-plugin:

plugin/m2rte$(soext):

endif


# Clean hooks:
# A lot of the ancillary files are deleted by the main makefile.
# We just have to delete files specific to us.

m2.mostlyclean:
	-rm -f m2/*.o

m2.clean:
	-rm -f m2/*.o
	-rm -f m2/gm2-libs/config.*
	-rm m2/gm2-libs/gm2-libs-host.h m2/gm2config.h

m2.extraclean:
m2.realclean:

quit: force
	echo "calling exit"
	exit 1

# Rules to build the compiler, pge and mc.

# MC_COPYRIGHT=--gpl-header --project="GNU Modula-2"
MC_COPYRIGHT=

MC_ARGS= --olang=c++ \
 --h-file-prefix=$(SRC_PREFIX) \
 -I$(srcdir)/m2/gm2-libs \
 -I$(srcdir)/m2/gm2-compiler \
 -I$(srcdir)/m2/gm2-libiberty \
 -I$(srcdir)/m2/gm2-gcc \
 --quiet \
 $(MC_COPYRIGHT) \
 --gcc-config-system

MC_EXTENDED_OPAQUE=
MCDEPS=m2/boot-bin/mc$(exeext)

MC=m2/boot-bin/mc$(exeext) $(MC_ARGS)

MC_LIBS=m2/mc-boot-ch/Glibc.o m2/mc-boot-ch/Gmcrts.o

MC_SRC_FLAGS=-DMC_M2

M2LINK=m2/boot-bin/mklink$(exeext)
GM2_O=-O0
GM2_O_S3=-O
GM2_OS=-Os
GM2_G=-g -fm2-g
GM2_CPP=
# GM2_DEBUG_STRMEM=-fcpp
GM2_DEBUG_STRMEM=
GM2_FLAGS=-fsoft-check-all \
 -fno-return \
 $(GM2_G) $(GM2_O) \
 -funbounded-by-reference -fpim -fextended-opaque \
 -Wcase-enum -Wpedantic-cast -Wpedantic-param-names \
 -Wreturn-type -Wunused-variable -Wuninit-variable-checking \
 -ffunction-sections -fdata-sections \
 $(GM2_CPP) # -fauto-init
GM2_ISO_FLAGS=-fsoft-check-all $(GM2_G) $(GM2_O) \
 -fno-return \
 -funbounded-by-reference -fiso -fextended-opaque \
 -Wcase-enum -Wpedantic-cast -Wpedantic-param-names -Wreturn-type \
 -Wunused-variable -Wuninit-variable-checking \
 -ffunction-sections -fdata-sections \
 $(GM2_CPP)
GM2_MIN_FLAGS=$(GM2_G) $(GM2_OS) \
 -funbounded-by-reference -fextended-opaque \
 -Wpedantic-cast -Wpedantic-param-names -fno-exceptions \
 -ffunction-sections -fdata-sections $(GM2_CPP)

# ALL_LINKERFLAGS may include -pie (when GCC is configured with
# --enable-host-pie), so use -fPIE if needed.  (It would not be
# a good idea to override CFLAGS.)
GM2_PICFLAGS = $(PICFLAG)

O2=-O2 -g
SO_O2=-O2 -g -fPIC
SO=-O0 -g -fPIC

# Language-specific object files for the gm2 compiler.

GM2_C_OBJS        = m2/gm2-lang.o \
                    m2/gm2-gcc/m2pp.o \
                    m2/gm2-gcc/m2assert.o \
                    m2/gm2-gcc/m2block.o \
                    m2/gm2-gcc/m2builtins.o \
                    m2/gm2-gcc/m2except.o \
                    m2/gm2-gcc/m2color.o \
                    m2/gm2-gcc/m2configure.o \
                    m2/gm2-gcc/m2convert.o \
                    m2/gm2-gcc/m2decl.o \
                    m2/gm2-gcc/m2expr.o \
                    m2/gm2-gcc/m2linemap.o \
                    m2/gm2-gcc/m2statement.o \
                    m2/gm2-gcc/m2type.o \
                    m2/gm2-gcc/m2tree.o \
                    m2/gm2-gcc/m2treelib.o \
                    m2/gm2-gcc/m2top.o \
                    m2/gm2-gcc/m2misc.o \
                    m2/gm2-gcc/init.o
GM2_LIBS          = m2/gm2-compiler/gm2.a \
                    m2/gm2-libs/libgm2.a \

GM2_LIBS_BOOT     = m2/gm2-compiler-boot/gm2.a \
                    m2/gm2-libs-boot/libgm2.a \
                    $(GM2-BOOT-O)

m2_OBJS = $(GM2_C_OBJS) m2/gm2-gcc/rtegraph.o \
	m2/gm2-compiler-boot/m2flex.o \
	$(BUILD-LIBS-BOOT) $(BUILD-COMPILER-BOOT) $(MC_LIBS)

cc1gm2$(exeext): m2/stage1/cc1gm2$(exeext) $(m2.prev)
	cp -p $< $@

m2/stage2/cc1gm2$(exeext): m2/stage1/cc1gm2$(exeext) m2/gm2-compiler/m2flex.o \
                            $(GM2_C_OBJS) $(BACKEND) $(LIBDEPS) $(GM2_LIBS) \
                            m2/gm2-gcc/rtegraph.o $(M2RTE_PLUGIN_SO)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	@$(call LINK_PROGRESS,$(INDEX.m2),start)
	+$(LLINKER) $(ALL_CFLAGS) $(LDFLAGS) -o $@ $(GM2_C_OBJS) m2/gm2-compiler/m2flex.o \
                            attribs.o \
                              $(GM2_LIBS) \
                              $(BACKEND) $(LIBS) m2/gm2-gcc/rtegraph.o \
                              $(BACKENDLIBS) $(LIBSTDCXX) -lm
	@$(call LINK_PROGRESS,$(INDEX.m2),end)

m2/stage1/cc1gm2$(exeext): gm2$(exeext) m2/gm2-compiler-boot/m2flex.o \
                            $(GM2_C_OBJS) $(BACKEND) $(LIBDEPS) \
                            $(GM2_LIBS_BOOT) $(MC_LIBS) \
                            m2/gm2-gcc/rtegraph.o $(M2RTE_PLUGIN_SO) \
                            $(m2.prev)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	@$(call LINK_PROGRESS,$(INDEX.m2),start)
	+$(LLINKER) $(ALL_CFLAGS) $(LDFLAGS) -o $@ $(GM2_C_OBJS) m2/gm2-compiler-boot/m2flex.o \
                            attribs.o \
                             $(GM2_LIBS_BOOT) $(MC_LIBS) \
                             m2/gm2-gcc/rtegraph.o \
                             $(BACKEND) $(LIBS) $(BACKENDLIBS)
	@$(call LINK_PROGRESS,$(INDEX.m2),end)

# Compiling object files from source files.

GCC_HEADER_DEPENDENCIES_FOR_M2 = $(BUILD-BOOT-H) $(TIMEVAR_H) m2/gm2config.h $(CONFIG_H) \
        $(TREE_H) $(RTL_H) $(TARGET_H) $(PLUGIN_HEADERS) \
        $(BCONFIG_H) $(CORETYPES_H) $(SYSTEM_H) \
        $(srcdir)/flags.h gtype-m2.h \
        $(generated_files) insn-attr-common.h

m2/gm2-gcc/%.o: $(srcdir)/m2/gm2-gcc/%.cc $(GCC_HEADER_DEPENDENCIES_FOR_M2)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c -g $(ALL_COMPILERFLAGS) \
             $(ALL_CPPFLAGS) $(INCLUDES) $< $(OUTPUT_OPTION)
	$(POSTCOMPILE)

m2/gm2-gcc/m2configure.o: $(srcdir)/m2/gm2-gcc/m2configure.cc \
               $(SYSTEM_H) $(GCC_H) $(CONFIG_H) \
               m2/gm2config.h $(TARGET_H) $(PLUGIN_HEADERS) \
               $(generated_files) $(C_TREE_H) insn-attr-common.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
         $(DRIVER_DEFINES) \
		-DLIBSUBDIR=\"$(libsubdir)\" \
                -DPREFIX=\"$(prefix)\" \
                -c $(srcdir)/m2/gm2-gcc/m2configure.cc $(OUTPUT_OPTION)
	$(POSTCOMPILE)

m2/gm2-lang.o: $(srcdir)/m2/gm2-lang.cc gt-m2-gm2-lang.h $(GCC_HEADER_DEPENDENCIES_FOR_M2)
	$(COMPILER) $(CM2DEP) -c -g $(GM2GCC) $(ALL_COMPILERFLAGS) \
	    -DLIBSUBDIR=\"$(libsubdir)\" \
            $(ALL_CPPFLAGS) $(INCLUDES) $< $(OUTPUT_OPTION)
	$(POSTCOMPILE)

m2/gm2-gcc/rtegraph.o: $(srcdir)/m2/gm2-gcc/rtegraph.cc $(GCC_HEADER_DEPENDENCIES_FOR_M2) \
                       gt-m2-rtegraph.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c -g $(GM2GCC) $(ALL_COMPILERFLAGS) \
            $(ALL_CPPFLAGS) $(INCLUDES) $< $(OUTPUT_OPTION)
	$(POSTCOMPILE)

m2/gm2-gcc/$(SRC_PREFIX)%.h: $(srcdir)/m2/gm2-gcc/%.def $(MCDEPS)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(MC) -o=$@ $(srcdir)/m2/gm2-gcc/$*.def

ifeq ($(HAVE_PYTHON),yes)
check-format-error:
	$(PYTHON) $(srcdir)/m2/tools-src/checkmeta.py -s $(srcdir)/m2/gm2-compiler
else
check-format-error:
endif

# The following tables define the source files which are translated into C using mc
# and defines the system interface C files.

# Core library definition modules found in gm2-libs.

GM2-LIBS-BOOT-DEFS = \
   ASCII.def \
   Args.def \
   Assertion.def \
   Break.def \
   CmdArgs.def \
   Debug.def \
   DynamicStrings.def \
   Environment.def \
   FIO.def \
   FormatStrings.def \
   FpuIO.def \
   IO.def \
   Indexing.def \
   M2Dependent.def \
   M2EXCEPTION.def \
   M2RTS.def \
   NumberIO.def \
   PushBackInput.def \
   RTExceptions.def \
   SArgs.def \
   SEnvironment.def \
   SFIO.def \
   SYSTEM.def \
   Scan.def \
   StdIO.def \
   Storage.def \
   StrCase.def \
   StrIO.def \
   StrLib.def \
   StringConvert.def \
   SysExceptions.def \
   SysStorage.def \
   TimeString.def \
   UnixArgs.def \
   dtoa.def \
   errno.def \
   ldtoa.def \
   libc.def \
   libm.def \
   termios.def \
   wrapc.def \

# Core library implementation modules found in gm2-libs.

GM2-LIBS-BOOT-MODS = \
   ASCII.mod \
   Args.mod \
   Assertion.mod \
   Break.mod \
   CmdArgs.mod \
   Debug.mod \
   DynamicStrings.mod \
   Environment.mod \
   FIO.mod \
   FormatStrings.mod \
   FpuIO.mod \
   IO.mod \
   Indexing.mod \
   M2Dependent.mod \
   M2EXCEPTION.mod \
   M2RTS.mod \
   NumberIO.mod \
   PushBackInput.mod \
   RTExceptions.mod \
   SArgs.mod \
   SEnvironment.mod \
   SFIO.mod \
   Scan.mod \
   Storage.mod \
   StrCase.mod \
   StrIO.mod \
   StrLib.mod \
   StringConvert.mod \
   SysStorage.mod \
   TimeString.mod \

# Hand translated C files and C files for definition module for "C" modules
# found in gm2-libs-ch.

GM2-LIBS-BOOT-C = \
   StdIO.c \
   SysExceptions.c \
   choosetemp.c \
   errno.c \
   termios.c \
   wrapc.c \

# C++ implemented modules found in gm2-libs-ch.

GM2-LIBS-BOOT-CC = \
   UnixArgs.cc \
   dtoa.cc \
   ldtoa.cc

# Definition modules for the front end found in gm2-compiler.

GM2-COMP-BOOT-DEFS = \
   FifoQueue.def \
   Lists.def \
   M2ALU.def \
   M2AsmUtil.def \
   M2Base.def \
   M2BasicBlock.def \
   M2Batch.def \
   M2Bitset.def \
   M2CaseList.def \
   M2Check.def \
   M2Code.def \
   M2ColorString.def \
   M2Comp.def \
   M2Const.def \
   M2Debug.def \
   M2DebugStack.def \
   M2Defaults.def \
   M2DriverOptions.def \
   DynamicPath.def \
   PathName.def \
   M2Emit.def \
   M2Error.def \
   M2EvalSym.def \
   M2FileName.def \
   M2GCCDeclare.def \
   M2GenGCC.def \
   M2Graph.def \
   M2LangDump.def \
   M2LexBuf.def \
   M2MetaError.def \
   M2Optimize.def \
   M2Options.def \
   M2Pass.def \
   M2Preprocess.def \
   M2Printf.def \
   M2Quads.def \
   M2Quiet.def \
   M2Range.def \
   M2Reserved.def \
   M2SSA.def \
   M2Scaffold.def \
   M2Scope.def \
   M2Search.def \
   M2Size.def \
   M2StackAddress.def \
   M2StackWord.def \
   M2StateCheck.def \
   M2Students.def \
   M2Swig.def \
   M2SymInit.def \
   M2System.def \
   NameKey.def \
   ObjectFiles.def \
   Output.def \
   P0SymBuild.def \
   P0SyntaxCheck.def \
   P1Build.def \
   P1SymBuild.def \
   P2Build.def \
   P2SymBuild.def \
   P3Build.def \
   P3SymBuild.def \
   PCBuild.def \
   PCSymBuild.def \
   PHBuild.def \
   Sets.def \
   SymbolConversion.def \
   SymbolKey.def \
   SymbolTable.def \
   bnflex.def \
   m2flex.def \

# Implementation modules for the front end found in gm2-compiler.

GM2-COMP-BOOT-MODS = \
   FifoQueue.mod \
   Lists.mod \
   Lists.mod \
   M2ALU.mod \
   M2AsmUtil.mod \
   M2Base.mod \
   M2BasicBlock.mod \
   M2Batch.mod \
   M2Bitset.mod \
   M2CaseList.mod \
   M2Check.mod \
   M2Code.mod \
   M2ColorString.mod \
   M2Comp.mod \
   M2Const.mod \
   M2Debug.mod \
   M2DebugStack.mod \
   M2Defaults.mod \
   M2DriverOptions.mod \
   DynamicPath.mod \
   PathName.mod \
   M2Emit.mod \
   M2Error.mod \
   M2FileName.mod \
   M2GCCDeclare.mod \
   M2GenGCC.mod \
   M2Graph.mod \
   M2LangDump.mod \
   M2LexBuf.mod \
   M2MetaError.mod \
   M2Optimize.mod \
   M2Options.mod \
   M2Pass.mod \
   M2Preprocess.mod \
   M2Printf.mod \
   M2Quads.mod \
   M2Quiet.mod \
   M2Range.mod \
   M2Reserved.mod \
   M2SSA.mod \
   M2Scaffold.mod \
   M2Scope.mod \
   M2Search.mod \
   M2Size.mod \
   M2StackAddress.mod \
   M2StackWord.mod \
   M2StateCheck.mod \
   M2Students.mod \
   M2Swig.mod \
   M2SymInit.mod \
   M2System.mod \
   NameKey.mod \
   NameKey.mod \
   ObjectFiles.mod \
   Output.mod \
   P0SymBuild.mod \
   P1SymBuild.mod \
   P2SymBuild.mod \
   P3SymBuild.mod \
   PCSymBuild.mod \
   Sets.mod \
   SymbolConversion.mod \
   SymbolKey.mod \
   SymbolKey.mod \
   SymbolTable.mod \
   bnflex.mod \

# The interface between the modula-2 front end and gimple/trees found in directory gm2-gcc.

GM2-GCC-DEFS = \
   gcctypes.def \
   m2block.def \
   m2builtins.def \
   m2color.def \
   m2configure.def \
   m2convert.def \
   m2decl.def \
   m2except.def \
   m2except.def \
   m2expr.def \
   m2linemap.def \
   m2misc.def \
   m2pp.def \
   m2statement.def \
   m2top.def \
   m2tree.def \
   m2treelib.def \
   m2type.def \
   CDataTypes.def \

# The following lists define the source files used to build gm2 using Modula-2
# sources directly.
#
# cc1gm2$(exeext) uses these definition modules from the core libraries.

GM2-LIBS-DEFS = \
   ASCII.def \
   Args.def \
   Assertion.def \
   Break.def \
   Builtins.def \
   COROUTINES.def \
   CmdArgs.def \
   Debug.def \
   DynamicStrings.def \
   Environment.def \
   FIO.def \
   FormatStrings.def \
   FpuIO.def \
   GetOpt.def \
   IO.def \
   Indexing.def \
   LMathLib0.def \
   LegacyReal.def \
   M2Dependent.def \
   M2EXCEPTION.def \
   M2RTS.def \
   MathLib0.def \
   MemUtils.def \
   NumberIO.def \
   PushBackInput.def \
   RTExceptions.def \
   RTint.def \
   SArgs.def \
   SEnvironment.def \
   SFIO.def \
   SMathLib0.def \
   SYSTEM.def \
   Scan.def \
   StdIO.def \
   Storage.def \
   StrCase.def \
   StrIO.def \
   StrLib.def \
   StringConvert.def \
   SysStorage.def \
   TimeString.def \
   UnixArgs.def \
   cbuiltin.def \
   dtoa.def \
   ldtoa.def \
   libc.def \
   termios.def \
   wrapc.def \

# cc1gm2$(exeext) uses these implementation modules from the core libraries.

GM2-LIBS-MODS = \
   ASCII.mod \
   Args.mod \
   Assertion.mod \
   Break.mod \
   Builtins.mod \
   COROUTINES.mod \
   CmdArgs.mod \
   Debug.mod \
   DynamicStrings.mod \
   Environment.mod \
   FIO.mod \
   FormatStrings.mod \
   FpuIO.mod \
   GetOpt.mod \
   IO.mod \
   Indexing.mod \
   LMathLib0.mod \
   LegacyReal.mod \
   M2Dependent.mod \
   M2EXCEPTION.mod \
   M2RTS.mod \
   MathLib0.mod \
   MemUtils.mod \
   NumberIO.mod \
   PushBackInput.mod \
   RTExceptions.mod \
   RTint.mod \
   SArgs.mod \
   SEnvironment.mod \
   SFIO.mod \
   SMathLib0.mod \
   SYSTEM.mod \
   Scan.mod \
   StdIO.mod \
   Storage.mod \
   StrCase.mod \
   StrIO.mod \
   StrLib.mod \
   StringConvert.mod \
   SysStorage.mod \
   TimeString.mod \

# cc1gm2$(exeext) uses these C modules from the core libraries.

GM2-LIBS-C = \
   Selective.c \
   SysExceptions.c \
   cgetopt.c \
   choosetemp.c \
   errno.c \
   host.c \
   termios.c \
   wrapc.c \

# cc1gm2$(exeext) uses these C++ modules from the core libraries.

GM2-LIBS-CC = \
   UnixArgs.cc \
   dtoa.cc \
   ldtoa.cc \

# cc1gm2$(exeext) uses these definition modules found in the gm2-compiler directory.

GM2-COMP-DEFS = \
   FifoQueue.def \
   Lists.def \
   M2ALU.def \
   M2AsmUtil.def   \
   M2Base.def \
   M2BasicBlock.def \
   M2Batch.def \
   M2Bitset.def \
   M2CaseList.def \
   M2Check.def \
   M2Code.def \
   M2ColorString.def \
   M2Comp.def \
   M2Const.def \
   M2Debug.def \
   M2DebugStack.def \
   M2Defaults.def \
   M2DriverOptions.def \
   DynamicPath.def \
   PathName.def \
   M2Emit.def \
   M2Error.def \
   M2FileName.def \
   M2GCCDeclare.def \
   M2GenGCC.def \
   M2Graph.def \
   M2LangDump.def \
   M2LexBuf.def \
   M2MetaError.def \
   M2Optimize.def \
   M2Options.def \
   M2Pass.def \
   M2Preprocess.def \
   M2Printf.def \
   M2Quads.def     \
   M2Quiet.def \
   M2Range.def \
   M2Reserved.def \
   M2SSA.def \
   M2Scaffold.def \
   M2Scope.def \
   M2Search.def \
   M2Size.def \
   M2StackAddress.def \
   M2StackWord.def \
   M2StateCheck.def \
   M2Students.def \
   M2Swig.def \
   M2SymInit.def \
   M2System.def \
   NameKey.def \
   ObjectFiles.def \
   P0SymBuild.def \
   P0SyntaxCheck.def \
   P1Build.def \
   P1SymBuild.def \
   P2Build.def \
   P2SymBuild.def \
   P3Build.def \
   P3SymBuild.def \
   PCBuild.def \
   PCSymBuild.def \
   PHBuild.def \
   Sets.def \
   SymbolConversion.def \
   SymbolKey.def \
   SymbolTable.def \
   bnflex.def \

# cc1gm2$(exeext) uses these implementation modules found in the gm2-compiler directory.

GM2-COMP-MODS = \
   FifoQueue.mod \
   Lists.mod \
   M2ALU.mod \
   M2AsmUtil.mod \
   M2Base.mod \
   M2BasicBlock.mod \
   M2Batch.mod \
   M2Bitset.mod \
   M2CaseList.mod \
   M2Check.mod \
   M2Code.mod \
   M2ColorString.mod \
   M2Comp.mod \
   M2Const.mod \
   M2Debug.mod \
   M2DebugStack.mod \
   M2Defaults.mod \
   M2DriverOptions.mod \
   DynamicPath.mod \
   PathName.mod \
   M2Emit.mod \
   M2Error.mod \
   M2FileName.mod \
   M2GCCDeclare.mod \
   M2GenGCC.mod \
   M2Graph.mod \
   M2LangDump.mod \
   M2LexBuf.mod \
   M2MetaError.mod \
   M2Optimize.mod \
   M2Options.mod \
   M2Pass.mod \
   M2Preprocess.mod \
   M2Printf.mod \
   M2Quads.mod \
   M2Quiet.mod \
   M2Range.mod \
   M2Reserved.mod \
   M2SSA.mod \
   M2Scaffold.mod \
   M2Scope.mod \
   M2Search.mod \
   M2Size.mod \
   M2StackAddress.mod \
   M2StackWord.mod \
   M2StateCheck.mod \
   M2Students.mod \
   M2Swig.mod \
   M2SymInit.mod \
   M2System.mod \
   NameKey.mod \
   ObjectFiles.mod \
   Output.mod \
   P0SymBuild.mod \
   P1SymBuild.mod \
   P2SymBuild.mod  \
   P3SymBuild.mod  \
   PCSymBuild.mod \
   Sets.mod \
   SymbolConversion.mod \
   SymbolKey.mod \
   SymbolTable.mod \
   bnflex.mod \

# Implementation modules created by the parser generator pge from .bnf files.

GM2-AUTO-MODS = \
   P2Build.mod \
   P3Build.mod \
   PHBuild.mod \
   PCBuild.mod \
   P1Build.mod \
   P0SyntaxCheck.mod \

# LIBIBERTY interface definition modules

GM2-LIBIBERTY-DEFS = \
   choosetemp.def \
   pexecute.def

BUILD-LIBS-BOOT-H = $(GM2-LIBS-BOOT-DEFS:%.def=m2/gm2-libs-boot/$(SRC_PREFIX)%.h)

BUILD-LIBS-BOOT = $(BUILD-LIBS-BOOT-H) \
                  $(GM2-LIBS-BOOT-MODS:%.mod=m2/gm2-libs-boot/%.o) \
                  $(GM2-LIBS-BOOT-CC:%.cc=m2/gm2-libs-boot/%.o) \
                  $(GM2-LIBS-BOOT-C:%.c=m2/gm2-libs-boot/%.o)

BUILD-COMPILER-BOOT-H = $(GM2-COMP-BOOT-DEFS:%.def=m2/gm2-compiler-boot/$(SRC_PREFIX)%.h) \
                        $(GM2-LIBIBERTY-DEFS:%.def=m2/gm2-libiberty/$(SRC_PREFIX)%.h) \
                        $(GM2-GCC-DEFS:%.def=m2/gm2-gcc/$(SRC_PREFIX)%.h)

BUILD-COMPILER-BOOT = $(BUILD-COMPILER-BOOT-H) \
                      $(GM2-COMP-BOOT-DEFS:%.def=m2/gm2-compiler-boot/$(SRC_PREFIX)%.h) \
                      $(GM2-AUTO-MODS:%.mod=m2/gm2-compiler-boot/%.o) \
                      $(GM2-COMP-BOOT-MODS:%.mod=m2/gm2-compiler-boot/%.o) \
                      m2/gm2-compiler-boot/m2flex.o

BUILD-BOOT-H = m2/boot-bin/mc$(exeext) \
               $(BUILD-LIBS-BOOT-H) $(BUILD-COMPILER-BOOT-H) $(TARGET_H) $(PLUGIN_HEADERS)

BUILD-LIBS = $(BUILD-LIBS-BOOT-H) \
             $(GM2-LIBS-MODS:%.mod=m2/gm2-libs/%.o) \
             $(GM2-LIBS-CC:%.cc=m2/gm2-libs/%.o) \
             $(GM2-LIBS-C:%.c=m2/gm2-libs/%.o)

# Core library definition modules used by the modula-2 to C++ translator.

MC-LIB-DEFS = \
   ASCII.def \
   Args.def \
   Assertion.def \
   Break.def  \
   COROUTINES.def \
   CmdArgs.def \
   CDataTypes.def \
   Debug.def \
   DynamicStrings.def \
   Environment.def \
   FIO.def \
   FormatStrings.def \
   FpuIO.def \
   IO.def \
   M2Dependent.def \
   M2EXCEPTION.def \
   M2RTS.def \
   MemUtils.def \
   NumberIO.def \
   PushBackInput.def \
   RTExceptions.def \
   RTco.def \
   RTentity.def \
   RTint.def \
   SArgs.def \
   SFIO.def \
   SYSTEM.def \
   Selective.def \
   StdIO.def \
   Storage.def \
   StrCase.def \
   StrIO.def \
   StrLib.def \
   StringConvert.def \
   SysExceptions.def \
   SysStorage.def \
   TimeString.def \
   UnixArgs.def \
   dtoa.def \
   errno.def \
   ldtoa.def \
   libc.def \
   libm.def \
   termios.def \
   wrapc.def \

# Core library implementation modules used by the modula-2 to C++ translator.

MC-LIB-MODS = \
   ASCII.mod \
   Args.mod \
   Assertion.mod \
   Break.mod  \
   CmdArgs.mod \
   Debug.mod \
   DynamicStrings.mod \
   Environment.mod \
   FIO.mod \
   FormatStrings.mod \
   FpuIO.mod \
   IO.mod \
   M2Dependent.mod \
   M2EXCEPTION.mod \
   M2RTS.mod \
   MemUtils.mod \
   NumberIO.mod \
   PushBackInput.mod \
   RTExceptions.mod \
   RTint.mod \
   SArgs.mod \
   SFIO.mod \
   StdIO.mod \
   Storage.mod \
   StrCase.mod \
   StrIO.mod \
   StrLib.mod \
   StringConvert.mod \
   SysStorage.mod \
   TimeString.mod \

MC-LIB-BOOT-CC = $(MC-LIB-MODS:%.mod=%.cc)

# Definition modules for the modula-2 to C++ translator found in mc.

MC-DEFS = \
   Indexing.def \
   alists.def \
   decl.def \
   keyc.def \
   lists.def \
   mcComment.def \
   mcComp.def \
   mcDebug.def \
   mcError.def \
   mcFileName.def \
   mcLexBuf.def \
   mcMetaError.def \
   mcOptions.def \
   mcPreprocess.def \
   mcPretty.def \
   mcPrintf.def \
   mcQuiet.def \
   mcReserved.def \
   mcSearch.def \
   mcStack.def \
   mcStream.def \
   mcflex.def \
   mcp1.def \
   mcp2.def \
   mcp3.def \
   mcp4.def \
   mcp5.def  \
   nameKey.def \
   symbolKey.def \
   varargs.def \
   wlists.def \

# Implementation modules for the modula-2 to C++ translator found in mc.

MC-MODS = \
   Indexing.mod \
   alists.mod \
   decl.mod \
   keyc.mod \
   lists.mod \
   mcComment.mod \
   mcComp.mod \
   mcDebug.mod \
   mcError.mod \
   mcFileName.mod \
   mcLexBuf.mod \
   mcMetaError.mod \
   mcOptions.mod \
   mcPreprocess.mod \
   mcPretty.mod \
   mcPrintf.mod \
   mcQuiet.mod \
   mcReserved.mod \
   mcSearch.mod \
   mcStack.mod \
   mcStream.mod \
   nameKey.mod \
   symbolKey.mod \
   top.mod \
   varargs.mod \
   wlists.mod \

# Parser files generated by pge from .bnf files.

MC-AUTO-MODS = \
   mcp1.mod \
   mcp2.mod \
   mcp3.mod \
   mcp4.mod \
   mcp5.mod

MC-BOOT-CC = $(MC-MODS:%.mod=%.cc) $(MC-AUTO-MODS:%.mod=%.cc)

# C interface files for mc.

MC-INTERFACE-C = \
   M2LINK.c \
   SYSTEM.c \
   Selective.c \
   SysExceptions.c \
   abort.c \
   errno.c \
   libc.c \
   mcrts.c \
   termios.c \
   wrapc.c \

# C++ interface files for mc.

MC-INTERFACE-CC = \
   UnixArgs.cc \
   dtoa.cc \
   ldtoa.cc \

BUILD-MC-BOOT-H =  $(MC-LIB-DEFS:%.def=m2/mc-boot-gen/$(SRC_PREFIX)%.h) \
                   $(MC-DEFS:%.def=m2/mc-boot-gen/$(SRC_PREFIX)%.h)

BUILD-MC-BOOT-CC =  $(MC-LIB-MODS:%.mod=m2/mc-boot-gen/$(SRC_PREFIX)%.cc) \
                    $(MC-MODS:%.mod=m2/mc-boot-gen/$(SRC_PREFIX)%.cc)

BUILD-MC-BOOT-AUTO-CC = $(MC-AUTO-MODS:%.mod=m2/mc-boot-gen/$(SRC_PREFIX)%.cc)

BUILD-MC-BOOT-O =  $(MC-LIB-BOOT-CC:%.cc=m2/mc-boot/$(SRC_PREFIX)%.o) \
                   $(MC-BOOT-CC:%.cc=m2/mc-boot/$(SRC_PREFIX)%.o)

BUILD-MC-INTERFACE-O = $(MC-INTERFACE-C:%.c=m2/mc-boot-ch/$(SRC_PREFIX)%.o) \
                       $(MC-INTERFACE-CC:%.cc=m2/mc-boot-ch/$(SRC_PREFIX)%.o)

GM2GCC = -I$(srcdir)/m2 -Im2 -I$(srcdir)/m2/gm2-gcc -Im2/gm2-gcc

MCINCLUDES= -I$(srcdir)/m2/mc-boot-ch
LOCAL_INCLUDES = -I. -I$(srcdir)/../include -I$(srcdir)
GM2_BOOT_INCLUDES=-I. \
  -I$(srcdir)/../include \
  -I$(srcdir) \
  -Im2/gm2-libs-boot \
  -I$(srcdir)/m2/gm2-libs-ch \
  -Im2/gm2-compiler-boot \
  -I$(srcdir)m2/gm2-gcc \
  -Im2/gm2-libiberty \
  -I$(srcdir)/m2/gm2-libiberty

GCC_COLOR=m2/gm2-gcc/m2color.o diagnostic-color.o

m2/boot-bin/mc$(exeext): $(BUILD-MC-BOOT-O) $(BUILD-MC-INTERFACE-O) \
        m2/mc-boot/main.o mcflex.o m2/gm2-libs-boot/RTcodummy.o
	+$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ $(BUILD-MC-BOOT-O) \
         $(BUILD-MC-INTERFACE-O) m2/mc-boot/main.o \
         mcflex.o m2/gm2-libs-boot/RTcodummy.o -lm

m2/mc-boot/$(SRC_PREFIX)%.o: m2/mc-boot/$(SRC_PREFIX)%.cc m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) $(CXXFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -g -c -I. -I$(srcdir)/m2/mc-boot-ch -I$(srcdir)/m2/mc-boot -I$(srcdir)/../include -I$(srcdir) $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/mc-boot-ch/$(SRC_PREFIX)%.o: m2/mc-boot-ch/$(SRC_PREFIX)%.c m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) $(CXXFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -DHAVE_CONFIG_H -g -c -I. -Im2/gm2-libs -I$(srcdir)/../include -I$(srcdir) $(INCLUDES) -Im2/gm2-libs $< -o $@
	$(POSTCOMPILE)

m2/mc-boot-ch/$(SRC_PREFIX)%.o: m2/mc-boot-ch/$(SRC_PREFIX)%.cc m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) $(CXXFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -DHAVE_CONFIG_H -g -c -I. -Im2/gm2-libs -I$(srcdir)/../include -I$(srcdir) $(INCLUDES) -Im2/gm2-libs $< -o $@
	$(POSTCOMPILE)

m2/mc-boot/main.o: $(M2LINK) $(srcdir)/m2/init/mcinit
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	unset CC ; $(M2LINK) -s --langc++ --exit --name m2/mc-boot/main.cc $(srcdir)/m2/init/mcinit
	$(CXX) $(CM2DEP) $(CXXFLAGS) $(GM2_PICFLAGS) -g -c -I. -I$(srcdir)/../include -I$(srcdir) $(INCLUDES) m2/mc-boot/main.cc -o $@
	$(POSTCOMPILE)

mcflex.o: mcflex.c m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CC) $(CM2DEP) $(CFLAGS) $(GM2_PICFLAGS) -I$(srcdir)/m2/mc -g -c $< -o $@   # remember that mcReserved.h is copied into m2/mc
	$(POSTCOMPILE)

mcflex.c: $(srcdir)/m2/mc/mc.flex
	flex -t $< > $@

m2/gm2-libs-boot/M2RTS.o: $(srcdir)/m2/gm2-libs/M2RTS.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-libs-boot/M2RTS.c $(srcdir)/m2/gm2-libs/M2RTS.mod
	$(COMPILER) $(CM2DEP) -c -DIN_GCC $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot -I$(srcdir)/m2/gm2-libs-ch $(MCINCLUDES) $(INCLUDES) m2/gm2-libs-boot/M2RTS.c -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/%.o: $(srcdir)/m2/gm2-libs-boot/%.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-libs-boot/$*.c $(srcdir)/m2/gm2-libs-boot/$*.mod
	$(COMPILER) $(CM2DEP) -c -DIN_GCC $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) $(MCINCLUDES) -I$(srcdir)/m2/gm2-libs-ch m2/gm2-libs-boot/$*.c -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/%.o: $(srcdir)/m2/gm2-libs/%.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-libs-boot/$*.c $(srcdir)/m2/gm2-libs/$*.mod
	$(COMPILER) $(CM2DEP) -c -DIN_GCC $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot -I$(srcdir)/m2/gm2-libs-ch $(MCINCLUDES) $(INCLUDES) m2/gm2-libs-boot/$*.c -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/$(SRC_PREFIX)%.h: $(srcdir)/m2/gm2-libs/%.def $(MCDEPS)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(MC) -o=$@ $(srcdir)/m2/gm2-libs/$*.def

GM2-LIBS-BOOT-H=m2/gm2-libs/gm2-libs-host.h m2/gm2-libs-boot/GM2RTS.h m2/gm2-libs-boot/GSYSTEM.h

m2/gm2-libs-boot/RTcodummy.o: $(srcdir)/m2/gm2-libs-ch/RTcodummy.c
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c -DIN_GCC $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/RTintdummy.o: $(srcdir)/m2/gm2-libs-ch/RTintdummy.c
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c -DIN_GCC $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/wrapc.o: $(srcdir)/m2/gm2-libs-ch/wrapc.c m2/gm2-libs-boot/$(SRC_PREFIX)wrapc.h $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c -DHAVE_CONFIG_H $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot -Im2/gm2-libs $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/UnixArgs.o: $(srcdir)/m2/gm2-libs-ch/UnixArgs.cc m2/gm2-libs-boot/$(SRC_PREFIX)UnixArgs.h $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c -DIN_GCC $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/choosetemp.o: m2/gm2-libs-ch/choosetemp.c m2/gm2-libiberty/Gchoosetemp.h $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot -Im2/gm2-libiberty -I$(srcdir)/m2/gm2-libiberty/ $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/errno.o: $(srcdir)/m2/gm2-libs-ch/errno.c m2/gm2-libs-boot/$(SRC_PREFIX)errno.h $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/dtoa.o: $(srcdir)/m2/gm2-libs-ch/dtoa.cc $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs-boot -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/ldtoa.o: $(srcdir)/m2/gm2-libs-ch/ldtoa.cc $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs-boot -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/termios.o: $(srcdir)/m2/gm2-libs-ch/termios.c $(BUILD-LIBS-BOOT-H) $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/SysExceptions.o: $(srcdir)/m2/gm2-libs-ch/SysExceptions.c \
                                  m2/gm2-libs-boot/$(SRC_PREFIX)SysExceptions.h $(GM2-LIBS-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/SysStorage.o: $(srcdir)/m2/gm2-libs/SysStorage.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-libs-boot/SysStorage.c $(srcdir)/m2/gm2-libs/SysStorage.mod
	$(COMPILER) $(CM2DEP) -DIN_GCC -c $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) \
          -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(MCINCLUDES) $(INCLUDES) \
          m2/gm2-libs-boot/SysStorage.c -o m2/gm2-libs-boot/SysStorage.o
	$(POSTCOMPILE)

m2/gm2-compiler-boot/M2GCCDeclare.o: $(srcdir)/m2/gm2-compiler/M2GCCDeclare.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) $(MC_EXTENDED_OPAQUE) -o=m2/gm2-compiler-boot/M2GCCDeclare.c $<
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/M2GCCDeclare.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/M2Error.o: $(srcdir)/m2/gm2-compiler/M2Error.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) $(MC_EXTENDED_OPAQUE) -o=m2/gm2-compiler-boot/M2Error.c $<
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/M2Error.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/%.o: $(srcdir)/m2/gm2-compiler/%.mod $(BUILD-BOOT-H) $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/$*.c $(srcdir)/m2/gm2-compiler/$*.mod
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/$*.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/%.o: m2/gm2-compiler-boot/%.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/$*.c m2/gm2-compiler-boot/$*.mod
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/$*.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/$(SRC_PREFIX)%.h: $(srcdir)/m2/gm2-compiler/%.def $(MCDEPS)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(MC) -o=$@ $(srcdir)/m2/gm2-compiler/$*.def

m2/gm2-compiler-boot/P0SyntaxCheck.c: m2/gm2-compiler-boot/P0SyntaxCheck.mod $(MCDEPS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/P0SyntaxCheck.c m2/gm2-compiler-boot/P0SyntaxCheck.mod

m2/gm2-compiler-boot/P0SyntaxCheck.o: m2/gm2-compiler-boot/P0SyntaxCheck.c $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/P0SyntaxCheck.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/P1Build.c: m2/gm2-compiler-boot/P1Build.mod $(MCDEPS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/P1Build.c m2/gm2-compiler-boot/P1Build.mod

m2/gm2-compiler-boot/P1Build.o: m2/gm2-compiler-boot/P1Build.c $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/P1Build.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/P2Build.c: m2/gm2-compiler-boot/P2Build.mod $(MCDEPS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/P2Build.c m2/gm2-compiler-boot/P2Build.mod

m2/gm2-compiler-boot/P2Build.o: m2/gm2-compiler-boot/P2Build.c $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/P2Build.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/P3Build.c: m2/gm2-compiler-boot/P3Build.mod $(MCDEPS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/P3Build.c m2/gm2-compiler-boot/P3Build.mod

m2/gm2-compiler-boot/P3Build.o: m2/gm2-compiler-boot/P3Build.c $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/P3Build.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/PCBuild.c: m2/gm2-compiler-boot/PCBuild.mod $(MCDEPS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/PCBuild.c m2/gm2-compiler-boot/PCBuild.mod

m2/gm2-compiler-boot/PCBuild.o: m2/gm2-compiler-boot/PCBuild.c $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/PCBuild.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/PHBuild.c: m2/gm2-compiler-boot/PHBuild.mod $(MCDEPS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(MC) -o=m2/gm2-compiler-boot/PHBuild.c m2/gm2-compiler-boot/PHBuild.mod

m2/gm2-compiler-boot/PHBuild.o: m2/gm2-compiler-boot/PHBuild.c $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(GM2GCC) \
            $(GM2_BOOT_INCLUDES) $(MCINCLUDES) $(INCLUDES) m2/gm2-compiler-boot/PHBuild.c -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/m2flex.o: m2/gm2-compiler/m2flex.c $(BUILD-BOOT-H) $(TIMEVAR_H) \
        $(BUILD-LIBS-BOOT-H) m2/gm2-compiler-boot/$(SRC_PREFIX)NameKey.h \
        $(CONFIG_H) m2/gm2config.h $(TARGET_H) $(PLUGIN_HEADERS)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c -g $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
          $(GM2GCC) $(INCLUDES) $(GM2_BOOT_INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-compiler/m2flex.c: $(srcdir)/m2/m2.flex $(TIMEVAR_H) insn-attr-common.h
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	flex -t $< | sed -e 's/ malloc/ xmalloc/' | sed -e 's/ realloc/ xrealloc/' > $@

m2/gm2-libiberty/$(SRC_PREFIX)%.h: $(srcdir)/m2/gm2-libiberty/%.def $(MCDEPS)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(MC) -o=$@ $(srcdir)/m2/gm2-libiberty/$*.def

# The rules to build objects in gm2-compiler and gm2-libs directories.

m2/gm2-compiler/%.o: $(srcdir)/m2/gm2-compiler/%.mod
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(GM2_1) $(GM2_FLAGS) -c -I$(srcdir)/m2/gm2-compiler -I$(srcdir)/m2/gm2-libs -I$(srcdir)/m2/gm2-gcc -I$(srcdir)/m2/gm2-libiberty $< -o $@
	$(POSTCOMPILE)

m2/gm2-compiler/m2flex.o: m2/gm2-compiler/m2flex.c m2/gm2-libs/gm2-libs-host.h $(TIMEVAR_H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(COMPILER) $(CM2DEP) -c -g $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) \
          $(GM2GCC) -Im2/gm2-compiler-boot -Im2/gm2-libs-boot $< -o $@
	$(POSTCOMPILE)

m2/gm2-compiler/%.o: m2/gm2-compiler/%.mod
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(GM2_1) $(GM2_FLAGS) -c -I$(srcdir)/m2/gm2-compiler -I$(srcdir)/m2/gm2-libs -I$(srcdir)/m2/gm2-gcc -I$(srcdir)/m2/gm2-libiberty $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-iso/%.o: $(srcdir)/m2/gm2-libs-iso/%.c m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -DBUILD_GM2_LIBS_TARGET -DBUILD_GM2_LIBS -c $(CFLAGS) $(GM2_PICFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-iso/%.o: $(srcdir)/m2/gm2-libs-iso/%.mod
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(GM2_1) $(GM2_ISO_FLAGS) -c -B./ -Im2/gm2-libs-iso:$(srcdir)/m2/gm2-libs-iso -I$(srcdir)/m2/gm2-libs $< -o $@
	$(POSTCOMPILE)


# We build the cc1gm2$(exeext) from the boot stage and then proceed to build it
# again using itself.

m2/gm2-libs/gm2-libs-host.h:
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	echo "Configuring to build libraries using native compiler" ; \
        NEW_SRCDIR=`${srcdir}/m2/tools-src/calcpath ../../ ${srcdir} m2/gm2-libs` ; \
        export NEW_SRCDIR ; \
        cd m2/gm2-libs ; \
        $(SHELL) -c '$${NEW_SRCDIR}/config-host \
               --srcdir=$${NEW_SRCDIR} \
               --target=$(target) \
               --program-suffix=$(exeext)'

# Autoconf inserts -DCROSS_DIRECTORY_STRUCTURE if we are building a
# cross compiler and the ../Makefile.in above appends this to INTERNAL_CFLAGS.

m2/gm2config.h: m2/gm2config.aci
	grep -v define\ PACKAGE_ $< > $@

m2/gm2config.aci:
	NEW_SRCDIR=`${srcdir}/m2/tools-src/calcpath ../ ${srcdir} m2` ; \
        export NEW_SRCDIR ; \
	cd m2 ; \
	if echo $(INTERNAL_CFLAGS) | grep \\-DCROSS_DIRECTORY_STRUCTURE; then \
            AR=$(echo $(AR_FOR_TARGET) | sed -e "s/^ //") ; \
            export AR ; \
            RANLIB=$(echo $(RANLIB_FOR_TARGET) | sed -e "s/^ //") ; \
            export RANLIB ; \
            $(SHELL) -c '$${NEW_SRCDIR}/configure --srcdir=$${NEW_SRCDIR} \
                --target=$(target) --program-suffix=$(exeext) \
                --includedir=$(SYSTEM_HEADER_DIR) --libdir=$(libdir) \
                --libexecdir=$(libexecdir)' ; \
        else \
            $(SHELL) -c '$${NEW_SRCDIR}/configure --srcdir=$(NEW_SRCDIR) \
                --target=$(target) --program-suffix=$(exeext)' ; \
        fi

$(objdir)/m2/gm2-libs-min/SYSTEM.def: $(GM2_PROG_DEP)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(SHELL) $(srcdir)/m2/tools-src/makeSystem -fpim \
             $(srcdir)/m2/gm2-libs-min/SYSTEM.def \
             $(srcdir)/m2/gm2-libs-min/SYSTEM.mod \
             -I$(srcdir)/m2/gm2-libs-min -I$(srcdir)/m2/gm2-libs \
             "$(GM2_FOR_TARGET)" $@

$(objdir)/m2/gm2-libs/SYSTEM.def: $(GM2_PROG_DEP)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	echo "GM2_FOR_TARGET $(GM2_FOR_TARGET)"
	echo "GCC_FOR_TARGET $(GCC_FOR_TARGET)"
	$(SHELL) $(srcdir)/m2/tools-src/makeSystem -fpim \
             $(srcdir)/m2/gm2-libs/SYSTEM.def \
             $(srcdir)/m2/gm2-libs/SYSTEM.mod \
             -I$(srcdir)/m2/gm2-libs \
             "$(GM2_FOR_TARGET)" $@

$(objdir)/m2/gm2-libs-iso/SYSTEM.def: $(GM2_PROG_DEP)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(SHELL) $(srcdir)/m2/tools-src/makeSystem -fiso \
             $(srcdir)/m2/gm2-libs-iso/SYSTEM.def \
             $(srcdir)/m2/gm2-libs-iso/SYSTEM.mod \
             -I$(srcdir)/m2/gm2-libs-iso -I$(srcdir)/m2/gm2-libs \
             "$(GM2_FOR_TARGET)" $@

$(objdir)/m2/gm2-libs-coroutines/SYSTEM.def: $(GM2_PROG_DEP)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(SHELL) $(srcdir)/m2/tools-src/makeSystem -fpim \
             $(srcdir)/m2/gm2-libs-coroutines/SYSTEM.def \
             $(srcdir)/m2/gm2-libs-coroutines/SYSTEM.mod \
             -I$(srcdir)/m2/gm2-libs-coroutines -I$(srcdir)/m2/gm2-libs-iso -I$(srcdir)/m2/gm2-libs \
             "$(GM2_FOR_TARGET)" $@

build-compiler: $(GM2-COMP-MODS:%.mod=m2/gm2-compiler/%.o) \
                $(GM2-AUTO-MODS:%.mod=m2/gm2-compiler/%.o) \
                m2/gm2-compiler/m2flex.o

m2/gm2-compiler/gm2.a: build-compiler gm2$(exeext)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(AR_FOR_TARGET) cr $@ $(GM2-COMP-MODS:%.mod=m2/gm2-compiler/%.o) \
                    $(GM2-AUTO-MODS:%.mod=m2/gm2-compiler/%.o)
	$(RANLIB) $@

m2/gm2-libs/libgm2.a: build-compiler gm2$(exeext) $(BUILD-LIBS)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(AR) cr $@ $(GM2-LIBS-MODS:%.mod=m2/gm2-libs/%.o) \
                    $(GM2-LIBS-CC:%.cc=m2/gm2-libs/%.o) \
                    $(GM2-LIBS-C:%.c=m2/gm2-libs/%.o)
	$(RANLIB) $@

m2/gm2-libs/%.o: $(srcdir)/m2/gm2-libs/%.mod $(MCDEPS) $(BUILD-BOOT-H)
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(GM2_1) -c $(GM2_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2/gm2-libs -I$(srcdir)/m2/gm2-libs-iso $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs/%.o: $(srcdir)/m2/gm2-libs-ch/%.c m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -DBUILD_GM2_LIBS -c $(CFLAGS) $(GM2_PICFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs/%.o: $(srcdir)/m2/gm2-libs-ch/%.cc m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs/choosetemp.o: m2/gm2-libs-ch/choosetemp.c m2/gm2-libiberty/Gchoosetemp.h m2/gm2-libs/gm2-libs-host.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) -c $(CFLAGS) $(GM2_PICFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot -Im2/gm2-libiberty -I$(srcdir)/m2/gm2-libiberty/ $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-libs-boot/libgm2.a: m2/boot-bin/mc$(exeext) $(BUILD-LIBS-BOOT)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(AR) cr $@ $(GM2-LIBS-BOOT-MODS:%.mod=m2/gm2-libs-boot/%.o) \
                    $(GM2-LIBS-BOOT-CC:%.cc=m2/gm2-libs-boot/%.o) \
                    $(GM2-LIBS-BOOT-C:%.c=m2/gm2-libs-boot/%.o)
	$(RANLIB) $@

m2/gm2-compiler-boot/gm2.a: m2/boot-bin/mc$(exeext) m2/boot-bin/mklink$(exeext) \
                         $(BUILD-LIBS-BOOT) $(BUILD-COMPILER-BOOT)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(AR) cr $@ $(GM2-COMP-BOOT-MODS:%.mod=m2/gm2-compiler-boot/%.o) \
                    $(GM2-AUTO-MODS:%.mod=m2/gm2-compiler-boot/%.o)
	$(RANLIB) $@

m2/gm2-compiler-boot/gm2.a: m2/boot-bin/mc$(exeext)

m2/boot-bin/mklink$(exeext): $(srcdir)/m2/tools-src/mklink.c
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) $(CFLAGS) $(LDFLAGS) -I$(srcdir)/m2 -Im2/gm2-libs-boot -Im2/gm2-compiler-boot -I$(srcdir)/m2/mc-boot-ch $(INCLUDES) $< -o $@
	$(POSTCOMPILE)

m2/gm2-compiler-boot/$(SRC_PREFIX)%.h: $(srcdir)/m2/gm2-compiler-boot/%.def $(MCDEPS)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(MC) --quiet -o=$@ $(srcdir)/m2/gm2-compiler-boot/$*.def

m2/gm2-compiler/%.mod: $(srcdir)/m2/gm2-compiler/%.bnf $(PGE)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(PGE) -k -l $< -o $@

m2/gm2-compiler-boot/%.mod: $(srcdir)/m2/gm2-compiler/%.bnf $(PGE)
	-test -d $(@D) || $(mkinstalldirs) $(@D)
	$(PGE) -k -l $< -o $@

check-m2: check-gm2
check_m2: check-gm2
check_gm2: check-gm2
check-modula2: check-gm2
check_modula2: check-gm2
check-modula-2: check-gm2
check_modula-2: check-gm2
check_modula_2: check-gm2

lang_checks += check-gm2
lang_checks_parallelized += check-gm2
# For description see the check_$lang_parallelize comment in gcc/Makefile.in.
check_gm2_parallelize = 10000

check-gm2-local: $(GM2TESTSUITEDIR)/site.exp
	-(rootme=`${PWD_COMMAND}`; export rootme; \
	srcdir=`cd ${srcdir}; ${PWD_COMMAND}` ; export srcdir ; \
	cd $(TESTSUITEDIR); \
	EXPECT=${EXPECT} ; export EXPECT ; \
	if [ -f $${rootme}/../expect/expect ] ; then  \
	   TCL_LIBRARY=`cd .. ; cd ${srcdir}/../tcl/library ; ${PWD_COMMAND}` ; \
	    export TCL_LIBRARY ; fi ; \
	$(RUNTEST) --tool gm2 --directory testsuite/m2/pim/pass)

BUILD-PGE-O = \
   m2/pge-boot/GArgs.o \
   m2/pge-boot/GASCII.o \
   m2/pge-boot/GAssertion.o \
   m2/pge-boot/Gbnflex.o \
   m2/pge-boot/GDebug.o \
   m2/pge-boot/GDynamicStrings.o \
   m2/pge-boot/GFIO.o \
   m2/pge-boot/GIndexing.o \
   m2/pge-boot/GIO.o \
   m2/pge-boot/GLists.o \
   m2/pge-boot/GM2Dependent.o \
   m2/pge-boot/GM2EXCEPTION.o \
   m2/pge-boot/GM2RTS.o \
   m2/pge-boot/GNameKey.o \
   m2/pge-boot/GNumberIO.o \
   m2/pge-boot/GOutput.o \
   m2/pge-boot/Gpge.o \
   m2/pge-boot/GPushBackInput.o \
   m2/pge-boot/GRTExceptions.o \
   m2/pge-boot/GSFIO.o \
   m2/pge-boot/GStdIO.o \
   m2/pge-boot/GStorage.o \
   m2/pge-boot/GStrCase.o \
   m2/pge-boot/GStrIO.o \
   m2/pge-boot/GStrLib.o \
   m2/pge-boot/GSymbolKey.o \
   m2/pge-boot/GSysStorage.o \
   m2/pge-boot/Glibc.o \
   m2/pge-boot/Gwrapc.o \
   m2/pge-boot/Gerrno.o \
   m2/pge-boot/GUnixArgs.o \
   m2/pge-boot/Gtermios.o \
   m2/pge-boot/GSysExceptions.o \
   m2/pge-boot/Gabort.o \
   m2/pge-boot/Gmcrts.o \
   m2/pge-boot/main.o

ifeq ($(M2_MAINTAINER),yes)
include m2/Make-maintainer
else
m2/pge-boot/%.o: m2/pge-boot/%.c m2/gm2-libs/gm2-libs-host.h m2/gm2config.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) $(CFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) $(INCLUDES) -I$(srcdir)/m2/pge-boot -Im2/gm2-libs -g -c $< -o $@
	$(POSTCOMPILE)

m2/pge-boot/%.o: m2/pge-boot/%.cc m2/gm2-libs/gm2-libs-host.h m2/gm2config.h
	-test -d $(@D)/$(DEPDIR) || $(mkinstalldirs) $(@D)/$(DEPDIR)
	$(CXX) $(CM2DEP) $(CXXFLAGS) $(GM2_PICFLAGS) $(MC_SRC_FLAGS) $(INCLUDES) -I$(srcdir)/m2/pge-boot -Im2/gm2-libs -g -c $< -o $@
	$(POSTCOMPILE)

$(PGE): $(BUILD-PGE-O)
	+$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ $(BUILD-PGE-O) -lm

endif
