program test
use compilation_details
write(*,'(A)') 'Compilation date = ' // compile_date
write(*,'(A)') 'Compilation string= ' // compile_string
write(*,*) 'hello world'
end program test
This is then compiled using the following script "mycompile"
#!/bin/bash
compiler=ifort
options="-O0 -check all -traceback -warn all"
src="test.f90"
output="mytest.exe"
echo "module compilation_details" > compilation_details.f90
echo " character(len= 50) :: compile_date= '" `date` "'" >>
compilation_details.f90
echo " character(len=100) :: compile_string='" $compiler $options $src
"-o" $output "'" >> compilation_details.f90
echo "end module compilation_details" >> compilation_details.f90
$compiler $options compilation_details.f90 $src -o $output
When executed my program displays
Compilation date = Wed Jun 3 17:52:55 BST 2009
Compilation string= ifort -O0 -check all -traceback -warn all test.f90
-o mytest.exe
hello world
Which is what I want. However, I wonder if there is a way to avoid this
roundabout procedure.
Also, I don't know how to do the same when using a Makefile and the Make
utility...
Anybody's got any ideas?
Lorenzo
!-------------------------------------------------------------------------------
! compile_time_stamp() -- set as public
! Returns a string with len = 80, that contains the program
compilation time stamp.
! Example :: 'May 16 2008 16:53:48'
! For this to work the fortran preprocessor should be enabled during
compilation
! When using Intel Visual Fortran Compiler
! /fpp option or Fortran --> Preprocessor -> "Preprocess Source File"
turned on
pure function compile_time_stamp( ) result ( tsstring )
implicit none
character (len = 80) :: tsstring
! Example :: 'May 16 2008 16:53:48'
write(tsstring,'(A,2X,A)') trim(__DATE__), trim(__TIME__ )
end function compile_time_stamp
!-------------------------------------------------------------------------------
You won't find a more portable way than that. Yes, there are options
using preprocessors, but then you have to worry about compatibility with
the various pre-processors out there.
> Also, I don't know how to do the same when using a Makefile and the Make
> utility...
It shouldn't be particularly hard to convince Make that updating your
module file is the first part of doing the compilation, but I haven't
diddled with make for several years, so I'll let others try that.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
make a script (mk.sh) that has:
#!/bin/bash
compiler=ifort
options="-O0 -check all -traceback -warn all"
src="test.f90"
output="mytest.exe"
echo "module compilation_details" > compilation_details.f90
echo " character(len= 50) :: compile_date= '" `date` "'" >>
compilation_details.f90
echo " character(len=100) :: compile_string='" $compiler $options
$src
"-o" $output "'" >> compilation_details.f90
echo "end module compilation_details" >> compilation_details.f90
then in the makefile, add compile_details.o to the dependencies the
executable requires and add something like:
compile_details.o: compile_details.f90
<ifort commandline here>
@rm -f compile_details.f90
compile_details.f90:
./mk.sh | tee compile_details.f90
remove "compile_details.f90" before the first run of the new
makefile. it should now recreate / compile / delete the details file
on each subsequent run
syntax might be off a little, but the above illustrates the point
anyway
> Lorents <lor...@amp.te> wrote:
> > In developmental versions of my programs I sometimes find it useful to
> > include in the executable the compile date and the compiler options
> > used. I'm trying to find the neatest way of doing this.
> > What I have done so far is to write a small shell script (BTW, I work on
> > Linux) which creates a small file containing a fortran module which is
> > then USE'd by my program.
> ...
> > Which is what I want. However, I wonder if there is a way to avoid this
> > roundabout procedure.
>
> You won't find a more portable way than that. Yes, there are options
> using preprocessors, but then you have to worry about compatibility with
> the various pre-processors out there.
Hmm. One possible issue does occur to me, though. Compilation of the
multiple object files in a single program can potentially occur at
multiple dates/times. The larger the program gets, the more likely this
is to be a significant issue.
I'll leave to you the question of how important it might be for you to
track the compilation data of individual object files; that's a bit
broader question. I'll just note that if you do try to track such
multiple compilation times, you could run into trouble if you have
multiple program units compiled using different versions of the same
module. Something like an include file might work out better for that.
I also note that your module declares compilation_date and
compilation_details to be variables instead of parameters. For something
that is inherently intended to be defined at compile time, I would think
a parameter would be a more appropriate choice.
> Also, I don't know how to do the same when using a Makefile and the Make
> utility...
> Anybody's got any ideas?
Using Make is pretty similar to your shell script:
my_date = $(shell date +"(%d-%b-%Y)")
compile_date = "'$(my_date)'"
my_compiler = $(shell g95 --version | head -1)
compile_string = "'$(my_compiler)'"
g95:
g95 -cpp -Dcompile_date=$(compile_date) -Dcompile_string=$(compile_string) test.f90
ifort:
ifort -fpp -Dcompile_date=$(compile_date) test.f90
David Duffy.
> In developmental versions of my programs I sometimes find it useful to
> include in the executable the compile date and the compiler options
> used. I'm trying to find the neatest way of doing this.
You have enough options that no one could begin to suggest how to choose
the "neatest" one. Creating a special source file as you suggested is one
of many frequently used methods.
> Also, I don't know how to do the same when using a Makefile and the Make
> utility...
>
> Anybody's got any ideas?
>
Check out the gnu make manual, for options you could use with any
compiler, and the ifort manual, for options specific to ifort with its
built-in preprocessor. Most Fortran compilers come with a pre-processor
facility, as OpenMP makes it a de facto requirement, so you can feed in
strings; usually, the C preprocessor date macros are available to the
companion Fortran preprocessor. Linux/Unix Fortran compilers mostly use
the convention where .F and .F90 files automatically get pre-processing,
as well as having command line options, plus the often used possibility of
simply using a command such as 'gfortran -E' (or the gcc equivalent) to
make a pre-processed copy of source.
In order to capture the compile option string, under make you would likely
define the option string as a make macro so it can be used multiple
places. You might possibly take advantage of the way compilers like ifort
write it into the output file under the -S option.
Interesting, our FUN3D code is on *many* platforms, and the only trouble
we've had with pre-processors^1 is a bug in Cray's that does not ignore
C-style comments as it claims it should.
Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov
http://twitter.com/bil_kleb
[1] Troubles other than figuring out how to turn preprocessing on
with some compilers, e.g., IBM's XLF.
He can write a short Fortran program that can generate
the module.
It's then fully portable.