10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-12-27 06:43:48 +01:00
quantum_package/scripts/compilation/qp_create_ninja.py

972 lines
28 KiB
Python
Raw Normal View History

2018-04-30 18:43:13 +02:00
#!/usr/bin/env python2
2015-05-18 10:09:45 +02:00
# -*- coding: utf-8 -*-
2015-06-02 15:46:01 +02:00
"""
2015-06-11 11:58:26 +02:00
Usage: qp_create_ninja.py create <config_file> (--development | --production)
2015-06-10 15:46:04 +02:00
qp_create_ninja.py update
2015-06-02 15:46:01 +02:00
"""
2015-05-18 10:09:45 +02:00
import os
2015-05-26 16:08:52 +02:00
import sys
2015-05-27 11:02:13 +02:00
import glob
2015-05-18 10:09:45 +02:00
from os.path import join
from collections import namedtuple
2015-06-03 17:52:41 +02:00
from collections import defaultdict
2015-06-10 15:46:04 +02:00
import pickle
try:
2015-06-04 16:40:00 +02:00
from module_handler import ModuleHandler
2015-05-26 16:08:52 +02:00
from read_compilation_cfg import get_compilation_option
2015-06-02 15:46:01 +02:00
from docopt import docopt
except ImportError:
2015-07-03 15:40:25 +02:00
f = os.path.realpath(os.path.join(os.path.dirname(__file__),
"..",
"..",
"quantum_package.rc"))
2015-07-28 16:45:45 +02:00
print "\n".join(["", "Error:", "source %s" % f, ""])
2015-05-26 16:08:52 +02:00
sys.exit(1)
2015-05-18 10:09:45 +02:00
2015-07-28 16:45:45 +02:00
# __
# /__ | _ |_ _. | _. ._ o _. |_ | _ _
# \_| | (_) |_) (_| | \/ (_| | | (_| |_) | (/_ _>
#
from qp_path import QP_ROOT, QP_SRC, QP_EZFIO
2015-11-17 00:16:00 +01:00
LIB = "" # join(QP_ROOT, "lib", "rdtsc.o")
2015-12-01 12:34:24 +01:00
EZFIO_LIB = join(QP_ROOT, "lib", "libezfio_irp.a")
2016-03-07 20:35:38 +01:00
ZMQ_LIB = join(QP_ROOT, "lib", "libf77zmq.a") + " " + join(QP_ROOT, "lib", "libzmq.a") + " -lstdc++ -lrt"
ROOT_BUILD_NINJA = join(QP_ROOT, "config", "build.ninja")
2015-06-11 11:58:26 +02:00
header = r"""#
# _______ _____
# __ __ \___ _______ _________ /____ ________ ___
# _ / / / / / / __ `/_ __ \ __/ / / /_ __ `__ \
# / /_/ // /_/ // /_/ /_ / / / /_ / /_/ /_ / / / / /
# \___\_\\__,_/ \__,_/ /_/ /_/\__/ \__,_/ /_/ /_/ /_/
#
# ________ ______
# ___ __ \_____ _________ /_______ _______ _____
# __ /_/ / __ `/ ___/_ //_/ __ `/_ __ `/ _ \
# _ ____// /_/ // /__ _ ,< / /_/ /_ /_/ // __/
# /_/ \__,_/ \___/ /_/|_| \__,_/ _\__, / \___/
# /____/
#
# https://github.com/LCPQ/quantum_package,
#
2015-06-11 16:25:35 +02:00
# Generated automatically by {0}
2015-06-11 11:58:26 +02:00
#
#
""".format(__file__).replace(QP_ROOT,"$QP_ROOT")
2015-06-11 11:58:26 +02:00
2015-06-02 15:46:01 +02:00
#
# |\ | _. ._ _ _ _| _|_ ._ | _
# | \| (_| | | | (/_ (_| |_ |_| |_) | (/_
# |
Path = namedtuple('Path', ['abs', 'rel'])
EZ_config_path = namedtuple('EZ_config', ['path_in_module', 'path_in_ezfio'])
2015-06-02 18:40:24 +02:00
EZ_handler = namedtuple('EZ_handler', ['ez_module', 'ez_cfg', 'ez_interface',
'ez_config'])
2015-06-02 15:46:01 +02:00
Sym_link = namedtuple('Sym_link', ['source', 'destination'])
module_instance = ModuleHandler()
2015-06-02 15:46:01 +02:00
2015-06-17 18:16:14 +02:00
def real_join(*args):
return os.path.realpath(join(*args))
# _
# |_ ._ _. ._ o _. |_ | _ _
# |_ | | \/ \/ (_| | | (_| |_) | (/_ _>
def ninja_create_env_variable(pwd_config_file):
2015-06-02 15:46:01 +02:00
"""
2015-06-10 12:26:16 +02:00
Return some ninja variable with the env variable expanded
2015-06-02 15:46:01 +02:00
FC, FCFLAGS, IRPF90, IRPF90_FLAGS
The env variable is usefull for the generation of EZFIO, and IRPF90
"""
2015-07-16 17:36:38 +02:00
l_string = ["builddir = {0}".format(os.path.dirname(ROOT_BUILD_NINJA)),
""]
for flag in ["FC", "FCFLAGS", "IRPF90", "IRPF90_FLAGS"]:
2015-06-02 18:40:24 +02:00
str_ = "{0} = {1}".format(flag, get_compilation_option(pwd_config_file,
flag))
l_string.append(str_)
lib_lapack = get_compilation_option(pwd_config_file, "LAPACK_LIB")
2017-11-27 12:05:51 +01:00
str_lib = " ".join([LIB, lib_lapack, EZFIO_LIB, ZMQ_LIB])
2016-03-01 15:41:41 +01:00
l_string.append("LIB = {0} ".format(str_lib))
l_string.append("")
return l_string
2015-06-02 15:46:01 +02:00
# __
# /__ _ ._ _ _. | _ _
# \_| (/_ | | (/_ (_| | (_) (_| \/
# _| /
def dict_module_genelogy_path(d_module_genelogy):
"""
Just a dict with relative, and absolue path for the
d_module_genelogy
"""
d = dict()
for module_rel, l_children_rel in d_module_genelogy.iteritems():
2015-06-18 11:11:14 +02:00
module_abs = real_join(QP_SRC, module_rel)
2015-06-02 18:40:24 +02:00
p = Path(module_abs, module_rel)
2015-06-02 15:46:01 +02:00
try:
2015-06-18 11:11:14 +02:00
d[p] = Path(real_join(QP_SRC, l_children_rel), l_children_rel)
2015-06-02 15:46:01 +02:00
except:
2015-06-18 11:11:14 +02:00
d[p] = [Path(real_join(QP_SRC, children), children)
2015-06-02 18:40:24 +02:00
for children in l_children_rel]
2015-06-02 15:46:01 +02:00
return d
2015-05-18 10:09:45 +02:00
# _ __ _ ___ _ _
# |_ / |_ | / \ _ _|_ _
# |_ /_ | _|_ \_/ o (_ | (_|
# _|
2015-06-02 18:40:24 +02:00
def get_l_module_with_ezfio_cfg():
2015-06-02 15:46:01 +02:00
"""
2015-07-24 11:23:54 +02:00
Return all the modules that have a EZFIO.cfg
2015-06-02 15:46:01 +02:00
"""
2015-05-21 11:30:15 +02:00
from os import listdir
2015-06-17 18:16:14 +02:00
from os.path import isfile
2015-05-21 11:30:15 +02:00
2015-06-18 11:11:14 +02:00
return [real_join(QP_SRC, m) for m in listdir(QP_SRC)
if isfile(real_join(QP_SRC, m, "EZFIO.cfg"))]
2015-05-21 11:30:15 +02:00
def get_l_ezfio_config():
2015-06-02 15:46:01 +02:00
"""
Return a namedtuple('EZ_config', ['path_in_module', 'path_in_ezfio'])
"""
2015-05-21 11:30:15 +02:00
l = []
2015-05-18 10:09:45 +02:00
2015-06-18 11:11:14 +02:00
cmd = "{0}/*/*.ezfio_config".format(QP_SRC)
for path_in_module in glob.glob(cmd):
2015-06-17 18:16:14 +02:00
real_path = real_join(path_in_module)
name_lower = os.path.split(real_path)[1].lower()
2015-06-18 11:11:14 +02:00
path_in_ezfio = join(QP_EZFIO, "config", name_lower)
2015-06-17 18:16:14 +02:00
l.append(EZ_config_path(real_path, path_in_ezfio))
2015-05-18 10:09:45 +02:00
return l
2015-05-18 10:09:45 +02:00
def ninja_ezfio_cfg_rule():
2015-06-02 15:46:01 +02:00
"""
2015-07-25 00:18:19 +02:00
Return the ezfio_interface rule which will create
2015-06-02 15:46:01 +02:00
the _ezfio_interface.irp.f the _ezfio_config from the EZFIO.cfg
"""
l_string = ["rule build_ezfio_interface",
2015-06-02 18:40:24 +02:00
" command = ei_handler.py --path_module $sub_module", ""]
2015-05-20 16:01:41 +02:00
2015-05-21 11:30:15 +02:00
return l_string
def ninja_ezfio_config_rule():
2015-06-02 15:46:01 +02:00
"""
If a ezfio_config existe you just need to move it
"""
2015-06-02 18:40:24 +02:00
l_string = ["rule build_ezfio_config", " command = cp $in $out", ""]
2015-05-21 11:30:15 +02:00
return l_string
2015-05-20 16:01:41 +02:00
def get_children_of_ezfio_cfg(l_module_with_ezfio_cfg):
2015-06-02 15:46:01 +02:00
"""
2016-04-16 21:18:38 +02:00
From a module list of ezfio_cfg return all the stuff created by it
2015-06-02 15:46:01 +02:00
"""
2015-06-18 11:11:14 +02:00
config_folder = join(QP_EZFIO, "config")
l_util = dict()
2015-05-21 11:30:15 +02:00
for m in l_module_with_ezfio_cfg:
2015-05-18 10:09:45 +02:00
name_module = os.path.split(m)[1]
name_module_lower = name_module.lower()
2015-05-18 10:09:45 +02:00
rel = name_module
abs_ = m
ez_module = Path(abs_, rel)
2015-05-18 10:09:45 +02:00
rel = "EZFIO.cfg"
abs_ = join(m, "EZFIO.cfg")
ez_cfg = Path(abs_, rel)
rel = "ezfio_interface.irp.f"
abs_ = join(m, rel)
ez_interface = Path(abs_, rel)
rel = "{0}.ezfio_interface_config".format(name_module_lower)
abs_ = join(config_folder, rel)
ez_config = Path(abs_, rel)
2015-06-02 18:40:24 +02:00
l_util[ez_module.rel] = EZ_handler(ez_module, ez_cfg, ez_interface,
2015-06-02 15:46:01 +02:00
ez_config)
return l_util
def ninja_ezfio_cfg_build(l_util):
2015-06-02 15:46:01 +02:00
"""
Return the children created by EZFIO.cfg
For us is only ez_interface.irp.f and ez_config
"""
2015-05-21 11:30:15 +02:00
l_string = []
for m in l_util.itervalues():
2015-06-02 15:46:01 +02:00
str_ = "build {1} {2}: build_ezfio_interface {0}"
2015-06-02 18:40:24 +02:00
l_string += [str_.format(m.ez_cfg.abs, m.ez_interface.abs,
2015-06-02 15:46:01 +02:00
m.ez_config.abs)]
l_string += [" sub_module = {0}".format(m.ez_module.abs)]
l_string += [""]
2015-05-21 11:30:15 +02:00
return l_string
2015-05-21 11:30:15 +02:00
def ninja_ezfio_config_build(l_ezfio_config):
2015-06-02 15:46:01 +02:00
"""
For the ezfio_config present in module move then
"""
l_string = []
2015-05-21 11:30:15 +02:00
for m in l_ezfio_config:
file_source = m.path_in_module
file_create = m.path_in_ezfio
2015-05-21 11:30:15 +02:00
2015-06-02 18:40:24 +02:00
l_string += ["build {0}: build_ezfio_config {1}".format(file_create,
file_source)]
2015-05-21 11:30:15 +02:00
l_string += [""]
return l_string
2015-05-21 11:30:15 +02:00
def ninja_ezfio_rule():
2015-06-02 15:46:01 +02:00
"""
Retun the rule for creation the ezfio
Set some variable
and run ninja
"""
2015-06-02 18:40:24 +02:00
l_flag = ["export {0}='${0}'".format(flag)
for flag in ["FC", "FCFLAGS", "IRPF90"]]
2015-05-26 16:08:52 +02:00
2015-12-01 12:34:24 +01:00
install_lib_ezfio = join(QP_ROOT, 'install', 'EZFIO', "lib", "libezfio_irp.a")
2015-06-18 11:11:14 +02:00
l_cmd = ["cd {0}".format(QP_EZFIO)] + l_flag
2017-10-09 09:46:46 +02:00
l_cmd += ["rm -f make.config ; ninja && rm -f {1} ; ln -sf {0} {1}".format(install_lib_ezfio, EZFIO_LIB)]
2015-05-21 11:30:15 +02:00
2015-06-02 15:46:01 +02:00
l_string = ["rule build_ezfio",
" command = {0}".format(" ; ".join(l_cmd)),
2015-06-11 11:58:26 +02:00
" pool = console", " description = Create $out", ""]
2015-05-21 11:30:15 +02:00
return l_string
def ninja_ezfio_build(l_ezfio_config, l_util):
2015-06-02 15:46:01 +02:00
"""
Rule for create the ezfio
we depend of the ezfio_config set by the user or created bu EZFIO.cfg
"""
2015-06-02 15:46:01 +02:00
l_ezfio_config = [i.path_in_ezfio for i in l_ezfio_config]
l_ezfio_from_cfg = [i.ez_config.abs for i in l_util.itervalues()]
2015-06-02 15:46:01 +02:00
str_ = " ".join(l_ezfio_config + l_ezfio_from_cfg)
2015-06-10 18:16:29 +02:00
l_string = ["build {0}: build_ezfio {1}".format(EZFIO_LIB, str_), ""]
2015-05-21 11:30:15 +02:00
return l_string
2015-05-18 10:09:45 +02:00
# __
# (_ ._ _ | o ._ |
# __) \/ | | | | | | | |<
# /
2015-06-02 15:46:01 +02:00
def get_source_destination(path_module, l_needed_molule):
"""
Return a list of Sym_link = namedtuple('Sym_link', ['source', 'destination'])
for a module
"""
2015-06-18 11:11:14 +02:00
return [Sym_link(m.abs, join(QP_SRC, path_module.rel, m.rel))
2015-06-02 18:40:24 +02:00
for m in l_needed_molule]
2015-05-18 10:09:45 +02:00
def ninja_symlink_rule():
2015-06-02 15:46:01 +02:00
"""
Return the command to create for the symlink
"""
2017-10-09 09:46:46 +02:00
return ["rule build_symlink", " command = rm -f $out ; ln -sf $in $out", ""]
2015-05-18 10:09:45 +02:00
2015-06-02 15:46:01 +02:00
def ninja_symlink_build(path_module, l_symlink):
"""
Create the symlink
2015-07-25 00:18:19 +02:00
and the l_symlink which are all the symlink list
2015-06-02 15:46:01 +02:00
"""
2015-06-02 15:46:01 +02:00
if not l_symlink:
return []
2015-07-15 11:00:23 +02:00
l_folder = [s.destination for s in l_symlink]
l_string = ["build l_symlink_{0} : phony {1}".format(path_module.rel,
" ".join(l_folder)),
""]
2015-05-18 10:09:45 +02:00
2015-06-02 15:46:01 +02:00
for symlink in l_symlink:
2015-06-02 18:40:24 +02:00
l_string += ["build {0}: build_symlink {1}".format(symlink.destination,
symlink.source), ""]
2015-05-18 10:09:45 +02:00
return l_string
2015-06-25 16:52:08 +02:00
#
# _ o _|_ o _ ._ _ ._ _
# o (_| | |_ | (_| | | (_) | (/_
# _| _|
def ninja_gitignore_rule():
"""
Return the command to create the gitignore
"""
return ["rule build_gitignore",
" command = module_handler.py create_git_ignore $module_rel",
" description = Create gitignore for $module_rel", ""]
2015-07-15 11:00:23 +02:00
def ninja_gitignore_build(path_module, d_binaries, l_symlink):
2015-06-25 16:52:08 +02:00
"""
"""
path_gitignore = join(path_module.abs, ".gitignore")
l_b = [i.abs for i in d_binaries[path_module]]
2015-07-15 11:00:23 +02:00
root = "build {0}: build_gitignore {1}".format(path_gitignore,
" ".join(l_b))
if l_symlink:
l_string = ["{0} || l_symlink_{1}".format(root, path_module.rel)]
else:
l_string = ["{0}".format(root)]
l_string.extend((" module_rel = {0}".format(path_module.rel), ""))
2015-06-25 16:52:08 +02:00
return l_string
2015-05-18 10:09:45 +02:00
# _ _ _
# o ._ ._ _|_ (_| / \ ._ _ _. | _
# | | |_) | | \_/ o | | | (_| |< (/_
# |
2015-06-05 22:33:19 +02:00
def get_l_file_for_module(path_module):
2015-06-02 15:46:01 +02:00
'''
return the list of irp.f in a module
'''
2015-06-06 10:44:04 +02:00
l_depend = []
2015-06-03 17:52:41 +02:00
l_src = []
l_obj = []
l_template = []
2015-06-03 10:19:02 +02:00
2015-06-05 22:33:19 +02:00
for f in os.listdir(path_module.abs):
2015-06-04 16:40:00 +02:00
if f.lower().endswith(tuple([".template.f", ".include.f"])):
2015-06-05 22:33:19 +02:00
l_template.append(join(path_module.abs, f))
2015-06-03 17:52:41 +02:00
elif f.endswith(".irp.f"):
2015-06-06 10:44:04 +02:00
l_depend.append(join(path_module.abs, f))
2015-06-04 16:40:00 +02:00
elif f.lower().endswith(tuple([".f", ".f90", ".c", ".cpp", ".cxx"])):
2015-06-10 13:44:26 +02:00
l_depend.append(join(path_module.abs, f))
2015-06-08 12:35:40 +02:00
l_src.append(f)
2015-06-03 17:52:41 +02:00
obj = '{0}.o'.format(os.path.splitext(f)[0])
2015-06-05 22:33:19 +02:00
l_obj.append(obj)
2016-03-01 15:41:41 +01:00
elif f.lower().endswith(".o"):
l_obj.append(join(path_module.abs, f))
2015-06-03 17:52:41 +02:00
elif f == "EZFIO.cfg":
2015-06-06 10:44:04 +02:00
l_depend.append(join(path_module.abs, "ezfio_interface.irp.f"))
2015-06-03 17:52:41 +02:00
2015-06-10 13:44:26 +02:00
d = {
"l_depend": l_depend,
"l_src": l_src,
"l_obj": l_obj,
"l_template": l_template
}
2015-06-03 17:52:41 +02:00
return d
2015-06-03 17:52:41 +02:00
def get_file_dependency(d_info_module):
2015-06-02 15:46:01 +02:00
"""
2015-07-25 00:18:19 +02:00
For a module return all the irp.f90 needed files
2015-06-02 15:46:01 +02:00
"""
2015-06-03 17:52:41 +02:00
d_irp = defaultdict(dict)
2015-06-02 15:46:01 +02:00
for module, l_children in d_info_module.iteritems():
2015-06-05 22:33:19 +02:00
for key, values in get_l_file_for_module(module).iteritems():
if key in ["l_src"]:
2015-06-10 13:44:26 +02:00
values = [join(module.abs, o) for o in values]
2015-06-08 12:35:40 +02:00
if key in ["l_obj"]:
2015-06-10 13:44:26 +02:00
values = [join(module.abs, "IRPF90_temp", o) for o in values]
2015-06-08 12:35:40 +02:00
2015-06-03 17:52:41 +02:00
d_irp[module][key] = values
2015-06-08 12:35:40 +02:00
2015-06-03 17:52:41 +02:00
for children in l_children:
2015-06-05 22:33:19 +02:00
for key, values in get_l_file_for_module(children).iteritems():
2015-06-08 12:35:40 +02:00
if key in ["l_src"]:
2015-06-10 13:44:26 +02:00
values = [join(module.abs, children.rel, o)
for o in values]
2015-06-08 12:35:40 +02:00
if key in ["l_obj"]:
2015-06-10 13:44:26 +02:00
values = [join(module.abs, "IRPF90_temp", children.rel, o)
for o in values]
2015-06-08 12:35:40 +02:00
d_irp[module][key].extend(values)
return d_irp
def ninja_irpf90_make_rule():
2015-06-02 15:46:01 +02:00
"""
The rule for creating the irpf90.make
Export the flag and compile
Only secontial make a possible
"""
2015-06-02 18:40:24 +02:00
# ~#~#~#~#~ #
# F l a g s #
# ~#~#~#~#~ #
l_flag = []
for flag in ["FC", "FCFLAGS", "LIB", "SRC", "OBJ"]:
str_ = "export {0}='${0}'".format(flag)
l_flag.append(str_)
2015-05-18 10:09:45 +02:00
2015-06-02 18:40:24 +02:00
# ~#~#~ #
# c m d #
# ~#~#~ #
2015-06-11 11:58:26 +02:00
l_cmd = ["cd $module_abs"] + l_flag + ["irpf90 $include_dir $IRPF90_FLAGS"]
2015-06-02 18:40:24 +02:00
# ~#~#~#~#~#~ #
# s t r i n g #
# ~#~#~#~#~#~ #
l_string = ["pool irp_pool", " depth = 1", "", "rule build_irpf90.ninja",
2015-06-02 15:46:01 +02:00
" command = {0}".format(" ; ".join(l_cmd)),
2015-06-05 09:56:05 +02:00
" pool = irp_pool",
2015-06-11 11:58:26 +02:00
" description = Running IRPF90 for $module_rel", ""]
return l_string
2015-06-02 18:40:24 +02:00
def ninja_irpf90_make_build(path_module, l_needed_molule, d_irp):
2015-06-02 15:46:01 +02:00
"""
2015-06-02 18:17:59 +02:00
Creatre the dependency for a irpf90.make
2015-06-02 15:46:01 +02:00
We need all the symklink and all the irp.f
"""
# ~#~#~#~#~#~ #
# O u t p u t #
# ~#~#~#~#~#~ #
2015-06-02 18:40:24 +02:00
l_creation = [join(path_module.abs, i)
2017-04-20 18:38:31 +02:00
for i in ["irpf90_entities", "tags",
2015-06-10 12:26:16 +02:00
"IRPF90_temp/build.ninja"]]
2015-05-27 17:49:31 +02:00
str_creation = " ".join(l_creation)
2015-06-02 15:46:01 +02:00
# ~#~#~#~#~#~#~#~#~#~ #
# D e p e n d a n c y #
# ~#~#~#~#~#~#~#~#~#~ #
2015-06-06 10:44:04 +02:00
l_depend = d_irp[path_module]["l_depend"]
2015-06-03 17:52:41 +02:00
l_src = d_irp[path_module]["l_src"]
l_obj = d_irp[path_module]["l_obj"]
l_template = d_irp[path_module]["l_template"]
2015-06-02 18:40:24 +02:00
if l_needed_molule:
2015-06-06 10:44:04 +02:00
l_symlink = ["l_symlink_{0}".format(path_module.rel)]
2015-06-02 18:40:24 +02:00
else:
2015-06-06 10:44:04 +02:00
l_symlink = []
2015-06-02 18:40:24 +02:00
2015-06-06 10:44:04 +02:00
str_depend = " ".join(l_depend + l_symlink + l_template)
2015-05-27 11:02:13 +02:00
2015-06-02 15:46:01 +02:00
# ~#~#~#~#~#~#~#~#~#~#~ #
# N i n j a _ b u i l d #
# ~#~#~#~#~#~#~#~#~#~#~ #
2015-05-18 10:09:45 +02:00
2015-06-02 15:46:01 +02:00
l_include_dir = ["-I {0}".format(m.rel) for m in l_needed_molule]
2015-05-27 11:02:13 +02:00
2015-06-02 18:40:24 +02:00
l_string = [
"build {0}: build_irpf90.ninja {1}".format(str_creation, str_depend),
2015-06-11 11:58:26 +02:00
" module_abs = {0}".format(path_module.abs),
" module_rel = {0}".format(path_module.rel),
2015-06-02 18:40:24 +02:00
" SRC = {0}".format(" ".join(l_src)),
" OBJ = {0}".format(" ".join(l_obj)),
" include_dir = {0}".format(" ".join(l_include_dir)), ""
]
2015-05-18 10:09:45 +02:00
return l_string
2015-05-26 17:40:39 +02:00
def ninja_readme_rule():
2015-06-02 15:46:01 +02:00
"""
Rule for creation the readme.
For not dealted the readme when ninja -t clean and the generator option
"""
l_string = ["rule build_readme",
2015-07-28 16:45:45 +02:00
" command = qp_update_readme.py $module_abs --root_module $module_root",
2015-06-24 12:25:19 +02:00
" description = update_README $module_rel",
2015-06-02 18:40:24 +02:00
" generator = 1", ""]
2015-05-26 17:40:39 +02:00
return l_string
2015-06-24 11:38:33 +02:00
def ninja_readme_build(path_module, d_irp, dict_root_path):
2015-06-02 15:46:01 +02:00
"""
Rule for creation the readme
"""
2015-05-26 17:40:39 +02:00
path_readme = join(path_module.abs, "README.rst")
2015-06-24 11:38:33 +02:00
root_module = dict_root_path[module]
2015-05-26 17:40:39 +02:00
tags = join(root_module.abs, "tags")
2015-07-03 13:37:10 +02:00
str_depend = " ".join(d_irp[path_module]["l_depend"])
2015-07-16 15:20:31 +02:00
tree = join(path_module.abs, "tree_dependency.png")
2015-06-24 12:25:19 +02:00
2015-07-03 13:37:10 +02:00
l_string = ["build {0}: build_readme {1} {2} {3}".format(path_readme,
tags,
str_depend,
tree),
2015-06-24 11:33:27 +02:00
" module_root = {0}".format(root_module.abs),
2015-06-11 11:58:26 +02:00
" module_abs = {0}".format(path_module.abs),
" module_rel = {0}".format(path_module.rel), ""]
2015-05-26 17:40:39 +02:00
return l_string
2015-05-18 10:09:45 +02:00
# _
# |_) o ._ _. ._
# |_) | | | (_| | \/
# /
2015-06-04 16:40:00 +02:00
def get_binaries(path_module):
2015-06-02 15:46:01 +02:00
"""
2015-07-28 16:45:45 +02:00
Return the list of binaries
(Path= namedtuple('Path', ['abs', 'rel']) for a module
2015-06-02 15:46:01 +02:00
"""
2015-05-18 10:09:45 +02:00
import subprocess
try:
cmd = 'grep -l "program" {0}/*.irp.f'.format(path_module.abs)
2015-06-02 18:40:24 +02:00
process = subprocess.Popen([cmd],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
except OSError:
2015-05-18 10:09:45 +02:00
return []
else:
2015-06-02 15:46:01 +02:00
if not stdout:
return []
elif "No such file or directory" not in stdout:
l_bin = [i.replace(".irp.f", "", 1) for i in stdout.split()]
2015-06-17 19:00:31 +02:00
return [Path(os.path.realpath(bin_), os.path.basename(bin_)) for bin_ in l_bin]
else:
return []
2015-05-18 10:09:45 +02:00
2015-06-04 16:40:00 +02:00
def get_dict_binaries(l_module, mode="production"):
2015-06-02 15:46:01 +02:00
"""
2015-06-02 18:17:59 +02:00
Return a dict [module] = list_binaries
2015-07-25 00:18:19 +02:00
If the production mode is enabled, return header modules
which will produce all binaries
2015-05-27 11:02:13 +02:00
2015-06-02 15:46:01 +02:00
Example : The module Full_CI can produce the binary SCF
2015-06-04 12:09:28 +02:00
so you dont need to compile at all the module Hartree-Fock
2015-06-10 10:51:29 +02:00
2015-07-25 00:18:19 +02:00
But you need to change the path accordingly
2015-06-10 10:51:29 +02:00
Full_CI/Hartree-Fock/SCF
2015-06-02 15:46:01 +02:00
"""
2015-06-02 18:17:59 +02:00
d_binaries = defaultdict(list)
2015-05-18 10:09:45 +02:00
2015-06-02 18:17:59 +02:00
# Create d_binaries
# Ake module => binaries generated
2015-06-04 16:40:00 +02:00
for module in l_module:
l_binaries = get_binaries(module)
2015-06-02 18:17:59 +02:00
if l_binaries:
d_binaries[module] += l_binaries
2015-05-27 11:02:13 +02:00
2015-06-02 15:46:01 +02:00
if mode == "production":
dict_root = module_instance.dict_root
2015-06-10 10:51:29 +02:00
dict_root_module_path = dict_module_genelogy_path(dict_root)
2015-06-02 18:17:59 +02:00
d_binaries_condensed = defaultdict(list)
2015-06-10 10:51:29 +02:00
2015-06-02 18:17:59 +02:00
for module in d_binaries:
2015-06-10 10:51:29 +02:00
root_module = dict_root_module_path[module]
if module == root_module:
d_binaries_condensed[root_module] += d_binaries[module]
else:
2015-06-10 13:44:26 +02:00
l_binaries = []
for binaries in d_binaries[module]:
2015-06-18 11:11:14 +02:00
p_abs = real_join(QP_SRC, root_module.rel)
2015-06-17 19:00:31 +02:00
p_abs = join(p_abs, module.rel, binaries.rel)
2015-06-10 13:44:26 +02:00
p_rel = binaries.rel
p = Path(p_abs, p_rel)
l_binaries.append(p)
d_binaries_condensed[root_module] += l_binaries
2015-05-18 10:09:45 +02:00
2015-06-02 18:17:59 +02:00
d_binaries = d_binaries_condensed
2015-05-18 10:09:45 +02:00
2015-06-02 18:17:59 +02:00
return d_binaries
2015-05-18 10:09:45 +02:00
2015-06-02 18:17:59 +02:00
def ninja_binaries_rule():
2015-06-02 15:46:01 +02:00
"""
2015-06-02 18:17:59 +02:00
Rule for creating the binaries
2015-06-02 15:46:01 +02:00
"""
2015-05-18 10:09:45 +02:00
2015-06-02 18:40:24 +02:00
# ~#~#~ #
# c m d #
# ~#~#~ #
2015-12-08 15:03:56 +01:00
l_cmd = ["cd $module_abs/IRPF90_temp", "ninja $out && for i in $out ; do [ -x $$i ] && touch $$i ; done"]
2015-05-20 16:01:41 +02:00
2015-06-02 18:40:24 +02:00
# ~#~#~#~#~#~ #
# s t r i n g #
# ~#~#~#~#~#~ #
2015-06-02 18:17:59 +02:00
l_string = ["rule build_binaries",
2015-06-02 15:46:01 +02:00
" command = {0}".format(" ; ".join(l_cmd)),
2015-06-11 11:58:26 +02:00
" pool = console",
" description = Create all the binaries from $module_rel",
""]
2015-05-20 16:01:41 +02:00
2015-06-02 15:46:01 +02:00
return l_string
2015-06-02 18:17:59 +02:00
def ninja_binaries_build(path_module, l_children, d_binaries):
2015-06-02 15:46:01 +02:00
"""
2015-06-02 18:17:59 +02:00
The binaries need the EZFIO_LIB, and the irpf90.make (aka build.ninja)
2015-06-02 15:46:01 +02:00
"""
2015-06-02 18:40:24 +02:00
# ~#~#~ #
# c m d #
# ~#~#~ #
2015-06-10 12:26:16 +02:00
ninja_module_path = join(path_module.abs, "IRPF90_temp/build.ninja")
2015-06-02 18:17:59 +02:00
l_abs_bin = [binary.abs for binary in d_binaries[path_module]]
2015-06-02 15:46:01 +02:00
2015-06-02 18:40:24 +02:00
# ~#~#~#~#~#~ #
# s t r i n g #
# ~#~#~#~#~#~ #
2015-06-02 18:17:59 +02:00
l_string = ["build {0}: build_binaries {1} {2}".format(" ".join(l_abs_bin),
EZFIO_LIB,
ninja_module_path),
2015-06-11 11:58:26 +02:00
" module_abs = {0}".format(path_module.abs),
" module_rel = {0}".format(path_module.rel), ""]
2015-05-20 16:01:41 +02:00
return l_string
def ninja_module_build(path_module, d_binaries):
l_abs_bin = [binary.abs for binary in d_binaries[path_module]]
path_readme = os.path.join(path_module.abs, "README.rst")
path_png = os.path.join(path_module.abs, "tree_dependency.png")
l_string = ["build module_{0}: phony {1} {2} {3}".format(path_module.rel,
" ".join(l_abs_bin),
path_readme,
path_png), ""]
2015-06-10 15:46:04 +02:00
2015-05-20 16:01:41 +02:00
return l_string
2015-06-02 15:46:01 +02:00
2015-06-02 18:17:59 +02:00
# ___
# | ._ _ _ _| _ ._ _ ._ _| _ ._ _ o _ _
# | | (/_ (/_ (_| (/_ |_) (/_ | | (_| (/_ | | (_ | (/_ _>
# |
def ninja_dot_tree_rule():
"""
Rule for creating the binaries
"""
2015-06-02 18:40:24 +02:00
# ~#~#~ #
# c m d #
# ~#~#~ #
2015-06-11 11:58:26 +02:00
l_cmd = ["cd $module_abs", "module_handler.py create_png"]
2015-06-02 18:17:59 +02:00
2015-06-10 13:44:26 +02:00
l_string = [
"rule build_dot_tree", " command = {0}".format(" ; ".join(l_cmd)),
" generator = 1",
2015-10-19 17:28:08 +02:00
" description = Generating Png representation of the Tree Dependencies of $module_rel",
2015-06-10 13:44:26 +02:00
""
]
2015-06-02 18:17:59 +02:00
return l_string
2015-06-24 10:42:53 +02:00
def ninja_dot_tree_build(path_module, l_module):
2015-06-02 18:17:59 +02:00
2015-06-02 18:40:24 +02:00
path_tree = join(path_module.abs, "tree_dependency.png")
2015-06-24 12:25:19 +02:00
l_dep = [join(path.abs, "NEEDED_CHILDREN_MODULES") for path in l_module]
2015-06-24 10:42:53 +02:00
l_string = ["build {0}: build_dot_tree {1}".format(path_tree, " ".join(l_dep)),
2015-06-11 11:58:26 +02:00
" module_abs = {0}".format(path_module.abs),
" module_rel = {0}".format(path_module.rel), ""]
2015-06-02 18:17:59 +02:00
return l_string
2015-06-11 11:58:26 +02:00
2015-06-10 15:46:04 +02:00
#
# |\/| _ _| | _
# | | (_) (_| |_| | (/_
#
2015-07-28 16:45:45 +02:00
def save_subninja_file(path_module):
2015-07-16 17:36:38 +02:00
l_string = ["builddir = {0}".format(os.path.dirname(ROOT_BUILD_NINJA)),
2015-06-22 10:04:22 +02:00
""]
2015-06-10 18:16:29 +02:00
2015-07-16 17:36:38 +02:00
l_string += ["rule update_build_ninja_root",
" command = {0} update".format(__file__),
""]
2015-06-10 18:16:29 +02:00
l_string += ["rule make_local_binaries",
" command = ninja -f {0} module_{1}".format(ROOT_BUILD_NINJA, path_module.rel),
" pool = console",
2015-06-10 18:16:29 +02:00
" description = Compile only {0}".format(path_module.rel),
""]
2015-06-10 15:46:04 +02:00
l_string += ["rule make_all_binaries",
2015-06-11 11:58:26 +02:00
" command = ninja -f {0}".format(ROOT_BUILD_NINJA),
" pool = console",
" description = Compiling all modules",
2015-06-11 11:58:26 +02:00
""]
l_string += ["rule make_clean",
" command = module_handler.py clean {0}".format(path_module.rel),
2015-06-11 11:58:26 +02:00
" description = Cleaning module {0}".format(path_module.rel),
""]
2015-06-10 15:46:04 +02:00
2015-06-11 11:58:26 +02:00
l_string += ["build dummy_target: update_build_ninja_root", "",
"build all: make_all_binaries dummy_target", "",
2015-06-30 10:10:38 +02:00
"build local: make_local_binaries dummy_target", "",
2015-06-11 11:58:26 +02:00
"default local", "", "build clean: make_clean dummy_target",
""]
2015-06-10 15:46:04 +02:00
path_ninja_cur = join(path_module.abs, "build.ninja")
with open(path_ninja_cur, "w") as f:
2015-06-11 11:58:26 +02:00
f.write(header)
f.write("\n".join(l_string))
2015-07-16 11:39:52 +02:00
def create_build_ninja_global():
2015-07-16 17:36:38 +02:00
l_string = ["builddir = {0}".format(os.path.dirname(ROOT_BUILD_NINJA)),
""]
2015-06-11 11:58:26 +02:00
l_string = ["rule update_build_ninja_root",
2015-06-22 10:04:22 +02:00
" command = {0} update".format(__file__),
""]
2015-06-11 11:58:26 +02:00
l_string += ["rule make_all",
2015-06-11 11:58:26 +02:00
" command = ninja -f {0}".format(ROOT_BUILD_NINJA),
2015-07-24 11:23:54 +02:00
" pool = console", " description = Compiling all modules",
2015-06-11 11:58:26 +02:00
""]
l_string += ["rule make_clean",
2015-07-16 11:39:52 +02:00
" command = module_handler.py clean --all",
2015-06-11 11:58:26 +02:00
" description = Cleaning all modules", ""]
2016-09-23 10:01:26 +02:00
l_string += ["rule make_ocaml",
" command = make -C {0}/ocaml".format(QP_ROOT),
" pool = console",
" description = Compiling OCaml tools",
""]
2015-06-11 11:58:26 +02:00
l_string += ["build dummy_target: update_build_ninja_root",
2016-09-25 23:28:25 +02:00
"build ocaml_target: make_ocaml all",
2015-06-11 11:58:26 +02:00
"",
2016-09-25 23:28:25 +02:00
"build all: make_all dummy_target",
"default ocaml_target",
2015-06-11 11:58:26 +02:00
"",
2015-06-30 10:10:38 +02:00
"build clean: make_clean dummy_target",
2015-06-11 11:58:26 +02:00
"", ]
path_ninja_cur = join(QP_ROOT, "build.ninja")
with open(path_ninja_cur, "w") as f:
f.write(header)
2015-06-10 15:46:04 +02:00
f.write("\n".join(l_string))
2015-06-02 18:17:59 +02:00
#
# |\/| _. o ._
# | | (_| | | |
#
2015-05-18 10:09:45 +02:00
if __name__ == "__main__":
2015-06-02 15:46:01 +02:00
arguments = docopt(__doc__)
2015-06-10 15:46:04 +02:00
pickle_path = os.path.join(QP_ROOT, "config", "qp_create_ninja.pickle")
if arguments["update"]:
with open(pickle_path, 'rb') as handle:
arguments = pickle.load(handle)
elif arguments["create"]:
2015-06-11 11:58:26 +02:00
arguments["<config_file>"] = os.path.realpath(arguments["<config_file>"])
2015-06-10 15:46:04 +02:00
with open(pickle_path, 'wb') as handle:
pickle.dump(arguments, handle)
2015-06-11 11:58:26 +02:00
pwd_config_file = arguments["<config_file>"]
2015-06-02 15:46:01 +02:00
# _
# |_ ._ _. ._ o _. |_ | _ _
# |_ | | \/ \/ (_| | | (_| |_) | (/_ _>
#
l_string = ninja_create_env_variable(pwd_config_file)
2015-05-27 11:02:13 +02:00
# _
# |_) | _
# | \ |_| | (/_
#
l_string += ninja_ezfio_cfg_rule()
2015-05-18 10:09:45 +02:00
l_string += ninja_symlink_rule()
l_string += ninja_irpf90_make_rule()
2015-05-26 17:40:39 +02:00
l_string += ninja_readme_rule()
2015-06-25 16:52:08 +02:00
l_string += ninja_gitignore_rule()
2015-06-02 18:17:59 +02:00
l_string += ninja_binaries_rule()
2015-05-21 11:30:15 +02:00
l_string += ninja_ezfio_config_rule()
l_string += ninja_ezfio_rule()
2015-05-18 10:09:45 +02:00
2015-06-02 18:17:59 +02:00
l_string += ninja_dot_tree_rule()
# _
# |_) o | _| _ _ ._ _ ._ _. |
# |_) |_| | | (_| (_| (/_ | | (/_ | (_| |
# _|
l_module_with_ezfio_cfg = get_l_module_with_ezfio_cfg()
l_util = get_children_of_ezfio_cfg(l_module_with_ezfio_cfg)
l_ezfio_config = get_l_ezfio_config()
2015-05-18 10:09:45 +02:00
l_string += ninja_ezfio_cfg_build(l_util)
l_string += ninja_ezfio_config_build(l_ezfio_config)
l_string += ninja_ezfio_build(l_ezfio_config, l_util)
2015-05-18 10:09:45 +02:00
# _ _
# |_) o | _| _|_ _ ._ ._ _ _ _| | _
# |_) |_| | | (_| | (_) | | | | (_) (_| |_| | (/_
#
#
2015-05-18 10:09:45 +02:00
2015-06-02 15:46:01 +02:00
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
# G e n e a l o g y _ d i c t #
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
2015-05-18 10:09:45 +02:00
d_genealogy = module_instance.dict_descendant
2015-06-02 15:46:01 +02:00
d_genealogy_path = dict_module_genelogy_path(d_genealogy)
2015-06-03 17:52:41 +02:00
d_irp = get_file_dependency(d_genealogy_path)
2015-05-20 16:01:41 +02:00
2015-06-24 11:38:33 +02:00
dict_root = module_instance.dict_root
dict_root_path = dict_module_genelogy_path(dict_root)
2015-07-16 10:40:57 +02:00
l_all_module = d_genealogy_path.keys()
2015-06-24 10:42:53 +02:00
2015-06-02 15:46:01 +02:00
# ~#~#~#~#~#~#~#~#~#~#~#~#~ #
# M o d u l e _ t o _ i r p #
# ~#~#~#~#~#~#~#~#~#~#~#~#~ #
2015-05-18 10:09:45 +02:00
2015-06-02 15:46:01 +02:00
if arguments["--production"]:
2015-06-10 10:51:29 +02:00
2015-07-16 10:40:57 +02:00
d_binaries = get_dict_binaries(l_all_module, mode="production")
2015-06-10 10:51:29 +02:00
l_module = d_binaries.keys()
2015-05-21 11:30:15 +02:00
2015-06-02 15:46:01 +02:00
elif arguments["--development"]:
2015-06-04 11:10:32 +02:00
2015-07-16 10:40:57 +02:00
d_binaries = get_dict_binaries(l_all_module, mode="development")
2015-06-10 10:51:29 +02:00
l_module = d_binaries.keys()
2015-06-11 16:03:05 +02:00
2015-07-16 15:20:31 +02:00
for module in l_all_module:
2015-07-16 10:40:57 +02:00
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
# d o t _ t r e e & r e a d m e #
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
l_string += ninja_dot_tree_build(module, l_all_module)
l_string += ninja_readme_build(module, d_irp, dict_root_path)
2015-07-16 15:20:31 +02:00
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
# C h e c k _ c o h e r e n c y #
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
for module in dict_root_path.values():
if module not in d_binaries:
2015-07-25 00:18:19 +02:00
l_msg = ["{0} is a root module but does not contain a main file.",
2015-07-16 15:20:31 +02:00
"- Create it in {0}",
2015-09-30 13:36:53 +02:00
"- Or delete {0} `qp_module.py uninstall {0}`",
2015-07-25 00:18:19 +02:00
"- Or install a module that needs {0} with a main "]
2015-07-16 15:20:31 +02:00
2015-07-16 15:44:51 +02:00
print "\n".join(l_msg).format(module.rel)
2015-07-16 15:20:31 +02:00
sys.exit(1)
# ~#~#~#~#~#~#~#~#~#~#~#~ #
# G l o b a l _ b u i l d #
# ~#~#~#~#~#~#~#~#~#~#~#~ #
2015-07-16 11:39:52 +02:00
create_build_ninja_global()
2015-06-10 10:51:29 +02:00
2015-07-16 15:20:31 +02:00
# ~#~#~#~#~#~#~#~#~#~#~#~ #
# C r e a t e _ r u l e s #
# ~#~#~#~#~#~#~#~#~#~#~#~ #
2015-06-10 10:51:29 +02:00
for module_to_compile in l_module:
2015-06-02 15:46:01 +02:00
# ~#~#~#~#~#~#~#~ #
# S y m l i n k #
# ~#~#~#~#~#~#~#~ #
l_children = d_genealogy_path[module_to_compile]
l_symlink = get_source_destination(module_to_compile, l_children)
l_string += ninja_symlink_build(module_to_compile, l_symlink)
# ~#~#~#~#~#~#~#~ #
# i r p . f 9 0 #
# ~#~#~#~#~#~#~#~ #
2015-06-02 18:40:24 +02:00
l_string += ninja_irpf90_make_build(module_to_compile, l_children,
d_irp)
2015-06-02 15:46:01 +02:00
2015-07-28 16:45:45 +02:00
l_string += ninja_binaries_build(module_to_compile, l_children,
d_binaries)
2015-07-28 16:45:45 +02:00
if arguments["--development"]:
l_string += ninja_module_build(module_to_compile, d_binaries)
2015-05-21 11:30:15 +02:00
l_string += ninja_gitignore_build(module_to_compile, d_binaries,
l_symlink)
2015-06-25 16:52:08 +02:00
2015-07-28 16:45:45 +02:00
save_subninja_file(module_to_compile)
2015-07-16 15:20:31 +02:00
# ~#~#~#~#~ #
# S a v e s #
# ~#~#~#~#~ #
2015-06-25 16:52:08 +02:00
2015-07-16 17:36:38 +02:00
with open(ROOT_BUILD_NINJA, "w+") as f:
2015-06-11 11:58:26 +02:00
f.write(header)
2015-06-03 17:52:41 +02:00
f.write("\n".join(l_string))