Personal tools

Makefile CCCP

From MohidWiki

Revision as of 17:09, 17 July 2009 by Guillaume (talk | contribs) (Special root folder's
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This section describes a Cross-Compiler, Cross-Platform (CCCP) makefile development methodology using the make tool from GNU, also known as gmake. The syntax to use would be:

make platform.action

(ex: make Win, make Unix.clean, etc...).


A CCCP makefile project a bunch of library, modules and program folders within a single root folder.

Tree description

Example tree

> tree cproj/
|-- Makefile
|-- Makefiles
|   |--
|   |--
|   `-- Makefile
|-- lib1
|   `-- Makefile
|-- lib2
|   `-- Makefile
`-- prog1
    `-- Makefile

Regular folders

Every folder must contain the following files:

encapsulates a list of all the folder's source-files and meta-files, a bunch of includes containing the makefile rules and, last but not least, the target dependencies. Several local variables are defined within:
contains the path to the source files
contains the list of source files (using the $(S) suffix)
contains the list of files required by the Makefile to run in this project (using the .smk suffix) besides the Makefile itself (of course).
is the target name that the local makefile is supposed to build.
contains the list of modules (special root folder only).
is a list of the dependent files and the respective dependencies.

Special folders and special files

There are two special folders, mandatory, and each containing special files, beyond the mandatory regular files listed above:

  1. the root folder
    it's completely different from the regular folder Makefile. It contains the rules to build the Solution for a series of platform.
    it contains the regular folder Makefile structure and content. It also contains all the necessary configuration global variables specific to the CCCP solution. It must be configured for a specific platform (Linux, windows,etc...) by the administrator.
    Each user must edit this configuration file before building the solution. It contains the paths to required external libraries such as HDF5 or Netcdf.
  2. the Makefiles folder
    contains the makefile rules to build (all), clean, install, etc, the target modules. It is invoked only from the root file
    contains the makefile rules to build, clean, install, etc, the target files.

The makefile's global variables

The idea is to use global variables to pass arguments to the different modules makefiles. Thus, each specific platform makefile in the root folder defines these global variables. Then, it calls the modules makefiles who will use the global variables in their syntax. If a systematic approach of using global variables is implemented, then it is fairly easy to have a cross-platform makefile project implemented; and easily extended.

A template CCCP makefile solution

Special root folder's Makefile

 SHELL = /bin/sh
 export MAKE = make
 .PHONY: default
 	@echo Please choose your platform from: 
 	@echo Type "make (win nix).(' '  clean install sos)".
 #make all (add other platforms "Nix Nix Nix_double Win" ...)
 PLAT := Nix
 	$(MAKE) -f $ all
 #make clean
 PLATCLEAN := $(addsuffix .clean, $(PLAT))
 	$(MAKE) -f $(@:.clean=).mk clean
 #make install
 PLATINST := $(addsuffix .install, $(PLAT))
 	$(MAKE) -f $(@:.install=).mk install

Special root folder's

#Edit the prefix of the installed binaries
# > make nix.install
export VER = x64_single

#Where do you want to install the binary files?
# > make nix.install
export DESTDIR = /usr/bin/mohid

#Where are the hdf5 libraries (with --enable-fortran) in your system?
export HDF5 = /home/Projects/hdf5/hdf5-1.6.5/hdf5/lib

#Where is the libz.a in your system?
export ZLIBINC = /usr/lib64

Special root folder's

SHELL = /bin/sh

#------User configuration file---------------


#-- NIX platform specific global variables --

export CP  = sudo cp
export DEL = rm
export O = o
export F = F90
export MOD = mod
export CC= ifort
export CCFLAGS  = -c -fpp -warn all -nologo -convert big_endian -D_USE_NIX# Debug: -g Profiling: -p
export LFLAGS   = -fpp -nologo -warn all -i-static -convert big_endian -D_USE_NIX# Profiling: -p
export LLFLAGS  =
export AR = ar rc
export SUFFLIB = .lib
export SUFFPROG =
export SRCBASE1 = ../../../Shared/MOHID.Base.1
export SRCBASE2 = ../../../Shared/MOHID.Base.2
export SRCWATER = ../../../Modulus.Software/MOHID.Water
export BASE1INC = ../Mohid_Base_1
export BASE1 = Mohid_Base_1.lib
export BASE2INC = ../Mohid_Base_2
export BASE2 = Mohid_Base_2.lib
export WATER = MohidWater
export LHDF5FORTRAN = libhdf5_fortran.a
export LHDF5 = libhdf5.a
export LHDF5HL = libhdf5_hl.a
export ZLIB = libz.a
export BASELIBS = \
       $(BASE1INC)/$(BASE1) \
       $(BASE2INC)/$(BASE2) \
       $(HDF5)/$(LHDF5FORTRAN) \
       $(HDF5)/$(LHDF5) \
       $(HDF5)/$(LHDF5HL) \
export LNETCDF  = netcdf.a
export NETCDFLIBS := \
                     $(BASELIBS) \

#------Files and modules lists------

       README \
       Editme_template.smk \
         Makefiles \
         MohidWater \
         Mohid_Base_2 \

#------Makefile rules---------------

include Makefiles/

#------Modules dependencies----------

Mohid_Base_2.all : Mohid_Base_1.all
MohidWater.all : Mohid_Base_2.all

Special Makefiles folder's Makefile

        Makefiles.smk \

SHELL = /bin/sh

.PHONY: all
        @echo No such action in this module

Special Makefiles folder's

OBJS = $(FILES:.$(S)=.$(O))
SUFF := $(suffix $(TARGET))

#Make all
.PHONY: all
all: $(TARGET)

ifeq ($(SUFF),$(SUFFLIB))
        @$(AR) $@ $^
        @$(CC) $(LFLAGS) -o $@ $^ $(LLFLAGS)
        @echo Finished building $@.

#Fortran compilation rule
%.$(O) : $(SRCF)/%.$(F)
        @$(CC) $(CCFLAGS) $(INCS) $<
        @echo $* .................. [OK]

#make clean
.PHONY: clean
        @-$(DEL) *.$(O) *.$(MOD) $(TARGET)
        @echo erased $(TARGET) files.

#make install
.PHONY: install
install: $(SOURCE)
        @-$(CP) $< $(DESTDIR)/`date +%G%m%d`_$(addprefix $(VER), $<)
        @echo Installed $<.

Special Makefiles folder's

#make all
MODALL = $(addsuffix .all, $(MODULES))
all: $(MODALL)
	-$(MAKE) -C $(@:.all=) all

#make clean
MODCLEAN = $(addsuffix .clean, $(MODULES))
clean: $(MODCLEAN)
	-$(MAKE) -C $(@:.clean=) clean

#make install
MODINSTALL = $(addsuffix .install, $(MODULES))
install: $(MODINSTALL)
	-$(MAKE) -C $(@:.install=) install

Regular folder typical Makefile

INCS = -I$(HDF5)
       ModuleGlobalData.$(S) \
       ModuleDrainageNetwork.$(S) \

SHELL = /bin/sh

include ../Makefiles/

ModuleMacroAlgae.${O} : ModuleFunctions.${O}
ModuleTimeSerie.${O} : ModuleEnterData.${O}
ModuleDischarges.${O} : ModuleFunctions.${O} \
                     ModuleTimeSerie.${O} \

See also