diff --git a/pkgs/development/libraries/nlopt/default.nix b/pkgs/development/libraries/nlopt/default.nix index 1e1f5f01c382..51386f659748 100644 --- a/pkgs/development/libraries/nlopt/default.nix +++ b/pkgs/development/libraries/nlopt/default.nix @@ -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" + ]; - configureFlags = - [ - "--with-cxx" - "--enable-shared" - "--with-pic" - "--without-guile" - "--without-python" - "--without-matlab" + 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="; + }) + + # 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 ] - ++ lib.optionals (octave != null) [ - "--with-octave" - "M_INSTALL_DIR=$(out)/${octave.sitePath}/m" - "OCT_INSTALL_DIR=$(out)/${octave.sitePath}/oct" - ]; + ## 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 = + [ + (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.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 ]; }; - -} +}) diff --git a/pkgs/development/python-modules/nlopt/default.nix b/pkgs/development/python-modules/nlopt/default.nix new file mode 100644 index 000000000000..55bfc2d2884c --- /dev/null +++ b/pkgs/development/python-modules/nlopt/default.nix @@ -0,0 +1,5 @@ +{ + toPythonModule, + pkgs, +}: +toPythonModule (pkgs.nlopt.override { withPython = true; }) diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 587278790793..87e9c90b0156 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -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; diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix index f0df408a0e00..3801f420f181 100644 --- a/pkgs/top-level/python-packages.nix +++ b/pkgs/top-level/python-packages.nix @@ -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 { };