Implementing cmake build

Replacement for PR #645.
Fixes: #647.
Fixes: #661.