Skip to content

Commit

Permalink
Rewrite NLopt 2.9 workaround for NLopt 2.10 (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored Feb 4, 2025
1 parent fc4b12c commit 0ac1b5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
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

0 comments on commit 0ac1b5b

Please sign in to comment.