diff --git a/src/config/gen_featureconfig.py b/src/config/gen_featureconfig.py index 59cf81068d..b8d713e947 100644 --- a/src/config/gen_featureconfig.py +++ b/src/config/gen_featureconfig.py @@ -1,3 +1,4 @@ +# # Copyright (C) 2013-2022 The ESPResSo project # Copyright (C) 2012 Olaf Lenz # @@ -16,9 +17,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # -# This script generates the files featureconfig.h and featureconfig.c. -# -import time + import string import inspect import sys @@ -32,67 +31,78 @@ if len(sys.argv) != 4: print(f"Usage: {sys.argv[0]} DEFFILE HPPFILE CPPFILE", file=sys.stderr) exit(2) -deffilename, hfilename, cfilename = sys.argv[1:5] - -print("Reading definitions from " + deffilename + "...") -defs = featuredefs.defs(deffilename) -print("Done.") - -print("Writing " + hfilename + "...") -hfile = open(hfilename, 'w') - -hfile.write("""/* -WARNING: This file was autogenerated by +path_def, path_hpp, path_cpp = sys.argv[1:5] - %s on %s +print(f"Reading definitions from {path_def}") +defs = featuredefs.defs(path_def) - Do not modify it or your changes will be overwritten! - Modify features.def instead. +disclaimer = f"""/* + WARNING: This file was generated automatically. + Do not modify it or your changes will be overwritten! + Modify features.def instead. */ -#ifndef ESPRESSO_FEATURECONFIG_HPP -#define ESPRESSO_FEATURECONFIG_HPP - -#include "cmake_config.hpp" -#include "myconfig-final.hpp" +""" -""" % (sys.argv[0], time.asctime())) +print(f"Writing {path_hpp}") +hfile = open(path_hpp, 'w') +hfile.write(disclaimer) +hfile.write(""" +#ifndef ESPRESSO_SRC_CONFIG_CONFIG_FEATURES_HPP +#define ESPRESSO_SRC_CONFIG_CONFIG_FEATURES_HPP +""") # external features can only be set by the build # system, so in case the user has defined some of # them, we undef all external features and include # the config from the build system again, to make # sure only the detected ones are set. -hfile.write('/* Guards for externals */') +hfile.write(""" +/*********************************/ +/* Handle definitions from CMake */ +/*********************************/ + +#include "cmake_config.hpp" +#include "myconfig-final.hpp" +""") external_template = string.Template(""" // $feature is external #if defined($feature) #undef $feature #endif """) -for feature in defs.externals: +for feature in sorted(defs.externals): hfile.write(external_template.substitute(feature=feature)) # Include definitions from CMake hfile.write(""" -/* Definitions from CMake */ #include "cmake_config.hpp" """) # handle implications -hfile.write('/* Handle implications */') +hfile.write("""\ +/***********************/ +/* Handle implications */ +/***********************/ +""") implication_template = string.Template(""" // $feature implies $implied #if defined($feature) && !defined($implied) #define $implied #endif """) -for feature, implied in defs.implications: +for feature, implied in sorted(defs.implications): hfile.write(implication_template.substitute( feature=feature, implied=implied)) +hfile.write("\n") + # output warnings if internal features are set manually -hfile.write('/* Warn when derived switches are specified manually */') +hfile.write("""\ +/*****************************************************/ +/* Warn when derived switches are specified manually */ +/*****************************************************/ +""") derivation_template = string.Template(""" // $feature equals $expr #ifdef $feature @@ -101,58 +111,52 @@ derivation_template = string.Template(""" #define $feature #endif """) -for feature, expr, cppexpr in defs.derivations: +for feature, expr, cppexpr in sorted(defs.derivations): hfile.write(derivation_template.substitute( feature=feature, cppexpr=cppexpr, expr=expr)) -# write footer -# define external FEATURES and NUM_FEATURES hfile.write(""" -extern const char* FEATURES[]; -extern const int NUM_FEATURES; -#endif /* of _FEATURECONFIG_HPP */""") -hfile.close() -print("Done.") +extern char const *const FEATURES[]; +extern char const *const FEATURES_ALL[]; +extern unsigned int const NUM_FEATURES; +extern unsigned int const NUM_FEATURES_ALL; -print("Writing " + cfilename + "...") -cfile = open(cfilename, 'w') - -# handle requirements - -cfile.write(f"""/* -WARNING: This file was autogenerated by +#endif +""") - {sys.argv[0]} - on - {time.asctime()} +hfile.close() - Do not modify it or your changes will be overwritten! - Modify features.def instead. -*/ +print(f"Writing {path_cpp}") +cfile = open(path_cpp, "w") -/* config.hpp includes config-features.hpp and myconfig.hpp */ +cfile.write(disclaimer) +cfile.write(f""" +#include "config-features.hpp" #include "config.hpp" +/***********************/ +/* Handle requirements */ +/***********************/ """) -cfile.write('/* Handle requirements */') - requirement_string = """ // {feature} requires {expr} #if defined({feature}) && !({cppexpr}) #error Feature {feature} requires {expr} #endif """ -for feature, expr, cppexpr in defs.requirements: +for feature, expr, cppexpr in sorted(defs.requirements): cfile.write( requirement_string.format( feature=feature, cppexpr=cppexpr, expr=expr)) cfile.write(""" - +/****************/ /* Feature list */ -const char* FEATURES[] = { +/****************/ + +char const *const FEATURES[] = { """) feature_string = """ @@ -161,14 +165,31 @@ feature_string = """ #endif """ -for feature in defs.externals.union(defs.features, defs.derived): +for feature in sorted(defs.externals.union(defs.features, defs.derived)): cfile.write(feature_string.format(feature=feature)) cfile.write(""" }; +unsigned int const NUM_FEATURES = sizeof(FEATURES) / sizeof(char*); +""") -const int NUM_FEATURES = sizeof(FEATURES)/sizeof(char*); +cfile.write(""" +/*********************/ +/* Feature full list */ +/*********************/ + +char const *const FEATURES_ALL[] = {\ +""") + +feature_string = """ + "{feature}",""" + +for feature in sorted(defs.allfeatures): + cfile.write(feature_string.format(feature=feature)) + +cfile.write(""" +}; +unsigned int const NUM_FEATURES_ALL = sizeof(FEATURES_ALL) / sizeof(char*); """) cfile.close() -print("Done.") diff --git a/src/python/espressomd/gen_code_info.py b/src/python/espressomd/gen_code_info.py index 4314d5e3c..2f14582b8 100644 --- a/src/python/espressomd/gen_code_info.py +++ b/src/python/espressomd/gen_code_info.py @@ -53,14 +53,14 @@ def features(): f = [] """) -for feature in defs.allfeatures: +for feature in sorted(defs.allfeatures): cfile.write(f"\n IF {feature} == 1:\n f.append(\"{feature}\")\n") cfile.write(f""" return sorted(f) def all_features(): - return {defs.allfeatures} + return {{{str(sorted(defs.allfeatures))[1:-1]}}} cdef extern from "version.hpp":