Does yang support feature based compilation

Is it possible to have ifdef kind of feature in yang so that, only if the feature is enabled, all the related leafs/lists gets compiled. Can you please suggest way to achieve this.

Yes, there is a if-feature option, which can be used to disable/enable parts of the data model. It can be
seen as #ifdef. Features have to be defined in the yang.

See confdc option -F. See feature and if-feature description in yang RFC https://tools.ietf.org/html/rfc6020#page-102

There is no #ifndef variant (you can define second feature meaning not) and if-feature cannot be used for some statments (e.g. import, etc.). For such cases it is possible to use tool to generate yang during build time, e.g. yangbuilder.