Upvote:2
The rule is to apply to the country that will be your main destination and if there is no main destination, apply to the country you will enter first. The problem here is that the way you describe your plan, there is no obvious main destination (you would be staying the same time in Spain and Portugal) but Germany is clearly a secondary destination.
How the rule applies to this situation seems quite clear to me: You should apply to Germany. But alas I have heard about consulates making difficulties in such cases. (Effectively they seem to consider that if you are staying for a longer time in several countries, you should apply to one of them, but this “rule” is nowhere to be found in the regulation, see also the discussion in this answer to another question).
Alternatively, you could change your plan a little bit to stay longer in Spain or in Portugal (depending on what you want to see or perhaps on the most convenient consulate) and make one of them the main destination.
In any case, just try to apply as soon as possible and don't worry too much. If the consulate declines to process your application for that reason, it won't count as a refusal and you should get back the visa fee (not the “processing” or “service” fee from outsourcing companies, however) and all the documents quickly, which would allow you to reapply easily to another consulate (do keep copies in any case, it's always useful).