Build system: Improved support for semidynamic modules.

Fixes cross-references and initialization order issues.