@@ -402,8 +402,34 @@ find_cuda_libraries_on_host() {
402402
403403 if [ -n " $matched " ]; then
404404 log_verbose " Found matches for ${library} : $matched "
405- # Do not quote $matched, since it can contain multiple libraries split by \n!
406- MATCHED_LIBRARIES+=($matched )
405+
406+ # Process each matched library and avoid duplicates by filename
407+ # Used `while - read <<< $matched`` to handle whitespaces and special characters.
408+ while IFS= read -r lib_path; do
409+ # Skip empty lines
410+ [ -z " $lib_path " ] && continue
411+
412+ # Extract just the filename from the path
413+ lib_name=$( basename " $lib_path " )
414+ echo " Checking library $lib_name for duplicates"
415+
416+ # Check if we already have this library filename in our matched libraries
417+ duplicate_found=0
418+ for existing_lib in " ${MATCHED_LIBRARIES[@]} " ; do
419+ existing_name=$( basename " $existing_lib " )
420+ if [ " $existing_name " = " $lib_name " ]; then
421+ log_verbose " Duplicate library found: $lib_name (existing: $existing_lib , currently processed: $lib_path )"
422+ log_verbose " Discarting $lib_path "
423+ duplicate_found=1
424+ break
425+ fi
426+ done
427+
428+ # If no duplicate found, add this library
429+ if [ " $duplicate_found " -eq 0 ]; then
430+ MATCHED_LIBRARIES+=(" $lib_path " )
431+ fi
432+ done <<< " $matched"
407433 else
408434 # There are some libraries, that weren't matched/found on the system
409435 log_verbose " No matches found for ${library} "
@@ -484,6 +510,22 @@ symlink_mode () {
484510 # Loop over each matched library
485511 for library in " ${MATCHED_LIBRARIES[@]} " ; do
486512 log_verbose " Linking library: ${library} "
513+
514+ # Get just the library filename
515+ lib_name=$( basename " $library " )
516+
517+ # Check if the symlink already exists
518+ if [ -L " $lib_name " ]; then
519+ # Check if it's pointing to the same target
520+ target=$( readlink " $lib_name " )
521+ if [ " $target " = " $library " ]; then
522+ log_verbose " Symlink for $lib_name already exists and points to correct target"
523+ continue
524+ else
525+ log_verbose " Symlink for $lib_name exists but points to wrong target: $target , updating..."
526+ rm " $lib_name "
527+ fi
528+ fi
487529
488530 # Create a symlink in the current directory
489531 # and check if the symlink was created successfully
0 commit comments