The pure or viscous Cahn Hilliard equation with possibly singular potentials and dynamic boundary conditions is considered and the well-posedness of the related initial value problem is discussed. Then, a boundary control problem for the viscous Cahn Hilliard system is studied and rst order necessary conditions for optimality are shown. Moreover, the same boundary control problem is addressed for the pure Cahn Hilliard system, by investigating it and passing to the limit in the analogous results for the viscous Cahn Hilliard system as the viscosity coecient tends to zero.