From 46f7501d94f8fb3432863344aeda6d4fd85fb1a4 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 13 Oct 2022 19:46:51 +0100 Subject: [PATCH] Cleanup SWIG_VERSION definition Add Swig_obligatory_macros which must be called by each target language to define SWIG_VERSION correctly in the generated code, as well as the language specific macro SWIGXXX where XXX is the target language name. Drop the #ifdef SWIGXXX that was previously generated - I can't see the point of this and if users are defining this macro somehow, then users will need to change this Closes #1050 --- Lib/csharp/csharp.swg | 2 -- Lib/go/go.swg | 2 -- Lib/guile/guile.i | 2 -- Lib/java/java.swg | 2 -- Lib/lua/lua.swg | 2 -- Lib/mzscheme/mzscheme.swg | 2 -- Lib/ocaml/ocaml.i | 2 -- Lib/php/php.swg | 2 -- Lib/typemaps/README | 1 - Lib/typemaps/swigmacros.swg | 2 -- Lib/typemaps/swigversion.swg | 16 --------------- Source/Modules/csharp.cxx | 2 +- Source/Modules/d.cxx | 2 +- Source/Modules/go.cxx | 4 +++- Source/Modules/guile.cxx | 2 +- Source/Modules/java.cxx | 2 +- Source/Modules/javascript.cxx | 4 ++-- Source/Modules/lua.cxx | 2 +- Source/Modules/main.cxx | 20 +++--------------- Source/Modules/mzscheme.cxx | 2 +- Source/Modules/ocaml.cxx | 2 +- Source/Modules/octave.cxx | 2 +- Source/Modules/perl5.cxx | 2 +- Source/Modules/php.cxx | 12 +++++------ Source/Modules/python.cxx | 2 +- Source/Modules/r.cxx | 3 +-- Source/Modules/ruby.cxx | 2 +- Source/Modules/scilab.cxx | 2 +- Source/Modules/tcl8.cxx | 2 +- Source/Swig/misc.c | 38 +++++++++++++++++++++++++++++++++++ Source/Swig/swig.h | 2 ++ 31 files changed, 68 insertions(+), 76 deletions(-) delete mode 100644 Lib/typemaps/swigversion.swg diff --git a/Lib/csharp/csharp.swg b/Lib/csharp/csharp.swg index 8514a7705..1f80d12a1 100644 --- a/Lib/csharp/csharp.swg +++ b/Lib/csharp/csharp.swg @@ -4,8 +4,6 @@ * C# typemaps * ----------------------------------------------------------------------------- */ -%include - %include /* The ctype, imtype and cstype typemaps work together and so there should be one of each. diff --git a/Lib/go/go.swg b/Lib/go/go.swg index 47486ab75..348ae5f0d 100644 --- a/Lib/go/go.swg +++ b/Lib/go/go.swg @@ -4,8 +4,6 @@ * Go configuration module. * ------------------------------------------------------------ */ -%include - %include /* Code insertion directives */ diff --git a/Lib/guile/guile.i b/Lib/guile/guile.i index 10438f483..ef270d74b 100644 --- a/Lib/guile/guile.i +++ b/Lib/guile/guile.i @@ -4,8 +4,6 @@ * SWIG Configuration File for Guile. * ----------------------------------------------------------------------------- */ -%include - /* Macro for inserting Scheme code into the stub */ #define %scheme %insert("scheme") #define %goops %insert("goops") diff --git a/Lib/java/java.swg b/Lib/java/java.swg index 28eb8fd0d..8719818bb 100644 --- a/Lib/java/java.swg +++ b/Lib/java/java.swg @@ -4,8 +4,6 @@ * Java typemaps * ----------------------------------------------------------------------------- */ -%include - %include /* The jni, jtype and jstype typemaps work together and so there should be one of each. diff --git a/Lib/lua/lua.swg b/Lib/lua/lua.swg index 263c3966f..12c635d77 100644 --- a/Lib/lua/lua.swg +++ b/Lib/lua/lua.swg @@ -5,8 +5,6 @@ * This file is parsed by SWIG before reading any other interface file. * ----------------------------------------------------------------------------- */ -%include - /* ----------------------------------------------------------------------------- * includes * ----------------------------------------------------------------------------- */ diff --git a/Lib/mzscheme/mzscheme.swg b/Lib/mzscheme/mzscheme.swg index 8ded91f3e..f45c87250 100644 --- a/Lib/mzscheme/mzscheme.swg +++ b/Lib/mzscheme/mzscheme.swg @@ -5,8 +5,6 @@ * This file is parsed by SWIG before reading any other interface file. * ----------------------------------------------------------------------------- */ -%include - /* Include headers */ %runtime "swigrun.swg" // Common C API type-checking code %runtime "swigerrors.swg" // SWIG errors diff --git a/Lib/ocaml/ocaml.i b/Lib/ocaml/ocaml.i index 117d0a9b5..cc26d1859 100644 --- a/Lib/ocaml/ocaml.i +++ b/Lib/ocaml/ocaml.i @@ -4,8 +4,6 @@ * SWIG Configuration File for Ocaml * ----------------------------------------------------------------------------- */ -%include - /* Insert common stuff */ %insert(runtime) "swigrun.swg" diff --git a/Lib/php/php.swg b/Lib/php/php.swg index e14401236..fd0928076 100644 --- a/Lib/php/php.swg +++ b/Lib/php/php.swg @@ -4,8 +4,6 @@ * PHP configuration file * ----------------------------------------------------------------------------- */ -%include - // Default to generating PHP type declarations (for PHP >= 8) except for // cases which are liable to cause compatibility issues with existing // bindings. diff --git a/Lib/typemaps/README b/Lib/typemaps/README index 22a0fc0bb..65134578d 100644 --- a/Lib/typemaps/README +++ b/Lib/typemaps/README @@ -48,7 +48,6 @@ std_strings.swg Common macros to implemented the std::string/std::wstring typem strings.swg Common macros and typemaps for string and wstring (char *, wchar_t *) swigmacros.swg Basic macros -swigversion.swg Define SWIG_VERSION fragments.swg Macros for fragment manipulations diff --git a/Lib/typemaps/swigmacros.swg b/Lib/typemaps/swigmacros.swg index 7db6871d4..b772eb04b 100644 --- a/Lib/typemaps/swigmacros.swg +++ b/Lib/typemaps/swigmacros.swg @@ -109,8 +109,6 @@ nocppval #endif %enddef -%include - /* ----------------------------------------------------------------------------- * Casting operators * ----------------------------------------------------------------------------- */ diff --git a/Lib/typemaps/swigversion.swg b/Lib/typemaps/swigversion.swg deleted file mode 100644 index ac2e42439..000000000 --- a/Lib/typemaps/swigversion.swg +++ /dev/null @@ -1,16 +0,0 @@ -/* ----------------------------------------------------------------------------- - * Define SWIG_VERSION - * ----------------------------------------------------------------------------- */ - -/* Define SWIG_VERSION in the interface and the wrapper code. - * - * Best practice is to use SWIG-time checks for SWIG_VERSION, but SWIG_VERSION - * was unintentionally defined like this for many years, and while it was never - * documented there are likely user interface files which rely on it. - */ -%define %define_swig_version_()%#define SWIG_VERSION_ SWIG_VERSION %enddef -%insert("header") { -%define_swig_version_() -%#define SWIG_VERSION SWIG_VERSION_ -} -#undef %define_swig_version_ diff --git a/Source/Modules/csharp.cxx b/Source/Modules/csharp.cxx index 3cebb346c..240a002b4 100644 --- a/Source/Modules/csharp.cxx +++ b/Source/Modules/csharp.cxx @@ -399,7 +399,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGCSHARP\n#define SWIGCSHARP\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "CSHARP"); if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); diff --git a/Source/Modules/d.cxx b/Source/Modules/d.cxx index 4704d8d54..31f300f2e 100644 --- a/Source/Modules/d.cxx +++ b/Source/Modules/d.cxx @@ -472,7 +472,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGD\n#define SWIGD\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "D"); if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); diff --git a/Source/Modules/go.cxx b/Source/Modules/go.cxx index d39d396fc..bf63bec63 100644 --- a/Source/Modules/go.cxx +++ b/Source/Modules/go.cxx @@ -512,6 +512,9 @@ private: Swig_register_filebyname("cgo_comment_typedefs", f_cgo_comment_typedefs); Swig_banner(f_c_begin); + + Swig_obligatory_macros(f_c_runtime, "GO"); + if (CPlusPlus) { Printf(f_c_begin, "\n// source: %s\n\n", swig_filename); } else { @@ -519,7 +522,6 @@ private: } Printf(f_c_runtime, "#define SWIGMODULE %s\n", module); - Printf(f_c_runtime, "#ifndef SWIGGO\n#define SWIGGO\n#endif\n\n"); if (gccgo_flag) { Printf(f_c_runtime, "#define SWIGGO_PREFIX %s\n", go_prefix); diff --git a/Source/Modules/guile.cxx b/Source/Modules/guile.cxx index 874150e3a..605e03197 100644 --- a/Source/Modules/guile.cxx +++ b/Source/Modules/guile.cxx @@ -321,7 +321,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGGUILE\n#define SWIGGUILE\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "GUILE"); /* Write out directives and declarations */ diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index 93d623703..773945af2 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -456,7 +456,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGJAVA\n#define SWIGJAVA\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "JAVA"); if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); diff --git a/Source/Modules/javascript.cxx b/Source/Modules/javascript.cxx index bf0f471b0..17effc220 100644 --- a/Source/Modules/javascript.cxx +++ b/Source/Modules/javascript.cxx @@ -1615,7 +1615,7 @@ int JSCEmitter::initialize(Node *n) { Swig_banner(f_wrap_cpp); - Printf(f_runtime, "#ifndef SWIGJAVASCRIPT\n#define SWIGJAVASCRIPT\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "JAVASCRIPT"); return SWIG_OK; } @@ -1947,7 +1947,7 @@ int V8Emitter::initialize(Node *n) { Swig_banner(f_wrap_cpp); - Printf(f_runtime, "#ifndef SWIGJAVASCRIPT\n#define SWIGJAVASCRIPT\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "JAVASCRIPT"); return SWIG_OK; } diff --git a/Source/Modules/lua.cxx b/Source/Modules/lua.cxx index 855acc69d..c2e65dba6 100644 --- a/Source/Modules/lua.cxx +++ b/Source/Modules/lua.cxx @@ -329,7 +329,7 @@ public: /* Standard stuff for the SWIG runtime section */ Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGLUA\n#define SWIGLUA\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "LUA"); emitLuaFlavor(f_runtime); diff --git a/Source/Modules/main.cxx b/Source/Modules/main.cxx index d2960e9f8..f5bdec644 100644 --- a/Source/Modules/main.cxx +++ b/Source/Modules/main.cxx @@ -911,25 +911,11 @@ int SWIG_main(int argc, char *argv[], const TargetLanguageModule *tlm) { Preprocessor_define((DOH *) "SWIG 1", 0); Preprocessor_define((DOH *) "__STDC__", 0); - // Set the SWIG version value in format 0xAABBCC from package version expected to be in format A.B.C - String *package_version = NewString(PACKAGE_VERSION); /* Note that the fakeversion has not been set at this point */ - char *token = strtok(Char(package_version), "."); - String *vers = NewString("SWIG_VERSION 0x"); - int count = 0; - while (token) { - int len = (int)strlen(token); - assert(len == 1 || len == 2); - Printf(vers, "%s%s", (len == 1) ? "0" : "", token); - token = strtok(NULL, "."); - count++; - } - Delete(package_version); - assert(count == 3); // Check version format is correct - - /* Turn on contracts */ + String *vers = Swig_package_version_hex(); + Preprocessor_define(vers, 0); + Delete(vers); Swig_contract_mode_set(1); - Preprocessor_define(vers, 0); /* Turn off directors mode */ Wrapper_director_mode_set(0); diff --git a/Source/Modules/mzscheme.cxx b/Source/Modules/mzscheme.cxx index 6e412a09c..e22f8bb7a 100644 --- a/Source/Modules/mzscheme.cxx +++ b/Source/Modules/mzscheme.cxx @@ -147,7 +147,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGMZSCHEME\n#define SWIGMZSCHEME\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "MZSCHEME"); module = Getattr(n, "name"); diff --git a/Source/Modules/ocaml.cxx b/Source/Modules/ocaml.cxx index c6748f571..963a0c2d1 100644 --- a/Source/Modules/ocaml.cxx +++ b/Source/Modules/ocaml.cxx @@ -276,7 +276,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGOCAML\n#define SWIGOCAML\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "OCAML"); Printf(f_runtime, "#define SWIG_MODULE \"%s\"\n", module); /* Module name */ diff --git a/Source/Modules/octave.cxx b/Source/Modules/octave.cxx index 04609b017..352105b9b 100644 --- a/Source/Modules/octave.cxx +++ b/Source/Modules/octave.cxx @@ -190,7 +190,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGOCTAVE\n#define SWIGOCTAVE\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "OCTAVE"); Printf(f_runtime, "#define SWIG_name_d \"%s\"\n", module); Printf(f_runtime, "#define SWIG_name %s\n", module); diff --git a/Source/Modules/perl5.cxx b/Source/Modules/perl5.cxx index add4fd0b6..0cbf6b17a 100644 --- a/Source/Modules/perl5.cxx +++ b/Source/Modules/perl5.cxx @@ -319,7 +319,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGPERL\n#define SWIGPERL\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "PERL"); if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx index 13b3df686..a2e741215 100644 --- a/Source/Modules/php.cxx +++ b/Source/Modules/php.cxx @@ -609,6 +609,12 @@ public: Swig_banner(f_begin); + Swig_obligatory_macros(f_runtime, "PHP"); + + if (directorsEnabled()) { + Printf(f_runtime, "#define SWIG_DIRECTORS\n"); + } + // We need to include php.h before string.h gets included, at least with // PHP 8.2. Otherwise string.h is included without _GNU_SOURCE being // included and memrchr() doesn't get declared, and then inline code in @@ -636,12 +642,6 @@ public: "# endif\n" "#endif\n\n"); - Printf(f_runtime, "#ifndef SWIGPHP\n#define SWIGPHP\n#endif\n\n"); - - if (directorsEnabled()) { - Printf(f_runtime, "#define SWIG_DIRECTORS\n"); - } - /* Set the module name */ module = Copy(Getattr(n, "name")); cap_module = NewStringf("%(upper)s", module); diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index a8175eb0e..6f30c1faa 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -587,7 +587,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGPYTHON\n#define SWIGPYTHON\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "PYTHON"); if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); diff --git a/Source/Modules/r.cxx b/Source/Modules/r.cxx index 3283c96ab..565145327 100644 --- a/Source/Modules/r.cxx +++ b/Source/Modules/r.cxx @@ -740,8 +740,7 @@ int R::top(Node *n) { Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGR\n#define SWIGR\n#endif\n\n"); - + Swig_obligatory_macros(f_runtime, "R"); Swig_banner_target_lang(s_init, "#"); outputCommandLineArguments(s_init); diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx index 268cb2775..0208435f0 100644 --- a/Source/Modules/ruby.cxx +++ b/Source/Modules/ruby.cxx @@ -1087,7 +1087,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGRUBY\n#define SWIGRUBY\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "RUBY"); if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index a99ce0025..aabd2d842 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -200,7 +200,7 @@ public: /* Output module initialization code */ Swig_banner(beginSection); - Printf(runtimeSection, "\n\n#ifndef SWIGSCILAB\n#define SWIGSCILAB\n#endif\n\n"); + Swig_obligatory_macros(runtimeSection, "SCILAB"); // Gateway header source merged with wrapper source in nobuilder mode if (!generateBuilder) diff --git a/Source/Modules/tcl8.cxx b/Source/Modules/tcl8.cxx index f65b3eccf..975230e84 100644 --- a/Source/Modules/tcl8.cxx +++ b/Source/Modules/tcl8.cxx @@ -161,7 +161,7 @@ public: Swig_banner(f_begin); - Printf(f_runtime, "\n\n#ifndef SWIGTCL\n#define SWIGTCL\n#endif\n\n"); + Swig_obligatory_macros(f_runtime, "TCL"); /* Set the module name, namespace, and prefix */ diff --git a/Source/Swig/misc.c b/Source/Swig/misc.c index 92e45fbe0..18c29f76a 100644 --- a/Source/Swig/misc.c +++ b/Source/Swig/misc.c @@ -62,6 +62,44 @@ const char *Swig_package_version(void) { return fake_version ? fake_version : PACKAGE_VERSION; } +/* ----------------------------------------------------------------------------- + * Swig_package_version_hex() + * + * Return the package version in hex format "0xAABBCC" such as "0x040200" for 4.2.0 + * ----------------------------------------------------------------------------- */ + +String *Swig_package_version_hex(void) { + String *package_version = NewString(Swig_package_version()); + char *token = strtok(Char(package_version), "."); + String *vers = NewString("SWIG_VERSION 0x"); + int count = 0; + while (token) { + int len = (int)strlen(token); + assert(len == 1 || len == 2); + Printf(vers, "%s%s", (len == 1) ? "0" : "", token); + token = strtok(NULL, "."); + count++; + } + Delete(package_version); + assert(count == 3); // Check version format is correct + return vers; +} + +/* ----------------------------------------------------------------------------- + * Swig_obligatory_macros() + * + * Generates the SWIG_VERSION and SWIGXXX macros where XXX is the target language + * name (must be provided uppercase). + * ----------------------------------------------------------------------------- */ + +void Swig_obligatory_macros(String *f_runtime, const char *language) { + String *version_hex = Swig_package_version_hex(); + Printf(f_runtime, "\n\n"); + Printf(f_runtime, "#define %s\n", version_hex); + Printf(f_runtime, "#define SWIG%s\n", language); + Delete(version_hex); +} + /* ----------------------------------------------------------------------------- * Swig_banner() * diff --git a/Source/Swig/swig.h b/Source/Swig/swig.h index e9f7c92ef..19e61b455 100644 --- a/Source/Swig/swig.h +++ b/Source/Swig/swig.h @@ -308,6 +308,8 @@ extern int ParmList_is_compactdefargs(ParmList *p); extern char *Swig_copy_string(const char *c); extern void Swig_set_fakeversion(const char *version); extern const char *Swig_package_version(void); + extern String *Swig_package_version_hex(void); + extern void Swig_obligatory_macros(String *f_runtime, const char *language); extern void Swig_banner(File *f); extern void Swig_banner_target_lang(File *f, const_String_or_char_ptr commentchar); extern String *Swig_strip_c_comments(const String *s);