How are CDMA codes distributed?
I don't know how WCDMA does it, but CDMA2000 encodes the data twice or three times (it's been a while): once with a "long code" (42 or 43 bits, so it takes days to cycle through even at chip rates in the MHz), once with a "short code" (16 bits, with zero-stuffing so that the cycle is exactly 65536 chips long, and the spectrum is flat), and IIRC once with a 64-bit long Walsh code.  But There's a channel that's only encoded with the long code; that one is used by the base station to assign a specific set of codes and timings to the phone.

So, basically, it's done dynamically.

The CDMA2000 specs are out there.  When I looked (over ten years ago, which is why this answer is a bit fuzzy) the "real" spec was behind a paywall, but there were draft specifications that were pretty close.