soimort / awkunit

Unit Testing Framework for GNU Awk
GNU General Public License v3.0
10 stars 3 forks source link

Unable to compile with gawk-5.0.1 with Ubuntu 20.04 !! FIX @ comment #5 #1

Open brewmanz opened 2 years ago

brewmanz commented 2 years ago

When trying to compile, I get:

xxx:~/git/awkunit$ AWKSRCPATH=~/gawk-src/gawk-5.0.1 make
gcc -c -Wall -fPIC -shared awkunit.c -I/home/xxx/gawk-src/gawk-5.0.1/ -I/home/xxx/gawk-src/gawk-5.0.1/extension
awkunit.c:89:19: warning: initialization of ‘awk_value_t * (*)(int,  awk_value_t *, struct awk_ext_func *)’ {aka ‘struct awk_value * (*)(int,  struct awk_value *, struct awk_ext_func *)’} from incompatible pointer type ‘awk_value_t * (*)(int,  awk_value_t *)’ {aka ‘struct awk_value * (*)(int,  struct awk_value *)’} [-Wincompatible-pointer-types]
   89 |      {"assertIO", do_assertIO, 3},
      |                   ^~~~~~~~~~~
awkunit.c:89:19: note: (near initialization for ‘func_table[0].function’)
gcc -o awkunit.so -shared awkunit.o
rm awkunit.o

The actve folder:

xxx:~/git/awkunit$ ls -ltr
total 76
-rw-rw-r-- 1 xxx xxx   492 Aug 11 12:20 awkunit.awk
-rw-rw-r-- 1 xxx xxx   488 Aug 11 12:20 README.md
-rw-rw-r-- 1 xxx xxx   470 Aug 11 12:20 Makefile
-rw-rw-r-- 1 xxx xxx  2515 Aug 11 12:20 awkunit.c
-rw-rw-r-- 1 xxx xxx 35192 Aug 11 12:20 LICENSE
drwxrwxr-x 2 xxx xxx  4096 Aug 11 12:20 examples
-rwxrwxr-x 1 xxx xxx 17400 Aug 11 13:15 awkunit.so

The gawk source folder:

xxx:~/git/awkunit$ ls ~/gawk-src/gawk-5.0.1
ABOUT-NLS   builtin.c     compile       custom.h   field.c       install-sh   mbsupport.h    NEWS.1      protos.h     test
aclocal.m4  ChangeLog     config.guess  debug.c    floatcomp.c   int_array.c  missing        node.c      README       TODO
array.c     ChangeLog.0   configh.in    depcomp    floatmagic.h  interpret.h  missing_d      nonposix.h  README_d     version.c
AUTHORS     ChangeLog.1   config.rpath  doc        gawkapi.c     io.c         mkinstalldirs  pc          re.c         vms
awkgram.c   cint_array.c  config.sub    eval.c     gawkapi.h     m4           mpfr.c         po          replace.c    ylwrap
awkgram.y   cmd.h         configure     ext.c      gawkmisc.c    main.c       msg.c          posix       str_array.c
awk.h       command.c     configure.ac  extension  gettext.h     Makefile.am  NEWS           POSIX.STD   support
awklib      command.y     COPYING       extras     INSTALL       Makefile.in  NEWS.0         profile.c   symbol.c
brewmanz commented 2 years ago

Looking further, it seems to have actually compiled, but maybe not installed properly. Makefile contains these lines:

PREFIX=/usr
LIBDIR=$(PREFIX)/lib/gawk
AWKDIR=$(PREFIX)/share/awk

Now, folder /usr/share/awk exists, and has an entry /usr/share/awk/awkunit.awk in it (created today). However, folder /usr/lib/gawk does NOT exist, but file /usr/lib/gawk DOES exist (17.4K), created today.

My suspicion is that file awkunit.so has been copied, not into (non existent) folder /usr/lib/gawk, but as a file of the same name.

brewmanz commented 2 years ago

I have tried various options, but am working at (beyond?) my limit of understanding. Still not working. I am getting:

~/git/awkunit/examples$ locate awkunit.so
/home/xxx/git/awkunit/awkunit.so
/usr/lib/awkunit.so

~/git/awkunit/examples$ ./test_asort.awk 
awk: In file included from ./test_asort.awk:2:
awk: awkunit:2: error: can't open shared library `awkunit.so' for reading (No such file or directory)

~/git/awkunit/examples$ cd ..

~/git/awkunit$ ./examples/test_asort.awk 
awk: In file included from ./examples/test_asort.awk:2:
awk: awkunit:2: error: can't open shared library `awkunit.so' for reading (No such file or directory)
brewmanz commented 2 years ago

I have checked various changes I've made, and now backed out my awkunit.awk modification from

@load "awkunit.so"

back to original

@load "awkunit"

so now error is

awk: awkunit:2: error: can't open shared library `awkunit' for reading (No such file or directory)
brewmanz commented 2 years ago

OK. This is what worked for me; YMMV:

diff --git a/Makefile b/Makefile
index 784e513..6eef6ab 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 PREFIX=/usr
-LIBDIR=$(PREFIX)/lib/gawk
+LIBDIR=$(PREFIX)/lib/x86_64-linux-gnu/gawk
 AWKDIR=$(PREFIX)/share/awk

 CC=gcc
diff --git a/awkunit.c b/awkunit.c
index be0004b..941dec2 100644
--- a/awkunit.c
+++ b/awkunit.c
@@ -30,20 +30,22 @@

 static awk_bool_t (*init_func)(void) = NULL;

-static awk_value_t *do_assertIO(int nargs, awk_value_t *result)
+#define DO_ASSERTIO_MAX_ARGS 3
+#define DO_ASSERTIO_MIN_ARGS 3
+static awk_value_t *do_assertIO(int nargs, awk_value_t *result, awk_ext_func_t *ext_func)
 {
      awk_value_t scriptFile, inFile, outFile;
      int ret = -1;
      FILE *fpipe, *fo;
      char *command = NULL, pbuf[BUFFER_SIZE], obuf[BUFFER_SIZE];
-     
+
      assert(result != NULL);
-     
+
      if (do_lint && nargs != 3)
           lintwarn(ext_id,
-                   _("assertIO: called with incorrect number of arguments, "
+                   _("awkunit::assertIO: called with incorrect number of arguments, "
                      "expecting 3"));
-     
+
      if (get_argument(0, AWK_STRING, &scriptFile) &&
          get_argument(1, AWK_STRING, &inFile) &&
          get_argument(2, AWK_STRING, &outFile)) {
@@ -58,7 +60,7 @@
      strcat(command, scriptFile.str_value.str);
      strcat(command, " < ");
      strcat(command, inFile.str_value.str);
-     
+
      if (!(fpipe = (FILE *)popen(command, "r"))) {
           perror("Fatal error: cannot open pipe");
           exit(-1);
@@ -67,7 +69,7 @@
           perror("Fatal error: cannot open file");
           exit(-1);
      }
-     
+
      while (fgets(pbuf, BUFFER_SIZE, fpipe)) {
           fgets(obuf, BUFFER_SIZE, fo);
           if (strcmp(pbuf, obuf) != 0) {
@@ -79,14 +81,14 @@
                exit(-1);
           }
      }
-     
+
      pclose(fpipe);
      fclose(fo);
      return make_number(ret, result);
 }

 static awk_ext_func_t func_table[] = {
-     {"assertIO", do_assertIO, 3},
+     {"assertIO", do_assertIO, DO_ASSERTIO_MAX_ARGS, DO_ASSERTIO_MIN_ARGS},
 };

-dl_load_func(func_table, some_name, "name_space_in_quotes")
+dl_load_func(func_table, some_name, "awkunit")
diff --git a/examples/test_asort.awk b/examples/test_asort.awk
index 2bc4321..cb10a31 100755
--- a/examples/test_asort.awk
+++ b/examples/test_asort.awk
@@ -3,7 +3,7 @@
 @include "asort"

 function testIO() {
-    assertIO("asort.awk", "/dev/stdin", "asort.ok")
+    awkunit::assertIO("asort.awk", "/dev/stdin", "asort.ok")
 }

 BEGIN {
diff --git a/examples/test_sum.awk b/examples/test_sum.awk
index 37d10f2..79d05b3 100755
--- a/examples/test_sum.awk
+++ b/examples/test_sum.awk
@@ -10,7 +10,7 @@
 }

 function testIO() {
-    assertIO("sum.awk", "sum.in", "sum.ok")
+    awkunit::assertIO("sum.awk", "sum.in", "sum.ok")
 }

 BEGIN {