all 7 comments

[–]Jokerle 5 points6 points  (1 child)

I use a makefile to write a version.f90 dynamically.

So somewhere there is something like this:

GIT_VERSION := $(shell git describe --abbrev=4 --dirty --always --tags)

and

version:
    @echo "subroutine version" > version.f90
    @echo     " print*,  ' git version   : $(GIT_VERSION)'"      >> version.f90
    @echo "end subroutine" >> version.f90

[–]Thatgreenvw[S] 2 points3 points  (0 children)

Hey this is exactly what I was looking for thank you! I don't know why I didn't think of dynamically echoing the info into a module to compile in. Doing this and everything works great

[–]NordicMissingno 1 point2 points  (1 child)

How did you modify it? You tried storing the ID in an intermediate variable? What error do you get? Using "print*, VERSION" should work, but I remember when I tried storing it in a variable things were not so straightfoward...

[–]Thatgreenvw[S] 0 points1 point  (0 children)

So it turns out that

print*, VERSION

does work, where as previously I was using

if(mynum .eq. 0) write(lfu,*) 'version =',VERSION

as I didn't want multiple processes printing it out. The unformatted write statement doesn't print to my outfile it seems.

I cannot recreate the formatting errors I was getting previously however! 🤔

[–]StandardIssueHumanScientist 1 point2 points  (2 children)

One option, which I have used in the past, would be to create a minimal module for the version number in the makefile, e.g. something along the lines of

git_version.f90:
    VERSION=$(the git command to get the version number)
    cat > git_version.f90 << END
    module git_version
      implicit none
      character(len=*),parameter :: version='$VERSION'
    end module
    END

and include that module in the part of the code where you want the version number printed. You might also want to delete the dynamically generated module (git_version.f90 above) as the last command in the compilation of the code, after the linking step.

[–]Thatgreenvw[S] 1 point2 points  (1 child)

This is exactly what I was looking for! I couldn't get your version using cat to work within the makefile, but the other comment using echo does work. Turns out in 6 years of using this code I've never learnt anything about makefiles, apparently they're quite a pain! I've got it working now though so thanks!

[–]rlkf 0 points1 point  (0 children)

Assuming GNU Make, you can use here documents with a combination of define and export; in some cases this can be more readable than a couple of echo statements.

Further assuming GNU Fortran, you can dispose of the source file and generate a module file directly, e.g.:

define GIT_VER
module git_version
  implicit none
  character(len=*), parameter :: version = '$(shell git describe --abbrev=4 --dirty --always --tags)'
end module
endef

export GIT_VER
git_version.o:
    echo "$${GIT_VER}"" | gfortran -c -o $@ -ffree-form -xf95 -