Unverified Commit 08a9ce32 authored by Niklas Korz's avatar Niklas Korz Committed by GitHub
Browse files

nlopt: fix build, python3Packages.nlopt: init, nlopt: 2.7.1 -> 2.10.0 (#392544)

parents bdb9e6c3 43a09052
Loading
Loading
Loading
Loading
+126 −27
Original line number Diff line number Diff line
@@ -2,50 +2,149 @@
  lib,
  stdenv,
  fetchFromGitHub,
  fetchpatch,
  cmake,
  octave ? null,
  libiconv,
}:
  nix-update-script,
  # Optionally build Python bindings
  withPython ? false,
  python3,
  python3Packages,
  # Optionally build Octave bindings
  withOctave ? false,
  octave,
  # Optionally build Java bindings
  withJava ? false,
  jdk,
  # Required for building the Python and Java bindings
  swig,
  # Optionally exclude Luksan solvers to allow licensing under MIT
  withoutLuksanSolvers ? false,
  # Build static on-demand
  withStatic ? stdenv.hostPlatform.isStatic,

stdenv.mkDerivation rec {
  # v2.8.0 introduced a regression where testing on Linux platforms fails with a buffer overflow
  # when compiled with -D_FORTIFY_SOURCE=3.
  # This was deemed to be a compiler false positive by the library's author in https://github.com/stevengj/nlopt/issues/563.
  # Building with `clangStdenv` prevents this from occurring.
  clangStdenv,
}:
let
  buildPythonBindingsEnv = python3.withPackages (p: [ p.numpy ]);
  buildDocsEnv = python3.withPackages (p: [
    p.mkdocs
    p.python-markdown-math
  ]);
in
clangStdenv.mkDerivation (finalAttrs: {
  pname = "nlopt";
  version = "2.7.1";
  version = "2.10.0";

  src = fetchFromGitHub {
    owner = "stevengj";
    repo = pname;
    rev = "v${version}";
    sha256 = "sha256-TgieCX7yUdTAEblzXY/gCN0r6F9TVDh4RdNDjQdXZ1o=";
    repo = "nlopt";
    tag = "v${finalAttrs.version}";
    hash = "sha256-mZRmhXrApxfiJedk+L/poIP2DR/BkV04c5fiwPGAyjI=";
  };

  nativeBuildInputs = [ cmake ] ++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ];
  buildInputs = [ octave ];
  outputs = [
    "out"
    "doc"
  ];

  patches = [
    # 26-03-2025: `mkdocs.yml` is missing a link for the subpage related to the Java bindings.
    # 26-03-2025: This commit was merged after v2.10.0 was released, and has not been made
    # 26-03-2025: part of a release.
    (fetchpatch {
      name = "missing-java-reference-mkdocs";
      url = "https://github.com/stevengj/nlopt/commit/7e34f1a6fe82ed27daa6111d83c4d5629555454b.patch";
      hash = "sha256-XivfZtgIGLyTtU+Zo2jSQAx2mVdGLJ8PD7VSSvGR/5Q=";
    })

    # 26-03-2025: The docs pages still list v2.7.1 as the newest version.
    # 26-03-2025: This commit was merged after v2.10.0 was released, and has not been made
    # 26-03-2025: part of a release.
    (fetchpatch {
      name = "update-index-md";
      url = "https://github.com/stevengj/nlopt/commit/2c4147832eff7ea15d0536c82351a9e169f85e43.patch";
      hash = "sha256-BXcbNUyu20f3N146v6v9cpjSj5CwuDtesp6lAqOK2KY=";
    })

  configureFlags =
    # 26-03-2025: There is an off-by-one error in the test/CMakeLists.txt
    # 26-03-2025: that causes the tests to attempt to run disabled Luksan solver code,
    # 26-03-2025: which in turn causes the test suite to fail.
    # 26-03-2025: See https://github.com/stevengj/nlopt/pull/605
    (fetchpatch {
      name = "fix-nondisabled-luksan-algorithm";
      url = "https://github.com/stevengj/nlopt/commit/7817ec19f21be6877a4b79777fc5315a52c6850b.patch";
      hash = "sha256-KgdAMSYKOQuraun4HNr9GOx48yjyeQk6W3IgWRA44oo=";
    })
  ];

  postPatch = ''
    substituteInPlace nlopt.pc.in \
      --replace-fail 'libdir=''${exec_prefix}/@NLOPT_INSTALL_LIBDIR@' 'libdir=@NLOPT_INSTALL_LIBDIR@'
  '';

  nativeBuildInputs =
    [ cmake ]
    ## Building the python bindings requires SWIG, and numpy in addition to the CXX routines.
    ## The tests also make use of the same interpreter to test the bindings.
    ++ lib.optionals withPython [
      swig
      buildPythonBindingsEnv
    ]
    ## Building the java bindings requires SWIG, C++, JNI and Java
    ++ lib.optionals withJava [
      swig
      jdk
    ]
    ## Building octave bindings requires `mkoctfile` to be installed.
    ++ lib.optional withOctave octave;

  # Python bindings depend on numpy at import time.
  propagatedBuildInputs = lib.optional withPython python3Packages.numpy;

  cmakeFlags =
    [
      "--with-cxx"
      "--enable-shared"
      "--with-pic"
      "--without-guile"
      "--without-python"
      "--without-matlab"
      (lib.cmakeBool "BUILD_SHARED_LIBS" (!withStatic))
      (lib.cmakeBool "NLOPT_CXX" true)
      (lib.cmakeBool "NLOPT_PYTHON" withPython)
      (lib.cmakeBool "NLOPT_OCTAVE" withOctave)
      (lib.cmakeBool "NLOPT_JAVA" withJava)
      (lib.cmakeBool "NLOPT_SWIG" (withPython || withJava))
      (lib.cmakeBool "NLOPT_FORTRAN" false)
      (lib.cmakeBool "NLOPT_MATLAB" false)
      (lib.cmakeBool "NLOPT_GUILE" false)
      (lib.cmakeBool "NLOPT_LUKSAN" (!withoutLuksanSolvers))
      (lib.cmakeBool "NLOPT_TESTS" finalAttrs.doCheck)
    ]
    ++ lib.optionals (octave != null) [
      "--with-octave"
      "M_INSTALL_DIR=$(out)/${octave.sitePath}/m"
      "OCT_INSTALL_DIR=$(out)/${octave.sitePath}/oct"
    ];
    ++ lib.optional withPython (
      lib.cmakeFeature "Python_EXECUTABLE" "${buildPythonBindingsEnv.interpreter}"
    );

  postBuild = ''
    ${buildDocsEnv.interpreter} -m mkdocs build \
      --config-file ../mkdocs.yml \
      --site-dir $doc \
      --no-directory-urls
  '';

  doCheck = true;

  postFixup = ''
    substituteInPlace $out/lib/cmake/nlopt/NLoptLibraryDepends.cmake --replace \
    substituteInPlace $out/lib/cmake/nlopt/NLoptLibraryDepends.cmake --replace-fail \
      'INTERFACE_INCLUDE_DIRECTORIES "''${_IMPORT_PREFIX}/' 'INTERFACE_INCLUDE_DIRECTORIES "'
  '';

  passthru.updateScript = nix-update-script { };

  meta = {
    homepage = "https://nlopt.readthedocs.io/en/latest/";
    changelog = "https://github.com/stevengj/nlopt/releases/tag/v${finalAttrs.version}";
    description = "Free open-source library for nonlinear optimization";
    license = lib.licenses.lgpl21Plus;
    hydraPlatforms = lib.platforms.linux;
    license = if withoutLuksanSolvers then lib.licenses.mit else lib.licenses.lgpl21Plus;
    platforms = lib.platforms.all;
    maintainers = [ lib.maintainers.bengsparks ];
  };

}
})
+5 −0
Original line number Diff line number Diff line
{
  toPythonModule,
  pkgs,
}:
toPythonModule (pkgs.nlopt.override { withPython = true; })
+1 −1
Original line number Diff line number Diff line
@@ -4493,7 +4493,7 @@ with pkgs;
  nifskope = libsForQt5.callPackage ../tools/graphics/nifskope { };
  nlopt = callPackage ../development/libraries/nlopt { octave = null; };
  nlopt = callPackage ../development/libraries/nlopt { };
  notation = callPackage ../by-name/no/notation/package.nix {
    buildGoModule = buildGo123Module;
+2 −0
Original line number Diff line number Diff line
@@ -9933,6 +9933,8 @@ self: super: with self; {
  nkdfu = callPackage ../development/python-modules/nkdfu { };
  nlopt = callPackage ../development/python-modules/nlopt { };
  nlpcloud = callPackage ../development/python-modules/nlpcloud { };
  nlpo3 = callPackage ../development/python-modules/nlpo3 { };