To spectrally characterize Earth-like planets around nearby stars with a coronagraph, an extreme adaptive optics (ExAO) system is mandatory. The correction of the amplitude and phase aberrations in the wavefront on both sides of the image plane and in sufficiently large bandwidths can be done with two deformable mirrors (DM) in a pupil mapping configuration. While this system is primarily intended to correct for aberrations, it can potentially be used to improve the contrast beyond the nominal value set by the coronagraph; the two DMs can be seen as a complex apodizer. We present solutions to two types of numerical optimization problems. Our first approach consists in maximizing the sum of the real and the imaginary parts of the electric field in the pupil plane, while constraining the intensity of the electric field in chosen regions of the the subsequent image plane to be less than a chosen extremum. The solutions can be translated in term of modulus and phase. The optimal modulus is very close to 1, and the high-contrast is induced by a binary phase shift, which cannot be induced with current deformable mirrors. Our second approach consists in directly optimizing the stroke commands sent to a deformable mirror. Solutions are computed by either solving successive linear optimizations or non-linear optimizations. For a telescope with a 30% central obscuration, a 3λ/D inner working angle and a 10λ/D outer working angle, a 10-6-10-7 is reached after a dozen iterations, and the coronagraph has a 60-80% throughput. Shaped pupils are then computed to lower that contrast down to 10-9-10-10.