1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
#!/usr/bin/env python3
#
# Copyright (C) 2006-2022 ABINIT Group (Yann Pouillon)
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#
from __future__ import print_function, division, absolute_import #, unicode_literals
try:
from ConfigParser import ConfigParser,NoOptionError
except ImportError:
from configparser import ConfigParser,NoOptionError
from time import gmtime,strftime
try:
from commands import getoutput
except:
from subprocess import getoutput
import os
import re
import sys
class MyConfigParser(ConfigParser):
def optionxform(self,option):
return str(option)
# ---------------------------------------------------------------------------- #
#
# Subroutines
#
# Macro header
def macro_header(name,stamp):
return """# Generated by %s on %s
#
# Environment variables relevant to ABINIT
#
#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. Any change will systematically be overwritten.
#
""" % (name,stamp,name)
# Autoconf macro template
def abi_macro_autoconf():
return """
# ABI_ENV_AC_UPDATE(LANG)
# -----------------------
#
# Updates Autoconf build environment.
#
AC_DEFUN([ABI_ENV_AC_UPDATE],[
dnl Please note that the existing environment will always be overwritten.
dnl Thus you should save sensistive data before calling this macro.
dnl Check arguments
m4_if([$1], [tmp_lang="$1"], [tmp_lang="Fortran"])dnl
dnl Update linker flags
case "$1" in
C)
LDFLAGS="${CC_LDFLAGS}"
;;
C++)
LDFLAGS="${CXX_LDFLAGS}"
;;
Fortran)
LDFLAGS="${FC_LDFLAGS}"
;;
esac
dnl Update linker additional libs
LIBS="${CC_LIBS}"
CXXLIBS="${CXX_LIBS}"
FCLIBS="${FC_LIBS}"
unset tmp_lang
]) # ABI_ENV_AC_UPDATE
"""
# Define macro template
def abi_macro_init():
return """
# ABI_ENV_INIT()
# --------------
#
# Declares ABINIT environment variables and initializes the internal
# variables.
#
AC_DEFUN([ABI_ENV_INIT],[
@MACRO@
]) # ABI_ENV_INIT
"""
# Backup macro template
def abi_macro_backup():
return """
# ABI_ENV_BACKUP()
# ----------------
#
# Saves all ABINIT environment variables.
#
AC_DEFUN([ABI_ENV_BACKUP],[
dnl All variables will be saved, yet please note that they may be
dnl conditionally restored (see ABI_ENV_RECALL for details).
@MACRO@
]) # ABI_ENV_BACKUP
"""
# Recall macro template
def abi_macro_recall():
return """
# ABI_ENV_RECALL()
# ----------------
#
# Restores all previously-saved non-empty ABINIT environment variables.
#
AC_DEFUN([ABI_ENV_RECALL],[
dnl The following ensures that non-empty environment variables always override
dnl what is read from the config files.
@MACRO@
]) # ABI_ENV_RECALL
"""
# Restore macro template
def abi_macro_restore():
return """
# ABI_ENV_RESTORE()
# -----------------
#
# Restores all previously-saved ABINIT environment variables.
#
AC_DEFUN([ABI_ENV_RESTORE],[
@MACRO@
]) # ABI_ENV_RESTORE
"""
# ---------------------------------------------------------------------------- #
#
# Main program
#
# Initial setup
my_name = "make-macros-env"
my_configs = ["config/specs/environment.conf"]
my_output = "config/m4/auto-env.m4"
# Check if we are in the top of the ABINIT source tree
if ( not os.path.exists("configure.ac") or
not os.path.exists("src/98_main/abinit.F90") ):
print("%s: You must be in the top of an ABINIT source tree." % my_name)
print("%s: Aborting now." % my_name)
sys.exit(1)
# Read config file(s)
for cnf_file in my_configs:
if ( not os.path.exists(cnf_file) ):
print("%s: Could not find config file (%s)." % (my_name,cnf_file))
print("%s: Aborting now." % my_name)
sys.exit(2)
# What time is it?
now = strftime("%Y/%m/%d %H:%M:%S +0000",gmtime())
# Init
cnf = MyConfigParser()
cnf.read(my_configs[0])
abinit_env_vars = [item for item in cnf.sections() \
if not cnf.get(item, "status") in ["dropped", "removed"]]
abinit_env_vars.sort()
# Start writing macro
m4 = open(my_output, "wt")
m4.write(macro_header(my_name,now))
# Write Autoconf macro
m4.write(abi_macro_autoconf())
# Process environment variables for init
src = ""
for var in abinit_env_vars:
dsc = cnf.get(var,"description")
try:
val = cnf.get(var,"value")
except NoOptionError:
val = ""
src += "\n dnl %s\n" % (dsc)
if ( cnf.get(var,"declare") == "yes" ):
src += " AC_ARG_VAR([%s],\n [%s])\n" % (var,dsc)
if ( cnf.get(var,"reset") == "yes" ):
src += " %s='%s'\n" % (var,val)
if ( cnf.get(var,"substitute") == "yes" ):
src += " AC_SUBST(%s)\n" % (var)
# Write init macro
m4.write(re.sub("@MACRO@",src,abi_macro_init()))
# Process environment variables for backup
src = ""
for var in abinit_env_vars:
src += "\n dnl Save %s\n" % (cnf.get(var,"description"))
src += " abi_env_%s=\"${%s}\"\n" % (var,var)
# Write backup macro
m4.write(re.sub("@MACRO@",src,abi_macro_backup()))
# Process environment variables for recall
src = ""
for var in abinit_env_vars:
src += "\n dnl Recall %s\n" % (cnf.get(var,"description"))
src += " if test \"${abi_env_%s}\" != \"\"; then\n" % (var) \
+ " test \"${%s}\" != \"${abi_env_%s}\" && \\\n" % (var,var) \
+ " AC_MSG_NOTICE([overriding configuration of %s from environment])\n" % (var) \
+ " %s=\"${abi_env_%s}\"\n" % (var,var) \
+ " fi\n"
# Write recall macro
m4.write(re.sub("@MACRO@",src,abi_macro_recall()))
# Process environment variables for restore
src = ""
for var in abinit_env_vars:
src += "\n dnl Restore %s\n" % (cnf.get(var,"description"))
src += " %s=\"${abi_env_%s}\"\n" % (var,var)
# Write restore macro
m4.write(re.sub("@MACRO@",src,abi_macro_restore()))
# Finish
m4.close()
tmp = getoutput("./config/scripts/add-header-typed Autoconf %s" % (my_output))
if ( tmp != "" ):
print(tmp)
# Write environment dumper (for debugging)
dumper = open("config.dump.in","a")
dumper.write("# Environment variables (script: %s)\n" % (my_name))
for var in abinit_env_vars:
dumper.write("%s=\"@%s@\"\n" % (var,var))
dumper.write("\n")
dumper.close()
|