Re: Is there a Fortran equivalent of C's #ifdef ??



Dave wrote:

Dave Allured wrote:

Dave wrote:
Many are the result of using GNU-specific flags. My aim is to replace
flags like -fPIC with a variable which can be set in advance, so the
code compiles and links on other platforms with other compilers. The
world does not start and end with GNU tools, though many software
developers acts as though it does.

To make the code work with non-GNU compilers, I need to know what flags
are needed on that platform. To do that, I need to know what compilers
are in use. For C and C++ I'm doing that by use of the pre-processor.

Any suggestions you have for how I might do that with Fortran are welcome.

Let's look at the main objective. From your original C example, it
seems that you want to start with an arbitrary command name for a
Fortran compiler, ignore any meaning of the literal spelling of the
command itself, and identify the compiler based on its behavior. Right?

Yes, basically. At one point, I thought a grep on the output of '

$somecompiler --version

would show GNU or GCC if it was a GNU compiler, but that is not true.

Please do not mix up C and Fortran issues in this topic. Your original
question was about Fortran compilers, not C. I get the message that C
is handled in special ways. If you want to identify gfortran, then
simply do the following. A case insensitive comparison seems to be a
bit more robust for this one:

$somecompiler --version | grep -i 'GNU Fortran'

On Ubuntu, 'gcc --version' will output 'gcc' but not GCC or GNU. But if
invoked by the name 'cc', it is clearly the GNU compiler, but will not
show GNU, GCC or gcc.

% cc --version
cc (Ubuntu 4.3.3-5ubuntu4) 4.3.3
Copyright (C) 2008 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.

So relying on the name of the command is not really helpful.

For Fortran as well as C, I agree on this particular point. As
previously discussed, some compilers are normally installed with
identifying command names e.g. gfortran, g95; others use generic
commands, e.g. Sun and others' f77, f90, etc.

OK, I could add "Free Software Foundation" to the list of things I grep
for, but it's anyones guess what information someone will output with
the GNU compiler.

For reasons I do not understand or wish to, it seems that use of
preprocessors evolved as the favored method of testing identification of
C compilers.

It was suggested by a few on comp.unix.shell when I originally asked
about this.

http://groups.google.co.uk/group/comp.unix.shell/browse_thread/thread/aa0dfac7c7aeb412/1d432f8b8bfc54a?hl=en&ie=UTF-8&q=How+can+I+tell+if+%27CC%27+is+the+GNU+C+compiler%3F#01d432f8b8bfc54a

The general opinion was that using the pre-processor is the most
reliable and that relying on the use of -v, -V, --version and other
similar things is not so useful.

I am sure this is sage advice for C compilers. Fortran is different.

I'm also aware the linker is an issue. I found when trying to build Sage
on Solaris, it went ok if gcc used the GNU linker and assembler, but
failed if gcc was configured to use Sun's linker and assembler. I've
since sorted all those issues out. Though I wish I'd given more thought
at the time to running on other platforms. There were specific reasons
for wanting Solaris.

* I own SPARC hardware myself
* Sun are providing support for the project in terms of hardware. They
donated a Sun T5240 (16 core box) to the Sage project. I do not use that
machine much, as I have a Sun of my own at home.

Now the few fortran compilers in my experience do a good
job identifying themselves directly with command switches like -V or
--version. No test program is involved.

Sun's does not on Solaris. Neither -v or --version work. -V does I would
admit.

So your test for Sun Fortran starts with $somecompiler -V !! What's the
problem? Don't expect all Fortrans to play so nicely that they all
implemented the same "version" flag! Heh, that's not in the standard!

However, by this direct method, compare strings must be built directly
into the invoking shell script. I do not see this as a problem. An
added advantage is that you also get a nice version number on your
platter, for when it might matter.

gcc and g++ will give you the version using the -dumpversion flag.
gfortran will not. I have reported that as a bug against gfortran, and
believe that will be fixed in the next release.

drkirkby@swan:[~] $ gcc -dumpversion
4.4.1
drkirkby@swan:[~] $ g++ -dumpversion
4.4.1
drkirkby@swan:[~] $ gfortran -dumpversion
GNU Fortran (GCC) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

drkirkby@swan:[~] $

This is far astray from "how to identify a fortran compiler". You
already know that gfortran --version will be fine for this purpose.

So why not use this much more straightforward method for compiler
identification, and skip the preprocessor gyrations?

Well, when 'cc' outputs neither gcc, GNU, or GCC, but is still the GNU
compiler, you can see doing this can be a bit tricky.

Again, that is C, this is Fortran.

In conclusion, modern fortran compilers usually support some kind of
reliable self identification with a command line flag. No dummy test
program is needed. It is the user's responsibility to determine and use
the correct ID flag for each compiler, and to do the appropriate custom
string matching on the output.

--Dave
.


Quantcast