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
|
#!/usr/bin/env python3
#
# Copyright (C) 2005-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
except ImportError:
from configparser import ConfigParser
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)
# ---------------------------------------------------------------------------- #
#
# Main program
#
# Initial setup
my_name = "make-doc-options"
my_configs = ["config/specs/options.conf"]
my_output = "doc/build/config-template.ac9"
# 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)
cnf = MyConfigParser()
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.read(my_configs[0])
re_en = re.compile("enable_")
re_gr = re.compile("group_")
re_wi = re.compile("with_")
all_args = cnf.sections()
all_args.sort()
ac_args = { "enable":list(), "group":list(), "with":list() }
for arg in all_args:
arg_stat = cnf.get(arg,"status")
if ( (arg_stat != "removed") and (arg_stat != "dropped") ):
if ( re_en.match(arg) ):
ac_args["enable"].append(arg)
elif ( re_gr.match(arg) ):
ac_args["group"].append(arg)
elif ( re_wi.match(arg) ):
ac_args["with"].append(arg)
else:
sys.stderr.write("%s: Warning: unknown option type\n" % (my_name))
ac_args["enable"].sort()
ac_args["group"].sort()
ac_args["with"].sort()
# Process arguments
defaults = ""
parse = ""
for arg in ("enable","with"):
m4.write("\n dnl\n dnl --%s arguments\n dnl\n" % (arg))
defaults += "\n dnl\n dnl --%s arguments\n dnl\n\n" % (arg)
for opt in ac_args[arg]:
var = re.sub(arg+"_","",opt)
opt_name = re.sub("_","-",var)
opt_desc = cnf.get(opt,"description")
opt_stat = cnf.get(opt,"status")
try:
opt_dflt = cnf.get(opt,"default")
except:
opt_dflt = None
try:
opt_nega = cnf.get(opt,"negative")
except:
opt_nega = ""
try:
opt_vals = cnf.get(opt,"values")
except:
opt_vals = None
if ( opt_stat == "hidden" ):
parse += """
dnl Hidden option: %s
ac_configure_args="${ac_configure_args} --%s=\\\"${%s}\\\""
""" % \
(opt,re.sub("_","-",opt),opt)
else:
m4.write("\n AC_ARG_%s(%s,\n" % (arg.upper(),opt_name) \
+ " AS_HELP_STRING([--%s-%s],\n [%s (default: %s)]))\n" % \
(arg,opt_name,opt_desc,opt_dflt))
if ( opt_dflt is not None ):
defaults += " if test \"${%s_%s}\" = \"\"; then\n %s_%s=\"%s\"\n fi\n" % \
(arg,var,arg,var,opt_dflt)
if ( arg == "with" ):
defaults += " if test \"${%s_%s}\" = \"no\"; then\n %s_%s=\"%s\"\n fi\n" % \
(arg,var,arg,var,opt_nega)
if ( opt_vals is not None ):
parse += "\n"+parse_opt(opt,opt_vals)
elif ( arg == "enable" ):
parse += "\n"+parse_opt(opt,"no yes")
m4.write(" AC_SUBST(%s_%s)\n" % (arg,var))
# Finish writing define macro
m4.write(macro_define_footer())
# Start writing setup macro
m4.write(macro_setup_header())
# Process arguments
m4.write(defaults)
# Finish writing setup macro
m4.write(macro_setup_footer())
# Start writing backup macro
m4.write(macro_backup_header())
# Process arguments
for arg in ("enable","with"):
m4.write("\n dnl\n dnl --%s arguments\n dnl\n" % (arg))
for opt in ac_args[arg]:
var = re.sub(arg+"_","",opt)
m4.write(" cmd_%s_%s=\"${%s_%s}\"\n" % (arg,var,arg,var))
# Do not forget "prefix"
m4.write("""
dnl
dnl Prefix
dnl
if test "${prefix}" != "NONE"; then
cmd_prefix="${prefix}"
fi
""")
# Finish writing backup macro
m4.write(macro_backup_footer())
# Start writing recall macro
m4.write(macro_recall_header())
# Process arguments
for arg in ("enable","with"):
m4.write("\n dnl\n dnl --%s arguments\n dnl\n" % (arg))
for opt in ac_args[arg]:
var = re.sub(arg+"_","",opt)
m4.write("\n if test \"${cmd_%s_%s}\" != \"\"; then\n %s_%s=\"${cmd_%s_%s}\"\n fi\n" % \
(arg,var,arg,var,arg,var))
# Do not forget "prefix"
m4.write("""
dnl
dnl Prefix
dnl
if test "${cmd_prefix}" != ""; then
prefix="${cmd_prefix}"
fi
""")
# Finish writing recall macro
m4.write(macro_recall_footer())
# Build changed macro
changed = ""
for arg in all_args:
# Init
arg_stat = cnf.get(arg,"status")
try:
arg_repl = cnf.get(arg,"use_instead")
except:
arg_repl = ""
# Removed options
if ( (arg_stat == "removed") or (arg_stat == "dropped") ):
arg1 = re.sub("_","-",arg)
changed += "\n dnl Removed --%s option" % (arg1)
if ( arg_stat == "removed" ):
changed += "\n AC_MSG_NOTICE([ * --%s removed])" % (arg1)
if ( arg_repl != "" ):
changed += "\n AC_MSG_NOTICE([ === please use --%s instead ===])" % \
(re.sub("_","-",arg_repl))
changed += """
if test "${%s}" != ""; then
AC_MSG_ERROR([removed option --%s has been used])
fi
""" % (re.sub("-","_",arg1),arg1)
# New options
elif ( arg_stat == "new" ):
arg2 = re.sub("_","-",arg)
changed += "\n dnl New --%s option\n" % (arg2)
changed += " AC_MSG_NOTICE([ * new option --%s is available])\n" % (arg2)
# Changed UIs
elif ( re.match("changed",arg_stat) ):
arg1 = re.sub("_","-",arg)
changed += """
dnl UI change for --%s option
AC_MSG_NOTICE([ * --%s has a new meaning])
AC_MSG_NOTICE([ please check that --%s="${%s}" is OK for you])
""" % (arg1,arg1,arg1,re.sub("-","_",arg1))
# Renamed options
elif ( re.match("renamed",arg_stat) ):
arg1 = re.sub("_","-",arg_stat.split()[1])
arg2 = re.sub("_","-",arg)
changed += """
dnl --%s > --%s
AC_MSG_NOTICE([ * --%s renamed --%s])
if test "${%s}" != ""; then
AC_MSG_ERROR([renamed option --%s has been used])
fi
""" % (arg1,arg2,arg1,arg2,re.sub("-","_",arg1),arg1)
# Write changed macro
m4.write(re.sub("@MACRO@",changed,macro_changed_template()))
# Write parse macro
m4.write(re.sub("@MACRO@",parse,macro_parse_template()))
# Finish
m4.close()
tmp = getoutput("./config/scripts/add-header-typed Autoconf %s" % (my_output))
if ( tmp != "" ):
print(tmp)
# Write option dumper (for debugging)
dumper = open("config.dump.in","a")
dumper.write("# Command-line options\n")
for arg in all_args:
arg_stat = cnf.get(arg,"status")
if ( (arg_stat != "hidden") and (arg_stat != "removed") ):
var = re.sub("-","_",opt_name)
dumper.write("%s=\"@%s@\"\n" % (arg,arg))
dumper.write("\n")
dumper.close()
|