forked from ahmadia/homebrew-science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp_solve.rb
165 lines (139 loc) · 5.49 KB
/
lp_solve.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'formula'
class NumpyHasHeaders < Requirement
def satisfied?
not Dir["#{numpy_include_dir}/numpy/*.h"].empty?
end
def fatal?
true
end
def message
s = <<-EOS.undent
Apple ships NumPy without the header files :-/
To fix this, do either of the following options:
EOS
unless MacOS::CLT.installed?
s += " * Install the 'Command Line Tools for Xcode' via the Preferences in Xcode.\n"
end
s += " * `brew tap samueljohn/python` and `brew install numpy`\n"
s += " * `pip install numpy`\n"
s
end
end
class LpSolvePython < Formula
homepage 'http://lpsolve.sourceforge.net/5.5/Python.htm'
url 'http://downloads.sourceforge.net/lpsolve/lp_solve_5.5.2.0_Python_source.tar.gz'
sha1 '058cced6b6a27cc160c9c5054c6b94b0eae6d989'
version '5.5.2.0'
end
class LpSolve < Formula
homepage 'http://sourceforge.net/projects/lpsolve/'
url 'http://downloads.sourceforge.net/lpsolve/lp_solve_5.5.2.0_source.tar.gz'
version '5.5.2.0' # automatic version parser spits out "solve" as version
sha1 'e2830053cf079839b9ce21662cbc886ac6d31c80'
option 'python', 'Python extension to interface with lp_solve'
depends_on 'numpy' => :python if build.include? 'python'
depends_on NumpyHasHeaders.new if build.include? 'python'
def patches
# Prefer OS X's fast BLAS implementation (patch stolen from fink-project)
DATA
end
def install
# Thanks to superenv, we don't have to care if the ccc.osx build script
# tells the compiler stupid things. And Xcode-only works like charm.
# Clang on :snow_leopard does not ignore `-Wno-long-double` and errors out.
["configure", "configure.ac", "demo/ccc.osx", "lp_solve/ccc.osx",
"lpsolve55/ccc.osx", "lpsolve55/cccLUSOL.osx"].each do |f|
inreplace f, '-Wno-long-double', ''
end if MacOS.version <= :snow_leopard
cd 'lpsolve55' do
system "sh ccc.osx # lpsolve55 library"
lib.install Dir['./bin/osx64/*.a']
lib.install Dir['./bin/osx64/*.dylib']
end
cd 'lp_solve' do
system "sh ccc.osx # lp_solve executable"
bin.install './bin/osx64/lp_solve'
end
# Note, the demo does not build with lpsolve55. Upstream issue.
include.install Dir['*.h']
if build.include? 'python' then
# In order to install into the Cellar, the dir must exist and be in the
# PYTHONPATH.
temp_site_packages = lib/which_python/'site-packages'
mkdir_p temp_site_packages
ENV['PYTHONPATH'] = temp_site_packages
args = [
"--no-user-cfg",
"install",
"--force",
"--verbose",
"--install-scripts=#{share}/python",
"--install-lib=#{temp_site_packages}",
"--record=installed-files.txt"
]
lp_solve_prefix = prefix
LpSolvePython.new.brew do |b|
cd 'extra/Python' do
# On OS X malloc there is <sys/malloc.h> and <malloc/malloc.h>
inreplace "hash.c", "#include <malloc.h>", "#include <sys/malloc.h>"
# We know where the lpsolve55 lib is...
inreplace "setup.py", "LPSOLVE55 = '../../lpsolve55/bin/ux32'", "LPSOLVE55 = '#{lp_solve_prefix}/lib'"
# Correct path to lpsolve's include dir and go the official way to find numpy include_dirs
inreplace "setup.py",
"include_dirs=['../..', NUMPYPATH],",
"include_dirs=['#{lp_solve_prefix}/include', '#{numpy_include_dir}'],"
inreplace 'setup.py', "(NUMPY, '1')", "('NUMPY', '1')"
# Even their version number is broken ...
inreplace "setup.py", 'version = "5.5.0.9",', "version = '#{version}',"
system "python", "setup.py", *args
# Save the examples
(lp_solve_prefix/'share/lp_solve').install Dir['ex*.py']
(lp_solve_prefix/'share/lp_solve').install 'lpdemo.py'
(lp_solve_prefix/'share/lp_solve').install 'Python.htm'
end
end
end
end
def caveats; <<-EOS.undent
For non-homebrew Python, you need to amend your PYTHONPATH like so:
export PYTHONPATH=#{HOMEBREW_PREFIX}/lib/#{which_python}/site-packages:$PYTHONPATH
Python examples and doc are installed to #{HOMEBREW_PREFIX}/share/lp_solve.
EOS
end if build.include? 'python'
def test
mktemp do
input = <<-EOS.undent
max: 143 x + 60 y;
120 x + 210 y <= 15000;
110 x + 30 y <= 4000;
x + y <= 75;
EOS
Pathname.new('input.lp').write(input)
output = `#{bin}/lp_solve -S3 input.lp`
#puts output
match = output =~ /Value of objective function: 6315\.6250/
raise if match.nil?
end
if build.include? 'python'
system 'python', "#{HOMEBREW_PREFIX}/share/lp_solve/lpdemo.py"
end
end
end
def which_python
"python" + `python -c 'import sys;print(sys.version[:3])'`.strip
end
def numpy_include_dir
"#{`python -c "import numpy.distutils.misc_util as u; print(u.get_numpy_include_dirs())[0]"`.strip}"
end
__END__
diff --git a/lp_lib.h b/lp_lib.h
index 2ef654f..7b06ef8 100644
--- a/lp_lib.h
+++ b/lp_lib.h
@@ -104,7 +104,7 @@
/* Specify use of the basic linear algebra subroutine library */
/* ------------------------------------------------------------------------- */
#define libBLAS 2 /* 0: No, 1: Internal, 2: External */
-#define libnameBLAS "myBLAS"
+#define libnameBLAS "/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/libBLAS.dylib"
/* Active inverse logic (default is optimized original etaPFI) */