Detect which Arduino board with code

I'd like my sketch to be able to detect which kind of Arduino board it's running on. How can I do that?

There is no way to do this from a running sketch.

For much the same effect, you can use #ifdefs to conditionally compile different code per Arduino platform.

I haven't found a way to make it pretty, but you can see how Bitlash does it at this link, starting around line 215: