From 8848b16e01af1b90c01d1c9e3fc6d92259df4d61 Mon Sep 17 00:00:00 2001 From: Xavier Delacour Date: Tue, 17 May 2011 01:56:43 +0000 Subject: [PATCH] Octave changes by Karl Wette: It's currently not possible to tell SWIG whether or not symbols should be loaded into the global namespace by default. In fact, it's not even possible to make the generated .oct module *not* load all symbols globally, due to a bug in Lib/octave/octruntime.swg (line 37: "noglobal" should be "global"). It's also not possible to change the name of the symbol used to access global variables/constants from the default "cvar". This patch adds 3 Octave-specific command-line options: * -global/-noglobal tell SWIG whether the generated .oct module should load symbols into the global namespace by default. The default option is -global to preserve existing behaviour. * -globals sets the name of the symbol used to access global variables/constants. It is set to "cvar" by default. These options are parsed in Source/Modules/octave.cxx, and written to the wrapping code as the macros SWIG_global_load and SWIG_global_name. In Lib/octave/octruntime.swg, the Octave entry point DEFUN_DLD function now contains a expanded input argument parser, which uses the same command-line arguments (-global/-noglobal, -globals) as can be passed to SWIG itself; this allows the module user to change the default loading behaviour of the module user at load time. The parser checks for non-string and unrecognised arguments, and also checks that the argument to -globals is a valid Octave identifier. A -help option prints a short usage message. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12670 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/octave/octruntime.swg | 51 ++++++++++++++++++++++++++++++++------- Source/Modules/octave.cxx | 33 ++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/Lib/octave/octruntime.swg b/Lib/octave/octruntime.swg index e19b781c2..48da0cf48 100644 --- a/Lib/octave/octruntime.swg +++ b/Lib/octave/octruntime.swg @@ -1,4 +1,5 @@ %insert(runtime) %{ +#include #include #include #include @@ -20,6 +21,45 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) { static bool already_init=false; if (already_init) return octave_value_list(); + + // parse command line + const char* usage="usage: " SWIG_name_d " [-global|-noglobal] [-globals ]"; + bool global_load=SWIG_global_load; + std::string global_name=SWIG_global_name; + for (int j=0;jassign(swig_globals[j].name,&swig_globals[j]); octave_swig_type* module_ns=new octave_swig_type(0, 0, 0, true); - module_ns->assign("cvar",Swig::swig_value_ref(cvar_ns)); + module_ns->assign(global_name,Swig::swig_value_ref(cvar_ns)); for (int j=0;swig_globals[j].name;++j) if (swig_globals[j].method) module_ns->assign(swig_globals[j].name,&swig_globals[j]); @@ -68,7 +101,7 @@ DEFUN_DLD (SWIG_name,args,nargout,SWIG_name_d) { // the incref is necessary so install_global doesn't destroy module_ns, // as it would if it installed something with the same name as the module. module_ns->incref(); - if (global_option) + if (global_load) module_ns->install_global(); module_ns->decref(); diff --git a/Source/Modules/octave.cxx b/Source/Modules/octave.cxx index ab53340fe..dcd690844 100644 --- a/Source/Modules/octave.cxx +++ b/Source/Modules/octave.cxx @@ -15,9 +15,14 @@ char cvsroot_octave_cxx[] = "$Id$"; #include "swigmod.h" +static bool global_load = true; +static String *global_name = 0; + static const char *usage = (char *) "\ Octave Options (available with -octave)\n\ - [no additional options]\n\ + -global - Load all symbols into the global namespace [default]\n\ + -globals - Set used to access C global variables [default: 'cvar']\n\ + -noglobal - Do not load all symbols into the global namespace\n\ \n"; @@ -64,11 +69,29 @@ public: for (int i = 1; i < argc; i++) { if (argv[i]) { if (strcmp(argv[i], "-help") == 0) { - fputs(usage, stdout); - } + fputs(usage, stderr); + } else if (strcmp(argv[i], "-global") == 0) { + global_load = true; + Swig_mark_arg(i); + } else if (strcmp(argv[i], "-noglobal") == 0) { + global_load = false; + Swig_mark_arg(i); + } else if (strcmp(argv[i], "-globals") == 0) { + if (argv[i + 1]) { + global_name = NewString(argv[i + 1]); + Swig_mark_arg(i); + Swig_mark_arg(i + 1); + i++; + } else { + Swig_arg_error(); + } + } } } + if (!global_name) + global_name = NewString("cvar"); + SWIG_library_directory("octave"); Preprocessor_define("SWIGOCTAVE 1", 0); SWIG_config_file("octave.swg"); @@ -131,6 +154,10 @@ public: Printf(f_runtime, "#define SWIG_name_d \"%s\"\n", module); Printf(f_runtime, "#define SWIG_name %s\n", module); + Printf(f_runtime, "\n"); + Printf(f_runtime, "#define SWIG_global_load %s\n", global_load ? "true" : "false"); + Printf(f_runtime, "#define SWIG_global_name \"%s\"\n", global_name); + if (directorsEnabled()) { Printf(f_runtime, "#define SWIG_DIRECTORS\n"); Swig_banner(f_directors_h);