Variational formulation of phase transitions with glass formation