From ac1c9db611003c45e5c13b0cc2ce2acc9d728452 Mon Sep 17 00:00:00 2001 From: Krzysztof Filipek Date: Thu, 6 Jun 2024 14:32:58 +0200 Subject: [PATCH] Add static linking option --- .github/workflows/basic.yml | 70 ++++++++++++++++++++++++++++++++++ CMakeLists.txt | 76 ++++++++++++++++++++++++++++--------- README.md | 1 + examples/CMakeLists.txt | 2 +- src/CMakeLists.txt | 8 +++- 5 files changed, 136 insertions(+), 21 deletions(-) diff --git a/.github/workflows/basic.yml b/.github/workflows/basic.yml index fbebf14ba9..48d81c4907 100644 --- a/.github/workflows/basic.yml +++ b/.github/workflows/basic.yml @@ -228,6 +228,76 @@ jobs: --umf-version ${{env.UMF_VERSION}} ${{ matrix.shared_library == 'ON' && '--shared-library' || ''}} + windows-dynamic_build_hwloc: + name: "Windows dynamic UMF + static hwloc" + env: + BUILD_DIR : "${{github.workspace}}/build/${{matrix.build_type}}" + strategy: + matrix: + build_type: [Release] + + runs-on: 'windows-2022' + + steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Configure build + run: > + cmake + -B ${{env.BUILD_DIR}} + -DUMF_BUILD_SHARED_LIBRARY=ON + -DUMF_BUILD_EXAMPLES=OFF + -DUMF_FORMAT_CODE_STYLE=OFF + -DUMF_DEVELOPER_MODE=ON + -DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON + -DUMF_BUILD_LIBUMF_POOL_JEMALLOC=OFF + -DUMF_BUILD_LEVEL_ZERO_PROVIDER=ON + -DUMF_TESTS_FAIL_ON_SKIP=ON + -DUMF_LINK_HWLOC_STATICALLY=ON + + - name: Build UMF + run: cmake --build ${{env.BUILD_DIR}} --config ${{matrix.build_type}} -j $Env:NUMBER_OF_PROCESSORS + + - name: Run tests + working-directory: ${{env.BUILD_DIR}} + run: ctest -C ${{matrix.build_type}} --output-on-failure --test-dir test + + windows-static_build_hwloc: + name: "Windows static UMF + static hwloc" + env: + BUILD_DIR : "${{github.workspace}}/build/${{matrix.build_type}}" + strategy: + matrix: + build_type: [Release] + + runs-on: 'windows-2022' + + steps: + - name: Checkout + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Configure build + run: > + cmake + -B ${{env.BUILD_DIR}} + -DUMF_BUILD_SHARED_LIBRARY=OFF + -DUMF_BUILD_EXAMPLES=OFF + -DUMF_FORMAT_CODE_STYLE=OFF + -DUMF_DEVELOPER_MODE=ON + -DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON + -DUMF_BUILD_LIBUMF_POOL_JEMALLOC=OFF + -DUMF_BUILD_LEVEL_ZERO_PROVIDER=ON + -DUMF_TESTS_FAIL_ON_SKIP=ON + -DUMF_LINK_HWLOC_STATICALLY=ON + + - name: Build UMF + run: cmake --build ${{env.BUILD_DIR}} --config ${{matrix.build_type}} -j $Env:NUMBER_OF_PROCESSORS + + - name: Run tests + working-directory: ${{env.BUILD_DIR}} + run: ctest -C ${{matrix.build_type}} --output-on-failure --test-dir test + macos-build: name: MacOS strategy: diff --git a/CMakeLists.txt b/CMakeLists.txt index f1b1ae993e..837d8e6dd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,11 @@ set(UMF_CMAKE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) list(APPEND CMAKE_MODULE_PATH "${UMF_CMAKE_SOURCE_DIR}/cmake") include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake) +include(CTest) +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) +find_package(PkgConfig) + # CMAKE_PROJECT_VERSION[_MAJOR|_MINOR|_PATCH] variables are set via 'project' # command. They cannot contain any "pre-release" part, though. We use custom # "UMF_SRC_VERSION" to store more accurate (source) version - this var should be @@ -39,6 +44,8 @@ option(UMF_BUILD_EXAMPLES "Build UMF examples" ON) option(UMF_BUILD_FUZZTESTS "Build UMF fuzz tests" OFF) option(UMF_BUILD_GPU_EXAMPLES "Build UMF GPU examples" OFF) option(UMF_DEVELOPER_MODE "Enable additional developer checks" OFF) +option(UMF_LINK_HWLOC_STATICALLY + "Link UMF with HWLOC library statically (Windows+Release only)" OFF) option(UMF_FORMAT_CODE_STYLE "Add clang, cmake, and black -format-check and -format-apply targets" OFF) @@ -83,6 +90,44 @@ else() message(FATAL_ERROR "Unknown OS type") endif() +if(NOT UMF_LINK_HWLOC_STATICALLY) + pkg_check_modules(LIBHWLOC hwloc>=2.3.0) + if(NOT LIBHWLOC_FOUND) + find_package(LIBHWLOC 2.3.0 REQUIRED hwloc) + endif() + # add PATH to DLL on Windows + set(DLL_PATH_LIST + "${DLL_PATH_LIST};PATH=path_list_append:${LIBHWLOC_LIBRARY_DIRS}/../bin" + ) +else() + if(NOT WINDOWS) + message(FATAL_ERROR "hwloc can be statically linked only on Windows") + endif() + include(FetchContent) + set(HWLOC_ENABLE_TESTING OFF) + set(HWLOC_SKIP_LSTOPO ON) + set(HWLOC_SKIP_TOOLS ON) + FetchContent_Declare( + hwloc_targ + GIT_REPOSITORY "https://github.com/open-mpi/hwloc.git" + GIT_TAG hwloc-2.10.0 + SOURCE_SUBDIR contrib/windows-cmake/ FIND_PACKAGE_ARGS) + + FetchContent_GetProperties(hwloc_targ) + if(NOT hwloc_targ_POPULATED) + FetchContent_MakeAvailable(hwloc_targ) + endif() + + set(LIBHWLOC_INCLUDE_DIRS + ${hwloc_targ_SOURCE_DIR}/include;${hwloc_targ_BINARY_DIR}/include) + set(LIBHWLOC_LIBRARY_DIRS + ${hwloc_targ_BINARY_DIR}/Release;${hwloc_targ_BINARY_DIR}/Debug) + + message(STATUS " LIBHWLOC_LIBRARIES = ${LIBHWLOC_LIBRARIES}") + message(STATUS " LIBHWLOC_INCLUDE_DIRS = ${LIBHWLOC_INCLUDE_DIRS}") + message(STATUS " LIBHWLOC_LIBRARY_DIRS = ${LIBHWLOC_LIBRARY_DIRS}") +endif() + # This build type check is not possible on Windows when CMAKE_BUILD_TYPE is not # set, because in this case the build type is determined after a CMake # configuration is done (at the build time) @@ -130,11 +175,6 @@ foreach(option_name ${OPTIONS_REQUIRING_CXX}) endif() endforeach() -include(CTest) -include(CMakePackageConfigHelpers) -include(GNUInstallDirs) -find_package(PkgConfig) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_UMF_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -186,7 +226,8 @@ if(WINDOWS) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) # set PATH to DLLs on Windows set(DLL_PATH_LIST - "PATH=path_list_append:${PROJECT_BINARY_DIR}/bin/$") + "${DLL_PATH_LIST};PATH=path_list_append:${PROJECT_BINARY_DIR}/bin/$" + ) # add path to the proxy lib DLL set(DLL_PATH_LIST "${DLL_PATH_LIST};PATH=path_list_append:${PROJECT_BINARY_DIR}/src/proxy_lib" @@ -197,14 +238,6 @@ if(WINDOWS) ) endif() -pkg_check_modules(LIBHWLOC hwloc>=2.3.0) -if(NOT LIBHWLOC_FOUND) - find_package(LIBHWLOC 2.3.0 REQUIRED hwloc) -endif() -# add PATH to DLL on Windows -set(DLL_PATH_LIST - "${DLL_PATH_LIST};PATH=path_list_append:${LIBHWLOC_LIBRARY_DIRS}/../bin") - pkg_check_modules(TBB tbb) if(NOT TBB_FOUND) find_package(TBB OPTIONAL_COMPONENTS tbb) @@ -240,10 +273,17 @@ if(WINDOWS) # In MSVC builds, there is no way to determine the actual build type during # the CMake configuration step. Therefore, this message is printed in all # MSVC builds. - message( - STATUS - "The proxy library will be built, however it is supported only in the Release build on Windows" - ) + if(UMF_LINK_HWLOC_STATICALLY) + message( + STATUS + "The proxy library will be disabled - static linkage with hwloc is not supported yet" + ) + else() + message( + STATUS + "The proxy library will be built, however it is supported only in the Release build on Windows" + ) + endif() endif() if(UMF_PROXY_LIB_BASED_ON_POOL STREQUAL SCALABLE) if(UMF_POOL_SCALABLE_ENABLED) diff --git a/README.md b/README.md index f1183b7357..65c29b76e1 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ List of options provided by CMake: | USE_MSAN | Enable MemorySanitizer checks | ON/OFF | OFF | | USE_VALGRIND | Enable Valgrind instrumentation | ON/OFF | OFF | | USE_GCOV | Enable gcov support (Linux only) | ON/OFF | OFF | +| UMF_LINK_HWLOC_STATICALLY | Link UMF with HWLOC library statically (Windows+Release only) | ON/OFF | OFF | ## Architecture: memory pools and providers diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 65dabf4110..032df0c032 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -18,7 +18,7 @@ set(EXAMPLE_NAME umf_example_basic) add_umf_executable( NAME ${EXAMPLE_NAME} SRCS basic/basic.c - LIBS umf) + LIBS umf hwloc) target_include_directories( ${EXAMPLE_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/src/utils diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e7e3bafdf2..4019deeeb3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -115,7 +115,7 @@ if(UMF_BUILD_SHARED_LIBRARY) NAME umf TYPE SHARED SRCS ${UMF_SOURCES} - LIBS ${UMF_LIBS} + LIBS ${UMF_LIBS} hwloc LINUX_MAP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/libumf.map WINDOWS_DEF_FILE ${CMAKE_CURRENT_BINARY_DIR}/libumf.def) set(UMF_PRIVATE_COMPILE_DEFINITIONS ${UMF_PRIVATE_COMPILE_DEFINITIONS} @@ -133,6 +133,10 @@ else() LIBS ${UMF_LIBS}) endif() +if(UMF_LINK_HWLOC_STATICALLY) + add_dependencies(umf hwloc) +endif() + target_link_directories(umf PRIVATE ${UMF_PRIVATE_LIBRARY_DIRS}) target_compile_definitions(umf PRIVATE ${UMF_PRIVATE_COMPILE_DEFINITIONS}) @@ -176,6 +180,6 @@ install(TARGETS umf EXPORT ${PROJECT_NAME}-targets) add_subdirectory(pool) -if(UMF_PROXY_LIB_ENABLED) +if(UMF_PROXY_LIB_ENABLED AND NOT UMF_LINK_HWLOC_STATICALLY) add_subdirectory(proxy_lib) endif()