Skip to content
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

compilation error in psutil/_psutil_linux.c when including ethtool.h #659

Closed
maozguttman opened this issue Jul 23, 2015 · 12 comments
Closed

Comments

@maozguttman
Copy link

Hi,

I was trying to build psutil 3.1.1 and got compilation errors from psutil/_psutil_linux.c:
/usr/include/linux/ethtool.h:18:2: error: expected specifier-qualifier-list before 'u32'
The problem is that u32 is undefined in /usr/include/linux/ethtool.h.
ethtool.h looks like: https://www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/include/linux/ethtool.h
It has no #include statements in it.

I am using: SUSE 10, Kernel 2.6.16.60

Thanks,
Maoz

@giampaolo
Copy link
Owner

can you provide a patch / PR?

@maozguttman
Copy link
Author

Adding following code to psutil/_psutil_linux.c before #include < linux/ethtool.h > will work for me (hoping it will not break things on other Unix platforms)

// hack, so we may include kernel's ethtool.h
#include < linux/types.h >
typedef __u64 u64;
typedef __u32 u32;
typedef __u16 u16;
typedef __u8 u8;

@giampaolo
Copy link
Owner

It seems https://www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/include/linux/types.h should define those types.
I tried to fix it in 9503109.
Please let me know it it works.

@maozguttman
Copy link
Author

Hi,

Your suggested fix doesn't work in my environment. Getting the same error.

In my environment /usr/include/linux/types.h includes /usr/include/asm/types.h which includes /usr/include/asm-x86_64/types.h or /usr/include/asm-i386/types.h based on the platform.
u8/u16/u32 u3/u16/u32 types defined in asm-x86_64/types.h and asm-i386/types.h.
However, they are wrapped with ifdef with the following comment above them: "These aren't exported outside the kernel to avoid name space clashes"
Please take a look in: https://www.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/include/asm-i386/types.h

Another way it to workaround asm-x86_64/types.h by adding the following code to _psutil_linux.c before the first #include in that file:

#ifdef KERNEL
#include < asm/types.h >
#else
#define KERNEL
#include < asm/types.h >
#undef KERNEL
#endif

But I don't really like it. I think that my first suggestion is the most generic.

Thanks,
Maoz

@maozguttman
Copy link
Author

I found the first workaround (with the typedefs) in the web.
For example:
http://net-snmp.sourcearchive.com/documentation/5.4.1~dfsg-12ubuntu3/interface__linux_8c-source.html

Maoz

mrjefftang added a commit to mrjefftang/psutil that referenced this issue Aug 3, 2015
@mrjefftang
Copy link
Collaborator

I took a look at that net-snmp fix and it looks like they are relying on a compile time flag.

@giampaolo
Copy link
Owner

@mrjefftang can you please make a PR based on mrjefftang@b82b8be? Or you can also commit it directly.

@mrjefftang
Copy link
Collaborator

I haven't made a PR because that commit didn't work. It relies on a compiler time flag.

@maozguttman
Copy link
Author

Maybe you can do like ./configure does when building a C++ applications.
Meaning, check if can include linux/ethtool.h with or without any workaround.

psutil/_psutil_linux.c will look like:

#ifdef ETHTOOL_MISSING_TYPES
#include <linux/types.h>
typedef __u64 u64;
typedef __u32 u32;
typedef __u16 u16;
typedef __u8 u8;
#endif
#include <linux/ethtool.h>

And in setup.py, you will check if to define ETHTOOL_MISSING_TYPES. Something like:

...

import os
import sys
try:
    from setuptools import setup, Extension, UnixCCompiler, CompileError
except ImportError:
    from distutils.core import setup, Extension
    from distutils.unixccompiler import UnixCCompiler
    from distutils.errors import CompileError

...

elif sys.platform.startswith("linux"):
    compiler = UnixCCompiler()
    ETHTOOL_MACRO = ()
    try:
        compiler.compile( ('psutil/_test_ethtool.c', ))
    except CompileError:
        ETHTOOL_MACRO = ( "ETHTOOL_MISSING_TYPES", 1 )
    extensions = [Extension(
        'psutil._psutil_linux',
        sources=['psutil/_psutil_linux.c'],
        define_macros=[VERSION_MACRO, ETHTOOL_MACRO]),
        posix_extension,
    ]

Where psutil/_test_ethtool.c looks like:

#include <linux/ethtool.h>
  • I don't know if the change in the import of setuptools is correct. If it has UnixCCompiler and CompileError classes
  • I know how to set an output area for compiler.compile. But I don't know how to get it in setup.py

@giampaolo
Copy link
Owner

I don't particularly like the solution of compiling a separate C file in setup.py buy I've extensively searched for a solution and couldn't find any. I committed a variant of your patch as of b8adcd7. Can you please confirm it works?

@maozguttman
Copy link
Author

It works!

There is one cosmetic fix that you might want to do.
Many errors (from gcc) are printed because the compilation of temporary c-file that includes "linux/ethtool.h" fails in my environment.
These errors might be confusing and people will not be aware that they should ignore them.

@giampaolo
Copy link
Owner

Done in 8d66475.
Closing.

mrjefftang added a commit to mrjefftang/psutil that referenced this issue Sep 2, 2015
* giampaolo/master: (34 commits)
  pre-release updates
  try to fix appveyor failure
  refactor setup.py + fix Makefile
  fix OSX test failure
  linux / setup.py: do not print warnings
  giampaolo#675: [Linux] net_connections(); UnicodeDecodeError may occur when listing UNIX sockets
  move unicode tests so that they are executed on all platforms
  skip failing test on appveyor
  try to fix appveyor failure
  test refactoring+
  try to fix appveyor failures
  open files by using sys.getfilesystemencoding() + refactor stuff
  giampaolo#675: try to fix encoding issue
  fix python 3 compilation error
  giampaolo#659: [Linux] compilation error on Suse 10. (patch by maozguttman)
  make flake8 happy
  fix giampaolo#644: add support for CTRL_* signals on Windows
  str-encode NIC names
  always return encoded strings instead of unicode
  giampaolo#650: make cmdline() handle unicode on python 2
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants