[C#] Single file mode

Added "-csout <path>" parameter. If supplied all generated C# code will
be written to the given file. Makes it easier to integrate SWIG into
automated script based build processes.
This commit is contained in:
Frederick Parotat 2015-06-03 14:19:13 +02:00
commit 747e22f714

View file

@ -34,6 +34,7 @@ class CSHARP:public Language {
File *f_init;
File *f_directors;
File *f_directors_h;
File *f_single;
List *filenames_list;
bool proxy_flag; // Flag for generating proxy classes
@ -78,6 +79,7 @@ class CSHARP:public Language {
String *director_method_types; // Director method types
String *director_connect_parms; // Director delegates parameter list for director connect call
String *destructor_call; //C++ destructor call if any
String *cs_out;
// Director method stuff:
List *dmethods_seq;
@ -108,6 +110,7 @@ public:
f_init(NULL),
f_directors(NULL),
f_directors_h(NULL),
f_single(NULL),
filenames_list(NULL),
proxy_flag(true),
native_function_flag(false),
@ -150,6 +153,7 @@ public:
director_method_types(NULL),
director_connect_parms(NULL),
destructor_call(NULL),
cs_out(NULL),
dmethods_seq(NULL),
dmethods_table(NULL),
n_dmethods(0),
@ -244,6 +248,16 @@ public:
} else if (strcmp(argv[i], "-oldvarnames") == 0) {
Swig_mark_arg(i);
old_variable_names = true;
} else if (strcmp(argv[i], "-csout") == 0) {
if (argv[i + 1]) {
cs_out = NewString("");
Printf(cs_out, argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-help") == 0) {
Printf(stdout, "%s\n", usage);
}
@ -423,18 +437,7 @@ public:
}
// Generate the intermediary class
{
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), imclass_name);
File *f_im = NewFile(filen, "w", SWIG_output_files());
if (!f_im) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the intermediary class file
emitBanner(f_im);
File *f_im = getFile(SWIG_output_directory(), imclass_name);
addOpenNamespace(0, f_im);
@ -462,23 +465,13 @@ public:
Printf(f_im, "}\n");
addCloseNamespace(0, f_im);
Delete(f_im);
if(f_im != f_single)
Delete(f_im);
}
// Generate the C# module class
{
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), module_class_name);
File *f_module = NewFile(filen, "w", SWIG_output_files());
if (!f_module) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the module class file
emitBanner(f_module);
File *f_module = getFile(SWIG_output_directory(), module_class_name);
addOpenNamespace(0, f_module);
@ -514,7 +507,8 @@ public:
Printf(f_module, "}\n");
addCloseNamespace(0, f_module);
Delete(f_module);
if(f_module != f_single)
Delete(f_module);
}
if (upcasts_code)
@ -613,6 +607,11 @@ public:
f_directors_h = NULL;
}
if(f_single) {
Dump(f_single, f_begin);
Delete(f_single);
}
Dump(f_wrappers, f_begin);
Wrapper_pretty_print(f_init, f_begin);
Delete(f_header);
@ -634,6 +633,44 @@ public:
Swig_banner_target_lang(f, "//");
Printf(f, "//------------------------------------------------------------------------------\n\n");
}
/* -----------------------------------------------------------------------------
* getFile()
* ----------------------------------------------------------------------------- */
File* getFile(const String* dir, String* name) {
if(cs_out) {
if(!f_single) {
String* filen = NewStringf("%s", cs_out);
f_single = NewFile(filen, "w", SWIG_output_files());
if(!f_single) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
Printf(stdout, "create single file");
emitBanner(f_single);
}
return f_single;
} else {
String* filen = NewStringf("%s%s.cs", dir, name);
File* f = NewFile(filen, "w", SWIG_output_files());
if(!f) {
FileErrorDisplay(f);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
emitBanner(f);
return f;
}
}
/*-----------------------------------------------------------------------
* Add new director upcall signature
@ -1198,18 +1235,7 @@ public:
} else {
// Global enums are defined in their own file
String *output_directory = outputDirectory(nspace);
String *filen = NewStringf("%s%s.cs", output_directory, symname);
File *f_enum = NewFile(filen, "w", SWIG_output_files());
if (!f_enum) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the enum file
emitBanner(f_enum);
File *f_enum = getFile(output_directory, symname);
addOpenNamespace(nspace, f_enum);
@ -1217,7 +1243,8 @@ public:
"\n", enum_code, "\n", NIL);
addCloseNamespace(nspace, f_enum);
Delete(f_enum);
if(f_enum != f_single)
Delete(f_enum);
Delete(output_directory);
}
} else {
@ -1920,18 +1947,7 @@ public:
// Each outer proxy class goes into a separate file
if (!has_outerclass) {
String *output_directory = outputDirectory(nspace);
String *filen = NewStringf("%s%s.cs", output_directory, proxy_class_name);
f_proxy = NewFile(filen, "w", SWIG_output_files());
if (!f_proxy) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the proxy class file
emitBanner(f_proxy);
f_proxy = getFile(output_directory, proxy_class_name);
addOpenNamespace(nspace, f_proxy);
}
@ -1991,7 +2007,8 @@ public:
if (!has_outerclass) {
Printf(f_proxy, "}\n");
addCloseNamespace(nspace, f_proxy);
Delete(f_proxy);
if(f_proxy != f_single)
Delete(f_proxy);
f_proxy = NULL;
} else {
for (int i = 0; i < nesting_depth; ++i)
@ -3216,18 +3233,7 @@ public:
Setline(n, line_number);
String *swigtype = NewString("");
String *filen = NewStringf("%s%s.cs", SWIG_output_directory(), classname);
File *f_swigtype = NewFile(filen, "w", SWIG_output_files());
if (!f_swigtype) {
FileErrorDisplay(filen);
SWIG_exit(EXIT_FAILURE);
}
Append(filenames_list, Copy(filen));
Delete(filen);
filen = NULL;
// Start writing out the type wrapper class file
emitBanner(f_swigtype);
File *f_swigtype = getFile(SWIG_output_directory(), classname);
addOpenNamespace(0, f_swigtype);
@ -3262,8 +3268,8 @@ public:
Printv(f_swigtype, swigtype, NIL);
addCloseNamespace(0, f_swigtype);
Delete(f_swigtype);
if(f_swigtype != f_single)
Delete(f_swigtype);
Delete(swigtype);
Delete(n);
}
@ -4285,4 +4291,5 @@ C# Options (available with -csharp)\n\
-noproxy - Generate the low-level functional interface instead\n\
of proxy classes\n\
-oldvarnames - Old intermediary method names for variable wrappers\n\
-csout <path> - Write all C# to a single file located at <path>\n\
\n";