Skip to content

Commit

Permalink
Install the python module inside a virtual environment (fix #243)
Browse files Browse the repository at this point in the history
The current installation process tries to install the ikos python module
as a system library. This is probably not what a typical user want.

Instead, let's install a virtual environment for ikos and install the
python module within it. Following homebrew's standard, this is
installed under <install-path>/libexec.
  • Loading branch information
arthaud committed Dec 10, 2023
1 parent 21cd2b5 commit ad754ac
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 124 deletions.
67 changes: 31 additions & 36 deletions analyzer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -333,48 +333,43 @@ else()
"${CMAKE_CURRENT_SOURCE_DIR}/python/ikos/settings.py.in"
VERBATIM)
endif()
add_custom_target(ikos-python-settings ALL
DEPENDS "python/ikos/settings/__init__.py"
)

# setup.py
configure_file(python/setup.py.in python/setup.py @ONLY)

file(GLOB PYTHON_FILES python/ikos/*.py)

# python module
add_custom_command(
OUTPUT
"python/build/lib/ikos/__init__.py"
COMMAND
${PYTHON_EXECUTABLE} setup.py -q build
--build-base=build
--build-purelib=build/lib
COMMAND
${CMAKE_COMMAND} -E touch "build/lib/ikos/__init__.py" # udpate timestamp
WORKING_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}/python"
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/python/setup.py"
"${CMAKE_CURRENT_BINARY_DIR}/python/ikos/settings/__init__.py"
${PYTHON_FILES}
)
add_custom_target(ikos-python ALL
DEPENDS "python/build/lib/ikos/__init__.py"
)
option(INSTALL_PYTHON_VIRTUALENV "Install a python virtual environment for ikos" ON)
option(USE_PYTHON_VIRTUALENV "Path to an existing python virtual environment")
if (INSTALL_PYTHON_VIRTUALENV)
install(CODE "
message(STATUS \"Running python -m venv ${CMAKE_INSTALL_PREFIX}/libexec\")
execute_process(COMMAND \"${PYTHON_EXECUTABLE}\" -m venv \"${CMAKE_INSTALL_PREFIX}/libexec\")
install(CODE "
set(PIP_INSTALL
\"${PYTHON_EXECUTABLE}\"
\"-m\"
\"pip\"
\"install\"
)
if (DEFINED ENV{DESTDIR})
list(APPEND PIP_INSTALL \"--target=\$ENV{DESTDIR}\")
endif()
list(APPEND PIP_INSTALL \".\")
message(STATUS \"Running ${CMAKE_INSTALL_PREFIX}/libexec/bin/python -m pip install -U pip\")
execute_process(COMMAND \"${CMAKE_INSTALL_PREFIX}/libexec/bin/python\" -m pip install -U pip)
execute_process(COMMAND \${PIP_INSTALL}
WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}/python\")
")
message(STATUS \"Running ${CMAKE_INSTALL_PREFIX}/libexec/bin/python -m pip install -U pygments setuptools\")
execute_process(COMMAND \"${CMAKE_INSTALL_PREFIX}/libexec/bin/python\" -m pip install pygments setuptools)
message(STATUS \"Running ${CMAKE_INSTALL_PREFIX}/libexec/bin/python -m pip install .\")
execute_process(COMMAND \"${CMAKE_INSTALL_PREFIX}/libexec/bin/python\" -m pip install .
WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}/python\")
message(STATUS \"Running ${PYTHON_VENV_EXECUTABLE} -m pip install .\")
execute_process(COMMAND \"${PYTHON_VENV_EXECUTABLE}\" -m pip install .
WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}/python\")
")
set(PYTHON_VENV_EXECUTABLE "${CMAKE_INSTALL_PREFIX}/libexec/bin/python")
else()
# This is used by the homebrew formula.
if (USE_PYTHON_VIRTUALENV)
set(PYTHON_VENV_EXECUTABLE "${USE_PYTHON_VIRTUALENV}/bin/python")
else()
message(FATAL_ERROR "Please specify -DUSE_PYTHON_VIRTUALENV=<path> when using -DINSTALL_PYTHON_VIRTUALENV=OFF")
endif()
endif()

# python web resources
install(DIRECTORY python/ikos/view DESTINATION share/ikos)
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-config.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-config: get configuration information
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.settings
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-report.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-report: generate an analysis report from a result database
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.report
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-scan-c++.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-scan-c++: wrapper around clang++ for ikos-scan
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.scan
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-scan-cc.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-scan-cc: wrapper around clang for ikos-scan
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.scan
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-scan-extract.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-scan-extract: extract the llvm bitcode generated for a given file
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.scan
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-scan.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-scan: tool to analyze a whole project
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.scan
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos-view.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos-view: web server to display an analysis report
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.view
except ImportError:
Expand Down
12 changes: 1 addition & 11 deletions analyzer/script/ikos.py.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!@PYTHON_EXECUTABLE@
#!@PYTHON_VENV_EXECUTABLE@
###############################################################################
#
# ikos: static analyzer for C/C++ programs
Expand Down Expand Up @@ -44,16 +44,6 @@ import os.path
import sys

if __name__ == '__main__':
# Add ../lib/pythonX.Y/site-packages at the beginning of the python path
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
site_pkg_dir = os.path.join(
root_dir,
'lib',
'python%d.%d' % (sys.version_info.major, sys.version_info.minor),
'site-packages',
)
sys.path.insert(1, site_pkg_dir)

try:
import ikos.analyzer
except ImportError:
Expand Down

0 comments on commit ad754ac

Please sign in to comment.