#!/usr/bin/env python3

# This file is autogenerated by Autocmake v1.0.0 http://autocmake.org
# Copyright (c) 2015-2023 by Radovan Bast, Roberto Di Remigio, Jonas Juselius, and contributors.

options = """
Usage:
  ./setup [options] [<builddir>]
  ./setup (-h | --help)

Options:
  --fc=<FC>                              Fortran compiler [default: gfortran].
  --extra-fc-flags=<EXTRA_FCFLAGS>       Extra Fortran compiler flags [default: ''].
  --cc=<CC>                              C compiler [default: gcc].
  --extra-cc-flags=<EXTRA_CFLAGS>        Extra C compiler flags [default: ''].
  --cxx=<CXX>                            C++ compiler [default: g++].
  --extra-cxx-flags=<EXTRA_CXXFLAGS>     Extra C++ compiler flags [default: ''].
  --add-definitions=<STRING>             Add preprocesor definitions [default: ''].
  --python=<PYTHON_INTERPRETER>          The Python interpreter (development version) to use. [default: ''].
  --blas=<BLAS>                          Detect and link BLAS library (auto or off) [default: auto].
  --lapack=<LAPACK>                      Detect and link LAPACK library (auto or off) [default: auto].
  --mkl=<MKL>                            Pass MKL flag to the Intel compiler and linker and skip BLAS/LAPACK detection (sequential, parallel, cluster, or off) [default: off].
  --mpi                                  Enable MPI parallelization [default: False].
  --coverage                             Enable code coverage [default: False].
  --static                               Enable static linking [default: False].
  --profile                              Enable profiling [default: False]
  --check                                Enable runtime checking of compiled executables [default: False]
  --int64                                Enable 64bit integers [default: False].
  --explicit-libs=<LIBS>                 Explicit linker specification for extra libraries passed directly to the linker [default: off].
  --exatensor=<ENABLE_EXATENSOR>         Toggle use of ExaTensor <ON/OFF> [default: ON].
  --pcmsolver=<ENABLE_PCMSOLVER>         Toggle use of PCMSolver <ON/OFF> [default: ON].
  --pcmsolver-dir=<PCMSOLVER_ROOT>       The PCMSolver module location. [default: ''].
  --type=<TYPE>                          Set the CMake build type (debug, release, relwithdebinfo, minsizerel) [default: release].
  --generator=<STRING>                   Set the CMake build system generator [default: Unix Makefiles].
  --show                                 Show CMake command and exit.
  --cmake-executable=<CMAKE_EXECUTABLE>  Set the CMake executable [default: cmake].
  --cmake-options=<STRING>               Define options to CMake [default: ''].
  --prefix=<PATH>                        Set the install path for make install.
  <builddir>                             Build directory.
  -h --help                              Show this screen.
"""

import os
import sys

if sys.version_info < (3, 0):
    sys.stderr.write(options)
    sys.stderr.write('\nERROR: Python 3 is required to build and run DIRAC\n')
    sys.exit(1)

sys.path.insert(0, 'cmake')
from autocmake import configure
from autocmake.external import docopt


def gen_cmake_command(options, arguments):
    """
    Generate CMake command based on options and arguments.
    """
    command = []
    command.append(arguments['--cmake-executable'])
    command.append('-DCMAKE_Fortran_COMPILER={0} -DEXTRA_FCFLAGS="{1}"'.format(arguments['--fc'], arguments['--extra-fc-flags']))
    command.append('-DCMAKE_C_COMPILER={0} -DEXTRA_CFLAGS="{1}"'.format(arguments['--cc'], arguments['--extra-cc-flags']))
    command.append('-DCMAKE_CXX_COMPILER={0} -DEXTRA_CXXFLAGS="{1}"'.format(arguments['--cxx'], arguments['--extra-cxx-flags']))
    command.append('-DPREPROCESSOR_DEFINITIONS="{0}"'.format(arguments['--add-definitions']))
    command.append('-DPYTHON_INTERPRETER="{0}"'.format(arguments['--python']))
    command.append('-DENABLE_BLAS={0}'.format(arguments['--blas']))
    command.append('-DENABLE_LAPACK={0}'.format(arguments['--lapack']))
    command.append('-DMKL_FLAG={0}'.format(arguments['--mkl']))
    command.append('-DMATH_LIB_SEARCH_ORDER="MKL;ESSL;OPENBLAS;ATLAS;ACML;SYSTEM_NATIVE"')
    command.append('-DBLAS_LANG=Fortran')
    command.append('-DLAPACK_LANG=Fortran')
    command.append('-DENABLE_MPI={0}'.format(arguments['--mpi']))
    command.append('-DENABLE_OPENMP=True')
    command.append('-DENABLE_CODE_COVERAGE={0}'.format(arguments['--coverage']))
    command.append('-DENABLE_STATIC_LINKING={0}'.format(arguments['--static']))
    command.append('-DENABLE_PROFILING={0}'.format(arguments['--profile']))
    command.append('-DENABLE_RUNTIMECHECK={0}'.format(arguments['--check']))
    command.append('-DENABLE_64BIT_INTEGERS={0}'.format(arguments['--int64']))
    command.append('-DEXPLICIT_LIBS="{0}"'.format(arguments['--explicit-libs'].strip()))
    command.append('-DENABLE_EXATENSOR={0}'.format(arguments['--exatensor']))
    command.append('-DENABLE_PCMSOLVER={0}'.format(arguments['--pcmsolver']))
    command.append('-DPCMSOLVER_ROOT={0}'.format(arguments['--pcmsolver-dir']))
    command.append('-DCMAKE_BUILD_TYPE={0}'.format(arguments['--type']))
    command.append('-G"{0}"'.format(arguments['--generator']))
    if arguments['--cmake-options'] != "''":
        command.append(arguments['--cmake-options'])
    if arguments['--prefix']:
        command.append('-DCMAKE_INSTALL_PREFIX="{0}"'.format(arguments['--prefix']))

    return ' '.join(command)


# parse command line args
try:
    arguments = docopt.docopt(options, argv=None)
except docopt.DocoptExit:
    sys.stderr.write('ERROR: bad input to {0}\n'.format(sys.argv[0]))
    sys.stderr.write(options)
    sys.exit(-1)


# use extensions to validate/post-process args
if configure.module_exists('extensions'):
    import extensions
    arguments = extensions.postprocess_args(sys.argv, arguments)


root_directory = os.path.dirname(os.path.realpath(__file__))


build_path = arguments['<builddir>']


# create cmake command
cmake_command = '{0} -H{1}'.format(gen_cmake_command(options, arguments), root_directory)


# run cmake
configure.configure(root_directory, build_path, cmake_command, arguments)
