Remove code duplication.

    We were doing the same thing for binname and libname. Just
    use one codepath with a bool for the final target type.