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

Fix: essentia on apple silicon #36

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 14 additions & 10 deletions essentia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Essentia < Formula
desc "Library for audio analysis and audio-based music information retrieval"
homepage "http://essentia.upf.edu"
head 'https://github.com/MTG/essentia.git'
revision 1

include Language::Python::Virtualenv

Expand All @@ -20,7 +21,7 @@ class Essentia < Formula
option "without-python", "Build without Python 3.9 support"

depends_on "python@3.9" if build.with? "python"
depends_on "numpy" if build.with? "python"
depends_on "mtg/essentia/numpy@1.23.3" if build.with? "python"

resource "six" do
url "https://files.pythonhosted.org/packages/21/9f/b251f7f8a76dec1d6651be194dfba8fb8d7781d10ab3987190de8391d08e/six-1.14.0.tar.gz"
Expand All @@ -44,9 +45,9 @@ def install
build_flags += ["--with-tensorflow"]
end

system Formula["python@3.9"].opt_bin/"python3", "waf", "configure", *build_flags
system Formula["python@3.9"].opt_bin/"python3", "waf"
system Formula["python@3.9"].opt_bin/"python3", "waf", "install"
system Formula["python@3.9"].opt_bin/"python3.9", "waf", "configure", *build_flags
system Formula["python@3.9"].opt_bin/"python3.9", "waf"
system Formula["python@3.9"].opt_bin/"python3.9", "waf", "install"

python_flags = [
"--mode=release",
Expand All @@ -58,15 +59,18 @@ def install
ENV['PKG_CONFIG_PATH'] = "#{prefix}/lib/pkgconfig:" + ENV['PKG_CONFIG_PATH']

if build.with? "python"
system Formula["python@3.9"].opt_bin/"python3", "waf", "configure", *python_flags
system Formula["python@3.9"].opt_bin/"python3", "waf"
system Formula["python@3.9"].opt_bin/"python3", "waf", "install"
site_packages = Language::Python.site_packages(Formula["python@3.9"].opt_libexec/"bin/python")
ENV.prepend_create_path "PYTHONPATH", Formula["mtg/essentia/numpy@1.23.3"].opt_prefix/site_packages

system Formula["python@3.9"].opt_bin/"python3.9", "waf", "configure", *python_flags
system Formula["python@3.9"].opt_bin/"python3.9", "waf"
system Formula["python@3.9"].opt_bin/"python3.9", "waf", "install"

resource("six").stage do
system Formula["python@3.9"].opt_bin/"python3", *Language::Python.setup_install_args(libexec)
system Formula["python@3.9"].opt_bin/"python3.9", *Language::Python.setup_install_args(libexec)
end

version = Language::Python.major_minor_version Formula["python@3.9"].opt_bin/"python3"
version = Language::Python.major_minor_version Formula["python@3.9"].opt_bin/"python3.9"
site_packages = "lib/python#{version}/site-packages"
pth_contents = "import site; site.addsitedir('#{libexec/site_packages}')\n"
(prefix/site_packages/"homebrew-essentia.pth").write pth_contents
Expand All @@ -85,7 +89,7 @@ def install
EOS

if build.with? "python"
system Formula["python@3.9"].opt_bin/"python3", "-c", "#{py_test}"
system Formula["python@3.9"].opt_bin/"python3.9", "-c", "#{py_test}"
end
end
end
52 changes: 52 additions & 0 deletions libcython@0.29.30.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class LibcythonAT02930 < Formula
desc "Compiler for writing C extensions for the Python language"
homepage "https://cython.org/"
url "https://files.pythonhosted.org/packages/d4/ad/7ce0cccd68824ac9623daf4e973c587aa7e2d23418cd028f8860c80651f5/Cython-0.29.30.tar.gz"
sha256 "2235b62da8fe6fa8b99422c8e583f2fb95e143867d337b5c75e4b9a1a865f9e3"
license "Apache-2.0"

livecheck do
formula "cython"
end

keg_only <<~EOS
this formula is mainly used internally by other formulae.
Users are advised to use `pip` to install cython
EOS

# depends_on "python@3.10" => [:build, :test]
depends_on "python@3.9" => [:build, :test]

def pythons
deps.map(&:to_formula)
.select { |f| f.name.match?(/python@\d\.\d+/) }
.map(&:opt_bin)
.map { |bin| bin/"python3.9" }
end

def install
pythons.each do |python|
ENV.prepend_create_path "PYTHONPATH", libexec/Language::Python.site_packages(python)
system python, *Language::Python.setup_install_args(libexec),
"--install-lib=#{libexec/Language::Python.site_packages(python)}"
end
end

test do
phrase = "You are using Homebrew"
(testpath/"package_manager.pyx").write "print '#{phrase}'"
(testpath/"setup.py").write <<~EOS
from distutils.core import setup
from Cython.Build import cythonize

setup(
ext_modules = cythonize("package_manager.pyx")
)
EOS
pythons.each do |python|
ENV.prepend_path "PYTHONPATH", libexec/Language::Python.site_packages(python)
system python, "setup.py", "build_ext", "--inplace"
assert_match phrase, shell_output("#{python} -c 'import package_manager'")
end
end
end
80 changes: 80 additions & 0 deletions numpy@1.23.3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class NumpyAT1233 < Formula
desc "Package for scientific computing with Python"
homepage "https://www.numpy.org/"
url "https://files.pythonhosted.org/packages/0a/88/f4f0c7a982efdf7bf22f283acf6009b29a9cc5835b684a49f8d3a4adb22f/numpy-1.23.3.tar.gz"
sha256 "51bf49c0cd1d52be0a240aa66f3458afc4b95d8993d2d04f0d91fa60c10af6cd"
license "BSD-3-Clause"
head "https://github.com/numpy/numpy.git", branch: "main"

depends_on "gcc" => :build # for gfortran
depends_on "mtg/essentia/libcython@0.29.30" => :build
# depends_on "python@3.10" => [:build, :test]
depends_on "python@3.9" => [:build, :test]
depends_on "openblas"

conflicts_with "numpy", because: "both install f2py and other binaries"

fails_with gcc: "5"

def pythons
deps.map(&:to_formula)
.select { |f| f.name.match?(/^python@\d\.\d+$/) }
.sort_by(&:version) # so that `bin/f2py` and `bin/f2py3` use python3.10
.map { |f| f.opt_libexec/"bin/python" }
end

def install
openblas = Formula["openblas"]
ENV["ATLAS"] = "None" # avoid linking against Accelerate.framework
ENV["BLAS"] = ENV["LAPACK"] = openblas.opt_lib/shared_library("libopenblas")

config = <<~EOS
[openblas]
libraries = openblas
library_dirs = #{openblas.opt_lib}
include_dirs = #{openblas.opt_include}
EOS

Pathname("site.cfg").write config

pythons.each do |python|
site_packages = Language::Python.site_packages(python)
ENV.prepend_path "PYTHONPATH", Formula["mtg/essentia/libcython@0.29.30"].opt_libexec/site_packages

system python, "setup.py", "build", "--fcompiler=#{Formula["gcc"].opt_bin}/gfortran",
"--parallel=#{ENV.make_jobs}"
system python, *Language::Python.setup_install_args(prefix, python)
end
end

def caveats
on_macos do
<<-EOS
This formula technically conflicts with the current Homebrew
version of `numpy`, as they both provide binaries. However,
the only result is that this formula can't be fully linked.

Since essentia only requires numpy's python bindings, there are
a few solutions (you only need to do one of the following):

1. `brew uninstall numpy` (easiest, only if you have no formulae that depend on numpy)
2. `brew unlink numpy`, then install this formula
3. If you need to keep `numpy` linked after installing this, you must run the
following to use this version of numpy when calling python3.9:

export PYTHONPATH="#{opt_prefix/Language::Python.site_packages(Formula["python@3.9"].opt_libexec/"bin/python")}"
EOS
end
end

test do
pythons.each do |python|
system python, "-c", <<~EOS
import numpy as np
t = np.ones((3,3), int)
assert t.sum() == 9
assert np.dot(t, t).sum() == 27
EOS
end
end
end