Make cpu/cuda lib detection more robust