Energetics of oxygen-octahedra rotations in perovskite oxides from first principles