Calculation of optical excitations in cubic semiconductors. II. Second-harmonic generation