Skip to content

Fix some ctypes errors #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Conversation

lbartoletti
Copy link
Contributor

This PR reduces error messages on BSD-like systems (FreeBSD and MacOs).
I am still stuck on xxintrin.h.

Only tested on my local machine (FreeBSD 12 amd64).

Still as a WIP for now.

@nilason
Copy link
Contributor

nilason commented Apr 15, 2020

If I'm correct this attempts to address the trac issue 3384.

@lbartoletti
Copy link
Contributor Author

If I'm correct this attempts to address the trac issue 3384.

Yes. Sorry I forgot to mention it.

@nilason
Copy link
Contributor

nilason commented Apr 16, 2020

For me, on mac with clang, only the addition of "__attribute__(x)=" to
lib/python/ctypes/ctypesgencore/parser/preprocessor.py:L126 made any difference, and quite a big too: ca 90 per cent reduction of (error) output.

It would be good if you could see what flags, or combinations thereof, causes actual improvements for you.

@neteler neteler added bug Something isn't working BSD Systems like FreeBSD labels Apr 29, 2020
@lbartoletti
Copy link
Contributor Author

@nilason all are needed and reduce noise.

I'm still stuck on xmmintrin.h:120: Syntax error at '{' blah. But it seems "normal"

@lbartoletti lbartoletti changed the title [WIP] Fix ctypes bsd Fix ctypes bsd May 4, 2020
@neteler
Copy link
Member

neteler commented May 20, 2020

Might it be an idea to sync this to upstream?

https://github.com/davidjamesca/ctypesgen/blob/master/ctypesgen/parser/preprocessor.py#L110

@nilason
Copy link
Contributor

nilason commented May 22, 2020

Might it be an idea to sync this to upstream?

https://github.com/davidjamesca/ctypesgen/blob/master/ctypesgen/parser/preprocessor.py#L110

I think so, when we found a good solution for this. It seems to me different platforms need different defines.
I recently updated the defines patch for macports (https://github.com/macports/macports-ports/blob/0ccc8cfc4d32f084daff81344593e22437d2c6c1/gis/grass7/Portfile#L263), but there are still a lot of "errors" to fix (which is why I haven't yet proposed to integrate).

@mboisson
Copy link

I have a very similar problem with Gentoo, by the way. No need to be on Mac to see this kind of errors.

@lbartoletti
Copy link
Contributor Author

@mboisson Is my patch can reduce the noise for you?

@neteler In a first step, this patch may be integrated into Grass?

@lbartoletti lbartoletti changed the title Fix ctypes bsd Fix some ctypes errors Nov 10, 2020
@mboisson
Copy link

@lbartoletti no, it doesn't. I still get stuff like :

Status: gcc -E -I/home/mboisson/.local/easybuild/software/2020/avx2/Core/wxwidgets/3.1.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/fftw/3.3.8/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/proj/6.3.2/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/geos/3.8.1/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/libspatialite/4.3.0a/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/gdal/3.0.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/netcdf/4.7.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/python/3.7.7/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/Core/imkl/2020.1.217/mkl/include       -I/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include -I/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include -D__GLIBC_HAVE_LONG_LONG -D__GNUCLIKE_BUILTIN_VARARGS -D__GNUCLIKE_BUILTIN_STDARG -U __GNUC__ -dD "-Dinline=" "-D__inline__=" "-D__extension__=" "-D_Bool=uint8_t" "-D__const=const" "-D__asm__(x)=" "-D__asm(x)=" "-DCTYPESGEN=1" "-D__attribute__(x)=" "-D__aligned(x)=" "-D_Noreturn=" "/tmp/tmp22ckoqkm.h"
Status: Parsing /tmp/tmp22ckoqkm.h
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:246: Syntax error at '__filename'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:247: Syntax error at '__modes'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:252: Syntax error at '__filename'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:253: Syntax error at '__modes'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:254: Syntax error at '__stream'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:304: Syntax error at '__stream'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:304: Syntax error at '__buf'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:308: Syntax error at '__stream'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:308: Syntax error at '__buf'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:309: Syntax error at 'size_t'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:314: Syntax error at '__stream'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:314: Syntax error at '__buf'
Error: /cvmfs/soft.computecanada.ca/gentoo/2020/usr/include/stdio.h:326: Syntax error at '__stream'
...

@mboisson
Copy link

Adding -std=c90 did get rid of those errors however... But it still fails with

Status: Processing description list.
Warning: Member "def" of Struct "Option" has been renamed to "_def" because it has the same name as a Python keyword.
Status: Writing to OBJ.x86_64-pc-linux-gnu/raster.py.
Status: Wrapping complete.
free(): invalid pointer
make[1]: *** [Makefile:107: OBJ.x86_64-pc-linux-gnu/raster.py] Abandon
make[1]: *** Suppression du fichier « OBJ.x86_64-pc-linux-gnu/raster.py »
make[1] : on quitte le répertoire « /tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/lib/python/ctypes »

@nilason
Copy link
Contributor

nilason commented Nov 10, 2020

It is my understanding these errors show on all platforms in various degree (see e.g. build logs on https://github.com/OSGeo/grass/actions), but they are all different and seemingly need a separate set of flags.
It would be ideal if we could find a way to address this in a more general way, perhaps through updating ctypesgen (see trac issue 3900).

@mboisson
Copy link

mboisson commented Nov 10, 2020

It sounds like you need to figure out what C or C++ standard the code is following, and stick with that in the compiler flags. The code is too exotic for its own good, and too reliant on special features.

@marisn
Copy link
Contributor

marisn commented Nov 11, 2020

Adding -std=c90 did get rid of those errors however... But it still fails with free(): invalid pointer

This is unrelated issue. Upgrade/downgrade your Python. No such problem with python 3.7.9 on ~AMD64.

@mboisson
Copy link

I'm using Python 3.7.7. You are saying this is an issue with specific versions of python ? Do you have a bug report to point to ? @marisn

@mboisson
Copy link

mboisson commented Nov 11, 2020

So, I tried with Python 3.8.2, and I get the exact same free(): invalid pointer error.
Also, with Python 3.7.9, I also get the exact same error. This does not seem to be related to the version of python.

GISRC=/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/demolocation/.grassrc78 GISBASE=/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu PATH="/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/bin:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/bin:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/scripts:$PATH" PYTHONPATH="/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/etc/python:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/gui/wxpython:$PYTHONPATH" LD_LIBRARY_PATH="/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/bin:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/bin:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/scripts:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/lib:/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/lib:" LC_ALL=C LANG=C LANGUAGE=C ./ctypesgen.py --cpp "gcc -E -I/home/mboisson/.local/easybuild/software/2020/avx2/Core/wxwidgets/3.1.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/fftw/3.3.8/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/proj/6.3.2/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/geos/3.8.1/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/libspatialite/4.3.0a/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/gdal/3.0.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/netcdf/4.7.4/include -I/home/mboisson/.local/easybuild/software/2020/avx2/Core/python/3.7.9/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/Core/imkl/2020.1.217/mkl/include  ""  -std=c90    -I/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include -I/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include -D__GLIBC_HAVE_LONG_LONG" -lgrass_raster.7.8   /tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include/grass/raster.h /tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include/grass/defs/raster.h -o OBJ.x86_64-pc-linux-gnu/raster.py
Status: Preprocessing /tmp/tmpwir6luuo.h
Status: gcc -E -I/home/mboisson/.local/easybuild/software/2020/avx2/Core/wxwidgets/3.1.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/fftw/3.3.8/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/proj/6.3.2/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/geos/3.8.1/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Core/libspatialite/4.3.0a/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/gdal/3.0.4/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx2/Compiler/gcc9/netcdf/4.7.4/include -I/home/mboisson/.local/easybuild/software/2020/avx2/Core/python/3.7.9/include -I/cvmfs/soft.computecanada.ca/easybuild/software/2020/Core/imkl/2020.1.217/mkl/include    -std=c90    -I/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include -I/tmp/mboisson/avx2/GRASS/7.8.4/gmkl-2020a/grass-7.8.4/dist.x86_64-pc-linux-gnu/include -D__GLIBC_HAVE_LONG_LONG -U __GNUC__ -dD "-Dinline=" "-D__inline__=" "-D__extension__=" "-D_Bool=uint8_t" "-D__const=const" "-D__asm__(x)=" "-D__asm(x)=" "-DCTYPESGEN=1" "/tmp/tmpwir6luuo.h"
Status: Parsing /tmp/tmpwir6luuo.h
Status: Processing description list.
Warning: Member "def" of Struct "Option" has been renamed to "_def" because it has the same name as a Python keyword.
Status: Writing to OBJ.x86_64-pc-linux-gnu/raster.py.
Status: Wrapping complete.
free(): invalid pointer
make[1]: *** [Makefile:107: OBJ.x86_64-pc-linux-gnu/raster.py] Abandon
make[1]: *** Suppression du fichier « OBJ.x86_64-pc-linux-gnu/raster.py »

@marisn
Copy link
Contributor

marisn commented Nov 12, 2020

So, I tried with Python 3.8.2, and I get the exact same free(): invalid pointer error.
Also, with Python 3.7.9, I also get the exact same error. This does not seem to be related to the version of python.

Sorry, my bad. I'm getting old and my memory isn't perfect any more. It is bug #435 Check for any extra proj .so files in your system.

@mboisson
Copy link

Thanks. That was it. I somehow had both PROJ 7.0.1 and PROJ 6.3.2...

mboisson added a commit to mboisson/grass that referenced this pull request Nov 12, 2020
@nilason
Copy link
Contributor

nilason commented Nov 18, 2020

The -std=c90 didn't improve this issue on mac, in fact compilation failed.

I did some further research into this. I do not think the "official" version of ctypesgen has changed that much in regard to this issue. Seems to me, it remains to tackle this through flags suited for different platforms.

With a simplified quantification of builds of the ctypes module with following code:

cd [grass-dir]
./configure [...]
make

# go to ctypes dir and remove compiled files
cd lib/python/ctypes
rm -rf OBJ*

# make changes in source- and/or make-files and compile ctypes module again
make &> make.log
# count number of lines starting with "Error:"
cat make.log | grep "^Error:*" | wc -l

I could reduce number of lines starting with "Error:" from 24547 to 551!
The ramaining errors springs from maybe only three separate error types (but difficult to handle).

Maybe changes along the following could be a way to address this (note I'm not sure how to correctly identify BDS systems):

diff --git a/lib/python/ctypes/Makefile b/lib/python/ctypes/Makefile
index f30c84011..9b1b88f8b 100644
--- a/lib/python/ctypes/Makefile
+++ b/lib/python/ctypes/Makefile
@@ -56,14 +56,18 @@ proj_INC        = $(PROJINC)
 vector_INC      = $(VECT_INC) $(VECT_CFLAGS)
 vedit_INC       = $(VECT_INC) $(VECT_CFLAGS)
 
-MAC_FLAGS = ""
+CTYPE_CFLAGS = -D__GLIBC_HAVE_LONG_LONG
 ifneq ($(findstring darwin,$(ARCH)),)
-MAC_FLAGS  = "-D_Nullable="
+CTYPE_CFLAGS += -D_Nullable= \"-D__attribute__(x)=\" -Drestrict= -D_Nonnull= -Dint8_t=char -DCF_INLINE= -D_Null_unspecified= -D__DARWIN_OS_INLINE= -DUC_INLINE=
+else
+ifneq ($(findstring freebsd,$(ARCH)),)
+CTYPE_CFLAGS += \"-D__attribute__(x)=\" \"-D__aligned(x)=\" -D_Noreturn= -D__GNUCLIKE_BUILTIN_VARARGS -D__GNUCLIKE_BUILTIN_STDARG
+endif
 endif
 
 SED = sed
 CTYPESGEN = ./ctypesgen.py
-CTYPESFLAGS = --cpp "$(CC) -E $(CPPFLAGS) $(LFS_CFLAGS) $(MAC_FLAGS) $(EXTRA_CFLAGS) $(NLS_CFLAGS) $(DEFS) $(EXTRA_INC) $(INC) -D__GLIBC_HAVE_LONG_LONG"
+CTYPESFLAGS = --cpp "$(CC) -E $(CPPFLAGS) $(LFS_CFLAGS) $(CTYPE_CFLAGS) $(EXTRA_CFLAGS) $(NLS_CFLAGS) $(DEFS) $(EXTRA_INC) $(INC)"
 EXTRA_CLEAN_FILES := $(wildcard ctypesgencore/*.pyc) $(wildcard ctypesgencore/*/*.pyc)
 
 ifneq ($(MINGW),)

@nilason nilason mentioned this pull request Jan 13, 2021
27 tasks
@neteler neteler requested a review from nilason October 10, 2021 19:31
Copy link
Contributor

@nilason nilason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lbartoletti
The ctypesgen package in main branch has been updated to community version of https://github.com/ctypesgen/ctypesgen. The patches needed for this to work with GRASS at all are listed in https://github.com/OSGeo/grass/blob/main/python/libgrass_interface_generator/README.md.
I believe __asm__ and __attribute__ definitions are now already dealt with. Please check out a build log from main branch.

I'm now actively contributing to incorporate these patches (in some way or another) upstreams. Ideally, real bugs (not annoying warnings) should be addressed over there [update: and should be patched in GRASS until it's fixed upstreams].
To silence some of these warnings, however, is best achieved through the Makefile, and preferably conditioned by platform.

@neteler neteler added this to the 8.0.1 milestone Dec 9, 2021
@wenzeslaus
Copy link
Member

The relevant code is now in python/libgrass_interface_generator. Unlike GitHub web interface, Git locally, should handle the merge with rename smoothly without a need to manually resolve the conflict unless there has been more changes.

@neteler
Copy link
Member

neteler commented Jan 12, 2022

The relevant code is now in python/libgrass_interface_generator. Unlike GitHub web interface, Git locally, should handle the merge with rename smoothly without a need to manually resolve the conflict unless there has been more changes.

@lbartoletti would you mind to rebase your PR?

@neteler neteler modified the milestones: 8.0.1, 8.0.2 Feb 20, 2022
@wenzeslaus wenzeslaus modified the milestones: 8.0.2, 8.2.0 Feb 27, 2022
Copy link
Member

@wenzeslaus wenzeslaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks okay, although I'm unsure about the bool there.

A ctypes related tests fails on Ubuntu, so that's suspicious (I didn't try to restart it). The Windows test fails, but perhaps for some unrelated reason. Maybe rebase/merge will help there.

There is a conflict with main (again, sorry) and it seems some things are now already fixed one main.

@nilason
Copy link
Contributor

nilason commented Apr 11, 2022

If there are any "defines" that are general and/or fixes real bugs they should be addressed to upstream ctypesgen. Otherwise I'm reluctant to add them at this point just to silence build log “Errors”, which are just failure to parse (mostly) non-standard C syntax (e.g. GNU C extensions).

Some notes:

  • A major culprit of these error messages are attributes which are heavily used in system libraries. Ctypesgen actually does support parsing attribute with packed, which is also supported with Ctypes. A proper fix for attributes with ctypesgen would be to implement full parsing of attribute syntax, not (re-)define.

  • There is already support for _Bool with GRASS embedded version of ctypesgen and I just added support for _Noreturn to upstream ctypesgen/ctypesgen@03ce79e.

  • By default ctypesgen undefines __GNUC__, to reduce the number of error messages connected with parsing GNU C extensions. I suppose this is the reason why defining __GNUCLIKE_BUILTIN_VARARGS and __GNUCLIKE_BUILTIN_STDARG may be needed to reduce still some. There is a new ctypesgen flag --allow-gnu-c which skips the undefinition of __GNUC__. Ideally, parsing with this flag should be the way to go, but for that to happen at least full parsing capabilities for attribute as well as asm syntax is needed.

Side note: it is not necessary to modify the ctypesgen source code (i.e. preprocessor.py) to add any “define”, the same is achieved by adding to CTYPESFLAGS in the Makefile (e.g. CTYPESFLAGS = -D __GNUCLIKE_BUILTIN_VARARGS … ).

In short: even though I really appreciate your effort @lbartoletti, I think this PR should be closed if none of the additions fixes real bugs (as the case was with e.g. #456).

@lbartoletti
Copy link
Contributor Author

In short: even though I really appreciate your effort @lbartoletti, I think this PR should be closed if none of the additions fixes real bugs (as the case was with e.g. #456).

Yes, this PR tends to reduce noise, but it was only a draft and I never succeeded to correct the xxintrin.h errors

@nilason
Copy link
Contributor

nilason commented Apr 12, 2022

In short: even though I really appreciate your effort @lbartoletti, I think this PR should be closed if none of the additions fixes real bugs (as the case was with e.g. #456).

Yes, this PR tends to reduce noise, but it was only a draft and I never succeeded to correct the xxintrin.h errors

I have been working for a while on a solution for parsing attributes and similar causes of error message. It's not easy, to say the least...
I hope you will not mind if I ping you for testing etc. whenever I put up a PR :-) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BSD Systems like FreeBSD bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants