Skip to content

Instantly share code, notes, and snippets.

@TakashiUNUMA
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakashiUNUMA/4f9c484872b62e0db9e6 to your computer and use it in GitHub Desktop.
Save TakashiUNUMA/4f9c484872b62e0db9e6 to your computer and use it in GitHub Desktop.
A sample SConstruct script for compiling your code(s) with NetCDF4 library.
# -*- coding: utf-8 -*-
#
# SConstruct for compiling Fortran90 code(s)
#
# coded by Takashi Unuma, Kyoto Univ.
# last modified: 2014/08/17
#
# load python modules
import os
import os.path
import sys
# check environmental value
FORTRAN = os.environ.get('FC')
# check compiler
if FORTRAN == "":
print u"Please set FORTRAN value as an environmental value"
print u" ex 1) $ export FC=gfortran"
print u" ex 2) $ export FC=ifort"
print u" ex 3) $ export FC=pgfortran"
print u" ex 4) $ export FC=f95"
print u" ex 5) $ export FC=g95"
sys.exit()
# define fortran flags
if FORTRAN == "gfortran":
FFLAG = ['-frecord-marker=4','-ffree-form','-fno-range-check','-O3','-ftree-vectorize','-funroll-loops','-fopenmp']
DFLAG = ['-frecord-marker=4','-ffree-form','-fno-range-check','-O','-fopenmp','-fbounds-check','-Wall','-Wuninitialized','-ffpe-trap=invalid,zero,overflow']
elif FORTRAN == "ifort":
FFLAG = ['-FR','-i-dynamic','-O3','-xHost','-fno-alias','-unroll0','-ipo','-openmp']
DFLAG = ['-FR','-i-dynamic','-O0','-openmp','-warn all','-check all','-gen_interfaces','-fpe0','-ftrapuv']
elif FORTRAN == "pgfortran":
FFLAG = ['-m64','-Mfree','-Kieee','-O3','-fast','-Ktrap=none','-mp','-Minfo']
DFLAG = ['-m64','-Mfree','-Kieee','-O0','-mp','-Minfo','-Ktrap=fp','-Minform=inform','-Mbounds','-Mlre=noassoc']
elif FORTRAN == "f95":
FFLAG = ['-ffree-form','-fno-range-check','-O3','-ftree-vectorize','-funroll-loops','-fopenmp']
DFLAG = ['-ffree-form','-fno-range-check','-O0','-fopenmp','-Wall','-Wuninitialized','-ffpe-trap=invalid,zero,overflow']
elif FORTRAN == "g95":
FFLAG = ['-ffree-form','-O3'] # not supported OpenMP directive
DFLAG = ['-ffree-form','-O0','-fbounds-check','-Wall','-Wuninitialized','-ftrace=full','-pedantic','-std=f95']
else:
print FORTRAN + u"is not supported on this SConstruct (for now)"
sys.exit()
# debug
# This debug section will be enabled with the scons option of "debug=0".
if ARGUMENTS.get('debug', 0):
FFLAGS = DFLAG
else:
FFLAGS = FFLAG
# define library path
# These variables will use the system environmental setting.
NETCDF = os.environ.get('NETCDF')
HDF = os.environ.get('HDF')
ZLIB = os.environ.get('ZLIB')
# check library path
if NETCDF == "":
print u"Please set NETCDF value as an environmental value"
sys.exit()
if HDF == "":
print u"Please set HDF value as an environmental value"
sys.exit()
if ZLIB == "":
print u"Please set ZLIB value as an environmental value"
sys.exit()
# define path to includes and libs
INCFLAG = [NETCDF + '/include', HDF5 + '/include', ZLIB + '/include']
LIBFLAG = [NETCDF + '/lib', HDF5 + '/lib', ZLIB + '/lib']
# define the required library
REQLIB = ['netcdf', 'netcdff', 'hdf5_hl', 'hdf5', 'z', 'm']
# set common env.
env = Environment(ENV=os.environ,F90=FORTRAN,LINK=FORTRAN,LINKFLAGS=FFLAGS,F90FLAGS=FFLAGS,F90PATH=INCFLAG)
# build your code(s)
env.Program('hoge.f90', LIBS=REQLIB, LIBPATH=LIBFLAG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment