string(REGEX REPLACE "[^\n]+cmd LC_RPATH\n[^\n]+\n[^\n]+path ([^\n]+) \\(offset[^\n]+\n" "rpath \\1\n" get_rpath_ov "${get_rpath_ov}")
string(REGEX MATCHALL "rpath [^\n]+" get_rpath_ov "${get_rpath_ov}")
string(REGEX REPLACE "rpath " "" rpath_list "${get_rpath_ov}")
list(FIND rpath_list "${new_rpath}" has_new_rpath)
if(NOT has_new_rpath EQUAL -1)
list(REMOVE_AT rpath_list ${has_new_rpath})
set(rpath_args)
else()
set(rpath_args -add_rpath "${new_rpath}")
endif()
foreach(rpath IN LISTS rpath_list)
list(APPEND rpath_args "-delete_rpath" "${rpath}")
endforeach()
if(NOT rpath_args)
continue()
endif()
execute_process(
COMMAND "${install_name_tool_cmd}" ${rpath_args} "${macho_file}"
OUTPUT_QUIET
ERROR_VARIABLE set_rpath_error
RESULT_VARIABLE set_rpath_exit_code
)
if(NOT "${set_rpath_error}" STREQUAL "" AND NOT set_rpath_exit_code EQUAL 0)
message(WARNING "Couldn't adjust RPATH of '${macho_file}': ${set_rpath_error}")
continue()
endif()
message(STATUS "Adjusted RPATH of '${macho_file}' to '${new_rpath}'")
endforeach()
endforeach()
list(LENGTH adjusted_shared_lib_old_ids last_adjusted_index)
if(NOT last_adjusted_index EQUAL 0)
math(EXPR last_adjusted_index "${last_adjusted_index} - 1")
foreach(macho_file IN LISTS macho_executables_and_shared_libs)
execute_process(
COMMAND "${otool_cmd}" -L "${macho_file}"
OUTPUT_VARIABLE get_deps_ov
RESULT_VARIABLE get_deps_rv
)
if(NOT get_deps_rv EQUAL 0)
message(FATAL_ERROR "Could not obtain dependencies list from '${macho_file}'")
endif()
foreach(i RANGE ${last_adjusted_index})
list(GET adjusted_shared_lib_old_ids ${i} adjusted_old_id)
z_vcpkg_regex_escape(
STRING "${adjusted_old_id}"
OUT_REGEX_ESCAPED_STRING_VAR regex
)
if(NOT get_deps_ov MATCHES "[ \t]${regex} ")
continue()
endif()
list(GET adjusted_shared_lib_new_ids ${i} adjusted_new_id)
execute_process(
COMMAND "${install_name_tool_cmd}" -change "${adjusted_old_id}" "${adjusted_new_id}" "${macho_file}"
OUTPUT_QUIET
ERROR_VARIABLE change_id_error
RESULT_VARIABLE change_id_exit_code
)
if(NOT "${change_id_error}" STREQUAL "" AND NOT change_id_exit_code EQUAL 0)
message(WARNING "Couldn't adjust dependent shared library install name in '${macho_file}': ${change_id_error}")
continue()
endif()
message(STATUS "Adjusted dependent shared library install name in '${macho_file}' (From '${adjusted_old_id}' -> To '${adjusted_new_id}')")
endforeach()
endforeach()
endif()
endfunction()