From 461158d4a54871fb2652d0831ebf4982b18fb4d9 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Mon, 3 Oct 2022 11:04:39 -0500 Subject: [PATCH 01/10] First commit for building ncargraphics libraries on Apple M1 under Monterey. (Has not yet tested compiling ncl) --- README.apple_m1 | 9 ++++ common/src/libncarg_c/yMakefile | 9 ++++ config/Darwin_Arm | 74 ++++++++++++++++++++++++++ config/ymake | 6 +++ ncarg2d/src/libncarg_gks/bwi/argb2ci.f | 16 +++--- 5 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 README.apple_m1 create mode 100644 config/Darwin_Arm diff --git a/README.apple_m1 b/README.apple_m1 new file mode 100644 index 0000000000..00d3551b3c --- /dev/null +++ b/README.apple_m1 @@ -0,0 +1,9 @@ +I have netcdf installed separately and other ncarg dependencies were installed with homebrew. GCC version 12 was installed with homebrew, as well. That version (gcc-12) is wired into the Darwin_Arm configure file and probably should be made generic. + +For Configure, my library and include settings are: + +/opt/X11/lib /Users/Shared/opt/local/netcdf480ser/lib /Users/Shared/homebrew/lib + +/opt/X11/include /opt/X11/include/freetype2 /Users/Shared/opt/local/netcdf480ser/include /Users/Shared/homebrew/include + + diff --git a/common/src/libncarg_c/yMakefile b/common/src/libncarg_c/yMakefile index e1e613988d..5057a591be 100644 --- a/common/src/libncarg_c/yMakefile +++ b/common/src/libncarg_c/yMakefile @@ -42,10 +42,19 @@ EXCSRCS = bcopyswap.c logic32.c EXFSRCS = gbytes.f sbytes.f EXOBJS = sbytes.o gbytes.o bcopyswap.o logic32.o +#elif defined(Darwin) && defined(arm64) && defined(__LP64__) +EXCSRCS = bcopyswap.c logic32.c +EXFSRCS = gbytes.f sbytes.f +EXOBJS = sbytes.o gbytes.o bcopyswap.o logic32.o + #elif defined(Darwin) && defined(i386) && !defined(__LP64__) EXCSRCS = bcopyswap.c logic32.c gsbytes.c EXOBJS = bcopyswap.o logic32.o gsbytes.o +#elif defined(Darwin) && defined(arm64) && !defined(__LP64__) +EXCSRCS = bcopyswap.c logic32.c gsbytes.c +EXOBJS = bcopyswap.o logic32.o gsbytes.o + #elif defined(Darwin) && !defined(i386) EXCSRCS = gsbytes.c logic32.c EXOBJS = gsbytes.o logic32.o diff --git a/config/Darwin_Arm b/config/Darwin_Arm new file mode 100644 index 0000000000..20f5110922 --- /dev/null +++ b/config/Darwin_Arm @@ -0,0 +1,74 @@ +/* + * Description: This file contains the configuration for a + * gfortran/gcc build on a *32 bit* Intel Mac system. + * + * If you have Mac OS 10.6 or later, you may have + * a 64-bit system, and hence you should copy + * Darwin_Intel.64 over this file. + * + * You may need to modify it to change or include + * options. + * + * If you want to use a different Fortran compiler + * like g95 or ifort, see the files + * "Darwin_Intel.g95" or "Darwin_Intel.ifort" in + * this directory for help in modifying this file. + * + * Note: if the build has trouble finding the + * "gfortran" library, then you may need to modify + * the "CtoFLibraries" line below and include a "-L" + * path to help it. For example: + * + * #define CtoFLibraries -L/usr/local/lib -lgfortran -lquadmath + * or + * #define CtoFLibraries /usr/local/lib/libgfortran.a /usr/local/lib/libquadmath.a + * + * You'll need to change "/usr/local/lib" to + * whatever directory contains "libgfortran.so". + * + * The references to the "User" macros below is for building the + * ncargcc, ncargf90, nhlcc, etc, scripts. We don't want the + * development stuff (like the CtoFLibraries) jammed into those + * scripts, like "/usr/local/lib/libcairo.a"; instead, we want + * clean paths like "-lcairo". + * #define CcOptions -ansi -fPIC -Wall -std=c99 + * #define CtoFLibraries -L/Users/Shared/homebrew/Cellar/gcc/12.2.0/lib/gcc/current -lgfortran -lquadmath + * #define CtoFLibrariesUser -L/Users/Shared/homebrew/Cellar/gcc/12.2.0/lib/gcc/current -lgfortran -lquadmath + */ +#define HdfDefines -DDARWIN +#define StdDefines -DSYSV -D_POSIX_SOURCE -D_XOPEN_SOURCE -DByteSwapped -D__UNIXOS2__ -D_DARWIN_C_SOURCE +#define ByteSwapped +#define Cstatic +#define Cdynamic +#define CppCommand '/usr/bin/cpp -traditional' +#define CCompiler gcc-12 +#define CxxCompiler g\+\+ +#define FCompiler gfortran +#define CcOptions -ansi -fPIC -Wall -Wno-error=implicit-function-declaration +#define FcOptions -fPIC -fno-range-check -Wall -fallow-argument-mismatch -fallow-invalid-boz +#define CtoFLibraries -lgfortran -lquadmath +#define CtoFLibrariesUser -lgfortran -lquadmath +#define XToolLibrary -lXt -lSM -lICE +#define BuildShared NO +#define XLibrary -lXpm -lX11 -lXext + +#define LibSearchUser -L/opt/X11/lib +#define IncSearchUser -I/opt/X11/include -I/opt/X11/include/freetype2 +#define ExtraIncSearch -I/opt/X11/include/freetype2 + +#define ArchRecLibSearch -L/opt/X11/lib +#define ArchRecIncSearch -I/opt/X11/include -I/opt/X11/include/freetype2 + +FC = $(F77) + +/*************** Redefine Macros from Rules ********************************/ + +/* + * Macro: MakeDir + * + * Description: This rule creates a directory - if a parent dir doesn't exist + * it attempts to create it. + */ +#ifndef MakeDir +#define MakeDir(dir) @if (test ! -d dir); then ($(MKDIRHIER) dir); fi +#endif diff --git a/config/ymake b/config/ymake index 6d5be970ee..91ab6a7c81 100755 --- a/config/ymake +++ b/config/ymake @@ -422,6 +422,12 @@ case Darwin: set sysincs = Darwin_Intel set vendor = Apple breaksw + case arm64: + set model = $mach + set arch = $mach + set sysincs = Darwin_Arm + set vendor = Apple + breaksw default: echo "$0 : Unknown machine type" > /dev/tty exit 1 diff --git a/ncarg2d/src/libncarg_gks/bwi/argb2ci.f b/ncarg2d/src/libncarg_gks/bwi/argb2ci.f index 1fc3946716..48673fd548 100644 --- a/ncarg2d/src/libncarg_gks/bwi/argb2ci.f +++ b/ncarg2d/src/libncarg_gks/bwi/argb2ci.f @@ -16,10 +16,14 @@ integer function argb2ci(index) integer index, nearest integer i integer ARGBMASK, RMASK, GMASK, BMASK - parameter (ARGBMASK = Z'40000000') - parameter (RMASK = Z'00FF0000') - parameter (GMASK = Z'0000FF00') - parameter (BMASK = Z'000000FF') + parameter (ARGBMASK = int(Z'40000000')) + parameter (RMASK = int(Z'00FF0000')) + parameter (GMASK = int(Z'0000FF00')) + parameter (BMASK = int(Z'000000FF')) +! parameter (ARGBMASK = Z'40000000') +! parameter (RMASK = Z'00FF0000') +! parameter (GMASK = Z'0000FF00') +! parameter (BMASK = Z'000000FF') real r, g, b, dist, mindist if (iand(index, ARGBMASK).eq.0) then @@ -31,8 +35,8 @@ integer function argb2ci(index) mindist = 2e31 nearest = 0 do i=1,mol - r = (iand(index, RMASK) / Z'0000FFFF') / 255. - g = (iand(index, GMASK) / Z'000000FF') / 255. + r = (iand(index, RMASK) / int(Z'0000FFFF')) / 255. + g = (iand(index, GMASK) / int(Z'000000FF')) / 255. b = (iand(index, BMASK)) / 255. C we don't need absolute distance, so forego the sqrt operation... From 00d926ec3e62bc6243713fb223c0da867ba323e3 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Mon, 3 Oct 2022 14:15:43 -0500 Subject: [PATCH 02/10] Some more little fixes. --- README.apple_m1 | 4 ++++ config/Darwin_Arm | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.apple_m1 b/README.apple_m1 index 00d3551b3c..dddc7d0c59 100644 --- a/README.apple_m1 +++ b/README.apple_m1 @@ -6,4 +6,8 @@ For Configure, my library and include settings are: /opt/X11/include /opt/X11/include/freetype2 /Users/Shared/opt/local/netcdf480ser/include /Users/Shared/homebrew/include +Make sure to install proj at version 7 in homebrew: + brew install proj@7 +And if proj v. 9 is install, then unlink that with 'brew unlink proj' and then link the v7 with 'brew link proj@7' +Also need to set g++ at as g\+\+-12 so that ncl linking finds the gfortran library. diff --git a/config/Darwin_Arm b/config/Darwin_Arm index 20f5110922..3307dcc839 100644 --- a/config/Darwin_Arm +++ b/config/Darwin_Arm @@ -42,9 +42,9 @@ #define Cdynamic #define CppCommand '/usr/bin/cpp -traditional' #define CCompiler gcc-12 -#define CxxCompiler g\+\+ +#define CxxCompiler g\+\+-12 #define FCompiler gfortran -#define CcOptions -ansi -fPIC -Wall -Wno-error=implicit-function-declaration +#define CcOptions -ansi -fPIC -Wall -std=c99 -Wno-error=implicit-function-declaration #define FcOptions -fPIC -fno-range-check -Wall -fallow-argument-mismatch -fallow-invalid-boz #define CtoFLibraries -lgfortran -lquadmath #define CtoFLibrariesUser -lgfortran -lquadmath @@ -59,6 +59,8 @@ #define ArchRecLibSearch -L/opt/X11/lib #define ArchRecIncSearch -I/opt/X11/include -I/opt/X11/include/freetype2 +#define LexLibrary -ll + FC = $(F77) /*************** Redefine Macros from Rules ********************************/ From a95986b5a881e305f924f3135b8eefd365f5dbfb Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Fri, 7 Oct 2022 15:48:37 -0500 Subject: [PATCH 03/10] Updated description text --- config/Darwin_Arm | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/config/Darwin_Arm b/config/Darwin_Arm index 3307dcc839..a193fd4468 100644 --- a/config/Darwin_Arm +++ b/config/Darwin_Arm @@ -1,18 +1,10 @@ /* * Description: This file contains the configuration for a - * gfortran/gcc build on a *32 bit* Intel Mac system. - * - * If you have Mac OS 10.6 or later, you may have - * a 64-bit system, and hence you should copy - * Darwin_Intel.64 over this file. + * gfortran/gcc build on a *64 bit* ARM (Apple Silicon) Mac system. * * You may need to modify it to change or include - * options. - * - * If you want to use a different Fortran compiler - * like g95 or ifort, see the files - * "Darwin_Intel.g95" or "Darwin_Intel.ifort" in - * this directory for help in modifying this file. + * options. This version was tested with GCC-12.2 + * built with Homebrew and XCode 13.4.1 on OS 12.6 (Monterey) * * Note: if the build has trouble finding the * "gfortran" library, then you may need to modify @@ -31,7 +23,6 @@ * development stuff (like the CtoFLibraries) jammed into those * scripts, like "/usr/local/lib/libcairo.a"; instead, we want * clean paths like "-lcairo". - * #define CcOptions -ansi -fPIC -Wall -std=c99 * #define CtoFLibraries -L/Users/Shared/homebrew/Cellar/gcc/12.2.0/lib/gcc/current -lgfortran -lquadmath * #define CtoFLibrariesUser -L/Users/Shared/homebrew/Cellar/gcc/12.2.0/lib/gcc/current -lgfortran -lquadmath */ From c7fd6860a0f0be0900a39f75d751f9b356421501 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Sun, 16 Oct 2022 22:35:22 -0500 Subject: [PATCH 04/10] Temporary fix to prevent out of bounds write when cpnumb is called from Format.c --- ncarg2d/src/libncarg/conpack/CodeIftran | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ncarg2d/src/libncarg/conpack/CodeIftran b/ncarg2d/src/libncarg/conpack/CodeIftran index 849dd57e66..1e1345bfe4 100644 --- a/ncarg2d/src/libncarg/conpack/CodeIftran +++ b/ncarg2d/src/libncarg/conpack/CodeIftran @@ -9666,7 +9666,10 @@ C C Find the length of the character buffer and initialize it to blanks. C LBUF=LEN(CBUF) - CBUF=' ' +C hack: Check if lbuf is "large" and, if it is, assume calling from C +C and length is bad and should be 128 + IF ( LBUF > 512 ) LBUF = 128 + CBUF(1:lbuf)=' ' C C Use the local I/O routines to generate an E-format representation of C the number. @@ -9788,7 +9791,7 @@ C which requires special action). C IF (NDGS.EQ.0.OR.IEXF.EQ.0) IF (IEXP.GT.0.OR.LMSD.EQ.-10000) - CBUF='0' + CBUF(1:LBUF)='0' NBUF=1 NDGS=1 IEVA=0 From 15671079addd94fcee8105c01099e3cafa51234b Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Mon, 6 Mar 2023 10:27:51 -0600 Subject: [PATCH 05/10] Added preprocessing flag to Format.c to declare string length variables as 'long int' for arm64. ARCH_DEF added to compile options in the appropriate yMakefile --- ni/src/lib/hlu/Format.c | 8 ++++++++ ni/src/lib/hlu/yMakefile | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ni/src/lib/hlu/Format.c b/ni/src/lib/hlu/Format.c index 506c0689e5..b6c06c7b6b 100644 --- a/ni/src/lib/hlu/Format.c +++ b/ni/src/lib/hlu/Format.c @@ -940,7 +940,11 @@ NhlString _NhlFormatFloat NGCALLF(cpinrc,CPINRC)(); { +#ifdef arm64 + long int len1,len2,len3,len4; +#else int len1,len2,len3,len4; +#endif NGstring cex1_f; NGstring cex2_f; NGstring cex3_f; @@ -1283,7 +1287,11 @@ NhlErrorTypes _NhlGetScaleInfo NGCALLF(cpinrc,CPINRC)(); { +#ifdef arm64 + long int len1,len2,len3,len4; +#else int len1,len2,len3,len4; +#endif NGstring cex1_f; NGstring cex2_f; NGstring cex3_f; diff --git a/ni/src/lib/hlu/yMakefile b/ni/src/lib/hlu/yMakefile index 2ca14e635f..15f3eb7fb4 100644 --- a/ni/src/lib/hlu/yMakefile +++ b/ni/src/lib/hlu/yMakefile @@ -48,7 +48,7 @@ Qt_DEFINES = -DBuildQtEnabled #define Qt_DEFINES #endif -EXTRA_CDEFINES = $(OS_DEF) $(TRIANGLE_DEF) $(PNG_DEF) $(Qt_DEFINES) +EXTRA_CDEFINES = $(OS_DEF) $(TRIANGLE_DEF) $(PNG_DEF) $(Qt_DEFINES) $(ARCH_DEF) HDRS1 = Base.h BaseI.h BaseP.h Convert.h ConvertP.h ConvertersP.h\ CoordApprox.h Error.h ErrorI.h ErrorP.h IrregularTransObj.h \ From 6a092e4d5f8cb792499871ee5d1855cfa1b02a1c Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Mon, 6 Mar 2023 11:20:31 -0600 Subject: [PATCH 06/10] Remove hack from cpnumb subroutine since the string length problem is fixed on the C side. --- ncarg2d/src/libncarg/conpack/CodeIftran | 3 --- 1 file changed, 3 deletions(-) diff --git a/ncarg2d/src/libncarg/conpack/CodeIftran b/ncarg2d/src/libncarg/conpack/CodeIftran index 1e1345bfe4..ac0b8353f5 100644 --- a/ncarg2d/src/libncarg/conpack/CodeIftran +++ b/ncarg2d/src/libncarg/conpack/CodeIftran @@ -9666,9 +9666,6 @@ C C Find the length of the character buffer and initialize it to blanks. C LBUF=LEN(CBUF) -C hack: Check if lbuf is "large" and, if it is, assume calling from C -C and length is bad and should be 128 - IF ( LBUF > 512 ) LBUF = 128 CBUF(1:lbuf)=' ' C C Use the local I/O routines to generate an E-format representation of From a216ba51dcccc6856a7b60f7c38ee92330291445 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Fri, 10 Mar 2023 19:41:14 -0600 Subject: [PATCH 07/10] Removed check for arm64 and simply declare len1 etc. as 'long' for passing string length to fortran. --- ni/src/lib/hlu/Format.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/ni/src/lib/hlu/Format.c b/ni/src/lib/hlu/Format.c index b6c06c7b6b..23f4b3b55f 100644 --- a/ni/src/lib/hlu/Format.c +++ b/ni/src/lib/hlu/Format.c @@ -940,11 +940,7 @@ NhlString _NhlFormatFloat NGCALLF(cpinrc,CPINRC)(); { -#ifdef arm64 - long int len1,len2,len3,len4; -#else - int len1,len2,len3,len4; -#endif + long len1,len2,len3,len4; NGstring cex1_f; NGstring cex2_f; NGstring cex3_f; @@ -1287,11 +1283,7 @@ NhlErrorTypes _NhlGetScaleInfo NGCALLF(cpinrc,CPINRC)(); { -#ifdef arm64 - long int len1,len2,len3,len4; -#else - int len1,len2,len3,len4; -#endif + long len1,len2,len3,len4; NGstring cex1_f; NGstring cex2_f; NGstring cex3_f; From 6089cf287773ca7a9873531b564555a419c00b99 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Fri, 10 Mar 2023 19:46:18 -0600 Subject: [PATCH 08/10] undid previous change as it is no longer needed --- ni/src/lib/hlu/yMakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ni/src/lib/hlu/yMakefile b/ni/src/lib/hlu/yMakefile index 15f3eb7fb4..2ca14e635f 100644 --- a/ni/src/lib/hlu/yMakefile +++ b/ni/src/lib/hlu/yMakefile @@ -48,7 +48,7 @@ Qt_DEFINES = -DBuildQtEnabled #define Qt_DEFINES #endif -EXTRA_CDEFINES = $(OS_DEF) $(TRIANGLE_DEF) $(PNG_DEF) $(Qt_DEFINES) $(ARCH_DEF) +EXTRA_CDEFINES = $(OS_DEF) $(TRIANGLE_DEF) $(PNG_DEF) $(Qt_DEFINES) HDRS1 = Base.h BaseI.h BaseP.h Convert.h ConvertP.h ConvertersP.h\ CoordApprox.h Error.h ErrorI.h ErrorP.h IrregularTransObj.h \ From d81f82d2b2f46126c03f2a4d83fb929722c5c131 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Sun, 3 Sep 2023 17:56:25 -0500 Subject: [PATCH 09/10] Add -O to compile options --- config/Darwin_Arm | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config/Darwin_Arm b/config/Darwin_Arm index a193fd4468..7f97028f78 100644 --- a/config/Darwin_Arm +++ b/config/Darwin_Arm @@ -26,6 +26,7 @@ * #define CtoFLibraries -L/Users/Shared/homebrew/Cellar/gcc/12.2.0/lib/gcc/current -lgfortran -lquadmath * #define CtoFLibrariesUser -L/Users/Shared/homebrew/Cellar/gcc/12.2.0/lib/gcc/current -lgfortran -lquadmath */ +#define DEBUG 1 #define HdfDefines -DDARWIN #define StdDefines -DSYSV -D_POSIX_SOURCE -D_XOPEN_SOURCE -DByteSwapped -D__UNIXOS2__ -D_DARWIN_C_SOURCE #define ByteSwapped @@ -34,9 +35,9 @@ #define CppCommand '/usr/bin/cpp -traditional' #define CCompiler gcc-12 #define CxxCompiler g\+\+-12 -#define FCompiler gfortran -#define CcOptions -ansi -fPIC -Wall -std=c99 -Wno-error=implicit-function-declaration -#define FcOptions -fPIC -fno-range-check -Wall -fallow-argument-mismatch -fallow-invalid-boz +#define FCompiler gfortran-12 +#define CcOptions -ansi -fPIC -Wall -std=c99 -Wno-error=implicit-function-declaration -O +#define FcOptions -fPIC -fno-range-check -Wall -fallow-argument-mismatch -fallow-invalid-boz -O #define CtoFLibraries -lgfortran -lquadmath #define CtoFLibrariesUser -lgfortran -lquadmath #define XToolLibrary -lXt -lSM -lICE From c4486cc08b4be583fc89aaacbbc55a3640aecb64 Mon Sep 17 00:00:00 2001 From: Ted Mansell Date: Sun, 3 Sep 2023 18:58:08 -0500 Subject: [PATCH 10/10] Removed wrf_constants.mod from object list because it doesn't need to go into a library --- ni/src/lib/nfpfort/yMakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ni/src/lib/nfpfort/yMakefile b/ni/src/lib/nfpfort/yMakefile index e421d3e959..0381d9ec8f 100644 --- a/ni/src/lib/nfpfort/yMakefile +++ b/ni/src/lib/nfpfort/yMakefile @@ -41,7 +41,7 @@ FOBJS = aam.o areaAve.o areaAve2.o areaRmse.o areaRmse2.o areaSum2.o \ det_code42.o kmeans_kmns_as136.o spi3.o wrf_vinterp.o wrf_fctt.o \ wrf_write_wps.o pres_hybrid_jra55_dp.o relhum_ice.o relhum_water.o \ wetbulb_profs.o wrf_cloud_fracf.o mlegev_memory.o kernel_density.o \ - meemd.o dpsort_large.o wrf_pw.o wrf_wind.o wrf_constants.o wrf_constants.mod + meemd.o dpsort_large.o wrf_pw.o wrf_wind.o wrf_constants.o COBJS =