For computer science, it is common to mix these two approaches. At some point in your work, you start thinking about nice little "packages" into which you can wrap part of your work and that are independent contributions to the state of the art...the papers. You have an idea what your paper is about, and then you search for a conference that fits both the topic and that has a suitable submission deadline (not everyone can wait 11 months for the next year's deadline). You don't necessarily write the whole paper before that, but perhaps you already experiment with a few different introductions and see how they read or already write up a couple of technical components of your work before selecting a conference.
After you fixed the conference, you start writing the paper so that it fits that conference. This not only means writing towards the respective page limit and LaTeX style, but also thinking about whether you covered the aspects that are usually expected at that conference. Writing once and then searching for a conference can work if your paper is already very good or your are very good with editing papers - once everything is polished, making changes that are necessary to adapt the paper to a conference becomes harder.
Having said this, papers are sometimes rejected and resubmitted elsewhere. This is a lot of work and close to your "write your paper once and then search for conference idea". However, the rejection will probably come with valuable suggestions for improvement, so the paper probably needs to be edited heavily anyway.
It should be noted, however, that adapting your paper so that it fits well to a conference requires lots of experience with what the researchers frequenting a conference actually like to read about. The call for papers does not necessarily tell the whole story. So advise (e.g., from your advisor) is really helpful here.