10
1
mirror of https://github.com/pfloos/quack synced 2024-10-31 11:13:47 +01:00
QuAcK/src/make_ninja.py

276 lines
6.9 KiB
Python
Raw Normal View History

2020-10-13 13:44:24 +02:00
#!/usr/bin/env python3
import os
import sys
2020-10-14 15:07:35 +02:00
DEBUG=False
try:
DEBUG = sys.argv[1] == "debug"
except:
pass
2020-10-13 13:44:24 +02:00
if "QUACK_ROOT" not in os.environ:
os.chdir("..")
print("")
print("Please set the QUACK_ROOT environment variable, for example:")
print("")
print("$ export QUACK_ROOT={0}".format(os.getcwd()))
print("")
sys.exit(1)
QUACK_ROOT=os.environ["QUACK_ROOT"]
2020-10-14 15:07:35 +02:00
if not DEBUG:
compile_gfortran_mac = """
2020-10-13 13:44:24 +02:00
FC = gfortran
2020-10-14 17:09:07 +02:00
AR = libtool -static -o
2023-11-22 14:54:05 +01:00
FFLAGS = -I$IDIR -J$IDIR -fbacktrace -g -Wall -Wno-unused-variable -Wno-unused -Wno-unused-dummy-argument -O3
2020-10-13 13:44:24 +02:00
CC = gcc
CXX = g++
LAPACK=-lblas -llapack
STDCXX=-lc++
2020-10-14 17:15:07 +02:00
FIX_ORDER_OF_LIBS=
2020-10-14 17:09:07 +02:00
"""
compile_gfortran_linux = """
FC = gfortran
AR = ar crs
FFLAGS = -I$IDIR -J$IDIR -fbacktrace -g -Wall -Wno-unused -Wno-unused-dummy-argument -O3
CC = gcc
CXX = g++
LAPACK=-lblas -llapack
STDCXX=-lstdc++
2020-10-14 17:15:07 +02:00
FIX_ORDER_OF_LIBS=-Wl,--start-group
2023-06-30 16:47:26 +02:00
"""
compile_ifort_linux = """
FC = ifort -mkl=parallel -qopenmp
AR = ar crs
FFLAGS = -I$IDIR -g -Ofast -traceback
CC = icc
CXX = icpc
LAPACK=
STDCXX=-lstdc++
FIX_ORDER_OF_LIBS=-Wl,--start-group
2020-10-13 13:44:24 +02:00
"""
2020-10-14 15:07:35 +02:00
else:
compile_gfortran_mac = """
FC = gfortran
2020-10-14 17:09:07 +02:00
AR = libtool -static -o
2023-11-22 14:54:05 +01:00
FFLAGS = -I$IDIR -J$IDIR -fbacktrace -Wall -Wno-unused-variable -g -fcheck=all -Waliasing -Wampersand -Wconversion -Wsurprising -Wintrinsics-std -Wno-tabs -Wintrinsic-shadow -Wline-truncation -Wreal-q-constant
2020-10-13 13:44:24 +02:00
CC = gcc
CXX = g++
LAPACK=-lblas -llapack
STDCXX=-lc++
2020-10-14 17:15:07 +02:00
FIX_ORDER_OF_LIBS=
2020-10-13 13:44:24 +02:00
"""
2020-10-14 17:09:07 +02:00
compile_gfortran_linux = """
FC = gfortran
AR = ar crs
2023-10-24 14:39:02 +02:00
FFLAGS = -I$IDIR -J$IDIR -fbacktrace -Wall -g -fcheck=all -Waliasing -Wampersand -Wconversion -Wsurprising -Wintrinsics-std -Wno-tabs -Wintrinsic-shadow -Wline-truncation -Wreal-q-constant
2020-10-14 17:09:07 +02:00
CC = gcc
CXX = g++
LAPACK=-lblas -llapack
STDCXX=-lstdc++
2020-10-14 17:15:07 +02:00
FIX_ORDER_OF_LIBS=-Wl,--start-group
2020-10-14 17:09:07 +02:00
"""
2024-08-22 16:48:15 +02:00
compile_olympe = """
FC = ifort -mkl=parallel -qopenmp
AR = ar crs
FFLAGS = -I$IDIR -Ofast -traceback -xCORE-AVX512
CC = icc
CXX = icpc
LAPACK=
STDCXX=-lstdc++
FIX_ORDER_OF_LIBS=-Wl,--start-group
"""
2020-10-13 13:44:24 +02:00
2020-10-14 17:20:57 +02:00
if sys.platform in ["linux", "linux2"]:
2024-08-22 16:48:15 +02:00
# compiler = compile_gfortran_linux
compiler = compile_ifort_linux
# compiler = compile_olympe
2020-10-14 17:20:57 +02:00
elif sys.platform == "darwin":
compiler = compile_gfortran_mac
else:
print("Unknown platform. Only Linux and Darwin are supported.")
sys.exit(-1)
2020-10-13 13:44:24 +02:00
header = """#
# This file was automatically generated. Do not modify this file.
# To change compiling options, make the modifications in
# {0}/src/make_ninja.py
#
QUACK_ROOT={0}
IDIR=$QUACK_ROOT/include
LDIR=$QUACK_ROOT/lib
BDIR=$QUACK_ROOT/bin
SDIR=$QUACK_ROOT/src
2020-10-14 08:22:48 +02:00
LIBXC_VERSION=5.0.0
2020-10-14 17:15:07 +02:00
2020-10-13 13:44:24 +02:00
""".format(QUACK_ROOT)
rule_fortran = """
rule fc
2020-10-14 17:13:13 +02:00
command = $FC $FFLAGS -c $in -o $out
2020-10-13 13:44:24 +02:00
"""
rule_build_lib = """
rule build_lib
2020-10-14 17:09:07 +02:00
command = $AR $out $in
2020-10-13 13:44:24 +02:00
description = Linking $out
"""
2023-07-03 14:33:48 +02:00
LIBS=""
2020-10-13 13:44:24 +02:00
rule_build_exe = """
2020-10-13 20:59:03 +02:00
LIBS = {0} $LAPACK $STDCXX
2020-10-13 13:44:24 +02:00
rule build_exe
2020-10-14 17:15:07 +02:00
command = $FC $FIX_ORDER_OF_LIBS $in $LIBS -o $out
2020-10-13 13:44:24 +02:00
pool = console
description = Linking $out
rule build_lib
command = cd $dir ; ninja $out
pool = console
description = Compiling $out
2020-10-13 20:59:03 +02:00
""".format(LIBS)
2020-10-13 13:44:24 +02:00
rule_git_clone = """
rule git_clone
command = cd $QUACK_ROOT ; git clone $url
pool = console
description = Cloning $in
"""
build_in_lib_dir = "\n".join([
header,
compiler,
rule_fortran,
rule_build_lib,
])
build_in_exe_dir = "\n".join([
header,
compiler,
rule_fortran,
rule_build_exe,
])
build_main = "\n".join([
header,
compiler,
rule_git_clone,
])
2020-10-14 09:44:03 +02:00
exe_dirs = [ "QuAcK"]
2020-10-13 13:44:24 +02:00
lib_dirs = list(filter(lambda x: os.path.isdir(x) and \
x not in exe_dirs, os.listdir(".")))
def create_ninja_in_libdir(directory):
def write_rule(f, source_file, replace):
obj_file = os.path.join("obj", source_file.replace(replace, ".o"))
f.write("build {0}: fc {1}\n".format(obj_file,source_file))
return obj_file
with open(os.path.join(directory, "build.ninja"),"w") as f:
f.write(build_in_lib_dir)
objects = []
for filename in os.listdir(directory):
for suffix in [".f", ".f90"]:
if filename.endswith(suffix):
obj_file = write_rule(f, filename, suffix)
objects.append(obj_file)
objects = " ".join(objects)
f.write("build $LDIR/{0}.a: build_lib {1}\n".format(directory,objects))
f.write("default $LDIR/{0}.a\n".format(directory))
def create_ninja_in_exedir(directory):
def write_rule(f, source_file, replace):
obj_file = os.path.join("obj", source_file.replace(replace, ".o"))
f.write("build {0}: fc {1}\n".format(obj_file,source_file))
return obj_file
with open(os.path.join(directory, "build.ninja"),"w") as f:
f.write(build_in_exe_dir)
objects = []
for filename in os.listdir(directory):
for suffix in [".f", ".f90"]:
if filename.endswith(suffix):
obj_file = write_rule(f, filename, suffix)
objects.append(obj_file)
objects = " ".join(objects)
for libname in lib_dirs:
f.write("build $LDIR/{0}.a: build_lib\n dir = $SDIR/{0}\n".format(libname))
2020-10-13 20:59:03 +02:00
libs = " ".join([ "$LDIR/{0}.a".format(x) for x in lib_dirs]) + " "+LIBS
2020-10-13 13:44:24 +02:00
f.write("build $BDIR/{0}: build_exe {1} {2}\n".format(directory,libs,objects))
f.write("default $BDIR/{0}\n".format(directory))
def create_main_ninja():
2020-10-13 20:59:03 +02:00
libs = " ".join([ "$LDIR/{0}.a".format(x) for x in lib_dirs]) + " "+LIBS
2020-10-13 13:44:24 +02:00
with open("build.ninja","w") as f:
f.write(build_main)
f.write("""
rule build_exe
command = cd $SDIR/$dir ; ninja $out
pool = console
rule build_lib
command = cd $dir ; ninja $out
pool = console
description = Compiling $out
""")
for exe_dir in exe_dirs:
2020-10-14 08:22:48 +02:00
sources = [ "$SDIR/{0}/{1}".format(exe_dir,x) for x in os.listdir(exe_dir) ]
sources = filter(lambda x: x.endswith(".f") or x.endswith(".f90"), sources)
sources = " ".join(sources)
2023-07-03 14:33:48 +02:00
f.write("build $BDIR/{0}: build_exe {1} {2}\n".format(exe_dir,libs,sources))
2020-10-13 13:44:24 +02:00
f.write(" dir = {0} \n".format(exe_dir) )
2020-10-14 08:22:48 +02:00
2020-10-13 13:44:24 +02:00
for libname in lib_dirs:
2020-10-14 08:22:48 +02:00
sources = [ "$SDIR/{0}/{1}".format(libname,x) for x in os.listdir(libname) ]
sources = filter(lambda x: x.endswith(".f") or x.endswith(".f90"), sources)
sources = " ".join(sources)
2023-07-03 14:33:48 +02:00
f.write("build $LDIR/{0}.a: build_lib {1} \n dir = $SDIR/{0}\n".format(libname, sources))
f.write("build all: phony $BDIR/QuAcK\n")
2020-10-13 13:44:24 +02:00
f.write("default all\n")
def create_makefile(directory):
with open(os.path.join(directory, "Makefile"),"w") as f:
f.write("""default:
ninja
make -C ..
2020-10-14 15:07:35 +02:00
2020-10-14 16:02:09 +02:00
clean:
ninja -t clean
2020-10-14 15:07:35 +02:00
debug:
2020-10-14 16:02:09 +02:00
ninja -t clean
2020-10-14 15:07:35 +02:00
make -C .. debug
2020-10-13 13:44:24 +02:00
""")
def main():
for lib_dir in lib_dirs:
create_ninja_in_libdir(lib_dir)
create_makefile(lib_dir)
for exe_dir in exe_dirs:
create_ninja_in_exedir(exe_dir)
2020-10-14 15:07:35 +02:00
create_makefile(exe_dir)
2020-10-13 13:44:24 +02:00
create_main_ninja()
if __name__ == '__main__':
main()