Skip to content

Commit 0ac1b5b

Browse files
authored
Rewrite NLopt 2.9 workaround for NLopt 2.10 (#254)
1 parent fc4b12c commit 0ac1b5b

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

src/NLopt.jl

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@ module NLopt
88
using CEnum: @cenum
99
using NLopt_jll: libnlopt
1010

11-
# [email protected] removed the LD_LBFGS_NOCEDAL enum.
12-
# See https://github.com/stevengj/nlopt/issues/584 for details.
13-
function _is_version_newer_than_2_9()
14-
major, minor, bugfix = Ref{Cint}(), Ref{Cint}(), Ref{Cint}()
15-
@ccall libnlopt.nlopt_version(
16-
major::Ptr{Cint},
17-
minor::Ptr{Cint},
18-
bugfix::Ptr{Cint},
19-
)::Cvoid
20-
return (major[] > 2) || (major[] == 2 && minor[] >= 9)
11+
############################################################################
12+
13+
function version()
14+
major, minor, patch = Ref{Cint}(), Ref{Cint}(), Ref{Cint}()
15+
nlopt_version(major, minor, patch)
16+
return VersionNumber(major[], minor[], patch[])
2117
end
2218

2319
include("libnlopt.jl")
2420

2521
############################################################################
2622
# Mirrors of NLopt's C enum constants:
2723

28-
@static if _is_version_newer_than_2_9()
24+
@static if v"2.9" version() < v"2.10"
2925
@enum Algorithm::Cint begin
3026
GN_DIRECT = 0
3127
GN_DIRECT_L
@@ -37,7 +33,7 @@ include("libnlopt.jl")
3733
GN_ORIG_DIRECT_L
3834
GD_STOGO
3935
GD_STOGO_RAND
40-
# LD_LBFGS_NOCEDAL
36+
# LD_LBFGS_NOCEDAL: temporarily removed in nlopt 2.9 (nlopt#584)
4137
LD_LBFGS
4238
LN_PRAXIS
4339
LD_VAR1
@@ -446,12 +442,6 @@ numevals(o::Opt) = nlopt_get_numevals(o)
446442

447443
############################################################################
448444

449-
function version()
450-
major, minor, patch = Ref{Cint}(), Ref{Cint}(), Ref{Cint}()
451-
nlopt_version(major, minor, patch)
452-
return VersionNumber(major[], minor[], patch[])
453-
end
454-
455445
const NLOPT_VERSION = version()
456446

457447
############################################################################

src/libnlopt.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const nlopt_mfunc = Ptr{Cvoid}
1616
# typedef void ( * nlopt_precond ) ( unsigned n , const double * x , const double * v , double * vpre , void * data )
1717
const nlopt_precond = Ptr{Cvoid}
1818

19-
@static if _is_version_newer_than_2_9()
19+
function nlopt_version(major, minor, bugfix)
20+
ccall((:nlopt_version, libnlopt), Cvoid, (Ptr{Cint}, Ptr{Cint}, Ptr{Cint}), major, minor, bugfix)
21+
end
22+
23+
@static if v"2.9" version() < v"2.10"
2024
@cenum nlopt_algorithm::UInt32 begin
2125
NLOPT_GN_DIRECT = 0
2226
NLOPT_GN_DIRECT_L
@@ -158,10 +162,6 @@ function nlopt_srand_time()
158162
ccall((:nlopt_srand_time, libnlopt), Cvoid, ())
159163
end
160164

161-
function nlopt_version(major, minor, bugfix)
162-
ccall((:nlopt_version, libnlopt), Cvoid, (Ptr{Cint}, Ptr{Cint}, Ptr{Cint}), major, minor, bugfix)
163-
end
164-
165165
mutable struct nlopt_opt_s end
166166

167167
const nlopt_opt = Ptr{Cvoid}

test/C_API.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ function test_algorithm_name()
567567
sol = "Limited-memory BFGS (L-BFGS) (local, derivative-based)"
568568
@test algorithm_name(algorithm) == sol
569569
@test algorithm_name(:LD_LBFGS) == sol
570-
if NLopt._is_version_newer_than_2_9()
570+
if v"2.9" NLopt.version() < v"2.10"
571571
@test algorithm_name(10) == sol
572572
else
573573
@test algorithm_name(11) == sol

0 commit comments

Comments
 (0)