MESH-Model / MESH-Dev

This repository contains the official MESH development code, which is the basis for the 'tags' listed under the MESH-Releases repository. The same tags are listed under this repository. Legacy branches and utilities have also been ported from the former SVN (Subversion) repository. Future developments must create 'forks' from this repository.
Other
1 stars 3 forks source link

`>gcc-9.4.0` throws function declaration error with the C codes on `arm64` #43

Closed kasra-keshavarz closed 6 months ago

kasra-keshavarz commented 9 months ago

Problem statement

I'm not quite sure about the source code of MESH, as I am not completely aware of all the ins and outs, but while trying to provide @roalva82 with an executable on an Ubuntu VM, I noticed >gcc-9.4.0 (could be earlier versions too) throws an error for the involved C codes in MESH. Are we missing a flag or something?

Here is the typical error:

ubuntu@focal-fossa:~/MESH-Dev$ make gfortran netcdf
gcc -c -O2   -I./LSS_Model/SVS/svs1/changes_to_rpnphy -I./LSS_Model/SVS/svs1/rpnphy_5.8.3_all -I./LSS_Model/SVS/svs1/svs_GEM_v5.8.rc2 -I./Modules/librmn/19.7.0/CUSTOM_INCLUDES/ ./Modules/librmn/19.7.0/primitives/getenvc.c
./Modules/librmn/19.7.0/primitives/getenvc.c:34:1: warning: parameter names (without types) in function declaration
   34 | F2Cl  len1, len2;
      | ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:33:1: error: ‘f77name’ declared as function returning a function
   33 | f77name(getenvc) ( name, value, len1, len2 )
      | ^~~~~~~
./Modules/librmn/19.7.0/primitives/getenvc.c: In function ‘f77name’:
./Modules/librmn/19.7.0/primitives/getenvc.c:33:1: warning: type of ‘getenvc’ defaults to ‘int’ [-Wimplicit-int]
./Modules/librmn/19.7.0/primitives/getenvc.c:35:15: error: declaration for parameter ‘value’ but no such parameter
   35 | char name[1], value[1];
      |               ^~~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:35:6: error: declaration for parameter ‘name’ but no such parameter
   35 | char name[1], value[1];
      |      ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:34:13: error: declaration for parameter ‘len2’ but no such parameter
   34 | F2Cl  len1, len2;
      |             ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:34:7: error: declaration for parameter ‘len1’ but no such parameter
   34 | F2Cl  len1, len2;
      |       ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:69:16: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
   69 |         size = strlen( hold ) ;
      |                ^~~~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:69:16: warning: incompatible implicit declaration of built-in function ‘strlen’
./Modules/librmn/19.7.0/primitives/getenvc.c:25:1: note: include ‘<string.h>’ or provide a declaration of ‘strlen’
   24 | #include <rpnmacros.h>
  +++ |+#include <string.h>
   25 | /*
make: *** [makefile:201: getenvc.o] Error 1

Suggested fix

It apparently relates to the SVS source code? I thought a quick fix could be like the following:

/* RMNLIB - Library of useful routines for C and FORTRAN programming
 * Copyright (C) 1975-2001  Division de Recherche en Prevision Numerique
 *                          Environnement Canada
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation,
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#if defined(NEC)
#endif

#include <stdlib.h>
#include <rpnmacros.h>
#include <string.h>

void getenvc_(char *name, char *value, F2Cl *len1, F2Cl *len2)
{
   wordint i;
   unsigned size;
   char *temp, *hold;

   /* Transfert de name dans une chaine C */
   size = *len1 + *len2 + 1;
   temp = (char *)malloc(size);

   for (i = 0; i < *len1 && name[i] != ' '; i++)
      temp[i] = name[i];

   temp[i] = '\0';

   /* Appel a la fonction C getenv */
   hold = getenv(temp);

   /*
      Si la chaine n'a pas ete trouvee, getenv retourne un
      pointeur null. value sera alors entierement vide.
      Sinon, on copie le contenu de hold dans value.
   */
   for (i = 0; i < *len2; i++)
      value[i] = ' ';

   if (hold != NULL && size != 1)
   {
      size = strlen(hold);
      for (i = 0; i < size; i++)
         value[i] = hold[i];
   }

   free(temp);
}

and, the original code is the following: https://github.com/MESH-Model/MESH-Dev/blob/ff1de25e67ef552844f1e789c0faffb4175a8dac/Modules/librmn/19.7.0/primitives/getenvc.c#L21-L75

Let me know if this is an acceptable fix, and I'll send it as a PR.

Suggestions

I know SVS is probably an upstream package, but not sure if it is under active development, so I can submit this there? Also, is there a possibility to use SVS as a shared library, rather than statically linking (if it is) it with MESH executables?

kasra-keshavarz commented 9 months ago

Forgot to mention that the VM is running on a arm64 computer (MacBook Pro M2 CPU).

dprincz commented 6 months ago

Including #include <string.h> does resolve the issue. librmn will be updated in the future, so we'll see if then they've already accounted for this. A pending update to the makefile targets will also provide more flexibility to omit inactive model codes from being compiled. This will also resolve the issue if SVS isn't required and would be disabled during compile-time outright.

Please re-open the ticket if further action is required for @roalva82's specific needs.

kasra-keshavarz commented 5 months ago

Thanks Dan, I tried to follow your suggestion:

$ make netcdf gfortran
...
gcc -c -O2   -I./LSS_Model/SVS/svs1/changes_to_rpnphy -I./LSS_Model/SVS/svs1/rpnphy_5.8.3_all -I./LSS_Model/SVS/svs1/svs_GEM_v5.8.rc2 -I./Modules/librmn/19.7.0/CUSTOM_INCLUDES/ ./Modules/librmn/19.7.0/primitives/getenvc.c
./Modules/librmn/19.7.0/primitives/getenvc.c:34:1: error: parameter names (without types) in function declaration [-Wdeclaration-missing-parameter-type]
   34 | F2Cl  len1, len2;
      | ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:33:1: error: 'f77name' declared as function returning a function
   33 | f77name(getenvc) ( name, value, len1, len2 )
      | ^~~~~~~
./Modules/librmn/19.7.0/primitives/getenvc.c: In function 'f77name':
./Modules/librmn/19.7.0/primitives/getenvc.c:33:1: error: type of 'getenvc' defaults to 'int' [-Wimplicit-int]
./Modules/librmn/19.7.0/primitives/getenvc.c:35:15: error: declaration for parameter 'value' but no such parameter
   35 | char name[1], value[1];
      |               ^~~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:35:6: error: declaration for parameter 'name' but no such parameter
   35 | char name[1], value[1];
      |      ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:34:13: error: declaration for parameter 'len2' but no such parameter
   34 | F2Cl  len1, len2;
      |             ^~~~
./Modules/librmn/19.7.0/primitives/getenvc.c:34:7: error: declaration for parameter 'len1' but no such parameter
   34 | F2Cl  len1, len2;
      |       ^~~~
make: *** [getenvc.o] Error 1
(base) kasras-mbp:MESH-Dev kasrakeshavarz$ git diff
diff --git a/Modules/librmn/19.7.0/primitives/getenvc.c b/Modules/librmn/19.7.0/primitives/getenvc.c
index bbd2dbf..d8bc783 100644
--- a/Modules/librmn/19.7.0/primitives/getenvc.c
+++ b/Modules/librmn/19.7.0/primitives/getenvc.c
@@ -17,9 +17,9 @@
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
-
 #if defined(NEC)
 #endif
+#include <string.h>
 #include <stdlib.h>
 #include <rpnmacros.h>
 /*
kasra-keshavarz commented 1 month ago

I'm still hitting this issue, and it needs a patch. Is the new file I have shared above acceptable? Let me know.

dprincz commented 1 month ago

With what version of gcc, is it still gcc9?

kasra-keshavarz commented 1 month ago

I think anything above gcc@9 (inclusive) is having this issue.

Right now, here are my specs:

$ gfortran --version
GNU Fortran (Spack GCC) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ git rev-parse --short HEAD # MESH's repository
ff1de25
dprincz commented 1 month ago

I'm aware of an issue with gcc12. We haven't moved ahead to gcc14 yet. However, I don't think this is the the same problem as with <string.h>, and from your error, it looks related to variable declarations. Are you using SVS?

kasra-keshavarz commented 1 month ago

No I'm not using SVS specifically, just trying to compile MESH with:

$ make netcdf gfortran
kasra-keshavarz commented 1 month ago

Let me test with gcc@12 as well. I know gcc@12 has this issue which I think is only isolated to c++ (not sure)?: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109359. It is resolved with @12.4.0. Let me try and report here.

dprincz commented 1 month ago

Ok, sounds good. I suggest creating a new issue for gcc12 and/or gcc14, since it's unrelated to the original problem described here with gcc9.