jacob-carlborg / dstep

A tool for converting C and Objective-C headers to D modules
204 stars 37 forks source link

Possible to ignore `#error` ? #234

Closed ghost closed 5 years ago

ghost commented 5 years ago

There's this C library that uses one include as a partial inclusion, which means that it's not supposed to work as a module, for example it relies on the includes of the files where is it itself included ( :clown_face: ) . With a little hack I managed to get dstep to generate the D interface but now the authors added an #error that breaks my workaround and prevent dstep to translate.

Can dstep ignore #error ?

ghost commented 5 years ago

This matches almost to this README paragraph

Umbrella headers. Some headers just serve to include other headers. If these other headers contain some form of protection, like #error, to be included directly this can cause problems for DStep

So i suppose it's dead...

jacob-carlborg commented 5 years ago

If you continue reading the readme you’ll see it mentions the -include flag. Can you use that?

ghost commented 5 years ago

No that does not work. Maybe I do this the wrong way. If someone wants to try:

The commands are

git clone https://github.com/Basile-z/libfirm-d/tree/master && git submodule --update --init
# required twice !
dub build && dub build

Then tweak the pre-generate command file:

diff --git a/pregenerate-linux.sh b/pregenerate-linux.sh
index 2f508c7..0e6b773 100644
--- a/pregenerate-linux.sh
+++ b/pregenerate-linux.sh
@@ -43,14 +43,14 @@ rm -rf "./src"
 mkdir "temp"
 cp -rf "./c/include/libfirm/." "./temp/"
 rm -rf "./temp/adt/"
-cp "./c/build/gen/include/libfirm/nodes.h" "./temp"
+#cp "./c/build/gen/include/libfirm/nodes.h" "./temp"

 ## fix https://github.com/libfirm/libfirm/issues/14
-FIXED=`grep 'include "firm_types.h"' "./temp/nodes.h"`
-if [ -z ${FIXED} ]; then
-    sed -i '5,8d' "./temp/nodes.h"
-    sed -i '5i #include <stddef.h>\n#include "firm_types.h"\n\n#include "begin.h"\n\n' "./temp/nodes.h"
-fi
+#FIXED=`grep 'include "firm_types.h"' "./temp/nodes.h"`
+#if [ -z ${FIXED} ]; then
+#    sed -i '5,8d' "./temp/nodes.h"
+#    sed -i '5i #include <stddef.h>\n#include "firm_types.h"\n\n#include "begin.h"\n\n' "./temp/nodes.h"
+#fi

 ## make the D interfaces
 ## FIXME: "dub run dstep -- ..." does not work
@@ -60,7 +60,8 @@ dstep ${H_FILES}\
  --package="firm"\
  --global-import="core.vararg"\
  --global-import="core.stdc.stddef"\
- --collision-action=ignore
+ --collision-action=ignore\
+ -<!! YOUR FIX HERE !!>
 mv "./src/temp" "./src/firm"
 rm -rf "./temp"

Basically this deactivates the copy of the generated nodes.h, comments the fix that uses sed and fixes the command line.

jacob-carlborg commented 5 years ago

The following command works (kind of): dstep nodes.h -- -include irnode.h. Kind of because I found an unrelated bug in DStep. The fix is available in master.

ghost commented 5 years ago

I tried this one after your first suggestion. So I need the next release to fix ? did you try the dub script in the test folder ? Because it may look it works while the declaration of some stuff miss.

jacob-carlborg commented 5 years ago

So I need the next release to fix ?

Yes, or compile yourself from master.

did you try the dub script in the test folder ? Because it may look it works while the declaration of some stuff miss.

No. I haven’t had time to do that yet.

ghost commented 5 years ago

Yeah thanks anyway for the support. i'll try myself right now.

jacob-carlborg commented 5 years ago

I could probably push out a new release later if you need it.

jacob-carlborg commented 5 years ago

did you try the dub script in the test folder ? Because it may look it works while the declaration of some stuff miss.

Hmm. It seems like typerep.d only contains comments and no declaration. When I run DStep only on the typerep.h it seems to work correctly. Not sure what's going on. This would require a debug session to figure out what's going on in DStep.

ghost commented 5 years ago

I didn't manage to get it to work so far (so with latest dstep changes). Only the dirty fix with sed works. Note that I don't blame dstep, rather the way the C library is organized.

jacob-carlborg commented 5 years ago

If you remove the usage of sed and invoke DStep like this it works for me:

dstep ${H_FILES}\
 -o "./src"\
 --package="firm"\
 --global-import="core.vararg"\
 --global-import="core.stdc.stddef"\
 --collision-action=ignore \
 -- -include ./c/include/libfirm/irnode.h \
 -I./temp

test/load_firm.d will fail because typerep.d doesn't contain any declarations. But I think that's another issue.

jacob-carlborg commented 5 years ago

BTW, please let me know if you need at new release of DStep.

ghost commented 5 years ago

for me, not so far thanks. I wouldn't use it for the CI of the library because of the test failure. Once again, it's very clear that the problem is not dstep.