From: Joshua H. <jha...@pe...> - 2013-02-04 23:24:54
|
So I'm up and running more or less, I figure I'll include some information to help out in case someone stumbles across this thread and is attempting to do something similar. I'll add some corrections to what actually works to my previous example of the swig interface file: /* headers where all declarations are wanted */ %include "../../include/header1" %include "../../include/header2" /* ignore all */ %ignore ""; /*establishing ctor and dtors from structs in header1 & 2 it seems all functions and data is pulled in and works fine from the first headers it's just the structures in my case that need special rename statements here */ %rename("%s") header1struct1; %rename("%s") ~header1struct1; %rename("%s") header1struct2; %rename("%s") ~header1struct2; /*everything that is pulled in from the headers below. For structures you must include all structure members and a dtor line*/ %rename("%s") header3struct1; %rename("%s") header3struct1member1; %rename("%s") header3struct1member2; %rename("%s") ~header3struct1; %rename("%s") header4contantdata; %rename("%s") header4contantdata; %rename("%s") header4contantdata; %rename("%s") header4contantdata; /* headers where only what is renamed above is wrapped */ %include "../../include/header3.h" %include "../../include/header4.h" While this seems to work I now run into a problem with the python help command. Furthermore since I have to specifically call out every struct in header1 & 2, as well as every member in 3 & 4 -- I'm not sure I'm actually gaining much by using this method. However if you are early in the dev cycle where you expect change of data and header files; this still may be a good option for people out there to have very selective imports. As always if someone has a good suggestion on a better way to accomplish my goals here, I'm all ears. If there is anything I can do to help out the project with this data, please feel free to let me know. While unfortunately I'm not a skilled C programmer. I'll be more than happy to file some bugs or do some testing along these lines Cheers Joshua Hawkinson Sr Test & Automation Engineer On Fri, Feb 1, 2013 at 6:47 PM, Joshua Hawkinson <jha...@pe...>wrote: > oh it occurs to me that I left out some basic information.. I'm running > swig 2.0.8 wrapping flat C to a python 3.3. > > so I've got the constructors working using a %inline and I basically have > to redefine struct again (which completely goes against the point). > > I was hoping that I could use an extend like this > %ignore ""; > %rename("$s") foo; > %extend foo { > foo() { > struct foo *ret = malloc(sizeof (*ret)); > return ret; > } > ~foo() { > free($self); > } > } > > this crashes with an invalid free of course... > > I was wondering if I could point to the default ctor and dtors with this > method, and wondering where I can find them > > I'm still up and running with the old model of duplicating header file > code, but once more any guidance about the methods I'm trying to use or if > there is a more appropriate way would be greatly appriciated > > Cheers! > Josh > > > On Thu, Jan 31, 2013 at 4:37 PM, Joshua Hawkinson <jha...@pe...>wrote: > >> >> Hello All, >>> >>> First off let me say that I've been using SWIG with a custom header file >>> by copying in all of the relevant data and then building the SWIG wrapper >>> from that, and it works great! However as time goes on, rather than have to >>> maintain header files with a lot of duplicate data I was hoping to make a >>> interface file that was more purely code generated so I didn't have to keep >>> duplicating checkins in multiple places (one for our real header files and >>> another for the SWIG interface). So my solution after reading some of the >>> Docs was to include everything from a few header files, and use a %ignore >>> ""; and %rename to import selective symbols from a few more header files. >>> Here is an example of my interface file... >>> >>> /* SWIG interface file for swig client */ >>> %module swigname >>> %{ >>> #include <stdio.h> >>> #include <stdlib.h> >>> #include <string.h> >>> #include <unistd.h> >>> #include <sys/types.h> >>> #include <sys/wait.h> >>> #include <arpa/inet.h> >>> #include <netinet/in.h> >>> #include <errno.h> >>> #include <time.h> >>> #include "custom hearder files" >>> ... >>> ... many lines of headers omitted >>> ... >>> %} >>> %include "stdint.i" >>> >>> /* SWIG Statemetns */ >>> >>> %apply uint16_t { internal data types }; >>> %apply uint32_t { internal data types }; >>> %apply uint64_t { internal data types }; >>> %apply long { ssize_t }; >>> %apply unsigned long { size_t }; >>> >>> %define %pybuffer_mutable_binary(TYPEMAP, SIZE) >>> %typemap(in) (TYPEMAP, SIZE) >>> (int res, Py_ssize_t size = 0, void *bufp = 0) { >>> res = PyObject_AsWriteBuffer($input, &bufp, &size); >>> if (res<0) { >>> PyErr_Clear(); >>> %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); >>> } >>> $1 = ($1_ltype) bufp; >>> $2 = ($2_ltype) (size/sizeof($*1_type)); >>> } >>> %enddef >>> >>> %pybuffer_mutable_binary(void *buf, uint64_t nbytes); >>> %pybuffer_mutable_binary(const void *buf, uint64_t nbytes); >>> %pybuffer_mutable_binary(char *buf, uint64_t nbytes); >>> >>> %typemap(in,numinputs=0) ns_dir_t **dir (ns_dir_t *temp1) { >>> $1 = &temp1; >>> } >>> %typemap(argout) ns_dir_t **dir { >>> %set_output(SWIG_NewPointerObj(SWIG_as_voidptr(*$1), $*1_descriptor, >>> SWIG_POINTER_OWN)); >>> } >>> >>> #define __attribute__(x) >>> >>> /* headers where all declarations are wanted */ >>> %include "../../include/header1" >>> %include "../../include/header2" >>> ... many lines of headers omitted >>> >>> /* ignore all */ >>> %ignore ""; >>> >>> /*everything that is pulled in from the headers below */ >>> %rename("%s") struct1; >>> %rename("%s") struct2; >>> %rename("%s") contantdata; >>> %rename("%s") contantdata; >>> %rename("%s") contantdata; >>> %rename("%s") contantdata; >>> >>> ... many lines of symbols omitted >>> >>> %rename("%s") O_RDONLY; >>> %rename("%s") O_WRONLY; >>> %rename("%s") O_APPEND; >>> %rename("%s") O_SYNC; >>> %rename("%s") O_CREATE; >>> %rename("%s") O_TRUNC; >>> %rename("%s") O_EXCL; >>> %rename("%s") O_RDWR; >>> >>> /* headers where only what is renamed above is wrapped */ >>> %include "../../include/header3.h" >>> %include "../../include/header4.h" >>> ... many lines of headers omitted >>> >>> /* end swig.i */ >>> >>> I thought this would be ideal as I would get all of the modifications to >>> the structs and data auto-magically. I would only have to touch this file >>> if there was a name change in something (which would be perfect!). >>> >>> so the problems.... I notice that all structs in the header files above >>> the %ignore ""; statement lose both their constructor and deconstructor. >>> All of the structs below lose only their dtors. According to the docs, the >>> ignore statement should only effect items below it so it seems that perhaps >>> there is a bug there. >>> >>> Also if you notice the fact that I have a portion of pybuffer.i in the >>> interface file it is due to the use of "buf" in it. As our code too uses >>> "buf" so there was a name conflict created. It seems like in the SWIG code >>> you should use crazy names (read: marked up or annotated) to prevent such >>> conflicts so I thought I'd bring that up as well. >>> >>> And finally the obvious questions, I'm still reading around on the >>> proper way to reproduce the default ctors and dtors (I'm still a swig >>> noob). so if anyone has some guidance there it would be most helpful. >>> >>> Thank you very much for your time >>> Joshua Hawkinson >>> >>> >>> >> > |