Building programs from separate makefiles | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Building programs from separate makefiles

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-22-2013
kristinu kristinu is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 30 July 2014, 10:40 PM EDT
Location: London
Posts: 844
Thanks: 59
Thanked 8 Times in 8 Posts
Building programs from separate makefiles

I have several makefiles to build various programs in a software suite (currently 4 programs). I want to create a main Makefile so that I can build everything I need.

Unsure on the way I should proceed, for example using


Code:
include fdtc.mk

or calling


Code:
$(MAKE) -f ./mk/Makefile nfdtc

Here is a listing of one makefile, the others are similar



Code:
#####
#
# Makefile for fdtc application
#
#####

#--------------------------------------------------------------------------------------

# Source code paths

OPSYS = $(shell uname -s )
TARGET = nraypk

FDTC_INCDIR = ../include/fdtc

FDTC_PRGDIR = ../src/app
FDTC_LIBDIR = ../src/flib/fdtc
PLOTX_LIBDIR = ../src/flib/plotx11

OBJDIR = ./obj
BINDIR = ./bin

APP = $(BINDIR)/$(TARGET)

FDTC_APPDIR = ../src/app
FDTC_LIBDIR = ../src/flib/fdtc
PLOT_LIBDIR = ../src/flib/pltlib

OBJDIR = ./obj
BINDIR = ./bin

#--------------------------------------------------------------------------------------

# Set Fortran compiler
FCOMP = gfortran

# Set Fortran compiler options
FCOMP_OPT = -O -Wall -fbacktrace -fno-align-commons -I$(FDTC_INCDIR)

# Set Fortran compiler options
FLINK_OPT = -O -Wall -fbacktrace -fno-align-commons -I$(FDTC_INCDIR)

#--------------------------------------------------------------------------------------

# Source files

FDTC_APPSRC = $(FDTC_APPDIR)/fdtc.f

FDTC_LIBSRC = $(FDTC_LIBDIR)/model.f \
    $(FDTC_LIBDIR)/time.f \
    $(FDTC_LIBDIR)/findiff.f \
    $(FDTC_LIBDIR)/findiff2d.f \
    $(FDTC_LIBDIR)/stencils.f \
    $(FDTC_LIBDIR)/stencils2d.f \
    $(FDTC_LIBDIR)/misc.f \
    $(FDTC_LIBDIR)/plt.f \
    $(FDTC_LIBDIR)/blkdat.f

PLOTX_SRC = $(PLOTX_LIBDIR)/nopltlib.f

FDTC_LIBINC = $(FDTC_INCDIR)/fd.par \
    $(FDTC_INCDIR)/fd.com

#--------------------------------------------------------------------------------------
# Object files

FDTC_PRGOBJ = $(subst .f,.o,$(subst $(RAYPK_PRGDIR),$(OBJDIR),$(RAYPK_PRGSRC)))
FDTC_LIBOBJ = $(subst .f,.o,$(subst $(RAYPK_LIBDIR),$(OBJDIR),$(RAYPK_LIBSRC)))
PLOTX_LIBOBJ = $(subst .f,.o,$(subst $(PLOTX_LIBDIR),$(OBJDIR),$(PLOTX_LIBSRC)))

FDTC_APPOBJ = $(OBJDIR)/fdtc.o

FDTC_LIBOBJ = $(OBJDIR)/model.o \
    $(OBJDIR)/time.o \
    $(OBJDIR)/findiff.o  \
    $(OBJDIR)/findiff2d.o \
    $(OBJDIR)/stencils.o \
    $(OBJDIR)/stencils2d.o \
    $(OBJDIR)/misc.o \
    $(OBJDIR)/plt.o \
    $(OBJDIR)/blkdat.o

PLOTX_LIBOBJ = $(OBJDIR)/nopltlib.o

FDTC_ALLOBJ = $(FDTC_APPOBJ) $(FDTC_LIBOBJ) $(PLOTX_LIBOBJ)

# Name of FDTC application
FDTC_APPNAME = $(BINDIR)/nfdtc

#--------------------------------------------------------------------------------------

default: help

help:
    @echo " "
    @echo "Operating System Detected: $(OPSYS) "
    @echo " "
    @echo "USAGE: "
    @echo " "
    @echo "  make -f nraypk.mk           To get this listing"
    @echo "  make -f nraypk.mk help      To get this listing"
    @echo "  make -f nraypk.mk list      List the details of building nraypk"
    @echo "  make -f nraypk.mk nraypk    Builds the application nraypk in BINDIR"
    @echo "  make -f nraypk.mk clean     Remove *.o and executable files"

list:
    @echo ""
    @echo "OPSYS:  $(OPSYS)"
    @echo "APPL:   $(TARGET)"
    @echo "FCOMP:  $(FORTRAN_COMPILER)"
    @echo "CCOMP:  $(C_COMPILER)"
    @echo "CURDIR: $(CURDIR)"
    @echo ""
    @echo "---------------"
    @echo ""
    @echo "DIRECTORIES (relative to CURDIR)"
    @echo ""
    @echo "  OBJDIR = $(OBJDIR)"
    @echo "  BINDIR = $(BINDIR)"
    @echo ""
    @echo "---------------"
    @echo ""
    @echo "SOURCE FILES (relative to CURDIR)"
    @echo ""
    @echo "  RAYPK_PRGSRC = $(RAYPK_PRGSRC)"
    @echo ""
    @echo "  RAYPK_LIBSRC = $(LIBSRC_MSG1)"
    @$(foreach s, $(LIBSRC_MSG2), echo "                 $(s)";)
    @echo ""
    @echo "  PLOTX_LIBSRC = $(PLOTX_LIBSRC)"
    @echo ""
    @echo "---------------"
    @echo ""
    @echo "OBJECT FILES (relative to CURDIR)"
    @echo ""
    @echo "  RAYPK_PRGOBJ = $(RAYPK_PRGOBJ)"
    @echo ""
    @echo "  RAYPK_LIBOBJ = $(LIBOBJ_MSG1)"
    @$(foreach s, $(LIBOBJ_MSG2), echo "                 $(s)";)
    @echo ""
    @echo "  PLOTX_LIBOBJ = $(PLOTX_LIBOBJ)"

#--------------------------------------------------------------------------------------

# Generate nfdtc application
nfdtc : $(FDTC_ALLOBJ) $(FDTC_LIBINC)
    $(FCOMP) $(FLINK_OPT) $(FDTC_ALLOBJ) -o $(FDTC_APPNAME)

# Compile main program
$(FDTC_APPOBJ) : $(FDTC_APPSRC) $(FDTC_LIBINC)
    $(FCOMP) $(FCOMP_OPT) -c $(FDTC_APPSRC)
    mv *.o $(OBJDIR)

# Compile FD Library
$(FDTC_LIBOBJ) : $(FDTC_LIBSRC) $(FDTC_LIBINC)
    $(FCOMP) $(FCOMP_OPT) -c $(FDTC_LIBSRC)
    mv *.o $(OBJDIR)

# Compile PLOT Library, with plotting mode disabled
$(FDTC_NOPLOT_LIBOBJ) : $(NOPLOT_SRC) $(FDTC_LIBINC)
    $(FCOMP) $(FCOMP_OPT) -c $(NOPLOT_SRC)
    mv *.o $(OBJDIR)

#--------------------------------------------------------------------------------------

clean:
    rm -f -I $(OBJDIR)/*.o $(BINDIR)/xzslice

allclean:
    rm -f -I $(OBJDIR)/*.o $(BINDIR)/*

#######################################################################################
# END

Sponsored Links
    #2  
Old 01-22-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 July 2014, 3:09 AM EDT
Location: Saskatchewan
Posts: 19,016
Thanks: 754
Thanked 3,157 Times in 2,961 Posts
One of the simpler ways to do this is recursive make:


Code:
outputfolder/outputfile:outputfolder/Makefile
        make -C outputfolder

which is equivalent to cd outputfolder ; make

This lets you call a makefile in another folder without having to incorporate it into your own lock, stock, and barrel.

make supports recursion directly, and will tell you how deep into recursion it is.
Sponsored Links
    #3  
Old 01-22-2013
kristinu kristinu is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 30 July 2014, 10:40 PM EDT
Location: London
Posts: 844
Thanks: 59
Thanked 8 Times in 8 Posts
A problem I have is how to pass the option from the main makefile to the subdirectory makefile. I do not like using recursive make build systems (example a make in every directory). However, since I have put all the make files in one place together with a main Makefile, I do not see a problem.
    #4  
Old 01-22-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 July 2014, 3:09 AM EDT
Location: Saskatchewan
Posts: 19,016
Thanks: 754
Thanked 3,157 Times in 2,961 Posts
I agree in principle. When writing new makefiles, one should write it as one system so it can run efficiently.

When you have to use someone else's makefiles however, you have very little control.

If you're not rewriting them, cramming them all together in one folder is hardly better than keeping them separate. When they were in folders, they were at least organized. Now it's one awful pile.

What option are you trying to pass, to what? If you're not calling make again, there's not a lot to pass options to.
Sponsored Links
    #5  
Old 01-22-2013
kristinu kristinu is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 30 July 2014, 10:40 PM EDT
Location: London
Posts: 844
Thanks: 59
Thanked 8 Times in 8 Posts
What I did was to have only one makefile for each application and store them in a single folder. Then the main Makefile will execute the appropriate Makefile

I think I have a solution. Let me try it and will report back.

---------- Post updated at 11:32 AM ---------- Previous update was at 11:26 AM ----------

I am rewriting them.
Sponsored Links
    #6  
Old 01-24-2013
kristinu kristinu is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 30 July 2014, 10:40 PM EDT
Location: London
Posts: 844
Thanks: 59
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by Corona688 View Post
One of the simpler ways to do this is recursive make:


Code:
outputfolder/outputfile:outputfolder/Makefile
        make -C outputfolder

which is equivalent to cd outputfolder ; make

This lets you call a makefile in another folder without having to incorporate it into your own lock, stock, and barrel.

make supports recursion directly, and will tell you how deep into recursion it is.
I cannot quite understand how to use the above code

My directory structure is as below. The user uses Makefile. The makefile calls
the makefile for the specific program.


Code:
.
├── Makefile
└── mk
    ├── fdtc.mk
    ├── raypk.mk
    └── zslice.mk

Here is the code for Makefile


Code:
default: help

help : help-nfdtc help-nraypk help-xraypk help-xzslice 

help-nfdtc : 
    $(MAKE) -f fdtc.mk help

help-nraypk : 
    $(MAKE) -f raypk.mk help

help-xraypk : 
    $(MAKE) -f raypk.mk help

help-xzslice : 
    $(MAKE) -f zslice.mk help

list : list-nfdtc list-nraypk list-xraypk list-xzslice

list-nfdtc :
    $(MAKE) -f fdtc.mk list
    
list-nraypk :
    $(MAKE) -f raypk.mk list

list-xraypk :
    $(MAKE) -f raypk.mk list
    
list-xzslice :
    $(MAKE) -f zslice.mk list
    
#---------------------------------------------------------------------------------------------------

nfdtc :
    $(MAKE) -f fdtc.mk nfdtc

nraypk :
    $(MAKE) -f raypk.mk nraypk

xraypk :
    $(MAKE) -f raypk.mk xraypk

xzslice :
    $(MAKE) -f zslice.mk xzslice

#---------------------------------------------------------------------------------------------------

clean : clean-fdtc clean-raypk clean-zslice

clean-fdtc :
    make -f fdtc.mk clean

clean-raypk :
    make -f raypk.mk clean

clean-zslice :
    make -f zslice.mk clean

Sponsored Links
    #7  
Old 01-24-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 July 2014, 3:09 AM EDT
Location: Saskatchewan
Posts: 19,016
Thanks: 754
Thanked 3,157 Times in 2,961 Posts
I have no idea why you have done what you have, now. Why bother rewriting it if you're going to do exactly what you said you were trying to avoid? You've got exactly what you had before, except instead of nicely organized into folders, it's one enormous mess.

You might as well have just let them live in their own folders and used their old makefiles. Then you could do this:


Code:
all:projectfolder1/projectoutputfile projectfolder2/projectoutputfile ...

projectfolder1/projectoutputfile:projectfolder1/Makefile
        make -C projectfolder1

projectfolder2/projectoutputfile:projectfolder2/Makefile
        make -C projectfolder2

Then you could do 'make all' and it would run make in each of the folders.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Makefiles for different programs kristinu Programming 1 01-03-2013 05:56 PM
Are programs like sys_open( ) ,sys_read( ) et al examples of system level programs ? vishwamitra UNIX for Dummies Questions & Answers 1 10-28-2009 12:00 PM
makefiles klam UNIX for Advanced & Expert Users 0 09-23-2006 02:56 PM
How can I print variables im using in makefiles? umen Shell Programming and Scripting 1 05-07-2006 09:57 AM
Combining makefiles Dan Rooney UNIX for Advanced & Expert Users 4 05-07-2004 08:19 AM



All times are GMT -4. The time now is 05:26 AM.