From 56a21a1c88a8dd4880a2dac68105d7e59faac205 Mon Sep 17 00:00:00 2001 From: "Austin S. Hemmelgarn" Date: Thu, 7 Nov 2024 13:02:32 -0500 Subject: [PATCH] Precompile Python code on Windows. (#18951) This ensures that the compiled Python code is included in the installer correctly, which in turn ensures it gets cleaned up correctly on uninstall. --- CMakeLists.txt | 5 +++++ packaging/cmake/Modules/NetdataUtil.cmake | 24 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98ad8a268eb74b..a5d8d959bccafa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/packaging/cmake/Modules/NetdataUtil.cmake b/packaging/cmake/Modules/NetdataUtil.cmake index 4b8565554c6b29..0c1d803ea3e0a4 100644 --- a/packaging/cmake/Modules/NetdataUtil.cmake +++ b/packaging/cmake/Modules/NetdataUtil.cmake @@ -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()