Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swig -includeall except...

Tags:

c++

swig

I have a large project where using the Swig -includeall flag seems to make sense. However, there are certain files I would not like expanded, primarily the STL libraries (e.g. vector and list). Is it possible to use the -includeall flag, but to blacklist certain files from expansion (like vector and list)?

like image 810
bremen_matt Avatar asked Sep 20 '17 19:09

bremen_matt


2 Answers

From the Docs about the SWIG Preprocessor:

SWIG fully supports the use of #if, #ifdef, #ifndef, #else, #endif to conditionally include parts of an interface. The following symbols are predefined by SWIG when it is parsing the interface:
SWIG - Always defined when SWIG is processing a file

So you can write something like this:

#ifndef SWIG

#include <string>
#include <vector>

#endif // !SWIG

and SWIG will ignore it during the -includeall pass.

like image 164
Shlomi Loubaton Avatar answered Oct 21 '22 20:10

Shlomi Loubaton


I'm not an expert in SWIG but taking a look at both documentation for latest version and source code (specifically to the Source/Modules/main.cxx file, where command line arguments are read), is it clear that such option doesn't exist (even not a hidden one).

On the other hand, if you feel like you can modify the source code quite easily to do so.

You can add a new command line option in the main.cxx file to add filenames to exclude and then compare such names to find a match. You can add a global function in the Source/Preprocessor/preprocessor.h file, which is already included by main.cxx.

The code for the -includeall option is in Source/Preprocessor/cpp.c. In that file there is also a global variable called include_all that is set to 1 when the analog argument is set in the command line (it will guide you to find where such option is executed too).

Now, in the Preprocessor_parse(...) function you can find where the header files are parsed (starting at line 1715 for version 3.0.12):

s1 = cpp_include(fn, sysfile);
if (s1) {
  /* ....... */
}

You will be interested in the String *Swig_last_file(void) function, that will return the filename of the header line just parsed.

s1 = cpp_include(fn, sysfile);
if (s1) {
  int found = 0;

  String* filename = Swig_last_file();
  /* Here find for a match in the exclusion list */

  if (!found) { /* keep working as usual */
    /* ....... */
  } /* if found, just ignore the include directive for that file */

  Delete(s1);
}

I know it is not a full solution but hope can guide you to get the desired behavior.

like image 43
cbuchart Avatar answered Oct 21 '22 21:10

cbuchart