1dnl/*D
2dnl PAC_PROG_F77_CMDARGS - Determine how to access the command line from
3dnl Fortran 77
4dnl
5dnl Output Effects:
6dnl  The following variables are set:
7dnl.vb
8dnl    F77_GETARG         - Statement to get an argument i into string s
9dnl    F77_IARGC          - Routine to return the number of arguments
10dnl    FXX_MODULE         - Module command when using Fortran 90 compiler
11dnl    F77_GETARGDECL     - Declaration of routine used for F77_GETARG
12dnl    F77_GETARG_FFLAGS  - Flags needed when compiling/linking
13dnl    F77_GETARG_LDFLAGS - Flags needed when linking
14dnl.ve
15dnl If 'F77_GETARG' has a value, then that value and the values for these
16dnl other symbols will be used instead.  If no approach is found, all of these
17dnl variables will have empty values.
18dnl If no other approach works and a file 'f77argdef' is in the directory,
19dnl that file will be sourced for the values of the above four variables.
20dnl
21dnl In most cases, you should add F77_GETARG_FFLAGS to the FFLAGS variable
22dnl and F77_GETARG_LDFLAGS to the LDFLAGS variable, to ensure that tests are
23dnl performed on the compiler version that will be used.
24dnl
25dnl 'AC_SUBST' is called for all six variables.
26dnl
27dnl One complication is that on systems with multiple Fortran compilers,
28dnl some libraries used by one Fortran compiler may have been (mis)placed
29dnl in a common location.  We have had trouble with libg2c in particular.
30dnl To work around this, we test whether iargc etc. work first.  This
31dnl will catch most systems and will speed up the tests.
32dnl
33dnl Next, the libraries are only added if they are needed to complete a
34dnl link; they aren''t added just because they exist.
35dnl
36dnl f77argdef
37dnl D*/
38dnl
39dnl Random notes
40dnl You can export the command line arguments from C to the g77 compiler
41dnl using
42dnl    extern char **__libc_argv;
43dnl    extern int  __libc_argc;
44dnl    f_setarg( __libc_argc, __libc_argv );
45dnl
46AC_DEFUN([PAC_PROG_F77_CMDARGS],[
47found_cached="yes"
48AC_MSG_CHECKING([for routines to access the command line from Fortran 77])
49AC_CACHE_VAL(pac_cv_prog_f77_cmdarg,
50[
51    AC_MSG_RESULT([searching...])
52    found_cached="no"
53    # First, we perform a quick check.  Does iargc and getarg work?
54    fxx_module="${FXX_MODULE:-}"
55    f77_getargdecl="${F77_GETARGDECL:-external getarg}"
56    f77_getarg="${F77_GETARG:-call GETARG(i,s)}"
57    f77_iargc="${F77_IARGC:-IARGC()}"
58    #
59    # Grumble.  The Absoft Fortran compiler computes i - i as 0 and then
60    # 1.0 / 0 at compile time, even though the code may never be executed.
61    # What we need is a way to generate an error, so the second usage of i
62    # was replaced with f77_iargc.
63    cat > conftest.f <<EOF
64        program main
65$fxx_module
66        integer i, j
67        character*20 s
68        $f77_getargdecl
69        i = 0
70        $f77_getarg
71        i=$f77_iargc
72        if (i .gt. 1) then
73            j = i - $f77_iargc
74            j = 1.0 / j
75        endif
76        end
77EOF
78    found_answer="no"
79    if test -z "$ac_fcompilelink" ; then
80        ac_fcompilelink="${F77-f77} -o conftest $FFLAGS $flags conftest.f $LDFLAGS $LIBS 1>&AC_FD_CC"
81    fi
82    AC_MSG_CHECKING([whether ${F77-f77} $flags $libs works with GETARG and IARGC])
83    if AC_TRY_EVAL(ac_fcompilelink) && test -x conftest ; then
84	# Check that cross != yes so that this works with autoconf 2.52
85	# Check that cross_compiling != yes so that this works with
86	# autoconf 2.6x for some (but almost certainly not all)x
87	# Question: why do we test that this runs?  It looks like we
88	# needed this for some old Fortran compilers that produced
89	# executable code that then did not run.
90	if test "$ac_cv_prog_f77_cross" != "yes" -a \
91	        "$cross_compiling" != "yes" ; then
92	    if ./conftest >/dev/null 2>&1 ; then
93		found_answer="yes"
94	        FXX_MODULE="$fxx_module"
95		F77_GETARGDECL="$f77_getargdecl"
96		F77_GETARG="$f77_getarg"
97		F77_IARGC="$f77_iargc"
98		AC_MSG_RESULT(yes)
99     	    fi
100        fi
101    fi
102    if test $found_answer = "no" ; then
103	AC_MSG_RESULT(no)
104    # Grumph.  Here are a bunch of different approaches
105    # We have several axes the check:
106    # Library to link with (none, -lU77 (HPUX), -lg2c (LINUX f77))
107    # PEPCF90 (Intel ifc)
108    # The first line is a dummy
109    # (we experimented with using a <space>, but this caused other
110    # problems because we need <space> in the IFS)
111    trial_LIBS="0 -lU77 -lPEPCF90"
112    if test "$NOG2C" != "1" ; then
113        trial_LIBS="$trial_LIBS -lg2c"
114    fi
115    # Discard libs that are not availble:
116    save_IFS="$IFS"
117    # Make sure that IFS includes a space, or the tests that run programs
118    # may fail
119    IFS=" ""
120"
121    save_trial_LIBS="$trial_LIBS"
122    trial_LIBS=""
123    cat > conftest.f <<EOF
124        program main
125        end
126EOF
127    ac_fcompilelink_test='${F77-f77} -o conftest $FFLAGS conftest.f $LDFLAGS $libs $LIBS 1>&AC_FD_CC'
128    for libs in $save_trial_LIBS ; do
129	if test "$libs" = "0" ; then
130	    lib_ok="yes"
131        else
132	    AC_MSG_CHECKING([whether Fortran 77 links with $libs])
133	    if AC_TRY_EVAL(ac_fcompilelink_test) && test -x conftest ; then
134		AC_MSG_RESULT([yes])
135	        lib_ok="yes"
136	    else
137		AC_MSG_RESULT([no])
138	        lib_ok="no"
139	    fi
140	fi
141	if test "$lib_ok" = "yes" ; then
142	    trial_LIBS="$trial_LIBS
143$libs"
144        fi
145    done
146
147    # Options to use when compiling and linking
148    # +U77 is needed by HP Fortran to access getarg etc.
149    # The -N109 was used for getarg before we realized that GETARG
150    # was necessary with the (non standard conforming) Absoft compiler
151    # (Fortran is monocase; Absoft uses mixedcase by default)
152    # The -f is used by Absoft and is the compiler switch that folds
153    # symbolic names to lower case.  Without this option, the compiler
154    # considers upper- and lower-case letters to be unique.
155    # The -YEXT_NAMES=LCS will cause external names to be output as lower
156    # case letter for Absoft F90 compilers (default is upper case)
157    # The first line is "<space><newline>, the space is important
158    # To make the Absoft f77 and f90 work together, we need to prefer the
159    # upper case versions of the arguments.  They also require libU77.
160    # -YCFRL=1 causes Absoft f90 to work with g77 and similar (f2c-based)
161    # Fortran compilers
162    #
163    # Problem:  The Intel efc compiler hangs when presented with -N109 .
164    # The only real fix for this is to detect this compiler and exclude
165    # the test.  We may want to reorganize these tests so that if we
166    # can compile code without special options, we never look for them.
167    #
168    using_intel_efc="no"
169    pac_test_msg=`$F77 -V 2>&1 | grep 'Intel(R) Fortran Itanium'`
170    if test "$pac_test_msg" != "" ; then
171	using_intel_efc="yes"
172    fi
173    if test "$using_intel_efc" = "yes" ; then
174        trial_FLAGS="000"
175    else
176        trial_FLAGS="000
177-N109
178-f
179-YEXT_NAMES=UCS
180-YEXT_NAMES=LCS
181-YCFRL=1
182+U77"
183    fi
184    # Discard options that are not available:
185    # (IFS already saved above)
186    IFS=" ""
187"
188    save_trial_FLAGS="$trial_FLAGS"
189    trial_FLAGS=""
190    for flag in $save_trial_FLAGS ; do
191	if test "$flag" = " " -o "$flag" = "000" ; then
192	    opt_ok="yes"
193        else
194            PAC_F77_CHECK_COMPILER_OPTION($flag,opt_ok=yes,opt_ok=no)
195        fi
196	if test "$opt_ok" = "yes" ; then
197	    if test "$flag" = " " -o "$flag" = "000" ; then
198		fflag=""
199	    else
200		fflag="$flag"
201	    fi
202	    # discard options that don't allow mixed-case name matching
203	    cat > conftest.f <<EOF
204        program main
205        call aB()
206        end
207        subroutine Ab()
208        end
209EOF
210	    if test -n "$fflag" ; then flagval="with $fflag" ; else flagval="" ; fi
211	    AC_MSG_CHECKING([whether Fortran 77 routine names are case-insensitive $flagval])
212	    dnl we can use double quotes here because all is already
213            dnl evaluated
214            ac_fcompilelink_test="${F77-f77} -o conftest $fflag $FFLAGS conftest.f $LDFLAGS $LIBS 1>&AC_FD_CC"
215	    if AC_TRY_EVAL(ac_fcompilelink_test) && test -x conftest ; then
216	        AC_MSG_RESULT(yes)
217	    else
218	        AC_MSG_RESULT(no)
219	        opt_ok="no"
220            fi
221        fi
222        if test "$opt_ok" = "yes" ; then
223	    trial_FLAGS="$trial_FLAGS
224$flag"
225        fi
226    done
227    IFS="$save_IFS"
228    # Name of routines.  Since these are in groups, we use a case statement
229    # and loop until the end (accomplished by reaching the end of the
230    # case statement
231    # For one version of Nag F90, the names are
232    # call f90_unix_MP_getarg(i,s) and f90_unix_MP_iargc().
233    trial=0
234    while test -z "$pac_cv_prog_f77_cmdarg" ; do
235        case $trial in
236	0) # User-specified values, if any
237	   if test -z "$F77_GETARG" -o -z "$F77_IARGC" ; then
238	       trial=`expr $trial + 1`
239	       continue
240           fi
241           MSG="Using environment values of F77_GETARG etc."
242	   ;;
243	1) # Standard practice, uppercase (some compilers are case-sensitive)
244	   FXX_MODULE=""
245	   F77_GETARGDECL="external GETARG"
246	   F77_GETARG="call GETARG(i,s)"
247	   F77_IARGC="IARGC()"
248	   MSG="GETARG and IARGC"
249	   ;;
250	2) # Standard practice, lowercase
251	   FXX_MODULE=""
252           F77_GETARGDECL="external getarg"
253	   F77_GETARG="call getarg(i,s)"
254	   F77_IARGC="iargc()"
255	   MSG="getarg and iargc"
256	   ;;
257	3) # Posix alternative
258	   FXX_MODULE=""
259	   F77_GETARGDECL="external pxfgetarg"
260	   F77_GETARG="call pxfgetarg(i,s,l,ier)"
261	   F77_IARGC="ipxfargc()"
262	   MSG="pxfgetarg and ipxfargc"
263	   ;;
264	4) # Nag f90_unix_env module
265	   FXX_MODULE="        use f90_unix_env"
266	   F77_GETARGDECL=""
267	   F77_GETARG="call getarg(i,s)"
268	   F77_IARGC="iargc()"
269	   MSG="f90_unix_env module"
270	   ;;
271        5) # Nag f90_unix module
272	   FXX_MODULE="        use f90_unix"
273	   F77_GETARGDECL=""
274	   F77_GETARG="call getarg(i,s)"
275	   F77_IARGC="iargc()"
276	   MSG="f90_unix module"
277	   ;;
278	6) # user spec in a file
279	   if test -s f77argdef ; then
280		. ./f77argdef
281	       MSG="Using definitions in the file f77argdef"
282	   else
283	        trial=`expr $trial + 1`
284		continue
285	   fi
286	   ;;
287	7) # gfortran won't find getarg if it is marked as external
288	   FXX_MODULE=""
289	   F77_GETARGDECL="intrinsic GETARG"
290	   F77_GETARG="call GETARG(i,s)"
291	   F77_IARGC="IARGC()"
292	   MSG="intrinsic GETARG and IARGC"
293	   ;;
294        *) # exit from while loop
295	   FXX_MODULE=""
296	   F77_GETARGDECL=""
297	   F77_GETARG=""
298	   F77_IARGC=""
299           break
300	   ;;
301	esac
302	# Create the program.  Make sure that we can run it.
303	# Force a divide-by-zero if there is a problem (but only at runtime!
304        # (the Absoft compiler does divide-by-zero at compile time)
305        cat > conftest.f <<EOF
306        program main
307$FXX_MODULE
308        integer i, j
309        character*20 s
310        $F77_GETARGDECL
311        i = 0
312        $F77_GETARG
313        i=$F77_IARGC
314        if (i .gt. 1) then
315            j = i - $F77_IARGC
316            j = 1.0 / j
317        endif
318        end
319EOF
320    #
321    # Now, try to find some way to compile and link that program, looping
322    # over the possibilities of options and libraries
323        save_IFS="$IFS"
324        IFS=" ""
325"
326        for libs in $trial_LIBS ; do
327            if test -n "$pac_cv_prog_f77_cmdarg" ; then break ; fi
328	    if test "$libs" = " " -o "$libs" = "0" ; then libs="" ; fi
329            for flags in $trial_FLAGS ; do
330	        if test "$flags" = " " -o "$flags" = "000"; then flags="" ; fi
331                AC_MSG_CHECKING([whether ${F77-f77} $flags $libs works with $MSG])
332		IFS="$save_IFS"
333		dnl We need this here because we've fiddled with IFS
334	        ac_fcompilelink_test="${F77-f77} -o conftest $FFLAGS $flags conftest.f $LDFLAGS $libs $LIBS 1>&AC_FD_CC"
335		found_answer="no"
336                if AC_TRY_EVAL(ac_fcompilelink_test) && test -x conftest ; then
337		    if test "$ac_cv_prog_f77_cross" != "yes" -a \
338		            "$cross_compiling" != "yes" ; then
339			if ./conftest >/dev/null 2>&1 ; then
340			    found_answer="yes"
341			fi
342		    else
343			found_answer="yes"
344		    fi
345                fi
346	        IFS=" ""
347"
348		if test "$found_answer" = "yes" ; then
349	            AC_MSG_RESULT([yes])
350		    pac_cv_prog_f77_cmdarg="$MSG"
351		    pac_cv_prog_f77_cmdarg_fflags="$flags"
352		    pac_cv_prog_f77_cmdarg_ldflags="$libs"
353		    break
354	        else
355                    AC_MSG_RESULT([no])
356		    echo "configure: failed program was:" >&AC_FD_CC
357                    cat conftest.f >&AC_FD_CC
358	        fi
359            done
360        done
361        IFS="$save_IFS"
362	rm -f conftest.*
363        trial=`expr $trial + 1`
364    done
365fi
366pac_cv_F77_GETARGDECL="$F77_GETARGDECL"
367pac_cv_F77_IARGC="$F77_IARGC"
368pac_cv_F77_GETARG="$F77_GETARG"
369pac_cv_FXX_MODULE="$FXX_MODULE"
370])
371if test "$found_cached" = "yes" ; then
372    AC_MSG_RESULT([$pac_cv_prog_f77_cmdarg])
373elif test -z "$pac_cv_F77_IARGC" ; then
374    AC_MSG_WARN([Could not find a way to access the command line from Fortran 77])
375fi
376# Set the variable values based on pac_cv_prog_xxx
377F77_GETARGDECL="$pac_cv_F77_GETARGDECL"
378F77_IARGC="$pac_cv_F77_IARGC"
379F77_GETARG="$pac_cv_F77_GETARG"
380FXX_MODULE="$pac_cv_FXX_MODULE"
381F77_GETARG_FFLAGS="$pac_cv_prog_f77_cmdarg_fflags"
382F77_GETARG_LDFLAGS="$pac_cv_prog_f77_cmdarg_ldflags"
383AC_SUBST(F77_GETARGDECL)
384AC_SUBST(F77_IARGC)
385AC_SUBST(F77_GETARG)
386AC_SUBST(FXX_MODULE)
387AC_SUBST(F77_GETARG_FFLAGS)
388AC_SUBST(F77_GETARG_LDFLAGS)
389])
390