From 08322e6c6d6d8c1e1767c9cef225f4f32c938bed Mon Sep 17 00:00:00 2001 From: Alexander Tsvyashchenko Date: Sat, 1 Jan 2022 22:45:58 +0100 Subject: [PATCH] python3Packages.dm-tree: refactor to build from sources (#152971) * python3Packages.dm-tree: refactor to build from sources * Addressed review comments --- .../python-modules/dm-tree/cmake.patch | 141 ++++++++++++++++++ .../python-modules/dm-tree/default.nix | 69 +++++---- 2 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 pkgs/development/python-modules/dm-tree/cmake.patch diff --git a/pkgs/development/python-modules/dm-tree/cmake.patch b/pkgs/development/python-modules/dm-tree/cmake.patch new file mode 100644 index 000000000000..e5698f457bc3 --- /dev/null +++ b/pkgs/development/python-modules/dm-tree/cmake.patch @@ -0,0 +1,141 @@ +diff --git a/tree/CMakeLists.txt b/tree/CMakeLists.txt +index 8f9946c..b9d6e9b 100644 +--- a/tree/CMakeLists.txt ++++ b/tree/CMakeLists.txt +@@ -50,70 +50,80 @@ if(APPLE) + set (CMAKE_FIND_FRAMEWORK LAST) + endif() + +-# Fetch pybind to be able to use pybind11_add_module symbol. +-set(PYBIND_VER v2.6.2) +-include(FetchContent) +-FetchContent_Declare( +- pybind11 +- GIT_REPOSITORY https://github.com/pybind/pybind11 +- GIT_TAG ${PYBIND_VER} +-) +-if(NOT pybind11_POPULATED) +- FetchContent_Populate(pybind11) +- add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR}) +- include_directories(${pybind11_INCLUDE_DIR}) +-endif() +- +-# Needed to disable Abseil tests. +-set (BUILD_TESTING OFF) +- +-# Include abseil-cpp. +-set(ABSEIL_VER 20210324.2) +-include(ExternalProject) +-set(ABSEIL_CMAKE_ARGS +- "-DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}/abseil-cpp" +- "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" +- "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" +- "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}" +- "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" +- "-DCMAKE_POSITION_INDEPENDENT_CODE=${CMAKE_POSITION_INDEPENDENT_CODE}" +- "-DLIBRARY_OUTPUT_PATH=${CMAKE_SOURCE_DIR}/abseil-cpp/lib") +-if(DEFINED CMAKE_OSX_ARCHITECTURES) +- set(ABSEIL_CMAKE_ARGS +- ${ABSEIL_CMAKE_ARGS} +- "-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}") ++find_package(pybind11 CONFIG) ++ ++if (NOT pybind11_FOUND) ++ # Fetch pybind to be able to use pybind11_add_module symbol. ++ set(PYBIND_VER v2.6.2) ++ include(FetchContent) ++ FetchContent_Declare( ++ pybind11 ++ GIT_REPOSITORY https://github.com/pybind/pybind11 ++ GIT_TAG ${PYBIND_VER} ++ ) ++ if(NOT pybind11_POPULATED) ++ FetchContent_Populate(pybind11) ++ add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR}) ++ include_directories(${pybind11_INCLUDE_DIR}) ++ endif() + endif() +-ExternalProject_Add(abseil-cpp +- GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git +- GIT_TAG ${ABSEIL_VER} +- PREFIX ${CMAKE_SOURCE_DIR}/abseil-cpp +- CMAKE_ARGS ${ABSEIL_CMAKE_ARGS} +-) +-ExternalProject_Get_Property(abseil-cpp install_dir) +-set(abseil_install_dir ${install_dir}) +-include_directories (${abseil_install_dir}/include) +- + + # Define pybind11 tree module. + pybind11_add_module(_tree tree.h tree.cc) +-add_dependencies(_tree abseil-cpp) + +-if (WIN32 OR MSVC) +- set(ABSEIL_LIB_PREF "absl") +- set(LIB_SUFF "lib") ++find_package(absl) ++ ++if (NOT absl_FOUND) ++ # Needed to disable Abseil tests. ++ set (BUILD_TESTING OFF) ++ ++ # Include abseil-cpp. ++ set(ABSEIL_VER 20210324.2) ++ include(ExternalProject) ++ set(ABSEIL_CMAKE_ARGS ++ "-DCMAKE_INSTALL_PREFIX=${CMAKE_SOURCE_DIR}/abseil-cpp" ++ "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}" ++ "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" ++ "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}" ++ "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" ++ "-DCMAKE_POSITION_INDEPENDENT_CODE=${CMAKE_POSITION_INDEPENDENT_CODE}" ++ "-DLIBRARY_OUTPUT_PATH=${CMAKE_SOURCE_DIR}/abseil-cpp/lib") ++ if(DEFINED CMAKE_OSX_ARCHITECTURES) ++ set(ABSEIL_CMAKE_ARGS ++ ${ABSEIL_CMAKE_ARGS} ++ "-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}") ++ endif() ++ ExternalProject_Add(abseil-cpp ++ GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git ++ GIT_TAG ${ABSEIL_VER} ++ PREFIX ${CMAKE_SOURCE_DIR}/abseil-cpp ++ CMAKE_ARGS ${ABSEIL_CMAKE_ARGS} ++ ) ++ ExternalProject_Get_Property(abseil-cpp install_dir) ++ set(abseil_install_dir ${install_dir}) ++ include_directories (${abseil_install_dir}/include) ++ ++ add_dependencies(_tree abseil-cpp) ++ ++ if (WIN32 OR MSVC) ++ set(ABSEIL_LIB_PREF "absl") ++ set(LIB_SUFF "lib") ++ else() ++ set(ABSEIL_LIB_PREF "libabsl") ++ set(LIB_SUFF "a") ++ endif() ++ ++ # Link abseil static libs. ++ # We don't use find_library here to force cmake to build abseil before linking. ++ set(ABSEIL_LIBS int128 raw_hash_set raw_logging_internal strings throw_delegate) ++ foreach(ABSEIL_LIB IN LISTS ABSEIL_LIBS) ++ target_link_libraries(_tree PRIVATE ++ "${abseil_install_dir}/lib/${ABSEIL_LIB_PREF}_${ABSEIL_LIB}.${LIB_SUFF}") ++ endforeach() + else() +- set(ABSEIL_LIB_PREF "libabsl") +- set(LIB_SUFF "a") ++ target_link_libraries(_tree PRIVATE absl::int128 absl::raw_hash_set absl::raw_logging_internal absl::strings absl::throw_delegate) + endif() + +-# Link abseil static libs. +-# We don't use find_library here to force cmake to build abseil before linking. +-set(ABSEIL_LIBS int128 raw_hash_set raw_logging_internal strings throw_delegate) +-foreach(ABSEIL_LIB IN LISTS ABSEIL_LIBS) +- target_link_libraries(_tree PRIVATE +- "${abseil_install_dir}/lib/${ABSEIL_LIB_PREF}_${ABSEIL_LIB}.${LIB_SUFF}") +-endforeach() +- + # Make the module private to tree package. + set_target_properties(_tree PROPERTIES OUTPUT_NAME tree/_tree) + diff --git a/pkgs/development/python-modules/dm-tree/default.nix b/pkgs/development/python-modules/dm-tree/default.nix index 81eb101399ae..307e932aae39 100644 --- a/pkgs/development/python-modules/dm-tree/default.nix +++ b/pkgs/development/python-modules/dm-tree/default.nix @@ -1,47 +1,56 @@ -{ autoPatchelfHook +{ abseil-cpp +, absl-py +, attrs , buildPythonPackage -, fetchPypi -, isPy39 +, cmake +, fetchFromGitHub , lib -, six -, stdenv +, numpy +, pybind11 +, wrapt }: buildPythonPackage rec { pname = "dm-tree"; - version = "0.1.6"; - format = "wheel"; + # As of 2021-12-29, the latest stable version still builds with Bazel. + version = "unstable-2021-12-20"; - # At the time of writing (8/19/21), there are releases for 3.6-3.9. Supporting - # all of them is a pain, so we focus on 3.9, the current nixpkgs python3 - # version. - disabled = !isPy39; - - src = fetchPypi { - inherit version format; - sha256 = "1f71dy5xa5ywa5chbdhpdf8k0w1v9cvpn3qyk8nnjm79j90la9c4"; - pname = "dm_tree"; - dist = "cp39"; - python = "cp39"; - abi = "cp39"; - platform = "manylinux_2_24_x86_64"; + src = fetchFromGitHub { + owner = "deepmind"; + repo = "tree"; + rev = "b452e5c2743e7489b4ba7f16ecd51c516d7cd8e3"; + sha256 = "1r187xwpvnnj98lyasngcv3lbxz0ziihpl5dbnjbfbjr0kh6z0j9"; }; - # Prebuilt wheels are dynamically linked against things that nix can't find. - # Run `autoPatchelfHook` to automagically fix them. - nativeBuildInputs = [ autoPatchelfHook ]; - # Dynamic link dependencies - buildInputs = [ stdenv.cc.cc ]; + patches = [ + ./cmake.patch + ]; - propagatedBuildInputs = [ six ]; + dontUseCmakeConfigure = true; + + nativeBuildInputs = [ + cmake + pybind11 + ]; + + buildInputs = [ + abseil-cpp + pybind11 + ]; + + checkInputs = [ + absl-py + attrs + numpy + wrapt + ]; pythonImportsCheck = [ "tree" ]; meta = with lib; { description = "Tree is a library for working with nested data structures."; - homepage = "https://github.com/deepmind/tree"; - license = licenses.asl20; - maintainers = with maintainers; [ samuela ]; - platforms = [ "x86_64-linux" ]; + homepage = "https://github.com/deepmind/tree"; + license = licenses.asl20; + maintainers = with maintainers; [ samuela ndl ]; }; }