Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite NLopt 2.9 workaround for NLopt 2.10 #254

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions src/NLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ module NLopt
using CEnum: @cenum
using NLopt_jll: libnlopt

# [email protected] removed the LD_LBFGS_NOCEDAL enum.
# See https://github.com/stevengj/nlopt/issues/584 for details.
function _is_version_newer_than_2_9()
major, minor, bugfix = Ref{Cint}(), Ref{Cint}(), Ref{Cint}()
@ccall libnlopt.nlopt_version(
major::Ptr{Cint},
minor::Ptr{Cint},
bugfix::Ptr{Cint},
)::Cvoid
return (major[] > 2) || (major[] == 2 && minor[] >= 9)
############################################################################

function version()
major, minor, patch = Ref{Cint}(), Ref{Cint}(), Ref{Cint}()
nlopt_version(major, minor, patch)
return VersionNumber(major[], minor[], patch[])
end

include("libnlopt.jl")

############################################################################
# Mirrors of NLopt's C enum constants:

@static if _is_version_newer_than_2_9()
@static if v"2.9" ≤ version() < v"2.10"
@enum Algorithm::Cint begin
GN_DIRECT = 0
GN_DIRECT_L
Expand All @@ -37,7 +33,7 @@ include("libnlopt.jl")
GN_ORIG_DIRECT_L
GD_STOGO
GD_STOGO_RAND
# LD_LBFGS_NOCEDAL
# LD_LBFGS_NOCEDAL: temporarily removed in nlopt 2.9 (nlopt#584)
LD_LBFGS
LN_PRAXIS
LD_VAR1
Expand Down Expand Up @@ -446,12 +442,6 @@ numevals(o::Opt) = nlopt_get_numevals(o)

############################################################################

function version()
major, minor, patch = Ref{Cint}(), Ref{Cint}(), Ref{Cint}()
nlopt_version(major, minor, patch)
return VersionNumber(major[], minor[], patch[])
end

const NLOPT_VERSION = version()

############################################################################
Expand Down
10 changes: 5 additions & 5 deletions src/libnlopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const nlopt_mfunc = Ptr{Cvoid}
# typedef void ( * nlopt_precond ) ( unsigned n , const double * x , const double * v , double * vpre , void * data )
const nlopt_precond = Ptr{Cvoid}

@static if _is_version_newer_than_2_9()
function nlopt_version(major, minor, bugfix)
ccall((:nlopt_version, libnlopt), Cvoid, (Ptr{Cint}, Ptr{Cint}, Ptr{Cint}), major, minor, bugfix)
end

@static if v"2.9" ≤ version() < v"2.10"
@cenum nlopt_algorithm::UInt32 begin
NLOPT_GN_DIRECT = 0
NLOPT_GN_DIRECT_L
Expand Down Expand Up @@ -158,10 +162,6 @@ function nlopt_srand_time()
ccall((:nlopt_srand_time, libnlopt), Cvoid, ())
end

function nlopt_version(major, minor, bugfix)
ccall((:nlopt_version, libnlopt), Cvoid, (Ptr{Cint}, Ptr{Cint}, Ptr{Cint}), major, minor, bugfix)
end

mutable struct nlopt_opt_s end

const nlopt_opt = Ptr{Cvoid}
Expand Down
2 changes: 1 addition & 1 deletion test/C_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ function test_algorithm_name()
sol = "Limited-memory BFGS (L-BFGS) (local, derivative-based)"
@test algorithm_name(algorithm) == sol
@test algorithm_name(:LD_LBFGS) == sol
if NLopt._is_version_newer_than_2_9()
if v"2.9" ≤ NLopt.version() < v"2.10"
@test algorithm_name(10) == sol
else
@test algorithm_name(11) == sol
Expand Down
Loading