Skip to content

Commit 09f4d6d

Browse files
wenju-heclaude
andcommitted
Build hwloc via ExternalProject_Add instead of raw add_custom_command
The static-hwloc autotools build (FetchContent + hand-written add_custom_command chain for autogen.sh/configure/make/make install) never forwarded CC/CXX to hwloc's ./configure, so it silently picked up the ambient host compiler instead of the one CMake was configured to use (e.g. icpx under a sysroot- or toolchain-file-pinned build), risking an ABI mismatch against the rest of the build. Switch to ExternalProject_Add, which fetches/patches/configures/builds/ installs hwloc as a single well-defined external project step and lets us explicitly pass CC/CXX through to configure. The Coverity security patch, previously applied via a post-FetchContent execute_process(), now runs through ExternalProject's PATCH_COMMAND for the non-Windows path. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent a81e75d commit 09f4d6d

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

CMakeLists.txt

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -319,43 +319,47 @@ if(UMF_LINK_HWLOC_STATICALLY)
319319
"Either install it, or set UMF_LINK_HWLOC_STATICALLY=OFF and install hwloc >= 2.3.0 in your system."
320320
)
321321
endif()
322-
FetchContent_Declare(
322+
include(ExternalProject)
323+
324+
set(hwloc_targ_PREFIX ${CMAKE_BINARY_DIR}/hwloc_targ-prefix)
325+
set(hwloc_targ_SOURCE_DIR ${hwloc_targ_PREFIX}/src/hwloc_targ)
326+
set(hwloc_targ_BINARY_DIR ${hwloc_targ_PREFIX})
327+
328+
# CC/CXX/CFLAGS/CXXFLAGS are not forwarded by CMake into the
329+
# autotools ./configure invocation below, so hwloc would otherwise
330+
# be built with the ambient host compiler instead of the one CMake
331+
# was configured to use (e.g. a sysroot- or toolchain-file-pinned
332+
# compiler), causing an ABI mismatch against the rest of the build.
333+
ExternalProject_Add(
323334
hwloc_targ
335+
PREFIX ${hwloc_targ_PREFIX}
324336
GIT_REPOSITORY ${UMF_HWLOC_REPO}
325-
GIT_TAG ${UMF_HWLOC_TAG})
326-
FetchContent_MakeAvailable(hwloc_targ)
327-
328-
add_custom_command(
329-
COMMAND ./autogen.sh
330-
WORKING_DIRECTORY ${hwloc_targ_SOURCE_DIR}
331-
OUTPUT ${hwloc_targ_SOURCE_DIR}/configure)
332-
add_custom_command(
333-
COMMAND
334-
./configure --prefix=${hwloc_targ_BINARY_DIR}
335-
--enable-static=yes --enable-shared=no --disable-libxml2
336-
--disable-pci --disable-levelzero --disable-opencl
337-
--disable-cuda --disable-nvml --disable-libudev --disable-rsmi
338-
CFLAGS=-fPIC CXXFLAGS=-fPIC
339-
WORKING_DIRECTORY ${hwloc_targ_SOURCE_DIR}
340-
OUTPUT ${hwloc_targ_SOURCE_DIR}/Makefile
341-
DEPENDS ${hwloc_targ_SOURCE_DIR}/configure)
342-
add_custom_command(
343-
COMMAND make
344-
WORKING_DIRECTORY ${hwloc_targ_SOURCE_DIR}
345-
OUTPUT ${hwloc_targ_SOURCE_DIR}/lib/libhwloc.la
346-
DEPENDS ${hwloc_targ_SOURCE_DIR}/Makefile)
347-
add_custom_command(
348-
COMMAND make install
349-
WORKING_DIRECTORY ${hwloc_targ_SOURCE_DIR}
350-
OUTPUT ${hwloc_targ_BINARY_DIR}/lib/libhwloc.a
351-
DEPENDS ${hwloc_targ_SOURCE_DIR}/lib/libhwloc.la)
337+
GIT_TAG ${UMF_HWLOC_TAG}
338+
UPDATE_DISCONNECTED TRUE
339+
PATCH_COMMAND git apply
340+
${PROJECT_SOURCE_DIR}/cmake/fix_coverity_issues.patch
341+
CONFIGURE_COMMAND
342+
${CMAKE_COMMAND} -E env
343+
CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER}
344+
<SOURCE_DIR>/autogen.sh
345+
COMMAND
346+
${CMAKE_COMMAND} -E env
347+
CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER}
348+
<SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
349+
--enable-static=yes --enable-shared=no
350+
--disable-libxml2 --disable-pci --disable-levelzero
351+
--disable-opencl --disable-cuda --disable-nvml
352+
--disable-libudev --disable-rsmi
353+
CFLAGS=-fPIC CXXFLAGS=-fPIC
354+
BUILD_COMMAND make
355+
BUILD_IN_SOURCE TRUE
356+
INSTALL_COMMAND make install
357+
BUILD_BYPRODUCTS <INSTALL_DIR>/lib/libhwloc.a)
352358

353-
add_custom_target(hwloc_prod
354-
DEPENDS ${hwloc_targ_BINARY_DIR}/lib/libhwloc.a)
355359
add_library(hwloc INTERFACE)
356360
target_link_libraries(hwloc
357361
INTERFACE ${hwloc_targ_BINARY_DIR}/lib/libhwloc.a)
358-
add_dependencies(hwloc hwloc_prod)
362+
add_dependencies(hwloc hwloc_targ)
359363

360364
set(LIBHWLOC_LIBRARY_DIRS ${hwloc_targ_BINARY_DIR}/lib)
361365
set(LIBHWLOC_INCLUDE_DIRS ${hwloc_targ_BINARY_DIR}/include)
@@ -372,8 +376,10 @@ if(WINDOWS)
372376
message(STATUS " LIBHWLOC_DLL_DIRS = ${LIBHWLOC_DLL_DIRS}")
373377
endif()
374378

375-
if(hwloc_targ_SOURCE_DIR)
376-
# Apply security patch for HWLOC
379+
if(WINDOWS AND hwloc_targ_SOURCE_DIR)
380+
# Apply security patch for HWLOC. On non-Windows this is applied via
381+
# ExternalProject_Add's PATCH_COMMAND instead, since that source tree
382+
# is only populated at build time, not at configure time.
377383
execute_process(
378384
COMMAND git apply ${PROJECT_SOURCE_DIR}/cmake/fix_coverity_issues.patch
379385
WORKING_DIRECTORY ${hwloc_targ_SOURCE_DIR}

0 commit comments

Comments
 (0)