homebrew-core/Formula/infer.rb
2017-11-03 03:05:52 -07:00

141 lines
4.2 KiB
Ruby

class Infer < Formula
desc "Static analyzer for Java, C, C++, and Objective-C"
homepage "http://fbinfer.com/"
# pull from git tag to get submodules
url "https://github.com/facebook/infer.git",
:tag => "v0.13.0",
:revision => "ddda04c92b03dc86a0c32feaf8544f23874de4b3"
bottle do
cellar :any
sha256 "cec01990ef619d19129f3b47c33c6e98e1fad1e2a453ec2d81ae1e9eeadeef15" => :high_sierra
sha256 "cb01220e0ac8d4c1d78230819706311ae2dc160ca8fbd30dfe7f19519dbba31f" => :sierra
sha256 "8cfddae067f407906fc983a69e8cdb5355cb766c7d43b853f0f4e45603ac2ecd" => :el_capitan
end
option "without-clang", "Build without the C/C++/Objective-C analyzers"
option "without-java", "Build without the Java analyzers"
depends_on "autoconf" => :build
depends_on "automake" => :build
depends_on "cmake" => :build
depends_on :java => ["1.8", :build]
depends_on "libtool" => :build
depends_on "ocaml" => :build
depends_on "opam" => :build
depends_on "pkg-config" => :build
def install
if build.without?("clang") && build.without?("java")
odie "infer: --without-clang and --without-java are mutually exclusive"
end
# fix symbol not found issue (_clock_gettime) on el_capitan
ENV.delete("SDKROOT")
if build.with?("clang")
# needed to build clang
ENV.permit_arch_flags
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
end
opamroot = buildpath/"opamroot"
opamroot.mkpath
ENV["OPAMROOT"] = opamroot
ENV["OPAMYES"] = "1"
# Some of the libraries installed by ./build-infer.sh do not
# support parallel builds, eg OCaml itself. ./build-infer.sh
# builds in its own parallelization logic to mitigate that.
ENV.deparallelize
# do not attempt to use the clang in facebook-clang-plugins/ as it hasn't been built yet
ENV["INFER_CONFIGURE_OPTS"] = "--prefix=#{prefix} --disable-ocaml-binannot --without-fcp-clang"
target_platform = if build.without?("clang")
"java"
elsif build.without?("java")
"clang"
else
"all"
end
system "opam", "init", "--no-setup"
ocaml_version = File.read("build-infer.sh").match(/OCAML_VERSION=\${OCAML_VERSION:-\"([^\"]+)\"}/)[1]
ocaml_version_number = ocaml_version.split("+", 2)[0]
inreplace "#{opamroot}/compilers/#{ocaml_version_number}/#{ocaml_version}/#{ocaml_version}.comp",
'["./configure"', '["./configure" "-no-graph"'
# so that `infer --version` reports a release version number
inreplace "infer/src/base/Version.ml.in", "let is_release = is_yes \"@IS_RELEASE_TREE@\"", "let is_release = true"
system "./build-infer.sh", target_platform, "--yes"
system "opam", "config", "exec", "--switch=infer-#{ocaml_version}", "--", "make", "install"
bin.env_script_all_files(libexec/"bin", Language::Java.java_home_env("1.8"))
end
test do
(testpath/"FailingTest.c").write <<~EOS
#include <stdio.h>
int main() {
int *s = NULL;
*s = 42;
return 0;
}
EOS
(testpath/"PassingTest.c").write <<~EOS
#include <stdio.h>
int main() {
int *s = NULL;
if (s != NULL) {
*s = 42;
}
return 0;
}
EOS
shell_output("#{bin}/infer --fail-on-issue -- clang -c FailingTest.c", 2)
shell_output("#{bin}/infer --fail-on-issue -- clang -c PassingTest.c", 0)
(testpath/"FailingTest.java").write <<~EOS
class FailingTest {
String mayReturnNull(int i) {
if (i > 0) {
return "Hello, Infer!";
}
return null;
}
int mayCauseNPE() {
String s = mayReturnNull(0);
return s.length();
}
}
EOS
(testpath/"PassingTest.java").write <<~EOS
class PassingTest {
String mayReturnNull(int i) {
if (i > 0) {
return "Hello, Infer!";
}
return null;
}
int mayCauseNPE() {
String s = mayReturnNull(0);
return s == null ? 0 : s.length();
}
}
EOS
shell_output("#{bin}/infer --fail-on-issue -- javac FailingTest.java", 2)
shell_output("#{bin}/infer --fail-on-issue -- javac PassingTest.java", 0)
end
end