How to have compiler know whether Arduino or ESP8266?
There are a number of macros defined to help you with this:


__AVR__ for any board with AVR architecture.
ARDUINO_AVR_PRO for the Arduino Pro or Pro Mini selection in the Tools > Board menu.
ESP8266 for any ESP8266 board.
ARDUINO_ESP8266_NODEMCU for the NodeMCU 0.9 (ESP-12 Module) or NodeMCU 1.0 (ESP-12E Module) selections in the Tools > Board menu.


This allows you to do things like this:

#if defined(__AVR__)
// AVR specific code here
#elif defined(ESP8266)
// ESP8266 specific code here
#endif