Skip to content

Commit

Permalink
Precompile Python code on Windows. (netdata#18951)
Browse files Browse the repository at this point in the history
This ensures that the compiled Python code is included in the installer
correctly, which in turn ensures it gets cleaned up correctly on
uninstall.
  • Loading branch information
Ferroin authored Nov 7, 2024
1 parent b7924fd commit 56a21a1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,11 @@ if(ENABLE_PLUGIN_PYTHON)
COMPONENT plugin-pythond
DESTINATION usr/libexec/netdata/python.d)

if(OS_WINDOWS)
include(NetdataUtil)
precompile_python(usr/libexec/netdata/python.d plugin-pythond)
endif()

install(FILES src/collectors/python.d.plugin/python.d.conf
COMPONENT plugin-pythond
DESTINATION usr/lib/netdata/conf.d)
Expand Down
24 changes: 24 additions & 0 deletions packaging/cmake/Modules/NetdataUtil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,27 @@ function(subdirlist result curdir)

set(${result} ${dirlist} PARENT_SCOPE)
endfunction()

# Precompile python code in the specified directory relative to the
# CMake install prefix at install time.
# This must be called _after_ the install directive for the python code
# in the specified directory
function(precompile_python dir component)
find_package(Python3)

if(NOT ${Python3_Interpreter_FOUND})
message(STATUS "Could not find Python3, skipping precompilation of Python code.")
return()
endif()

set(prefix [=[${CMAKE_INSTALL_PREFIX}]=])

install(
CODE "message(STATUS \"Precompiling Python3 code in ${prefix}/${dir}\")"
COMPONENT ${component}
)
install(
CODE "execute_process(COMMAND ${Python3_Interpreter} -O -m compileall -j0 -o2 ${prefix}/${dir} WORKING_DIRECTORY ${prefix}/${dir})"
COMPONENT ${component}
)
endfunction()

0 comments on commit 56a21a1

Please sign in to comment.