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.
Austin S. Hemmelgarn committed
Nov 7, 2024 at 13:02 UTC
56a21a1c88a8dd4880a2dac68105d7e59faac205
2 files changed
+29
CMakeLists.txt
+5
@@ -3128,6 +3128,11 @@ if(ENABLE_PLUGIN_PYTHON)
3128
COMPONENT plugin-pythond
3129
DESTINATION usr/libexec/netdata/python.d)
3130
3131
+ if(OS_WINDOWS)
3132
+ include(NetdataUtil)
3133
+ precompile_python(usr/libexec/netdata/python.d plugin-pythond)
3134
+ endif()
3135
+
3136
install(FILES src/collectors/python.d.plugin/python.d.conf
3137
COMPONENT plugin-pythond
3138
DESTINATION usr/lib/netdata/conf.d)
packaging/cmake/Modules/NetdataUtil.cmake
+24
@@ -195,3 +195,27 @@ function(subdirlist result curdir)
195
196
set(${result} ${dirlist} PARENT_SCOPE)
197
endfunction()
198
+
199
+# Precompile python code in the specified directory relative to the
200
+# CMake install prefix at install time.
201
+# This must be called _after_ the install directive for the python code
202
+# in the specified directory
203
+function(precompile_python dir component)
204
+ find_package(Python3)
205
+
206
+ if(NOT ${Python3_Interpreter_FOUND})
207
+ message(STATUS "Could not find Python3, skipping precompilation of Python code.")
208
+ return()
209
+ endif()
210
+
211
+ set(prefix [=[${CMAKE_INSTALL_PREFIX}]=])
212
+
213
+ install(
214
+ CODE "message(STATUS \"Precompiling Python3 code in ${prefix}/${dir}\")"
215
+ COMPONENT ${component}
216
+ )
217
+ install(
218
+ CODE "execute_process(COMMAND ${Python3_Interpreter} -O -m compileall -j0 -o2 ${prefix}/${dir} WORKING_DIRECTORY ${prefix}/${dir})"
219
+ COMPONENT ${component}
220
+ )
221
+endfunction()