homebrew-core/Formula/infer.rb
2018-05-22 04:33:01 -07:00

142 lines
4.1 KiB
Ruby

class Infer < Formula
desc "Static analyzer for Java, C, C++, and Objective-C"
homepage "https://fbinfer.com/"
# pull from git tag to get submodules
url "https://github.com/facebook/infer.git",
:tag => "v0.14.0",
:revision => "9ed60bc93613b6c232ef37803dd5fb74c8071acf"
bottle do
cellar :any
sha256 "5ace91fd29d841213572610a39c6d569d2173faa8523973049192585a610c44d" => :high_sierra
sha256 "502d37e6c91efeb5a83209746da749efa5e88f215cbfc62262d1b02625182ec5" => :sierra
sha256 "663784ad5b63cc01cfb9479859d86071d80bb5d575ed516bf2c6eeef7e874242" => :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.7+", :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
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"
# do not attempt to use the clang in facebook-clang-plugins/ as it hasn't been built yet
ENV["INFER_CONFIGURE_OPTS"] = "--prefix=#{prefix} --without-fcp-clang"
target_platform = if build.without?("clang")
"java"
elsif build.without?("java")
"clang"
else
"all"
end
llvm_args = %w[
-DLLVM_INCLUDE_DOCS=OFF
-DLLVM_INSTALL_UTILS=OFF
-DLLVM_TARGETS_TO_BUILD=all
-DLIBOMP_ARCH=x86_64
-DLLVM_BUILD_EXTERNAL_COMPILER_RT=ON
-DLLVM_BUILD_LLVM_DYLIB=ON
]
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"
inreplace "facebook-clang-plugins/clang/setup.sh", "CMAKE_ARGS=(", "CMAKE_ARGS=(\n " + llvm_args.join("\n ")
system "./build-infer.sh", target_platform, "--yes"
system "opam", "config", "exec", "--switch=infer-#{ocaml_version}", "--", "make", "install"
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