Skip to content

Commit

Permalink
Merge pull request #493 from scivision/win32-gcc
Browse files Browse the repository at this point in the history
WIndows CMake: work with non-MSVC compilers too

This was tested with GCC, Clang, and Intel oneAPI on Windows.
Fixes #492

Adds numerous tests as in Autotools-based builds.

This is also the foundation for using CMake on any platform instead of autotools.

Avoids the constant full rebuilding by using configure_file() instead of explicit file()
  • Loading branch information
bgoglin committed Nov 26, 2021
2 parents 67644a5 + 23506ae commit 69dd7f8
Show file tree
Hide file tree
Showing 6 changed files with 902 additions and 49 deletions.
9 changes: 6 additions & 3 deletions contrib/ci.inria.fr/job-1-wincmake.bat
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ if %errorlevel% neq 0 exit /b %errorlevel%
cd %TARBALL:~0,-7%\contrib\windows-cmake
if %errorlevel% neq 0 exit /b %errorlevel%

cmake -DCMAKE_INSTALL_PREFIX=%cd%\install -DCMAKE_BUILD_TYPE=Release .
cmake --install-prefix=%cd%/install -DCMAKE_BUILD_TYPE=Release -B build
if %errorlevel% neq 0 exit /b %errorlevel%

cmake --build .
cmake --build build --parallel
if %errorlevel% neq 0 exit /b %errorlevel%

cmake --build . --target INSTALL
cmake --install build
if %errorlevel% neq 0 exit /b %errorlevel%

install\bin\lstopo-no-graphics.exe -v
Expand All @@ -38,4 +38,7 @@ if %errorlevel% neq 0 exit /b %errorlevel%
install\bin\hwloc-info.exe --support
if %errorlevel% neq 0 exit /b %errorlevel%

ctest --test-dir build -V
if %errorlevel% neq 0 exit /b %errorlevel%

exit /b 0
161 changes: 116 additions & 45 deletions contrib/windows-cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,107 @@ project(hwloc
LANGUAGES C
VERSION 2.7.0)

# Available configuration options
# HWLOC_SKIP_LSTOPO doesn't build/install lstopo
# HWLOC_SKIP_TOOLS doesn't build/install other hwloc tools
# HWLOC_SKIP_INCLUDES doesn't install headers
enable_testing()

option(HWLOC_ENABLE_TESTING "Enable testing" ON)
option(HWLOC_SKIP_LSTOPO "don't build/install lstopo")
option(HWLOC_SKIP_TOOLS "don't build/install other hwloc tools")
option(HWLOC_SKIP_INCLUDES "don't install headers")
option(HWLOC_WITH_LIBXML2 "use libxml2 instead of minimal XML")
option(HWLOC_WITH_OPENCL "enable OpenCL support")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
option(HWLOC_WITH_CUDA "enable CUDA support")
endif()
option(HWLOC_BUILD_SHARED_LIBS "build shared libraries" ${BUILD_SHARED_LIBS})

set(TOPDIR ../..)
set(TOPDIR ${PROJECT_SOURCE_DIR}/../..)

# Configure based on the MSVC config
# "libhwloc.*" naming for MSVC and non-MSVC

configure_file(${TOPDIR}/contrib/windows/hwloc_config.h include/hwloc/autogen/config.h COPYONLY)
configure_file(${TOPDIR}/contrib/windows/static-components.h include/static-components.h COPYONLY)
configure_file(${TOPDIR}/contrib/windows/private_config.h include/private/autogen/config.h COPYONLY)

file(READ ${CMAKE_CURRENT_BINARY_DIR}/include/private/autogen/config.h PRIVATE_CONFIG_H)
string(REPLACE "#define SIZEOF_VOID_P 8" "#define SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
# Configure dynamically based on platform capabilities
include(CheckIncludeFile)
include(CheckSymbolExists)
include(CheckCSourceCompiles)

check_include_file("dirent.h" HAVE_DIRENT_H)
check_include_file("unistd.h" HAVE_UNISTD_H)
check_include_file("malloc.h" HAVE_MALLOC_H)
check_include_file("memory.h" HAVE_MEMORY_H)

check_symbol_exists(mkstemp "stdlib.h" HAVE_MKSTEMP)
check_symbol_exists(memalign "malloc.h" HAVE_MEMALIGN)

check_symbol_exists(strncasecmp "strings.h" HAVE_STRNCASECMP)
if(MSVC AND HAVE_STRNCASECMP)
set(hwloc_strncasecmp 1)
set(hwloc_strncasecmp_fcn strncasecmp)
else()
set(hwloc_strncasecmp 0)
set(hwloc_strncasecmp_fcn strncmp)
endif()


set(SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
# disable x86 entirely by default
string(REPLACE "#define HWLOC_X86_32_ARCH 1" "/* #undef HWLOC_X86_32_ARCH */" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
string(REPLACE "#define HWLOC_X86_64_ARCH 1" "/* #undef HWLOC_X86_64_ARCH */" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
# and now reenable x86-36 or x86-64 if detected
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set(HWLOC_X86_32_ARCH)
set(HWLOC_X86_64_ARCH)
set(HWLOC_HAVE_X86_CPUID 1)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(^AMD64$|^x86_64$)")
# "AMD64" on Windows, "x86_64" on Linux
string(REPLACE "/* #undef HWLOC_X86_64_ARCH */" "#define HWLOC_X86_64_ARCH 1" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86" OR CMAKE_SYSTEM_PROCESSOR MATCHES "i.86")
set(HWLOC_X86_64_ARCH 1)
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "(^x86$|i.86)")
# "x86" on Windows, "i.86" on Linux
string(REPLACE "/* #undef HWLOC_X86_32_ARCH */" "#define HWLOC_X86_32_ARCH 1" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
set(HWLOC_X86_32_ARCH 1)
else()
set(HWLOC_HAVE_X86_CPUID 0)
endif()

check_c_source_compiles("int main(void) {int cpuinfo[4]; __cpuidex(cpuinfo,0,0); return 0;}"
HWLOC_HAVE_MSVC_CPUIDEX
)

# the following lines are disabled until we are sure they are safe with old build environmentx
# - snprintf() returned broken values in the past, hwloc detects it during configure (see 7a4ee26510c06b55fc04aaccbfa18d0ca3b87198)
# string(REPLACE "#define HAVE_DECL_SNPRINTF 0" "#define HAVE_DECL_SNPRINTF 1" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
# set(HAVE_DECL_SNPRINTF 1)
# - strtoull() had some issues in the past (see 9559bd08b79ef63dce45df87fb7f875b73ecb512)
# string(REPLACE "#define HAVE_DECL_STRTOULL 0" "#define HAVE_DECL_STRTOULL 1" PRIVATE_CONFIG_H "${PRIVATE_CONFIG_H}")
# set(HAVE_DECL_STRTOULL 1)

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/include/private/autogen/config.h "${PRIVATE_CONFIG_H}")
# --- optional external libraries
set(HWLOC_HAVE_LIBXML2)
if(HWLOC_WITH_LIBXML2)
find_package(LibXml2 REQUIRED)
set(HWLOC_HAVE_LIBXML2 1)
endif()

set(HWLOC_HAVE_OPENCL)
if(HWLOC_WITH_OPENCL)
find_package(OpenCL REQUIRED)
set(HWLOC_HAVE_OPENCL 1)
endif()

set(HAVE_CUDA)
set(HAVE_CUDA_H)
set(HAVE_CUDA_RUNTIME_API_H)
set(HWLOC_HAVE_CUDART)
if(HWLOC_WITH_CUDA)
find_package(CUDAToolkit REQUIRED)
set(HAVE_CUDA 1)
set(HAVE_CUDA_H 1)
set(HAVE_CUDA_RUNTIME_API_H 1)
set(HWLOC_HAVE_CUDART 1)
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/private_config.h.in include/private/autogen/config.h)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/static-components.h.in include/static-components.h)

# Library

add_compile_options("$<$<CONFIG:DEBUG>:-DHWLOC_DEBUG=1>")
add_compile_definitions($<$<CONFIG:DEBUG>:HWLOC_DEBUG=1>)

# FIXME dll soname
add_library(libhwloc
add_library(hwloc
${TOPDIR}/hwloc/topology.c
${TOPDIR}/hwloc/traversal.c
${TOPDIR}/hwloc/distances.c
Expand All @@ -64,11 +123,25 @@ add_library(libhwloc
${TOPDIR}/hwloc/topology-xml-nolibxml.c
${TOPDIR}/hwloc/topology-windows.c
${TOPDIR}/hwloc/topology-x86.c
$<$<BOOL:${HWLOC_HAVE_LIBXML2}>:${TOPDIR}/hwloc/topology-xml-libxml.c>
$<$<BOOL:${HWLOC_HAVE_OPENCL}>:${TOPDIR}/hwloc/topology-opencl.c>
$<$<BOOL:${HAVE_CUDA}>:${TOPDIR}/hwloc/topology-cuda.c>
)

target_link_libraries(hwloc PRIVATE
$<$<BOOL:${HWLOC_HAVE_LIBXML2}>:LibXml2::LibXml2>
$<$<BOOL:${HWLOC_HAVE_OPENCL}>:OpenCL::OpenCL>
"$<$<BOOL:${HAVE_CUDA}>:CUDA::cudart;CUDA::cuda_driver>"
)

set_target_properties(libhwloc PROPERTIES DEFINE_SYMBOL _USRDLL)
if(HWLOC_BUILD_SHARED_LIBS)
target_compile_definitions(hwloc PRIVATE $<$<BOOL:${MSVC}>:_USRDLL>)
endif()

target_include_directories(libhwloc PRIVATE ${TOPDIR}/include ${TOPDIR}/hwloc ${CMAKE_CURRENT_BINARY_DIR}/include)
target_include_directories(hwloc PUBLIC
"$<BUILD_INTERFACE:${TOPDIR}/include;${CMAKE_CURRENT_BINARY_DIR}/include>"
$<INSTALL_INTERFACE:include>
)

# Tools under utils/hwloc

Expand All @@ -87,8 +160,7 @@ if(NOT HWLOC_SKIP_TOOLS)
foreach(tool IN ITEMS ${TOOLS})
add_executable(${tool}
${TOPDIR}/utils/hwloc/${tool}.c)
target_include_directories(${tool} PRIVATE ${TOPDIR}/include ${CMAKE_CURRENT_BINARY_DIR}/include)
target_link_libraries(${tool} PRIVATE libhwloc)
target_link_libraries(${tool} PRIVATE hwloc)
endforeach(tool)

endif()
Expand Down Expand Up @@ -118,53 +190,52 @@ if(NOT HWLOC_SKIP_LSTOPO)
add_executable(lstopo-no-graphics
${LSTOPO_COMMON_SOURCES}
)
target_link_libraries(lstopo-no-graphics PRIVATE libhwloc)
target_link_libraries(lstopo-no-graphics PRIVATE hwloc)

add_executable(lstopo
${LSTOPO_COMMON_SOURCES}
${TOPDIR}/utils/lstopo/lstopo-windows.c
)
set_target_properties(lstopo PROPERTIES COMPILE_FLAGS "-DLSTOPO_HAVE_GRAPHICS")
target_compile_definitions(lstopo PRIVATE LSTOPO_HAVE_GRAPHICS)

add_executable(lstopo-win
add_executable(lstopo-win WIN32
${LSTOPO_COMMON_SOURCES}
${TOPDIR}/utils/lstopo/lstopo-windows.c
)
set_target_properties(lstopo-win PROPERTIES COMPILE_FLAGS "-DLSTOPO_HAVE_GRAPHICS")
target_link_options(lstopo-win PRIVATE "/subsystem:windows" "/entry:mainCRTStartup")
target_compile_definitions(lstopo-win PRIVATE LSTOPO_HAVE_GRAPHICS)
target_link_options(lstopo-win PRIVATE "$<$<BOOL:${MSVC}>:/subsystem:windows;/entry:mainCRTStartup>")

foreach(tool IN ITEMS ${LSTOPOS})
target_include_directories(${tool} PRIVATE ${TOPDIR}/include ${CMAKE_CURRENT_BINARY_DIR}/include ${TOPDIR}/utils/hwloc)
target_link_libraries(${tool} PRIVATE libhwloc)
target_include_directories(${tool} PRIVATE ${TOPDIR}/utils/hwloc)
target_link_libraries(${tool} PRIVATE hwloc)
endforeach(tool)

endif()

# Misc

foreach(target IN ITEMS libhwloc ${TOOLS} ${LSTOPOS})
target_compile_definitions(${target} PRIVATE _CRT_SECURE_NO_WARNINGS)
foreach(target IN ITEMS hwloc ${TOOLS} ${LSTOPOS})
target_compile_definitions(${target} PRIVATE $<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>)
endforeach(target)

# Install

install(TARGETS libhwloc
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)
install(TARGETS hwloc)

if(NOT HWLOC_SKIP_TOOLS)
install(TARGETS ${TOOLS}
RUNTIME DESTINATION bin)
install(TARGETS ${TOOLS})
endif()

if(NOT HWLOC_SKIP_LSTOPO)
install(TARGETS ${LSTOPOS}
RUNTIME DESTINATION bin)
install(TARGETS ${LSTOPOS})
endif()

if(NOT HWLOC_SKIP_INCLUDES)
install(FILES ${TOPDIR}/include/hwloc.h DESTINATION include)
install(DIRECTORY ${TOPDIR}/include/hwloc DESTINATION include FILES_MATCHING PATTERN "*.h")
install(FILES ${TOPDIR}/include/hwloc.h TYPE INCLUDE)
install(DIRECTORY ${TOPDIR}/include/hwloc TYPE INCLUDE FILES_MATCHING PATTERN "*.h")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/hwloc/autogen/config.h DESTINATION include/hwloc/autogen)
endif()

if(HWLOC_ENABLE_TESTING)
add_subdirectory(${TOPDIR}/tests/hwloc ${CMAKE_CURRENT_BINARY_DIR}/tests/hwloc)
endif()
Loading

0 comments on commit 69dd7f8

Please sign in to comment.