What are the rules for declaring capital gains and cost basis when selling a portion of a property, not the whole thing?

For example, suppose I buy a property for $1 million and half of that is paid for by a mortgage, then several years later I sell a portion of the propery for $1 million. What are my capital gains? Can I say I "spent" $1 million even though I borrowed half of the purchase price, and therefore claim I had no capital gains, so I owe no taxes?

If I can't declare the full $1 million as my cost because I sold only a portion of the property, then how is it decided what the cost basis is for the portion that was sold?