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
This commit is contained in:
William S Fulton 2022-10-13 19:46:51 +01:00
commit 46f7501d94
31 changed files with 68 additions and 76 deletions

View file

@ -4,8 +4,6 @@
* C# typemaps * C# typemaps
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
%include <csharphead.swg> %include <csharphead.swg>
/* The ctype, imtype and cstype typemaps work together and so there should be one of each. /* The ctype, imtype and cstype typemaps work together and so there should be one of each.

View file

@ -4,8 +4,6 @@
* Go configuration module. * Go configuration module.
* ------------------------------------------------------------ */ * ------------------------------------------------------------ */
%include <typemaps/swigversion.swg>
%include <gostring.swg> %include <gostring.swg>
/* Code insertion directives */ /* Code insertion directives */

View file

@ -4,8 +4,6 @@
* SWIG Configuration File for Guile. * SWIG Configuration File for Guile.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
/* Macro for inserting Scheme code into the stub */ /* Macro for inserting Scheme code into the stub */
#define %scheme %insert("scheme") #define %scheme %insert("scheme")
#define %goops %insert("goops") #define %goops %insert("goops")

View file

@ -4,8 +4,6 @@
* Java typemaps * Java typemaps
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
%include <javahead.swg> %include <javahead.swg>
/* The jni, jtype and jstype typemaps work together and so there should be one of each. /* The jni, jtype and jstype typemaps work together and so there should be one of each.

View file

@ -5,8 +5,6 @@
* This file is parsed by SWIG before reading any other interface file. * This file is parsed by SWIG before reading any other interface file.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* includes * includes
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */

View file

@ -5,8 +5,6 @@
* This file is parsed by SWIG before reading any other interface file. * This file is parsed by SWIG before reading any other interface file.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
/* Include headers */ /* Include headers */
%runtime "swigrun.swg" // Common C API type-checking code %runtime "swigrun.swg" // Common C API type-checking code
%runtime "swigerrors.swg" // SWIG errors %runtime "swigerrors.swg" // SWIG errors

View file

@ -4,8 +4,6 @@
* SWIG Configuration File for Ocaml * SWIG Configuration File for Ocaml
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
/* Insert common stuff */ /* Insert common stuff */
%insert(runtime) "swigrun.swg" %insert(runtime) "swigrun.swg"

View file

@ -4,8 +4,6 @@
* PHP configuration file * PHP configuration file
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
%include <typemaps/swigversion.swg>
// Default to generating PHP type declarations (for PHP >= 8) except for // Default to generating PHP type declarations (for PHP >= 8) except for
// cases which are liable to cause compatibility issues with existing // cases which are liable to cause compatibility issues with existing
// bindings. // bindings.

View file

@ -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 *) strings.swg Common macros and typemaps for string and wstring (char *, wchar_t *)
swigmacros.swg Basic macros swigmacros.swg Basic macros
swigversion.swg Define SWIG_VERSION
fragments.swg Macros for fragment manipulations fragments.swg Macros for fragment manipulations

View file

@ -109,8 +109,6 @@ nocppval
#endif #endif
%enddef %enddef
%include <typemaps/swigversion.swg>
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Casting operators * Casting operators
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */

View file

@ -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_

View file

@ -399,7 +399,7 @@ public:
Swig_banner(f_begin); 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()) { if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_runtime, "#define SWIG_DIRECTORS\n");

View file

@ -472,7 +472,7 @@ public:
Swig_banner(f_begin); 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()) { if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_runtime, "#define SWIG_DIRECTORS\n");

View file

@ -512,6 +512,9 @@ private:
Swig_register_filebyname("cgo_comment_typedefs", f_cgo_comment_typedefs); Swig_register_filebyname("cgo_comment_typedefs", f_cgo_comment_typedefs);
Swig_banner(f_c_begin); Swig_banner(f_c_begin);
Swig_obligatory_macros(f_c_runtime, "GO");
if (CPlusPlus) { if (CPlusPlus) {
Printf(f_c_begin, "\n// source: %s\n\n", swig_filename); Printf(f_c_begin, "\n// source: %s\n\n", swig_filename);
} else { } else {
@ -519,7 +522,6 @@ private:
} }
Printf(f_c_runtime, "#define SWIGMODULE %s\n", module); 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) { if (gccgo_flag) {
Printf(f_c_runtime, "#define SWIGGO_PREFIX %s\n", go_prefix); Printf(f_c_runtime, "#define SWIGGO_PREFIX %s\n", go_prefix);

View file

@ -321,7 +321,7 @@ public:
Swig_banner(f_begin); 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 */ /* Write out directives and declarations */

View file

@ -456,7 +456,7 @@ public:
Swig_banner(f_begin); 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()) { if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_runtime, "#define SWIG_DIRECTORS\n");

View file

@ -1615,7 +1615,7 @@ int JSCEmitter::initialize(Node *n) {
Swig_banner(f_wrap_cpp); 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; return SWIG_OK;
} }
@ -1947,7 +1947,7 @@ int V8Emitter::initialize(Node *n) {
Swig_banner(f_wrap_cpp); 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; return SWIG_OK;
} }

View file

@ -329,7 +329,7 @@ public:
/* Standard stuff for the SWIG runtime section */ /* Standard stuff for the SWIG runtime section */
Swig_banner(f_begin); 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); emitLuaFlavor(f_runtime);

View file

@ -911,25 +911,11 @@ int SWIG_main(int argc, char *argv[], const TargetLanguageModule *tlm) {
Preprocessor_define((DOH *) "SWIG 1", 0); Preprocessor_define((DOH *) "SWIG 1", 0);
Preprocessor_define((DOH *) "__STDC__", 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 *vers = Swig_package_version_hex();
String *package_version = NewString(PACKAGE_VERSION); /* Note that the fakeversion has not been set at this point */ Preprocessor_define(vers, 0);
char *token = strtok(Char(package_version), "."); Delete(vers);
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 */
Swig_contract_mode_set(1); Swig_contract_mode_set(1);
Preprocessor_define(vers, 0);
/* Turn off directors mode */ /* Turn off directors mode */
Wrapper_director_mode_set(0); Wrapper_director_mode_set(0);

View file

@ -147,7 +147,7 @@ public:
Swig_banner(f_begin); 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"); module = Getattr(n, "name");

View file

@ -276,7 +276,7 @@ public:
Swig_banner(f_begin); 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); Printf(f_runtime, "#define SWIG_MODULE \"%s\"\n", module);
/* Module name */ /* Module name */

View file

@ -190,7 +190,7 @@ public:
Swig_banner(f_begin); 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_d \"%s\"\n", module);
Printf(f_runtime, "#define SWIG_name %s\n", module); Printf(f_runtime, "#define SWIG_name %s\n", module);

View file

@ -319,7 +319,7 @@ public:
Swig_banner(f_begin); 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()) { if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_runtime, "#define SWIG_DIRECTORS\n");

View file

@ -609,6 +609,12 @@ public:
Swig_banner(f_begin); 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 // 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 // PHP 8.2. Otherwise string.h is included without _GNU_SOURCE being
// included and memrchr() doesn't get declared, and then inline code in // included and memrchr() doesn't get declared, and then inline code in
@ -636,12 +642,6 @@ public:
"# endif\n" "# endif\n"
"#endif\n\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 */ /* Set the module name */
module = Copy(Getattr(n, "name")); module = Copy(Getattr(n, "name"));
cap_module = NewStringf("%(upper)s", module); cap_module = NewStringf("%(upper)s", module);

View file

@ -587,7 +587,7 @@ public:
Swig_banner(f_begin); 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()) { if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_runtime, "#define SWIG_DIRECTORS\n");

View file

@ -740,8 +740,7 @@ int R::top(Node *n) {
Swig_banner(f_begin); 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, "#"); Swig_banner_target_lang(s_init, "#");
outputCommandLineArguments(s_init); outputCommandLineArguments(s_init);

View file

@ -1087,7 +1087,7 @@ public:
Swig_banner(f_begin); 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()) { if (directorsEnabled()) {
Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_runtime, "#define SWIG_DIRECTORS\n");

View file

@ -200,7 +200,7 @@ public:
/* Output module initialization code */ /* Output module initialization code */
Swig_banner(beginSection); 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 // Gateway header source merged with wrapper source in nobuilder mode
if (!generateBuilder) if (!generateBuilder)

View file

@ -161,7 +161,7 @@ public:
Swig_banner(f_begin); 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 */ /* Set the module name, namespace, and prefix */

View file

@ -62,6 +62,44 @@ const char *Swig_package_version(void) {
return fake_version ? fake_version : PACKAGE_VERSION; 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() * Swig_banner()
* *

View file

@ -308,6 +308,8 @@ extern int ParmList_is_compactdefargs(ParmList *p);
extern char *Swig_copy_string(const char *c); extern char *Swig_copy_string(const char *c);
extern void Swig_set_fakeversion(const char *version); extern void Swig_set_fakeversion(const char *version);
extern const char *Swig_package_version(void); 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(File *f);
extern void Swig_banner_target_lang(File *f, const_String_or_char_ptr commentchar); extern void Swig_banner_target_lang(File *f, const_String_or_char_ptr commentchar);
extern String *Swig_strip_c_comments(const String *s); extern String *Swig_strip_c_comments(const String *s);