Skip to content

Guidelines to platform specific code

LKostyra edited this page Oct 18, 2015 · 1 revision

Platform-specific code

Currently nfEngine supports two platforms - Windows and Linux (most preferable distribution and the one used to verify the project is latest Fedora version). To allow compilation on multiple platforms, nfEngine follows some minor guidelines.

Which modules are multi-platform

The only multi-platform modules should be the ones on the lowest level. Current modules containing platform-specific code are:

  • nfCommon
  • nfRendererOGL4

All higher level modules should be written without platform-specific code. This makes the process of development much easier.

Organisation in multi-platform modules

Multi-platform modules should be split into following parts:

  • Root directory of the module should contain platform-independent code and headers for multi-platform code.
  • "Win" directory should keep Windows-specific code and headers. The directory should be kept inside root directory of the module.
  • "Linux" directory should keep Linux-specific code and headers. The directory should be kept inside root directory of the module

Visual Studio Project Files should contain only platform-independent code and Win directory contents (other platform directories should be excluded).

CMakeLists should contain only platform-independent code and Linux directory contents (other platform directories should not be listed by CMakeLists).

Separating the code

The only way to separate the code according to currently compiled platform is by using Preprocessor Directives. These should be used only inside module header files on platform-independent part of the module.

Windows-specific code should be surrounded by following directives:

#ifdef WIN32
// Windows-specific code...
#endif // WIN32

Linux-specific code should be surrounded by following directives:

#if defined(__linux__) | defined(__LINUX__)
// Linux-specific code...
#endif // defined(__linux__) | defined(__LINUX__)

If all platform defines are unspecified, the preprocessor should report an error. Following block is very common across the entire project and is recommended to be used:

#ifdef WIN32
// Windows-specific code...
#elif defined(__linux__) | defined(__LINUX__)
// Linux-specific code...
#else
#error "Target platform not supported."
#endif // WIN32