  We consider a variety of physical systems in which one has states that can be
thought of as generalised instantons. These include Yang--Mills theories on
manifolds with a torsion-free $G$-structure, analogous gravitational instantons
and certain supersymmetric solutions of ten-dimensional supergravity, using
their formulation as generalised $G$-structures on Courant algebroids. We
provide a universal algebraic construction of a complex, which we call the BPS
complex, that computes the infinitesimal moduli space of the instanton as one
of its cohomologies. We call a class of these spinor type complexes, which are
closely connected to supersymmetric systems, and show how their Laplacians have
nice properties. In the supergravity context, the BPS complex becomes a double
complex, in a way that corresponds to the left- and right-moving sectors of the
string, and becomes much like the double complex of $(p,q)$-forms on a K\"ahler
manifold. If the BPS complex has a symplectic inner product, one can write down
an associated linearised BV Chern--Simons theory, which reproduces several
classic examples in gauge theory. We discuss applications to
(quasi-)topological string theories and heterotic superpotential functionals,
whose quadratic parts can also be constructed naturally from the BPS complex.
