<commit_before>require 'formula'

class Octave < Formula
  homepage 'http://www.gnu.org/software/octave/index.html'
  url 'http://ftpmirror.gnu.org/octave/octave-3.8.0.tar.bz2'
  mirror 'http://ftp.gnu.org/gnu/octave/octave-3.8.0.tar.bz2'
  sha1 'ebb03485b72d97fa01f105460f81016f94680f77'

  skip_clean 'share/info' # Keep the docs

  option 'without-check', 'Skip build-time tests (not recommended)'
  option 'without-docs', 'Do not build documentation'

  depends_on :fortran

  depends_on 'pkg-config' => :build
  depends_on 'gnu-sed' => :build
  depends_on 'texinfo' => :build     # OS X's makeinfo won't work for this

  depends_on :x11
  depends_on 'fftw'
  # When building 64-bit binaries on Snow Leopard, there are naming issues with
  # the dot product functions in the BLAS library provided by Apple's
  # Accelerate framework. See the following thread for the gory details:
  #
  #   http://www.macresearch.org/lapackblas-fortran-106
  #
  # We can work around the issues using dotwrp.
  depends_on 'dotwrp' if MacOS.version == :snow_leopard and MacOS.prefer_64_bit?
  # octave refuses to work with BSD readline, so it's either this or --disable-readline
  depends_on 'readline'
  depends_on 'curl' if MacOS.version == :leopard # Leopard's libcurl is too old

  depends_on 'arpack'
  depends_on 'suite-sparse'
  depends_on 'hdf5'
  depends_on 'pcre'
  depends_on 'qhull'
  depends_on 'qrupdate'

  depends_on 'openblas' => :optional
  depends_on 'graphicsmagick' => :recommended
  depends_on 'gnuplot' => :recommended
  depends_on 'fltk' => :optional
  depends_on 'qt' => :optional

  # The fix for std::unordered_map requires regenerating
  # configure. Remove once the fix is in next release.
  depends_on :autoconf => :build
  depends_on :automake => :build
  depends_on :libtool => :build

  def blas_flags
    flags = []
    if build.with? 'openblas'
      flags << "-L#{Formula["openblas"].lib} -lopenblas"
    else
      flags << "-ldotwrp" if MacOS.version == :snow_leopard and MacOS.prefer_64_bit?
      # Cant use `-framework Accelerate` because `mkoctfile`, the tool used to
      # compile extension packages, can't parse `-framework` flags.
      flags << "-Wl,-framework,Accelerate"
    end
    flags.join(" ")
  end

  def install
    ENV.m64 if MacOS.prefer_64_bit?
    ENV.append_to_cflags "-D_REENTRANT"

    args = [
      '--disable-dependency-tracking',
      "--prefix=#{prefix}",
      "--with-blas=#{blas_flags}",
      "--with-lapack=#{blas_flags}",
      # SuiteSparse-4.x.x fix, see http://savannah.gnu.org/bugs/?37031
      '--with-umfpack=-lumfpack -lsuitesparseconfig',
      '--disable-jit'
    ]
    args << '--without-framework-carbon' if MacOS.version >= :lion
    # avoid spurious 'invalid assignment to cs-list' erorrs on 32 bit installs:
    args << 'CXXFLAGS=-O0' unless MacOS.prefer_64_bit?
    args << '--disable-docs' if build.without? 'docs'
    args << '--without-opengl' if build.without? 'fltk'
    args << '--disable-gui' if build.without? 'qt'

    # The fix for std::unordered_map requires regenerating
    # configure. Remove once the fix is in next release.
    system "autoreconf", "-ivf"

    system "./configure", *args
    system "make all"
    system "make check 2>&1 | tee make-check.log" if build.with? 'check'
    system "make install"

    prefix.install ["test/fntests.log", "make-check.log"] if build.with? 'check'
  end

  def caveats
    native_caveats = <<-EOS.undent
      Octave supports "native" plotting using OpenGL and FLTK. You can activate
      it for all future figures using the Octave command

          graphics_toolkit ("fltk")

      or for a specific figure handle h using

          graphics_toolkit (h, "fltk")

      Otherwise, gnuplot is still used by default, if available.
    EOS

    gnuplot_caveats = <<-EOS.undent
      When plotting with gnuplot, you should set "GNUTERM=x11" before running octave;
      if you are using Aquaterm, use "GNUTERM=aqua".
    EOS

    glpk_caveats = <<-EOS.undent

      GLPK functionality has been disabled due to API incompatibility.
    EOS

    s = glpk_caveats
    s = gnuplot_caveats + s if build.with? 'gnuplot'
    s = native_caveats + s if build.with? 'fltk'
  end
end<commit_msg>octave 3.8.1: optional GUI & JIT<commit_after>require "formula"

class Octave < Formula
  homepage "http://www.gnu.org/software/octave/index.html"
  url      "http://ftpmirror.gnu.org/octave/octave-3.8.1.tar.bz2"
  mirror   "http://ftp.gnu.org/gnu/octave/octave-3.8.1.tar.bz2"
  sha1     "2951aeafe58d562672feb80dd8c3cfe0643a5087"
  head     "http://www.octave.org/hg/octave", :branch => "gui-release", :using => :hg

  unless build.head?
    # Allows the arrow keys to page through command history.
    # See: https://savannah.gnu.org/bugs/?41337
    patch do
      url "https://savannah.gnu.org/bugs/download.php?file_id=30734"
      sha1 "e8fb39b7ca1525d67e6d24f3c189b441b60fcdab"
    end
  end

  # Allows mkoctfile to process "-framework vecLib" properly.
  # See: https://savannah.gnu.org/bugs/?42002
  patch do
    url "https://savannah.gnu.org/patch/download.php?file_id=31072"
    sha1 "19f2dcaf636f1968c4b1639797415f83fb21d5a3"
  end

  skip_clean "share/info" # Keep the docs

  option "without-check",          "Skip build-time tests (not recommended)"
  option "without-docs",           "Do not build documentation"
  option "without-gui",            "Do not build the experimental GUI"
  option "with-native-graphics",   "Use native OpenGL/FLTKgraphics (does not work with the GUI)"
  option "without-gnuplot",        "Do not use gnuplot graphics"
  option "with-jit",               "Use the experimental JIT support (not recommended)"

  option "with-openblas",          "Use OpenBLAS instead of native LAPACK/BLAS"
  option "without-curl",           "Do not use cURL (urlread/urlwrite/@ftp)"
  option "without-fftw",           "Do not use FFTW (fft,ifft,fft2,etc.)"
  option "without-glpk",           "Do not use GLPK"
  option "without-ghostscript",    "Do not use Ghostscript (PS/PDF image output)"
  option "without-graphicsmagick", "Do not use GraphicsMagick++ (imread,imwrite)"
  option "without-hdf5",           "Do not use HDF5 (hdf5 data file support)"
  option "without-qhull",          "Do not use the Qhull library (delaunay,voronoi,etc.)"
  option "without-qrupdate",       "Do not use the QRupdate package (qrdelete,qrinsert,qrshift,qrupdate)"
  option "without-suite-sparse",   "Do not use SuiteSparse (sparse matrix operations)"
  option "without-zlib",           "Do not use zlib (compressed MATLAB file formats)"

  depends_on :fortran

  depends_on "pkg-config"     => :build
  depends_on "gnu-sed"        => :build
  if build.with? "docs"
    depends_on "texinfo"      => :build
    depends_on :tex           => :build
  end
  if build.head?
    depends_on "bison"        => :build
    depends_on "automake"     => :build
    depends_on "autoconf"     => :build
  end

  depends_on "pcre"
  if build.with? "gui" or build.head?
    depends_on "qscintilla2"
    depends_on "qt"
  end
  if build.with? "native-graphics" or build.head?
    depends_on "fltk"
    depends_on "fontconfig"
    depends_on "freetype"
  end
  depends_on "llvm"           if build.with? "jit"
  depends_on "curl"           if build.with? "curl" and MacOS.version == :leopard

  depends_on "gnuplot"        => :recommended
  depends_on "suite-sparse"   => :recommended
  depends_on "readline"       => :recommended
  depends_on "arpack"         => :recommended
  depends_on "fftw"           => :recommended
  depends_on "glpk"           => :recommended
  depends_on "gl2ps"          => :recommended
  depends_on "graphicsmagick" => :recommended
  depends_on "ghostscript"    => :recommended
  depends_on "hdf5"           => :recommended
  depends_on "qhull"          => :recommended
  depends_on "qrupdate"       => :recommended
  depends_on "pstoedit"       => :recommended
  depends_on "epstool"        => :recommended

  depends_on "openblas"       => :optional

  def install
    ENV.m64 if MacOS.prefer_64_bit?
    ENV.append_to_cflags "-D_REENTRANT"
    ENV["JAVA_HOME"] = `/usr/libexec/java_home`.chomp!

    args = [ "--prefix=#{prefix}" ]

    args << "--with-blas=-L#{Formula["openblas"].opt_lib} -lopenblas" if build.with? "openblas"
    args << "--disable-docs"     if build.without? "docs"
    args << "--enable-jit"       if build.with?    "jit"
    args << "--disable-gui"      if build.without? "gui"
    args << "--without-opengl"   if build.without? "native-graphics" and not build.head?

    args << "--disable-readline" if build.without? "readline"
    args << "--without-curl"     if build.without? "curl"
    args << "--without-fftw3"    if build.without? "fftw"
    args << "--without-glpk"     if build.without? "glpk"
    args << "--without-hdf5"     if build.without? "hdf5"
    args << "--without-qhull"    if build.without? "qhull"
    args << "--without-qrupdate" if build.without? "qrupdate"

    if build.without? "suite-sparse"
      args << "--without-amd"
      args << "--without-camd"
      args << "--without-colamd"
      args << "--without-ccolamd"
      args << "--without-cxsparse"
      args << "--without-camd"
      args << "--without-cholmod"
      args << "--without-umfpack"
    else
      sparse = Tab.for_name("suite-sparse").used_options
      ENV.append_to_cflags "-L#{Formula["metis4"].opt_lib} -lmetis" if sparse.include? "with-metis4"
    end

    args << "--without-zlib"     if build.without? "zlib"
    args << "--with-x=no"     #We don't need X11 for Mac at all

    system "./bootstrap" if build.head?

    # Libtool needs to see -framework to handle dependencies better.
    inreplace "configure", "-Wl,-framework -Wl,", "-framework "

    # The Mac build configuration passes all linker flags to mkoctfile to
    # be inserted into every oct/mex build. This is actually unnecessary and
    # can cause linking problems.
    inreplace "src/mkoctfile.in.cc", /%OCTAVE_CONF_OCT(AVE)?_LINK_(DEPS|OPTS)%/, '""'

    system "./configure", *args
    system "make all"
    system "make check 2>&1 | tee make-check.log" if build.with? "check"
    system "make install"
    prefix.install "make-check.log" if File.exists? "make-check.log"
    prefix.install "test/fntests.log" if File.exists? "test/fntests.log"
  end

  def caveats
    s = ""

    if build.with? "gnuplot"
      s = s + <<-EOS.undent

        To use the gnuplot plotting engine, you must set the environment
        variable GNUTERM. Valid choices include:
            GNUTERM=x11   # X windows must be installed
            GNUTERM=qt    # gnuplot must have been compiled with Qt support
            GNUTERM=aqua  # if you are using Aquaterm
        You may also set this variable from within Octave with the command
            setenv('GNUTERM','qt') % or 'x11', or 'aqua'
      EOS
    end

    if build.with? "native graphics" or build.head?
      s = s + <<-EOS.undent

        You have configured Octave to use "native" OpenGL/FLTK plotting by
        default. If you prefer gnuplot, you can activate it for all future
        figures with the command
            graphics_toolkit ('gnuplot')
        or for a specific figure handle h using
            graphics_toolkit (h,'gnuplot')
      EOS
    end

    if build.head?
      s = s + <<-EOS.undent

        The HEAD installation activates the experimental GUI by default.
        To use the CLI version of octave, run the command "octave-cli".
      EOS
    elsif build.with? "gui"
      s = s + <<-EOS.undent

        The Octave GUI is experimental and not enabled by default. To use it,
        use the command-line argument "--force-gui"; e.g.,
            octave --force-gui
      EOS
      if build.with? "native-graphics"
        s = s + <<-EOS.undent

          Native graphics do *not* work with the GUI. You must switch to
          gnuplot when using it.
        EOS
      end
    end

    logfile = "#{prefix}/make-check.log"
    if File.exists? logfile
      logs = `grep 'libinterp/array/.*FAIL \\d' #{logfile}`
      unless logs.empty?
        s = s + <<-EOS.undent

            Octave's self-tests for this installation produced the following failues:
            --------
        EOS
        s = s + logs + <<-EOS.undent
            --------
            These failures indicate a conflict between Octave and its BLAS-related
            dependencies. You can likely correct these by removing and reinstalling
            arpack, qrupdate, suite-sparse, and octave. Please use the same BLAS
            settings for all (i.e., with the default, or "--with-openblas").
        EOS
        end
    end

    s = s + "\n" unless s.empty?
    s
  end
end
{'fraction_non_alphanumeric': 0.11872434480580991, 'fraction_numerical': 0.015787811809283233, 'mean_word_length': 2.6753698868581375, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 41, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6141891', 'n_tokens_mistral': 4434, 'n_tokens_neox': 4219, 'n_words': 1192}