Amitava
Amitava

Reputation: 431

How to write `make` file for separate source and build directory for large program with mixed Fortran F77 and F90 code

I have about 39 Fortran F90 source files and 35 Fortran 77 Lapack related files. I am using include statement in my main program to connect all these files.

I have created a batch file make.bat with command ifort "MDL HydroD.F90" which compiles my code and generates the mdlhydrod.exe file. In the process the Fortran compiler creates many .mod and .obj build files which makes it difficult to manage. I would like to put my source files under a directory Source and lapack library files in a directory lapack and build files in a directory Debug.

Could anyone help me modify my make.bat file so that ifort looks at Source directory and build in Debug directory.

Thank you for help.

Currently using make.bat has only one line of command:

File Name: make.bat

ifort  "MDL HydroD.F90"

Working on a make file to be used with nmake (incomplete):

File Name: make.mak:

#Make File for MDL HydroD
# Compiler options
FC          :=  ifort
VPATH       :=  src
BINDIR      :=  bin

$(BINDIR):
    mkdir -p $(BINDIR)

clean:
@rm -rf $(BINDIR)

Upvotes: 2

Views: 1164

Answers (1)

Because you are using a strange way of working with source files, which you showed in your other question, it will be very difficult to change this.

For recapitulation, you include everything in a single source file using the include statement. This looks pretty unfortunate to me and I commented on that there. If you have one source file, you are forced to build it with one command, there is no place for any fine control. This is not the issue of a bash or bat script vs. Makefile.

You can probably still keep some files included in some groups that are logically similar, if you need no finer control on that, but I see not much reason for that.

Remove the includes or at least the relevant ones. Then you can just do

ifort Source/the_source_file1 -c Output/name_of_obj1 -module the_directory_for_modules -I the_directory_for_modules  -other_flags

for every file. And then in the end:

ifort Output/name_of_obj1 Output/name_of_obj2 Output/name_of_obj3 .... -o the_result

In Scons (which I would use) it would be like this (tested on couple of dummy files). The file Sconstruct:

import os

env = Environment(tools=['default','ifort'])

env.Append(ENV = {'PATH' : os.environ['PATH']})
try:
  env.Append(ENV = {'LIBRARY_PATH' : os.environ['LIBRARY_PATH']})
except:
  pass

env.Append(F90FLAGS='-g -fast') #whatever flags you need
env.Append(FORTRANFLAGS='-g -fast') #whatever flags you need

outdir = "Output/"

srcdir = "Sources/"

lapackdir = "lapack/"


objs = []
for file in os.listdir(srcdir):
  objs += env.Object(target=outdir+os.path.splitext(file)[0], source=srcdir+file)

for file in os.listdir(lapackdir):
  objs += env.Object(target=outdir+os.path.splitext(file)[0], source=lapackdir+file)

env.Append(FORTRANMODDIR = outdir)

objs = filter(lambda o: str(o)[-4:] != '.mod', objs)

prg = env.Program(target="bin/result.exe", source= objs)

Default(prg)

Upvotes: 1

Related Questions