Refactor pkg-config calls with a bash function, so as to ensure the correct environment everywhere. (cherry picked from commit fd44d929)