diff --git a/scripts/compilation/create_ninja_build.py b/scripts/compilation/create_ninja_build.py new file mode 100755 index 00000000..9a313e3c --- /dev/null +++ b/scripts/compilation/create_ninja_build.py @@ -0,0 +1,266 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os + +from os.path import join + +from module_handler import module_genealogy + +qpackage_root = os.environ['QPACKAGE_ROOT'] +qpackage_root_src = join(qpackage_root, 'src') + + +# _ +# |\/| _. | _ _|_ o | _ _| _ ._ ._ _| +# | | (_| |< (/_ | | | (/_ o (_| (/_ |_) | | (_| +# +def ninja_makefile_depend_rule(): + # Rule + l_string = ["rule build_makefile.depend"] + l_string += [ + " command = module_handler.py save_makefile_depend $module/NEEDED_CHILDREN_MODULES"] + l_string += [""] + + return l_string + + +def ninja_makefile_depend_build(l_all_needed_molule, path_module): + l_makefile = [join(qpackage_root_src, i, "Makefile") + for i in l_all_needed_molule] + + # Build + path_mkdepend = join(path_module.abs, "Makefile.depend") + str_l_makefile = " ".join(l_makefile) + + l_string = ["build {0}: build_makefile.depend {1}".format(path_mkdepend, + str_l_makefile)] + + l_string += [" module = {0}".format(path_module.abs)] + l_string += [""] + + return l_string + + +# _ __ _ ___ _ _ +# |_ / |_ | / \ _ _|_ _ +# |_ /_ | _|_ \_/ o (_ | (_| +# _| +def get_l_ezfio_irp(l_all_needed_molule, path_module): + + l_module_abs = [join(qpackage_root_src, m) for m in l_all_needed_molule] + + l_irp = [] + l_ezfio_module_abs = [] + + for m in l_module_abs + [path_module.abs]: + + for file in os.listdir(m): + if file.endswith(".irp.f"): + l_irp.append(join(m, file)) + if file == "EZFIO.cfg": + l_ezfio_module_abs.append(m) + l_irp.append(join(m, "ezfio_interface.irp.f")) + + return l_irp, l_ezfio_module_abs + + +def ninja_ezfio_cfg_rule(): + # Rule + l_string = ["rule build_ezfio_interface"] + l_string += [ + " command = cd $sub_module ; ei_handler.py ; cd -"] + l_string += [""] + + return l_string + + +def ninja_ezfio_cfg_build(l_ezfio_module_abs): + # Build + l_string = [] + for m in l_ezfio_module_abs: + ez_interface = join(m, "ezfio_interface.irp.f") + ez_cfg = join(m, "EZFIO.cfg") + + l_string += ["build {0}: build_ezfio_interface {1}".format(ez_interface, + ez_cfg)] + l_string += [" sub_module = {0}".format(m)] + l_string += [""] + + return l_string + + +# __ +# (_ ._ _ | o ._ | +# __) \/ | | | | | | | |< +# / +def get_source_destination(l_all_needed_molule, path_module): + l_all_needed_molule_include = l_all_needed_molule + ["include"] + + l_source = [join(qpackage_root_src, m) + for m in l_all_needed_molule_include] + l_destination = [join(qpackage_root_src, path_module.rel, m) + for m in l_all_needed_molule_include] + + return l_source, l_destination + + +def ninja_symlink_rule(): + # Rule + l_string = ["rule build_symlink"] + l_string += [" command = ln -s $module_source $module_destination"] + l_string += [""] + + return l_string + + +def ninja_symlink_build(l_source, l_destination): + # Rule + l_string = [] + for source, destination in zip(l_source, l_destination): + l_string += ["build {0}: build_symlink".format(destination)] + l_string += [" module_source = {0}".format(source)] + l_string += [" module_destination = {0}".format(destination)] + l_string += [""] + + return l_string + + +# _ _ _ +# o ._ ._ _|_ (_| / \ ._ _ _. | _ +# | | |_) | | \_/ o | | | (_| |< (/_ +# | +def ninja_irpf90_make_rule(): + # Rule + l_string = ["rule build_irpf90.make"] + l_string += [" command = cd $module ; irpf90 $include_dir $irpf90_flag ; cd -"] + l_string += [""] + + return l_string + + +def ninja_irpf90_make_build(path_module, + l_all_needed_molule_include, + l_irp, + l_destination): + + path_irpf90_make = join(path_module.abs, "irpf90.make") + str_l_irp_need = " ".join(l_irp) + str_l_destination = " ".join(l_destination) + path_makefiledepend = join(path_module.abs, "Makefile.depend") + + str_depend = "{0} {1} {2}".format(str_l_irp_need, + path_makefiledepend, + str_l_destination) + + # Build + l_string = ["build {0}: build_irpf90.make {1}".format(path_irpf90_make, + str_depend)] + l_string += [" module = {0}".format(path_module.abs)] + + # Option + str_include_dir = " ".join(["-I {0}".format(m) + for m in l_all_needed_molule_include]) + + l_string += [" include_dir = {0}".format(str_include_dir)] + l_string += [" irpf90_flag = {0}".format("--align=32 --openmp")] + l_string += [""] + + return l_string + + +# _ +# |_) o ._ _. ._ +# |_) | | | (_| | \/ +# / +def get_program(path_module): + import subprocess + + try: + cmd = 'grep -l "program" {0}/*.irp.f'.format(path_module.abs) + p = subprocess.check_output([cmd], shell=True) + except subprocess.CalledProcessError: + return [] + else: + return [os.path.basename(f).split(".")[0] for f in p.split()] + + +def ninja_binary_rule(): + + # Rule + l_string = ["rule build_binary"] + l_string += [" command = cd $module ; make $binary ; touch $binary; cd -"] + l_string += [""] + + return l_string + + +def ninja_binary_build(l_bin, path_module): + + # Build + irpf90mk_path = join(path_module.abs, "irpf90.make") + + l_abs_bin = [join(path_module.abs, binary) for binary in l_bin] + + l_string = [] + for path, abs_path in zip(l_bin, l_abs_bin): + l_string += ["build {0}: build_binary {1}".format(abs_path, + irpf90mk_path)] + l_string += [" module = {0}".format(path_module.abs)] + l_string += [" binary = {0}".format(path)] + + l_string += [""] + + str_l_abs_bin = " ".join(l_abs_bin) + + l_string += ["build build_all_binary_{0}: phony {1}".format(path_module.rel, + str_l_abs_bin)] + + return l_string + +if __name__ == "__main__": + + with open(join(qpackage_root_src, "NEEDED_MODULES"), "r") as f: + l_module_to_compile = f.read().split() + + l_string = ninja_makefile_depend_rule() + l_string += ninja_ezfio_cfg_rule() + l_string += ninja_symlink_rule() + l_string += ninja_irpf90_make_rule() + l_string += ninja_binary_rule() + + from collections import namedtuple + + for module_to_consider in ["Hartree_Fock", "AOs"]: #l_module_to_compile: + + Path = namedtuple('Path', ['abs', 'rel']) + + path_module = Path(join(qpackage_root_src, module_to_consider), + module_to_consider) + + path_neeeded_module = join(path_module.abs, "NEEDED_CHILDREN_MODULES") + l_all_needed_molule = module_genealogy(path_neeeded_module) + + # Make.depend rule and build + l_string += ninja_makefile_depend_build(l_all_needed_molule, path_module) + + # EZFIO.cfg rule and build + l_irp, l_ezfio_module_abs = get_l_ezfio_irp(l_all_needed_molule, path_module) + l_string += ninja_ezfio_cfg_build(l_ezfio_module_abs) + + # Symlink rule and build + l_source, l_destination = get_source_destination(l_all_needed_molule, + path_module) + l_string += ninja_symlink_build(l_source, l_destination) + + # irpf90.make + + l_string += ninja_irpf90_make_build(path_module, + l_all_needed_molule + ["include"], + l_irp, + l_destination) + # ninja_binary + l_binary = get_program(path_module) + l_string += ninja_binary_build(l_binary, path_module) + + print "\n".join(l_string) diff --git a/scripts/module/module_handler.py b/scripts/module/module_handler.py index fa02d407..91c8f6f5 100755 --- a/scripts/module/module_handler.py +++ b/scripts/module/module_handler.py @@ -8,7 +8,7 @@ of a NEEDED_CHILDREN_MODULES file Usage: module_handler.py print_genealogy [] - module_handler.py save_makefile_depend + module_handler.py save_makefile_depend [] module_handler.py create_symlink [] module_handler.py create_png [] @@ -162,7 +162,7 @@ def get_list_depend(l_module): return l_src, l_obj -def save_makefile_depend(l_src, l_obj): +def save_makefile_depend(l_src, l_obj, dir_): header = "# This file was created by the module_handler.py script. Do not modify it by hand." try: @@ -178,7 +178,7 @@ def save_makefile_depend(l_src, l_obj): "\n"]) if output != old_output: - with open("Makefile.depend", "w+") as f: + with open(os.path.join(dir_,"Makefile.depend"), "w+") as f: f.write(output) @@ -237,35 +237,33 @@ if __name__ == '__main__': if not arguments['']: dir_ = os.getcwd() - path = os.path.join(dir_, "NEEDED_CHILDREN_MODULES") + path_file = os.path.join(dir_, "NEEDED_CHILDREN_MODULES") else: - path = os.path.abspath(arguments['']) - path = os.path.expanduser(path) - path = os.path.expandvars(path) + path_file = os.path.abspath(arguments['']) + path_file = os.path.expanduser(path_file) + path_file = os.path.expandvars(path_file) + dir_ = os.path.dirname(path_file) if arguments['print_genealogy']: - l_all_needed_molule = module_genealogy(path) + l_all_needed_molule = module_genealogy(path_file) print " ".join(sorted(l_all_needed_molule)) - get_list_depend(l_all_needed_molule) - if arguments['create_symlink']: src = os.getcwd() - for link_name in module_genealogy(path) + ["include"]: + for link_name in module_genealogy(path_file) + ["include"]: - source = os.path.join( - "/home/razoa/quantum_package/src/", - link_name) + source = os.path.join("/home/razoa/quantum_package/src/", + link_name) try: os.symlink(source, link_name) except OSError: pass if arguments['save_makefile_depend']: - l_all_needed_molule = module_genealogy(path) + l_all_needed_molule = module_genealogy(path_file) l_src, l_obj = get_list_depend(l_all_needed_molule) - save_makefile_depend(l_src, l_obj) + save_makefile_depend(l_src, l_obj, dir_) if arguments["create_png"]: - create_png_from_path(path) + create_png_from_path(path_file) diff --git a/src/Makefile.common b/src/Makefile.common index 049ea5b6..ea8c3af0 100644 --- a/src/Makefile.common +++ b/src/Makefile.common @@ -24,8 +24,6 @@ IRPF90+=$(patsubst %, -I %, $(INCLUDE_DIRS)) $(IRPF90_FLAGS) Makefile.depend: $(wildcard */Makefile) ${QPACKAGE_ROOT}/scripts/module/module_handler.py save_makefile_depend -# Check and update dependencies -include Makefile.depend # Create symlink symlink: $(wildcard $(QPACKAGE_ROOT)/src/*/NEEDED_CHILDREN_MODULES) @@ -44,8 +42,9 @@ ezfio_interface: symlink $(wildcard $(QPACKAGE_ROOT)/src/*/EZFIO.cfg) ${QPACKAGE_ROOT}/scripts/ezfio_interface/ei_handler.py --irpf90 --ocaml --recursif irpf90.make: $(filter-out IRPF90_temp/%, $(wildcard */*.irp.f)) $(wildcard *.irp.f) $(wildcard .inc.f) Makefile.depend Makefile $(EZFIO) $(wildcard *.py) symlink ezfio_interface - $(IRPF90) + echo $(IRPF90) +include Makefile.depend include irpf90.make # Need NEEDED_CHILDREN_MODULES and IRPMAN