[m-rev.] diff: update the Mercury-Fortran interface example

Julien Fischer jfischer at opturion.com
Mon Mar 28 15:23:38 AEDT 2022


This is for the release branch too.

------------------------------------

Update the Mercury-Fortran interface example.

samples/c_interface/mercury_calls_fortran/Mmakefile:
     g77 was replaced by gfortran in GCC 4.0, use the latter in
     this example.

samples/c_interface/mercury_calls_fortran/fortran_main_int.m:
      Replace a ':- pragma import' with an equivalent foreign_proc.

      Describe the current gfortran name mangling scheme and delete
      references to g77 command line options not supported by gfortran.

samples/c_interface/mercury_calls_fortran/mercury_main.m:
      Replace tabs with spaces.

Julien.

diff --git a/samples/c_interface/mercury_calls_fortran/Mmakefile b/samples/c_interface/mercury_calls_fortran/Mmakefile
index da573df..11d28a2 100644
--- a/samples/c_interface/mercury_calls_fortran/Mmakefile
+++ b/samples/c_interface/mercury_calls_fortran/Mmakefile
@@ -6,7 +6,7 @@

  # Define rules for compiling Fortran

-FC=g77
+FC=gfortran
  FFLAGS=

  .SUFFIXES: .f
@@ -21,12 +21,12 @@ MAIN_TARGET=all
  depend: mercury_main.depend
  all: mercury_main

-# tell the Mercury linker to link in fortran_main.o and libf2c
+# Tell the Mercury linker to link in fortran_main.o and libgfortran.
  MLOBJS=fortran_main.o
-MLLIBS=-lg2c
+MLLIBS=-lgfortran

-# tell mmake that it needs to make fortran_main.o before it can make
-# mercury_main
+# Tell mmake that it needs to make fortran_main.o before it can make
+# mercury_main.
  mercury_main: fortran_main.o

  # make sure that `mmake clean' removes fortran_main.o
diff --git a/samples/c_interface/mercury_calls_fortran/fortran_main_int.m b/samples/c_interface/mercury_calls_fortran/fortran_main_int.m
index 56523cf..dcfe69b 100644
--- a/samples/c_interface/mercury_calls_fortran/fortran_main_int.m
+++ b/samples/c_interface/mercury_calls_fortran/fortran_main_int.m
@@ -1,6 +1,6 @@
  % This module fortran_main_int defines a Mercury predicate fortran_main
  % which acts as an interface to the Fortran subroutine FORTRAN_MAIN,
-% which is defined in fortran_main.f.
+% which is defined in the file fortran_main.f.

  % This source file is hereby placed in the public domain.  -fjh (the author).

@@ -10,22 +10,20 @@
  :- import_module io.

  % Since the fortran_main() function has side effects, we declare the
-% corresponding Mercury predicate as one that takes an io__state pair. 
-% If we didn't do this, the Mercury compiler might optimize away calls to it!
+% corresponding Mercury predicate as one that takes an io.state pair.
+% If we did not do this, the Mercury compiler might optimize away calls to it!

  :- pred fortran_main(io::di, io::uo) is det.

  :- implementation.

-% Define the Mercury predicate fortran_main to call the Fortran
-% function FORTRAN_MAIN.  Note that g77 mangles names by converting
-% them to lowercase, and appending one or two underscores
-% (two if the name already contains any underscores, one otherwise).
-% So we need to use the name "fortran_main__" rather than "FORTRAN_MAIN".
+% Define the Mercury predicate fortran_main to call the Fortran function
+% FORTRAN_MAIN. Note that by default gfortran mangles names by converting
+% them to lowercase and appending an underscore.

-:- pragma import(fortran_main(di, uo), "fortran_main__").
-
-% Alternatively, you could use the "-fno-underscoring" and "-fcase-preserve"
-% options to g77 when compiling the Fortran.  Then the above declaration
-% could be like this instead:
-%	:- pragma import(fortran_main(di, uo), "FORTRAN_MAIN").
+:- pragma foreign_proc("C",
+    fortran_main(_IO0::di, _IO::uo),
+    [will_not_call_mercury, promise_pure],
+"
+    fortran_main_();
+").
diff --git a/samples/c_interface/mercury_calls_fortran/mercury_main.m b/samples/c_interface/mercury_calls_fortran/mercury_main.m
index d4ff300..6c4c101 100644
--- a/samples/c_interface/mercury_calls_fortran/mercury_main.m
+++ b/samples/c_interface/mercury_calls_fortran/mercury_main.m
@@ -14,7 +14,6 @@

  % main just invokes fortran_main
  main(!IO) :-
-	io.write_string("In Mercury main, about to call fortran_main...\n",
-		!IO),
-	fortran_main(!IO),
-	io.write_string("Back in Mercury main.\n", !IO).
+    io.write_string("In Mercury main, about to call fortran_main...\n", !IO),
+    fortran_main(!IO),
+    io.write_string("Back in Mercury main.\n", !IO).


More information about the reviews mailing list