Skip to content

Commit

Permalink
[python] add configure switch
Browse files Browse the repository at this point in the history
Summary:
The main initial goal was to disable unit tests when python 3.8 is not
installed to prevent breaking the tests in that case.

- add a `--disable-python-analyzers` switch to `./configure`
- wire it to `./build-infer.sh`
- set a `PYTHON` variable from `./configure` into the Makefiles and use it
- export it to OCaml too in `Version.ml`
- finally the main thing: enable the python unit tests only when we have python support

Not done: propagate `BUILD_PYTHON_ANALYZERS` to OCaml as the python frontend
isn't currently wired into the infer executable itself

Reviewed By: vsiles

Differential Revision: D45008136

fbshipit-source-id: ccf848d922144348c700ed58b45ca862de12fc7f
  • Loading branch information
jvillard authored and facebook-github-bot committed Apr 18, 2023
1 parent 4590a90 commit b176245
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ rebar3.crashdump
/infer/src/java/dune
/infer/src/nullsafe/unit/dune
/infer/src/opensource/dune
/infer/src/python/unit/dune
/infer/src/unit/dune
.merlin

Expand Down
1 change: 1 addition & 0 deletions Makefile.autoconf.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ PATCHELF := @PATCHELF@
PATH := @PATH@
PLATFORM_ENV := @PLATFORM_ENV@
prefix := @prefix@
PYTHON := @PYTHON@
REALPATH := @REALPATH@
REBAR3 := @REBAR3@
SDKROOT := @SDKROOT@
Expand Down
14 changes: 13 additions & 1 deletion build-infer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function usage() {
echo " erlang build Erlang analyzer"
echo " hack build Hack analyzer"
echo " java build Java analyzer"
echo " python build Python analyzer"
echo
echo " options:"
echo " -h,--help show this message"
Expand All @@ -56,6 +57,7 @@ BUILD_CLANG=${BUILD_CLANG:-no}
BUILD_ERLANG=${BUILD_ERLANG:-no}
BUILD_HACK=${BUILD_HACK:-no}
BUILD_JAVA=${BUILD_JAVA:-no}
BUILD_PYTHON=${BUILD_PYTHON:-no}
INTERACTIVE=${INTERACTIVE:-yes}
JOBS=${JOBS:-$NCPU}
ONLY_SETUP_OPAM=${ONLY_SETUP_OPAM:-no}
Expand All @@ -69,6 +71,7 @@ function build_all() {
BUILD_ERLANG=yes
BUILD_HACK=yes
BUILD_JAVA=yes
BUILD_PYTHON=yes
}

while [[ $# -gt 0 ]]; do
Expand Down Expand Up @@ -99,6 +102,11 @@ while [[ $# -gt 0 ]]; do
shift
continue
;;
python)
BUILD_PYTHON=yes
shift
continue
;;
-h|--help)
usage
exit 0
Expand Down Expand Up @@ -137,7 +145,8 @@ while [[ $# -gt 0 ]]; do
done

if [ "$BUILD_CLANG" == "no" ] && [ "$BUILD_ERLANG" == "no" ] && \
[ "$BUILD_HACK" == "no" ] && [ "$BUILD_JAVA" == "no" ]; then
[ "$BUILD_HACK" == "no" ] && [ "$BUILD_JAVA" == "no" ] && \
[ "$BUILD_PYTHON" == "no" ]; then
build_all
fi

Expand Down Expand Up @@ -198,6 +207,9 @@ fi
if [ "$BUILD_JAVA" == "no" ]; then
CONFIGURE_PREPEND_OPTS+=" --disable-java-analyzers"
fi
if [ "$BUILD_PYTHON" == "no" ]; then
CONFIGURE_PREPEND_OPTS+=" --disable-python-analyzers"
fi

set -x
# empty arrays trigger unbound variable errors, use horrible syntax to work around it
Expand Down
21 changes: 20 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ AC_ARG_ENABLE(java-analyzers,
BUILD_JAVA_ANALYZERS=$enable_java_analyzers
AC_SUBST([BUILD_JAVA_ANALYZERS])

AC_ARG_ENABLE(python-analyzers,
AS_HELP_STRING([--disable-python-analyzers],
[do not build the Python analyzers (default is to build them)]),
,
enable_python_analyzers=yes)
BUILD_python_ANALYZERS=$enable_python_analyzers
AC_SUBST([BUILD_python_ANALYZERS])

AC_ARG_WITH(fcp-clang,
AS_HELP_STRING([--with-fcp-clang],
[use $CLANG_PREFIX/bin/clang to override the default compiler (default is not to override)]),
Expand Down Expand Up @@ -167,6 +175,17 @@ AS_IF([test "x$enable_c_analyzers" = "xyes"], [
])
# end if($enable_c_analyzers)

AS_IF([test "x$enable_python_analyzers" = "xyes"], [
AC_CHECK_TOOL([PYTHON], [python3], [no])
AC_ASSERT_PROG([python3], [$PYTHON])
AC_MSG_CHECKING([python version])
python_version=$("$PYTHON" --version)
case $python_version in
Python\ 3.8.*) AC_MSG_RESULT([$python_version]); break;;
*) AC_MSG_ERROR([python version $python_version is not supported, please install python version 3.8 instead or disable python support with --disable-python-analyzers]); break;;
esac
]) # end if($enable_python_analyzers)

AC_CHECK_TOOL([XCODE_SELECT], [xcode-select], [no])
AS_IF([test "x$XCODE_SELECT" != "xno"], [XCODE_SELECT_OUT=`xcode-select -p`])

Expand Down Expand Up @@ -281,7 +300,7 @@ AC_ARG_VAR([OPAMSWITCH], [Opam switch used for building infer.])
AC_CHECK_TOOL([OPAM], [opam], [no])
AS_IF([test "$OPAM" != "no"], [
AC_MSG_CHECKING([opam version])
opam_version=$(opam --version)
opam_version=$("$OPAM" --version)
case $opam_version in
2.*) AC_MSG_RESULT([$opam_version]); break;;
*) AC_MSG_ERROR([opam version $opam_version is not supported, please install opam version 2 instead]); break;;
Expand Down
5 changes: 4 additions & 1 deletion infer/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ GENERATED_FROM_AUTOCONF = dune.common ../dune-workspace base/Version.ml
.PHONY: dune-workspace
dune-workspace: ../dune-workspace

GENERATED_DUNES += dune clang/dune integration/dune java/dune opensource/dune unit/dune
GENERATED_DUNES += \
dune clang/dune integration/dune java/dune opensource/dune python/unit/dune unit/dune

SRC_BUILD_COMMON = $(GENERATED_FROM_AUTOCONF) $(GENERATED_DUNES) $(OCAML_SOURCES)
ifeq ($(BUILD_C_ANALYZERS),yes)
Expand Down Expand Up @@ -233,6 +234,7 @@ $(GENERATED_FROM_AUTOCONF): $(MAKEFILE_LIST)
-e "s|@OPAMSWITCH[@]|$(OPAMSWITCH)|g" \
-e "s|@XCODE_SELECT[@]|$(XCODE_SELECT)|g" \
-e "s|@INFER_MAN_LAST_MODIFIED[@]|$(INFER_MAN_LAST_MODIFIED)|g" \
-e "s|@PYTHON[@]|$(PYTHON)|g" \
$@.in > "$$TMPFILE"; \
cat "$$TMPFILE" > $@; \
echo -e "\n;;" >> $@; \
Expand All @@ -249,6 +251,7 @@ clang/dune: clang/dune.in
integration/dune: integration/dune.in
java/dune: java/dune.in
opensource/dune: opensource/dune.in
python/unit/dune: python/unit/dune.in
unit/dune: unit/dune.in

.PHONY: clean
Expand Down
2 changes: 2 additions & 0 deletions infer/src/base/Version.ml.in
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ let java_version = int_of_string_opt "@JAVA_MAJOR_VERSION@"
let xcode_enabled = is_not_no "@XCODE_SELECT@"

let man_pages_last_modify_date = "@INFER_MAN_LAST_MODIFIED@"

let python_exe = "@PYTHON@"
2 changes: 2 additions & 0 deletions infer/src/base/Version.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ val java_version : int option
val xcode_enabled : bool

val man_pages_last_modify_date : string

val python_exe : string [@@warning "-unused-value-declaration"] (* used in unit tests *)
2 changes: 1 addition & 1 deletion infer/src/datalog/scripts/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ build-targets:
make -C java_targets

test-classdump: build-targets
python3 $(SCRIPTS_DIR)/classdump.py --output $(SCRIPTS_DIR)/test/classdump_out $(JAR_FILES)
"$PYTHON" $(SCRIPTS_DIR)/classdump.py --output $(SCRIPTS_DIR)/test/classdump_out $(JAR_FILES)
@$(foreach jarfile,$(notdir $(JAR_FILES)),$(call check_no_diff,$(SCRIPTS_DIR)/test/classdump_out/$(jarfile).classes,$(SCRIPTS_DIR)/test/classdump_exp/$(jarfile).classes.exp);)

clean:
Expand Down
2 changes: 2 additions & 0 deletions infer/src/dune.common.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let hack = is_yes "@BUILD_HACK_ANALYZERS@"

let java = is_yes "@BUILD_JAVA_ANALYZERS@"

let python = is_yes "@BUILD_PYTHON_ANALYZERS@"

let facebook = is_yes "@IS_FACEBOOK_TREE@"

let extra_cflags = if is_empty "@EXTRA_CFLAGS@" then [] else ["@EXTRA_CFLAGS@"]
2 changes: 1 addition & 1 deletion infer/src/python/FFI.ml
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ let from_bytecode filename =


let from_file ~is_binary filename =
Py.initialize ~interpreter:"/usr/bin/python3.8" () ;
Py.initialize ~interpreter:Version.python_exe () ;
let code = if is_binary then from_bytecode filename else from_source filename in
Py.finalize () ;
code
2 changes: 1 addition & 1 deletion infer/src/python/unit/FFITest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let%test_module "load_code" =
let source = "x = 42"

let%expect_test _ =
Py.initialize ~version:3 ~minor:8 () ;
Py.initialize ~interpreter:Version.python_exe () ;
let res = FFI.from_string ~source ~filename:"dummy" in
Py.finalize () ;
F.printf "%s" (FFI.Code.show res) ;
Expand Down
10 changes: 5 additions & 5 deletions infer/src/python/unit/PyTransTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let%test_module "to_proc_desc" =

let%expect_test _ =
let source = "x = 42" in
Py.initialize ~version:3 ~minor:8 () ;
Py.initialize ~interpreter:Version.python_exe () ;
let code = FFI.from_string ~source ~filename:"dummy" in
Py.finalize () ;
let res = PyTrans.to_module ~sourcefile "$toplevel::main" code in
Expand Down Expand Up @@ -44,7 +44,7 @@ let%test_module "to_proc_desc" =
x = 42
print(x)
|} in
Py.initialize ~version:3 ~minor:8 () ;
Py.initialize ~interpreter:Version.python_exe () ;
let code = FFI.from_string ~source ~filename:"dummy" in
Py.finalize () ;
let res = PyTrans.to_module ~sourcefile "$toplevel::main" code in
Expand Down Expand Up @@ -79,7 +79,7 @@ x = 42
y = 10
print(x + y)
|} in
Py.initialize ~version:3 ~minor:8 () ;
Py.initialize ~interpreter:Version.python_exe () ;
let code = FFI.from_string ~source ~filename:"dummy" in
Py.finalize () ;
let res = PyTrans.to_module ~sourcefile "$toplevel::main" code in
Expand Down Expand Up @@ -132,7 +132,7 @@ z = my_fun(42, a)
print(z)
|}
in
Py.initialize ~version:3 ~minor:8 () ;
Py.initialize ~interpreter:Version.python_exe () ;
let code = FFI.from_string ~source ~filename:"dummy" in
Py.finalize () ;
let res = PyTrans.to_module ~sourcefile "$toplevel::main" code in
Expand Down Expand Up @@ -197,7 +197,7 @@ update_global()
print(z)
|}
in
Py.initialize ~version:3 ~minor:8 () ;
Py.initialize ~interpreter:Version.python_exe () ;
let code = FFI.from_string ~source ~filename:"dummy" in
Py.finalize () ;
let res = PyTrans.to_module ~sourcefile "$toplevel::main" code in
Expand Down
23 changes: 0 additions & 23 deletions infer/src/python/unit/dune

This file was deleted.

35 changes: 35 additions & 0 deletions infer/src/python/unit/dune.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(* -*- tuareg -*- *)
(*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)
(* NOTE: prepend dune.common to this file! *)

let library =
Printf.sprintf
{|
(library
(name PythonFrontendTest)
(flags
(:standard
-open
IStdlib
-open
IStd
-open
IBase
-open
PythonFrontend
-open
Textuallib))
(libraries IStdlib IBase PythonFrontend Textuallib)
(preprocess
(pps ppx_deriving.std ppx_expect ppx_inline_test))
%s
)
|} (if python then "(inline_tests)" else "")
;;

Jbuild_plugin.V1.send library

0 comments on commit b176245

Please sign in to comment.