Skip to content

Commit 4f8a207

Browse files
committed
fixing cmake function checking library compatibility
1 parent 2419789 commit 4f8a207

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

conf/cmake/functions.cmake

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ endmacro(macro_get_processor)
132132
# ---------------------------------------------------------------------------
133133
macro(macro_check_module_architect path_module target_name target_proc var_compatible)
134134
message(STATUS "Areg: >>> Validating binary '${path_module}' for compatibility with processor '${target_proc}'")
135-
135+
136136
# Determine the appropriate objdump command
137137
if (NOT "${CMAKE_OBJDUMP}" STREQUAL "")
138138
set(_objdump "${CMAKE_OBJDUMP}")
@@ -145,14 +145,31 @@ macro(macro_check_module_architect path_module target_name target_proc var_compa
145145
if (EXISTS "${path_module}")
146146

147147
set(_tool_exists FALSE)
148+
set(_data "")
148149
if (APPLE)
149-
set(_tool_exists TRUE) # lipo is always available
150-
execute_process(
151-
COMMAND bash -c "lipo -info ${path_module} | grep ^architecture | cut -d' ' -f2 | sort -u"
152-
OUTPUT_VARIABLE _data
153-
OUTPUT_STRIP_TRAILING_WHITESPACE
154-
ERROR_QUIET
155-
)
150+
# Check if it's a .tbd file (text-based stub) - these are multi-arch by design
151+
cmake_path(GET path_module EXTENSION _file_ext)
152+
if ("${_file_ext}" STREQUAL ".tbd")
153+
# .tbd files are text-based stubs that contain architecture info
154+
# Parse the targets line: "targets: [ x86_64-macos, arm64-macos, arm64e-macos ]"
155+
set(_tool_exists TRUE)
156+
file(STRINGS "${path_module}" _tbd_targets REGEX "^targets:")
157+
set(_data "${_tbd_targets}")
158+
else()
159+
# Use lipo for actual Mach-O binaries
160+
# lipo output: "Non-fat file: /path is architecture: arm64" or
161+
# "Architectures in the fat file: /path are: x86_64 arm64"
162+
execute_process(
163+
COMMAND lipo -info "${path_module}"
164+
OUTPUT_VARIABLE _data
165+
OUTPUT_STRIP_TRAILING_WHITESPACE
166+
ERROR_QUIET
167+
RESULT_VARIABLE _lipo_result
168+
)
169+
if (_lipo_result EQUAL 0)
170+
set(_tool_exists TRUE)
171+
endif()
172+
endif()
156173

157174
elseif (EXISTS "${_objdump}")
158175
set(_tool_exists TRUE)
@@ -166,20 +183,47 @@ macro(macro_check_module_architect path_module target_name target_proc var_compa
166183

167184
if (_tool_exists)
168185
macro_get_processor(${target_proc} _proc _bitness _found)
186+
set(_pos -1)
169187
# Match the processor type with extracted architecture
170188
if (${_proc} STREQUAL ${_proc_x86})
171-
string(FIND "${_data}" "x86-64" _pos)
189+
string(FIND "${_data}" "x86_64" _pos)
190+
if (_pos EQUAL -1)
191+
string(FIND "${_data}" "x86-64" _pos)
192+
endif()
172193
if (_pos EQUAL -1)
173194
string(FIND "${_data}" "i386" _pos)
174-
else()
175-
set(_pos -1)
195+
endif()
196+
# For x86 (32-bit), exclude if x86_64 is found
197+
if (_pos GREATER -1)
198+
string(FIND "${_data}" "x86_64" _pos64)
199+
string(FIND "${_data}" "x86-64" _pos64_alt)
200+
if ((_pos64 GREATER -1) OR (_pos64_alt GREATER -1))
201+
set(_pos -1)
202+
endif()
176203
endif()
177204
elseif (${_proc} STREQUAL ${_proc_x64})
178-
string(FIND "${_data}" "x86-64" _pos)
205+
string(FIND "${_data}" "x86_64" _pos)
206+
if (_pos EQUAL -1)
207+
string(FIND "${_data}" "x86-64" _pos)
208+
endif()
179209
elseif (${_proc} STREQUAL ${_proc_arm32})
180-
string(FIND "${_data}" "ARM" _pos)
210+
string(FIND "${_data}" "arm" _pos)
211+
# Exclude arm64
212+
if (_pos GREATER -1)
213+
string(FIND "${_data}" "arm64" _pos64)
214+
if (_pos64 GREATER -1)
215+
set(_pos -1)
216+
endif()
217+
endif()
181218
elseif (${_proc} STREQUAL ${_proc_arm64})
182-
string(FIND "${_data}" "AARCH64" _pos)
219+
# macOS uses "arm64", Linux uses "AARCH64" or "aarch64"
220+
string(FIND "${_data}" "arm64" _pos)
221+
if (_pos EQUAL -1)
222+
string(FIND "${_data}" "AARCH64" _pos)
223+
endif()
224+
if (_pos EQUAL -1)
225+
string(FIND "${_data}" "aarch64" _pos)
226+
endif()
183227
else()
184228
string(FIND "${_data}" "${_proc}" _pos)
185229
endif()
@@ -188,7 +232,7 @@ macro(macro_check_module_architect path_module target_name target_proc var_compa
188232
if (_pos GREATER -1)
189233
set(${var_compatible} TRUE)
190234
else()
191-
message(WARNING "Areg: >>> Binary '${path_module}' is NOT compatible with target processor '${target_proc}'")
235+
message(WARNING "Areg: >>> Binary '${path_module}' is NOT compatible with target processor '${target_proc}'. Detected: '${_data}'")
192236
endif()
193237
elseif (AREG_PLATFORM_WINDOWS)
194238
set(${var_compatible} TRUE)

0 commit comments

Comments
 (0)