From a1e2b00903f3a4bb7701926ea3f3c812f17f1f48 Mon Sep 17 00:00:00 2001
From: Simon Sobisch <simonsobisch@gnu.org>
Date: Wed, 29 Jan 2025 12:13:32 +0000
Subject: [PATCH] fix cobc compiler driver

* UDF: use packaged datadir
* cover correct --fixed, --free and matching -fformat
* pass -W, -Wextra and -dumpversion to COBOL driver
* minor syntax adjustments
---
 gcc/cobol/gcobc | 80 +++++++++++++++++++++++++++++++------------------
 1 file changed, 51 insertions(+), 29 deletions(-)

diff --git a/gcc/cobol/gcobc b/gcc/cobol/gcobc
index 9afd8fd93fef..74f54a4ef9f1 100755
--- a/gcc/cobol/gcobc
+++ b/gcc/cobol/gcobc
@@ -13,8 +13,8 @@
 #
 # User-defined variables, and their defaults:
 #
-#     Variable	Default		Effect 
-#     echo	 none	  	If defined, echo the gcobol command	
+#     Variable	Default		Effect
+#     echo	 none	  	If defined, echo the gcobol command
 #     gcobcx	 none		Produce verbose messages
 #     gcobol	./gcobol	Name of the gcobol binary
 #     GCOBCUDF	PREFIX/share/cobol/udf/    Location of UDFs to be prepended to input
@@ -22,7 +22,7 @@
 # By default, this script includes all files in $GCOBCUDF.  To defeat
 # that behavior, use GCOBCUDF=none.
 #
-# A list of supported options is produced with "gcobc -HELP". 
+# A list of supported options is produced with "gcobc -HELP".
 #
 ## Maintainer note. In modifying this file, the following may make
 ## your life easier:
@@ -46,20 +46,26 @@ then
     copydir="-I$COB_COPY_DIR"
 fi
 
-udf_default="${0%/*}/../share/cobol/udf"
+# TODO: this file likely needs to query gcobol for its shared path instead
+udf_default="${0%/*}/../share/gcobol/udf"
+if [ ! -d "$udfdir" ]
+then
+   # the one above is the installed one from the packages this one was previously used
+   udf_default="${0%/*}/../share/cobol/udf"
+fi
 udfdir="${GCOBCUDF:-$udf_default}"
 
 if [ -d "$udfdir" ]
 then
     for F in "$udfdir"/*
     do
-        if [ -f $F ]
+        if [ -f "$F" ]
         then
             includes="$includes -include $F "
         fi
     done
 else
-    if [ "$GCOBCUDF" -a "$GCOBCUDF" != "none" ]
+    if [ "${GCOBCUDF:-none}" != "none" ]
     then
         echo warning: no such directory: "'$GCOBCUDF'"
     fi
@@ -92,16 +98,19 @@ no_warn() { :; } # silence is golden
 
 help() {
     cat<<EOF
-$0 recognizes the following GnuCOBOL cobc output mode options: 
+$0 recognizes the following GnuCOBOL cobc output mode options:
         -b, -c, -m, -S, -x
-$0 recognizes the following GnuCOBOL cobc compilation options: 
+$0 recognizes the following GnuCOBOL cobc compilation options:
         -C
         -d, --debug
         -E
+        -g
+        --coverage
         -ext
         -fec=exception-name, -fno-ec=exception-name
-        -fixed
-        -F, -free
+        -fformat
+        --fixed
+        -F, --free
         -fimplicit-init
         -h, --help
         -save-temps=
@@ -110,10 +119,10 @@ $0 recognizes the following GnuCOBOL cobc compilation options:
         -std=mf
 Options that are the same in gcobol and cobc are passed through verbatim.
 Options that have no analog in gcobol produce a warning message.
-To produce this message, use -HELP.  
+To produce this message, use -HELP.
 To see the constructed cobc command-line, use -echo.
 To override the default cobc, set the "cobc" environment variable.
-By default, gcobc invokes the gcobol the same directory the gcobc resides.  
+By default, gcobc invokes the gcobol the same directory the gcobc resides.
 To override, set the gcobol environment variable.
 EOF
 }
@@ -137,7 +146,7 @@ do
         pending_arg=
         continue
     fi
-    
+
     case $opt in
         -A | -Q) warn "$opt"
                  ;;
@@ -147,7 +156,7 @@ do
             ;;
         --conf=*) warn "$opt"
                   ;;
-        -C) error "$opt $incomparable" 
+        -C) error "$opt $incomparable"
             ;;
         -d | --debug) opts="$opts -fcobol-exceptions=EC-ALL"
                       warn "$opt implies -fstack-check:"
@@ -165,7 +174,7 @@ do
         -ext)
             pending_arg=$opt
             ;;
-        -ext=*) opts="$opts $(echo $opt | sed 's/-ext=/-copyext ./')"
+        -ext=*) opts="$opts $(echo "$opt" | sed 's/-ext=/-copyext ./')"
                 ;;
 
         # A.3 Compiler options
@@ -337,28 +346,39 @@ do
         -fassign-disk-from=*) warn "$opt" ;;
         -fvsam-status=*) warn "$opt" ;;
         -fself-call-recursive=*) warn "$opt" ;;
+
+        # TODO: create a temporary COBOL file with COBOL-WORDS directives
+        # and force-include it
         -fnot-reserved=*) warn "$opt" ;;
         -freserved=*) warn "$opt" ;;
         -fnot-register=*) warn "$opt" ;;
         -fregister=*) warn "$opt" ;;
 
-        -fixed)     opts="$opts -ffixed-form"
+        -fformat=auto ) ;; # gcobol and gnucobol default
+
+        -fixed | --fixed | -fformat=fixed | -fformat=variable | -fformat=xcard)
+                    # note: variable + xcard are only _more similar_ to fixed than free,
+                    # (with changing right-column to 250/255, which isn't supported in gcobol, yet)
+                    opts="$opts -ffixed-form"
                     ;;
-        -F | -free) opts="$opts -ffree-form"
+
+        -F | -free | --free | -fformat=free | -fformat=* )
+                    # note: "all other formats" are only _more similar_ to free than fixed
+                    opts="$opts -ffree-form"
                     ;;
-        # -g
+
 	-h | --help) opts="$opts --help"
                      ;;
-       
+
         -HELP) help && exit
                ;;
         -i | --info) warn "$opt"
                      ;;
-        
+
 	# -I
         -fimplicit-init) warn "$opt"
                          ;;
-        -j | -job)  warn "$opt" 
+        -j | -job)  warn "$opt"
                     ;;
         -K) ignore_arg "$opt"
             ;;
@@ -371,14 +391,14 @@ do
 	-m) mode="-shared"
             ;;
         # -main
-        # -nomain  
+        # -nomain
 	# -o
         # -O0, -Ox
         -O | -O2 | -Os) warn "$opt"
                         ;;
 	-S) mode="$opt"
             ;;
-        -save-temps=*) opt="$(echo $opt | sed -E 's/^.+=//')"
+        -save-temps=*) opt="$(echo "$opt" | sed -E 's/^.+=//')"
                        export GCOBOL_TEMPDIR="$opt"
                        ;;
         -save-temps)  export GCOBOL_TEMPDIR="${PWD:-$(pwd)}"
@@ -387,7 +407,7 @@ do
 
         -std=mvs) opts="$opts -dialect ibm"
                   ;;
-        -std=mf)  opts="$opts -dialect mf" 
+        -std=mf)  opts="$opts -dialect mf"
                   ;;
         -t | -T | -tlines=* | -P | -P=* | -X | --Xref)
             warn "$opt (no listing)"
@@ -396,16 +416,19 @@ do
                       ;;
         -v | --verbose) opts="$opts -V"
                         ;;
-        -V | --version | --dumpversion) opts="$opts --version"
+        # note: we want -dumpversion to be passed to gcc
+        -V | --version | -version) opts="$opts --version"
                                         ;;
 
-        -w) opts="$opts $opt"
-            ;;        
+        # pass through, strangely -Wall is not supported
+        -w | -W | -Wextra) opts="$opts $opt"
+             ;;
         -Wno-*) no_warn "$opt"
              ;;
+
         -W*) ignore_arg "$opt"
              ;;
-        
+
         -x) mode=
             ;;
 
@@ -440,4 +463,3 @@ then
 fi
 
 exec $gcobol $mode $opts
-
-- 
GitLab