Revision ff5aeb20f7fbcffcfc463b85fea4bc01aff3ef03 authored by Jason Boutte on 10 June 2021, 02:40:47 UTC, committed by Jason Boutte on 10 June 2021, 02:40:47 UTC
1 parent a06969a
Raw File
configure
#!/usr/bin/env python

"""This script writes CIME build information to a directory.

The pieces of information that will be written include:

1. Machine-specific build settings (i.e. the "Macros" file).
2. File-specific build settings (i.e. "Depends" files).
3. Environment variable loads (i.e. the env_mach_specific files).

The .env_mach_specific.sh and .env_mach_specific.csh files are specific to a
given compiler, MPI library, and DEBUG setting. By default, these will be the
machine's default compiler, the machine's default MPI library, and FALSE,
respectively. These can be changed by setting the environment variables
COMPILER, MPILIB, and DEBUG, respectively.
"""

import os
import sys

_CIMEROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
sys.path.append(os.path.join(_CIMEROOT, "scripts", "Tools"))
sys.path.append(os.path.join(_CIMEROOT, "scripts", "lib"))

from standard_script_setup import *
from CIME.utils import expect, get_model
from CIME.BuildTools.configure import configure
from CIME.XML.machines import Machines

logger = logging.getLogger(__name__)

def parse_command_line(args):
    """Command line argument parser for configure."""
    description = __doc__
    parser = argparse.ArgumentParser(description=description)
    CIME.utils.setup_standard_logging_options(parser)

    parser.add_argument("--machine",
                        help="The machine to create build information for.")
    parser.add_argument("--machines-dir",
                        help="The machines directory to take build information "
                        "from. Overrides the CIME_MODEL environment variable, "
                        "and must be specified if that variable is not set.")
    parser.add_argument("--macros-format", action='append',
                        choices=['Makefile', 'CMake'],
                        help="The format of Macros file to generate. If "
                        "'Makefile' is passed in, a file called 'Macros.make' "
                        "is generated. If 'CMake' is passed in, a file called "
                        "'Macros.cmake' is generated. This option can be "
                        "specified multiple times to generate multiple files. "
                        "If not used at all, Macros generation is skipped. "
                        "Note that Depends files are currently always in "
                        "Makefile format, regardless of this option.")
    parser.add_argument("--output-dir", default=os.getcwd(),
                        help="The directory to write files to. If not "
                        "specified, defaults to the current working directory.")

    parser.add_argument("--compiler", "-compiler",
                        help="Specify a compiler. "
                        "To see list of supported compilers for each machine, use the utility query_config in this directory")

    parser.add_argument("--mpilib", "-mpilib",
                        help="Specify the mpilib. "
                        "To see list of supported mpilibs for each machine, use the utility query_config in this directory. "
                        "The default is the first listing in MPILIBS in config_machines.xml")

    parser.add_argument("--clean", action="store_true",
                        help="Remove old Macros and env files before attempting to create new ones")

    parser.add_argument("--comp-interface",
                        default="mct",
                        help="""The cime driver/cpl interface to use."""
                        )

    argcnt = len(args)
    args = parser.parse_args()
    CIME.utils.parse_args_and_handle_standard_logging_options(args)

    opts = {}
    if args.machines_dir is not None:
        machines_file = os.path.join(args.machines_dir, "config_machines.xml")
        machobj = Machines(infile=machines_file, machine=args.machine)
    else:
        model = get_model()
        if model is not None:
            machobj = Machines(machine=args.machine)
        else:
            expect(False, "Either --mach-dir or the CIME_MODEL environment "
                   "variable must be specified!")

    opts['machobj'] = machobj

    if args.macros_format is None:
        opts['macros_format'] = []
    else:
        opts['macros_format'] = args.macros_format

    expect(os.path.isdir(args.output_dir),
           "Output directory '%s' does not exist." % args.output_dir)

    opts['output_dir'] = args.output_dir

    # Set compiler.
    if args.compiler is not None:
        compiler = args.compiler
    elif "COMPILER" in os.environ:
        compiler = os.environ["COMPILER"]
    else:
        compiler = machobj.get_default_compiler()
        os.environ["COMPILER"] = compiler
    expect(opts['machobj'].is_valid_compiler(compiler),
           "Invalid compiler vendor given in COMPILER environment variable: %s"
           % compiler)
    opts['compiler'] = compiler
    opts['os'] = machobj.get_value('OS')
    opts['comp_interface'] = args.comp_interface

    if args.clean:
        files = ["Macros.make", "Macros.cmake", "env_mach_specific.xml", ".env_mach_specific.sh",
                 ".env_mach_specific.csh", "Depends.%s"%compiler, "Depends.%s"%args.machine,
                  "Depends.%s.%s"%(args.machine,compiler)]
        for file_ in files:
            if os.path.isfile(file_):
                logger.warn("Removing file %s"%file_)
                os.remove(file_)
        if argcnt == 2:
            opts['clean_only'] = True
            return opts

    # Set MPI library.
    if args.mpilib is not None:
        mpilib = args.mpilib
    elif "MPILIB" in os.environ:
        mpilib = os.environ["MPILIB"]
    else:
        mpilib = machobj.get_default_MPIlib(attributes={"compiler":compiler})
        os.environ["MPILIB"] = mpilib

    expect(opts['machobj'].is_valid_MPIlib(mpilib, attributes={"compiler":compiler}),
           "Invalid MPI library name given in MPILIB environment variable: %s" %
           mpilib)
    opts['mpilib'] = mpilib

    # Set DEBUG flag.
    if "DEBUG" in os.environ:
        expect(os.environ["DEBUG"].lower() in ('true', 'false'),
               "Invalid DEBUG environment variable value (must be 'TRUE' or "
               "'FALSE'): %s" % os.environ["DEBUG"])
        debug = os.environ["DEBUG"].lower() == "true"
    else:
        debug = False
        os.environ["DEBUG"] = "FALSE"
    opts['debug'] = debug

    return opts

def _main():
    opts = parse_command_line(sys.argv)
    if "clean_only" not in opts or not opts["clean_only"]:
        configure(opts['machobj'], opts['output_dir'], opts['macros_format'],
                  opts['compiler'], opts['mpilib'], opts['debug'], opts['comp_interface'], opts['os'])

if __name__ == "__main__":
    _main()
back to top