  We study how the shape of the spectrum of primordial gravitational waves can
be constrained by future experiments looking at the B-mode of the Cosmic
Microwave Background (CMB) polarization. We implement a Principal Component
Analysis (PCA) including the effects of diffuse foreground residuals, following
component separation, in the uncertainty of CMB angular power spectra, and
taking into account the gravitational lensing by Large Scale Structure. We
perform our study by considering the capabilities of future B-mode CMB
experiments such as LiteBIRD, the Simons Observatory (SO) and Stage-IV
(CMB-S4), in particular in detecting deviations of the primordial tensor
spectrum from the scale-invariant behavior. We find that diffuse foreground
residuals impact substantially both the derivation of the PCA basis and the
corresponding constraining power, in all cases. In particular, depending on
which experimental specifications and which value $r$ of tensor-to-scalar ratio
for cosmological perturbations are considered, adding foregrounds residuals can
determine an increase as large as a factor $\sim 4$ both on the uncertainty on
$r$ and on the recovery of the PCA modes. We study the limitations of the
methodology, including the effect of physicality priors on the PCA, which we
quantify via a Monte Carlo Markov chain (MCMC) analysis of the combined
cosmological and PCA power spectrum parameter space.
