ibc / AsyncEngine

Ruby asynchronous event driven framework on top of libuv
6 stars 1 forks source link

Detect OS and create #define #9

Open ibc opened 12 years ago

ibc commented 12 years ago

Common approach is to detect OS within extconf.rb and set a #define via this workaround:

def add_define(name)
  $defs.push("-D#{name}")
end

add_define "OS_UNIX"

But it's also possible to detect OS during C compilation. It seems that Windows compilers (including MinGW) sets _WIN32 and Unix compilers set __unix__. Not tested however. And what about BSD, OSX and so?

Useful info: http://en.wikipedia.org/wiki/C_preprocessor

ibc commented 12 years ago

Reply in ruby-talk by Jon Forums:

From libuv's include/uv.h header:

#if defined(__unix__) || defined(__POSIX__) || defined(__APPLE__)
# include "uv-private/uv-unix.h"
#else
# include "uv-private/uv-win.h"
#endif

To help prevent multi-platform lunacy, as I find useful macros I add them to a simple little C helper such as: https://gist.github.com/968522