Solution (source code)

= Solution

Let the surface be at $R$ with vanishing external <pressure>. Multiply <hydrostatic equilibrium> by $4\pi r^3$ and integrate:
$$
\int_0^R4\pi r^3P'(r)\,dr
=[4\pi r^3P]_0^R-3\int_0^R4\pi r^2P\,dr
=-3\langle P\rangle V.
$$
The other side is $-\int_0^R4\pi Gm(r)\rho(r)r\,dr=-\int_0^M Gm/r(m)\,dm$. Building the star one shell at a time identifies this as its negative <gravitational potential energy> $E_{\rm grav}$. Therefore
$$
\boxed{3\langle P\rangle V=-E_{\rm grav}.}
$$
If the star has nonzero surface <pressure> $P_s$, the integration instead gives $3\langle P\rangle V-3P_sV=-E_{\rm grav}$; the requested form assumes $P_s=0$.