From 5bb27a1bb2f1c0533e1dda6c4a326e50756830f1 Mon Sep 17 00:00:00 2001
From: Gaius Mulley <gaiusmod2@gmail.com>
Date: Sun, 2 Apr 2023 20:30:43 +0100
Subject: [PATCH] PR modula2/109336 pass -fmod= though and build
 m2/stage2/cc1gm2 libs

This patch enables gm2 to pass -fmod= though to cc1gm2.  It also
builds the libraries for m2/stage2/cc1gm2 with no named path and
full debugging.

gcc/m2/ChangeLog:

	PR modula2/109336
	* Make-lang.in (GM2_O): Set to -O0.
	(GM2_LIBS): Remove target libraries and replace with build libs.
	(BUILD-LIBS): New declaration.
	(m2/gm2-libs/libgm2.a): New rule.
	(m2/gm2-libs/%.o): New rule.
	(m2/gm2-libs/choosetemp.o): New rule.
	* gm2-compiler/M2ColorString.mod (append): Use ADR rather than
	implicit conversion.
	* gm2-compiler/M2Comp.mod (Compile): Add qprintf messages for when
	a source file is not found.  Improve comments and formatting.
	* gm2-libs-ch/cgetopt.c (cgetopt_cgetopt_long): Remove
	ansi-decl.h.  Add getopt.h.
	(cgetopt_cgetopt_long_only): Change cgetopt_ to getopt_.
	* gm2spec.cc (lang_specific_driver): Do not skip -fmod=.
	Remove comment.

Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
---
 gcc/m2/Make-lang.in                   | 32 +++++++++++++++++++++++++--
 gcc/m2/gm2-compiler/M2ColorString.mod |  3 ++-
 gcc/m2/gm2-compiler/M2Comp.mod        | 13 ++++++-----
 gcc/m2/gm2-libs-ch/cgetopt.c          |  7 +++---
 gcc/m2/gm2spec.cc                     |  4 +---
 5 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/gcc/m2/Make-lang.in b/gcc/m2/Make-lang.in
index ad21b208f09b..b34db0d91563 100644
--- a/gcc/m2/Make-lang.in
+++ b/gcc/m2/Make-lang.in
@@ -455,7 +455,7 @@ MC=m2/boot-bin/mc$(exeext) $(MC_ARGS)
 MC_LIBS=m2/mc-boot-ch/Glibc.o m2/mc-boot-ch/Gmcrts.o
 
 M2LINK=m2/boot-bin/mklink$(exeext)
-GM2_O=
+GM2_O=-O0
 GM2_O_S3=-O
 GM2_OS=-Os
 GM2_G=-g -fm2-g
@@ -505,7 +505,7 @@ GM2_C_OBJS        = m2/gm2-lang.o \
                     m2/gm2-gcc/m2misc.o \
                     m2/gm2-gcc/init.o
 GM2_LIBS          = m2/gm2-compiler/gm2.a \
-                    ../$(target_subdir)/libgm2/libm2pim/.libs/libm2pim.a m2/gm2-libs-boot/choosetemp.o
+                    m2/gm2-libs/libgm2.a \
 
 GM2_LIBS_BOOT     = m2/gm2-compiler-boot/gm2.a \
                     m2/gm2-libs-boot/libgm2.a \
@@ -1138,6 +1138,11 @@ BUILD-COMPILER-BOOT = $(BUILD-COMPILER-BOOT-H) \
 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 = \
@@ -1595,6 +1600,29 @@ m2/gm2-compiler/gm2.a: build-compiler gm2$(exeext)
                     $(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) || $(mkinstalldirs) $(@D)
+	$(GM2_1) -c $(GM2_FLAGS) -Im2/gm2-libs -I$(srcdir)/m2/gm2-libs -I$(srcdir)/m2/gm2-libs-iso $< -o $@
+
+m2/gm2-libs/%.o: $(srcdir)/m2/gm2-libs-ch/%.c m2/gm2-libs/gm2-libs-host.h
+	-test -d $(@D) || $(mkinstalldirs) $(@D)
+	$(CXX) -DBUILD_GM2_LIBS -c $(CFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
+
+m2/gm2-libs/%.o: $(srcdir)/m2/gm2-libs-ch/%.cc m2/gm2-libs/gm2-libs-host.h
+	-test -d $(@D) || $(mkinstalldirs) $(@D)
+	$(CXX) -c $(CFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot $(INCLUDES) $< -o $@
+
+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) || $(mkinstalldirs) $(@D)
+	$(CXX) -c $(CFLAGS) -Im2/gm2-libs -I$(srcdir)/m2 -Im2 -I. -Im2/gm2-libs-boot -Im2/gm2-libiberty -I$(srcdir)/m2/gm2-libiberty/ $(INCLUDES) $< -o $@
+
 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) \
diff --git a/gcc/m2/gm2-compiler/M2ColorString.mod b/gcc/m2/gm2-compiler/M2ColorString.mod
index f0ba7966b028..ec44b8607b00 100644
--- a/gcc/m2/gm2-compiler/M2ColorString.mod
+++ b/gcc/m2/gm2-compiler/M2ColorString.mod
@@ -27,6 +27,7 @@ FROM DynamicStrings IMPORT InitString, InitStringCharStar,
                            Dup, char, Length, Mult ;
 FROM StrLib IMPORT StrLen ;
 FROM libc IMPORT printf ;
+FROM SYSTEM IMPORT ADR ;
 
 
 VAR
@@ -57,7 +58,7 @@ PROCEDURE append (s: String; name: ARRAY OF CHAR) : String ;
 VAR
    c: String ;
 BEGIN
-   c := InitStringCharStar (colorize_start (EnableColor, name, StrLen (name))) ;
+   c := InitStringCharStar (colorize_start (EnableColor, ADR (name), StrLen (name))) ;
    s := ConCat (s, c) ;
    c := KillString (c) ;
    RETURN s
diff --git a/gcc/m2/gm2-compiler/M2Comp.mod b/gcc/m2/gm2-compiler/M2Comp.mod
index a2a7797397d1..e33363e9f834 100644
--- a/gcc/m2/gm2-compiler/M2Comp.mod
+++ b/gcc/m2/gm2-compiler/M2Comp.mod
@@ -319,9 +319,9 @@ BEGIN
       ELSE
          ModuleType := Program
       END ;
-      IF (Main=Sym) OR NeedToParseImplementation(Sym)
+      IF (Main=Sym) OR NeedToParseImplementation (Sym)
       THEN
-         (* only need to read implementation module if hidden types are declared or it is the main module *)
+         (* Only need to read implementation module if hidden types are declared or it is the main module *)
          LibName := NIL ;
          IF Main=Sym
          THEN
@@ -333,13 +333,16 @@ BEGIN
             THEN
                FileName := PreprocessModule (FileName, FALSE) ;
                PutLibName (Sym, makekey (string (LibName)))
+            ELSE
+               qprintf1 ('   Module %-20s : implementation source file not found\n', SymName)
             END
          END ;
+
          IF FileName#NIL
          THEN
             IF OpenSource (AssociateModule (Dup (FileName), Sym))
             THEN
-               IF NOT P0SyntaxCheck.CompilationUnit()
+               IF NOT P0SyntaxCheck.CompilationUnit ()
                THEN
                   WriteFormat0 ('compilation failed') ;
                   CloseSource ;
@@ -356,9 +359,9 @@ BEGIN
             ELSE
                (* It is quite legitimate to implement a module in C (and pretend it was a M2
                   implementation) providing that it is not the main program module and the
-                  definition module do not declare a hidden type when -fextended-opaque
+                  definition module does not declare a hidden type when -fextended-opaque
                   is used.  *)
-               IF (NOT WholeProgram) OR (Sym=Main) OR IsHiddenTypeDeclared(Sym)
+               IF (NOT WholeProgram) OR (Sym=Main) OR IsHiddenTypeDeclared (Sym)
                THEN
                   (* Unrecoverable error.  *)
                   MetaErrorString1 (Sprintf1 (InitString ('file {%%1EUAF%s} containing module {%%1a} cannot be found'),
diff --git a/gcc/m2/gm2-libs-ch/cgetopt.c b/gcc/m2/gm2-libs-ch/cgetopt.c
index d6b40b183b43..017fe91ad95d 100644
--- a/gcc/m2/gm2-libs-ch/cgetopt.c
+++ b/gcc/m2/gm2-libs-ch/cgetopt.c
@@ -26,7 +26,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #include "config.h"
 #include "system.h"
-#include "ansi-decl.h"
+#include <getopt.h>
+// #include "ansi-decl.h"
 
 
 #ifdef __cplusplus
@@ -60,7 +61,7 @@ int
 cgetopt_cgetopt_long (int argc, char *argv[], char *optstring, const struct option *longopts,
                     int *longindex)
 {
-  int r = cgetopt_long (argc, argv, optstring, longopts, longindex);
+  int r = getopt_long (argc, argv, optstring, longopts, longindex);
 
   cgetopt_optarg = optarg;
   cgetopt_optind = optind;
@@ -75,7 +76,7 @@ int
 cgetopt_cgetopt_long_only (int argc, char *argv[], char *optstring,
                          const struct option *longopts, int *longindex)
 {
-  int r = cgetopt_long_only (argc, argv, optstring, longopts, longindex);
+  int r = getopt_long_only (argc, argv, optstring, longopts, longindex);
 
   cgetopt_optarg = optarg;
   cgetopt_optind = optind;
diff --git a/gcc/m2/gm2spec.cc b/gcc/m2/gm2spec.cc
index 8b4010838ec5..cd7ae808d987 100644
--- a/gcc/m2/gm2spec.cc
+++ b/gcc/m2/gm2spec.cc
@@ -451,7 +451,7 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
   int need_math = (MATH_LIBRARY[0] != '\0');
 
   /* 1 if we should add -lpthread to the command-line.
-    FIXME: the default should be a configuration choice.  */
+     FIXME: the default should be a configuration choice.  */
   int need_pthread = 1;
 
   /* True if we saw -static.  */
@@ -533,7 +533,6 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
 	  break;
 	case OPT_fmod_:
 	  module_extension = xstrdup (arg);
-	  args[i] |= SKIPOPT; /* We will add the option if it is needed.  */
 #if defined(DEBUG_ARG)
 	  printf ("seen -fmod=%s\n", module_extension);
 #endif
@@ -701,7 +700,6 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options,
 		    printf ("modula-2 source file detected: %s\n", source_file);
 #endif
 		    args[i] |= M2SOURCE;
-		    // args[i] = 0;
 		  }
 	      }
 	  }
-- 
GitLab