Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Created July 25, 2022 12:24
Show Gist options
  • Save T1T4N/2c93997d50157c0fab580f13ac3c7048 to your computer and use it in GitHub Desktop.
Save T1T4N/2c93997d50157c0fab580f13ac3c7048 to your computer and use it in GitHub Desktop.
Build Infer on macOS 12 Monterey using Xcode 13.4.1 on Apple Silicon M1 (Pro)
class InferCustom < Formula
desc "Static analyzer for Java, C, C++, and Objective-C"
homepage "https://fbinfer.com/"
license "MIT"
revision 1
version "532dd663d"
head "https://github.com/facebook/infer.git", branch: "main"
stable do
url "https://github.com/facebook/infer/archive/532dd663d.tar.gz"
sha256 "af6fc4d5da5957457da50ae0379ef98885af3939f910f0025b1ed47b34a49af6"
# Fix FileUtils.cpp:44:57: error: invalid initialization of reference of type 'const string& ...
# Remove in the next release.
#patch do
#url "https://github.com/facebook/infer/commit/c90ec0683456e0f03135e7c059a1233351440736.patch?full_index=1"
#sha256 "516585352727c5372c4d4582ed9a64bc12e7a9eb59386aa3cec9908f0cfc86a8"
#end
# Apply patch for finding correct C++ header from Apple SDKs.
# Remove in the next release.
#patch do
#url "https://github.com/facebook/infer/commit/ec976d3be4e78dbbb019b3be941066f74e826880.patch?full_index=1"
#sha256 "4f299566c88dd5b6761d36fcb090d238c216d3721dde9037c725dac255be9d3b"
#end
end
livecheck do
url :stable
regex(/^v?(\d+(?:\.\d+)+)$/i)
end
bottle do
sha256 cellar: :any, big_sur: "2c27006c4ccbc05c6bb405c626d188e2dd5ca183a0a2f4441385def480ba4f34"
sha256 cellar: :any, catalina: "7a2816d33cea3053531f51402e0145e4e8ee7537570ba2a580c3ef8aaff379fc"
sha256 cellar: :any, mojave: "2bda6a47ff87c9d79a3f937c9cf24771798107c1bc215823e6156d6ba414f40d"
sha256 x86_64_linux: "987d26d95d3e073a96c683710ab0298a1674d2ee6e7a2ee4cb0d8914f2b0139d"
end
# https://github.com/Homebrew/homebrew-core/pull/87904
# https://github.com/facebook/infer/issues/1568
#deprecate! date: "2021-12-20", because: :does_not_build
depends_on "autoconf" => :build
depends_on "automake" => :build
depends_on "cmake" => :build
depends_on "libtool" => :build
depends_on "ninja" => :build
depends_on "opam" => :build
depends_on "openjdk@11" => [:build, :test]
depends_on "pkg-config" => :build
depends_on "python@3.9" => :build
depends_on "gmp"
depends_on "mpfr"
depends_on "sqlite"
# Add `llvm` for lld due to CMake bug where CC=clang doesn't fallback to ld.
# This causes error: /bin/sh: 1: CMAKE_LINKER-NOTFOUND: not found
# CMake PR ref: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6457
uses_from_macos "llvm" => :build # TODO: remove when `cmake` is fixed
uses_from_macos "m4" => :build
uses_from_macos "unzip" => :build
uses_from_macos "libedit"
uses_from_macos "libffi"
uses_from_macos "libxml2"
uses_from_macos "ncurses"
uses_from_macos "xz"
uses_from_macos "zlib"
on_linux do
depends_on "patchelf" => :build
depends_on "elfutils" # openmp requires <gelf.h>
end
def install
# Fixes: Uncaught Internal Error: ("unknown zone" (zone UTC0))
# https://github.com/facebook/infer/issues/1548
ENV.delete "TZ"
# needed to build clang
ENV.permit_arch_flags
# Apple's libstdc++ is too old to build LLVM
ENV.libcxx if ENV.compiler == :clang
# Use JDK11
ENV["JAVA_HOME"] = Formula["openjdk@11"].opt_prefix
opamroot = buildpath/"opamroot"
opamroot.mkpath
ENV["OPAMROOT"] = opamroot
ENV["OPAMYES"] = "1"
ENV["OPAMVERBOSE"] = "1"
ENV["PATCHELF"] = Formula["patchelf"].opt_bin/"patchelf" if OS.linux?
system "opam", "init", "--no-setup", "--disable-sandboxing"
#system "opam", "option", "--global", "depext=false"
# 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"
#ENV["INFER_CONFIGURE_OPTS"] = "--prefix=#{prefix} --without-fcp-clang"
# Let's try build clang faster
ENV["JOBS"] = ENV.make_jobs.to_s
# Release build
touch ".release"
# Disable handling external dependencies as opam is not aware of Homebrew on Linux.
# Error: Package conflict! * Missing dependency: - conf-autoconf
#inreplace "build-infer.sh", "infer \"$INFER_ROOT\" $locked", "\\0 --no-depexts" if OS.linux?
inreplace "build-infer.sh", "\"$INFER_ROOT\"/opam/infer.opam$locked", "\\0 --no-depexts"
system "./build-infer.sh", "all", "--yes"
system "make", "install-with-libs"
end
test do
ENV["JAVA_HOME"] = Formula["openjdk@11"].opt_prefix
ENV.append_path "PATH", Formula["openjdk@11"].opt_bin
(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
no_issues_output = "\n No issues found \n"
failing_c_output = <<~EOS
FailingTest.c:5: error: Null Dereference
\ pointer `s` last assigned on line 4 could be null and is dereferenced at line 5, column 3.
\ 3. int main() {
\ 4. int *s = NULL;
\ 5. *s = 42;
\ ^
\ 6. return 0;
\ 7. }
Found 1 issue
\ Issue Type(ISSUED_TYPE_ID): #
\ Null Dereference(NULL_DEREFERENCE): 1
EOS
assert_equal failing_c_output.to_s,
shell_output("#{bin}/infer --fail-on-issue -P -- clang -c FailingTest.c", 2)
assert_equal no_issues_output.to_s,
shell_output("#{bin}/infer --fail-on-issue -P -- clang -c PassingTest.c")
(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
failing_java_output = <<~EOS
FailingTest.java:12: error: Null Dereference
\ object `s` last assigned on line 11 could be null and is dereferenced at line 12.
\ 10. int mayCauseNPE() {
\ 11. String s = mayReturnNull(0);
\ 12. > return s.length();
\ 13. }
\ 14. }
Found 1 issue
\ Issue Type(ISSUED_TYPE_ID): #
\ Null Dereference(NULL_DEREFERENCE): 1
EOS
assert_equal failing_java_output.to_s,
shell_output("#{bin}/infer --fail-on-issue -P -- javac FailingTest.java", 2)
assert_equal no_issues_output.to_s,
shell_output("#{bin}/infer --fail-on-issue -P -- javac PassingTest.java")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment