Don't create Python bytecode when building the kernel
Subsystems (4)
v2 cover Don't create Python bytecode when building the kernel
As reported by Andy, the Kernel build system runs kernel-doc script for DRM, when W=1. Due to Python's normal behavior, its JIT compiler will create a bytecode and store it under scripts/lib/*/__pycache__.
As one may be using O= and even having the sources on a read-only mount point, disable its creation during build time.
This only solves half of the issue though, as one may be manually running the script by hand, without asking Python to not store any bytecode. This should be OK, but afterwards, git status will list the __pycache__ as not committed. To prevent that, add *.pyc to .gitignore.
Mauro Carvalho Chehab (2):
scripts/kernel-doc.py: don't create *.pyc files
.gitignore: ignore Python compiled bytecode
.gitignore | 1 + drivers/gpu/drm/Makefile | 2 +- drivers/gpu/drm/i915/Makefile | 2 +- include/drm/Makefile | 2 +- scripts/find-unused-docs.sh | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.49.0
PATCH v2 1/2 scripts/kernel-doc.py: don't create *.pyc files
As reported by Andy, kernel-doc.py is creating a __pycache__ directory at build time.
Disable creation of __pycache__ for the libraries used by kernel-doc.py, when excecuted via the build system or via scripts/find-unused-docs.sh.
--- drivers/gpu/drm/Makefile | 2 +- drivers/gpu/drm/i915/Makefile | 2 +- include/drm/Makefile | 2 +- scripts/find-unused-docs.sh | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/Makefile +1 -1
@@ -236,7 +236,7 @@ always-$(CONFIG_DRM_HEADER_TEST) += \ quiet_cmd_hdrtest = HDRTEST $(patsubst %.hdrtest,%.h,$@) cmd_hdrtest = \ $(CC) $(c_flags) -fsyntax-only -x c /dev/null -include $< -include $<; \ - $(srctree)/scripts/kernel-doc -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ + $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ touch $@ $(obj)/%.hdrtest: $(src)/%.h FORCE
drivers/gpu/drm/i915/Makefile +1 -1
@@ -408,7 +408,7 @@ obj-$(CONFIG_DRM_I915_GVT_KVMGT) += kvmgt.o # # Enable locally for CONFIG_DRM_I915_WERROR=y. See also scripts/Makefile.build ifdef CONFIG_DRM_I915_WERROR - cmd_checkdoc = $(srctree)/scripts/kernel-doc -none -Werror $< + cmd_checkdoc = $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none -Werror $< endif # header test
include/drm/Makefile +1 -1
@@ -11,7 +11,7 @@ always-$(CONFIG_DRM_HEADER_TEST) += \ quiet_cmd_hdrtest = HDRTEST $(patsubst %.hdrtest,%.h,$@) cmd_hdrtest = \ $(CC) $(c_flags) -fsyntax-only -x c /dev/null -include $< -include $<; \ - $(srctree)/scripts/kernel-doc -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ + $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ touch $@ $(obj)/%.hdrtest: $(src)/%.h FORCE
scripts/find-unused-docs.sh +1 -1
@@ -54,7 +54,7 @@ for file in `find $1 -name '*.c'`; do if [[ ${FILES_INCLUDED[$file]+_} ]]; then continue; fi - str=$(scripts/kernel-doc -export "$file" 2>/dev/null) + str=$(PYTHONDONTWRITEBYTECODE=1 scripts/kernel-doc -export "$file" 2>/dev/null) if [[ -n "$str" ]]; then echo "$file" fi
Signature · 1 line
2.49.0
Re: [PATCH v2 1/2] scripts/kernel-doc.py: don't create *.pyc files
Quoted 6 lines · Mauro Carvalho Chehab
As reported by Andy,
kernel-doc.pyis creating a__pycache__directory at build time.Disable creation of
__pycache__for the libraries used bykernel-doc.py, when excecuted via the build system or viascripts/find-unused-docs.sh.
Nope, still have it.
I used today's Linux Next with some local code patches (unrelated to any scripts or kernel doc or python).
To confirm, I even done again a clean build.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 1/2] scripts/kernel-doc.py: don't create *.pyc files
Em Wed, 16 Apr 2025 10:22:24 +0300
Andy Shevchenko <[email protected]> escreveu:
Quoted 8 lines · Mauro Carvalho Chehab
Quoted 6 lines
As reported by Andy,
kernel-doc.pyis creating a__pycache__directory at build time.Disable creation of
__pycache__for the libraries used bykernel-doc.py, when excecuted via the build system or viascripts/find-unused-docs.sh.
Quoted 7 lines
Nope, still have it.
I used today's Linux Next with some local code patches (unrelated to any scripts or kernel doc or python).
To confirm, I even done again a clean build.
There were one missing place.
Sent a v3. Please test.
Regards,
Mauro
Jani Nikula
maintainerRe: [PATCH v2 1/2] scripts/kernel-doc.py: don't create *.pyc files
In reply to Mauro Carvalho ChehabQuoted 22 lines · Mauro Carvalho Chehab
As reported by Andy,
kernel-doc.pyis creating a__pycache__directory at build time.Disable creation of
__pycache__for the libraries used bykernel-doc.py, when excecuted via the build system or viascripts/find-unused-docs.sh.Reported-by Andy Shevchenko <[email protected]>Signed-off-by Mauro Carvalho Chehab <[email protected]>--- drivers/gpu/drm/Makefile | 2 +- drivers/gpu/drm/i915/Makefile | 2 +- include/drm/Makefile | 2 +- scripts/find-unused-docs.sh | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)drivers/gpu/drm/Makefile +0
-0
drivers/gpu/drm/Makefile always-$(CONFIG_DRM_HEADER_TEST) += \ · line 239
Quoted 5 lines
drivers/gpu/drm/Makefile +1
-1@@ -236,7 +236,7 @@ always-$(CONFIG_DRM_HEADER_TEST) += \ quiet_cmd_hdrtest = HDRTEST $(patsubst %.hdrtest,%.h,$@) cmd_hdrtest = \ $(CC) $(c_flags) -fsyntax-only -x c /dev/null -include $< -include $<; \ - $(srctree)/scripts/kernel-doc -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ + $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \
It doesn't work to put PYTHONDONTWRITEBYTECODE=1 as a parameter to kernel-doc...
BR,
Jani.
Quoted 42 lines
drivers/gpu/drm/Makefile +0
-0touch $@ $(obj)/%.hdrtest: $(src)/%.h FORCEdrivers/gpu/drm/i915/Makefile +1
-1@@ -408,7 +408,7 @@ obj-$(CONFIG_DRM_I915_GVT_KVMGT) += kvmgt.o # # Enable locally for CONFIG_DRM_I915_WERROR=y. See also scripts/Makefile.build ifdef CONFIG_DRM_I915_WERROR - cmd_checkdoc = $(srctree)/scripts/kernel-doc -none -Werror $< + cmd_checkdoc = $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none -Werror $< endif # header testinclude/drm/Makefile +1
-1@@ -11,7 +11,7 @@ always-$(CONFIG_DRM_HEADER_TEST) += \ quiet_cmd_hdrtest = HDRTEST $(patsubst %.hdrtest,%.h,$@) cmd_hdrtest = \ $(CC) $(c_flags) -fsyntax-only -x c /dev/null -include $< -include $<; \ - $(srctree)/scripts/kernel-doc -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ + $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ touch $@ $(obj)/%.hdrtest: $(src)/%.h FORCEscripts/find-unused-docs.sh +1
-1@@ -54,7 +54,7 @@ for file in `find $1 -name '*.c'`; do if [[ ${FILES_INCLUDED[$file]+_} ]]; then continue; fi - str=$(scripts/kernel-doc -export "$file" 2>/dev/null) + str=$(PYTHONDONTWRITEBYTECODE=1 scripts/kernel-doc -export "$file" 2>/dev/null) if [[ -n "$str" ]]; then echo "$file" fi
Signature · 1 line
Jani Nikula, Intel
Jani Nikula
maintainerRe: [PATCH v2 1/2] scripts/kernel-doc.py: don't create *.pyc files
Quoted 18 lines · Jani Nikula
Quoted 17 lines · Mauro Carvalho Chehab
As reported by Andy,
kernel-doc.pyis creating a__pycache__directory at build time.Disable creation of
__pycache__for the libraries used bykernel-doc.py, when excecuted via the build system or viascripts/find-unused-docs.sh.Reported-by Andy Shevchenko <[email protected]>Signed-off-by Mauro Carvalho Chehab <[email protected]>--- drivers/gpu/drm/Makefile | 2 +- drivers/gpu/drm/i915/Makefile | 2 +- include/drm/Makefile | 2 +- scripts/find-unused-docs.sh | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines
drivers/gpu/drm/Makefile always-$(CONFIG_DRM_HEADER_TEST) += \ · line 239
Quoted 10 lines
drivers/gpu/drm/Makefile +1
-1@@ -236,7 +236,7 @@ always-$(CONFIG_DRM_HEADER_TEST) += \ quiet_cmd_hdrtest = HDRTEST $(patsubst %.hdrtest,%.h,$@) cmd_hdrtest = \ $(CC) $(c_flags) -fsyntax-only -x c /dev/null -include $< -include $<; \ - $(srctree)/scripts/kernel-doc -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ + $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \It doesn't work to put PYTHONDONTWRITEBYTECODE=1 as a parameter to kernel-doc...
Moreover, KERNELDOC is only defined in Documentation/Makefile. It's empty here.
Also scripts/Makefile.build uses kernel-doc, which is probably the one creating __pycache__ Andy sees.
Quoted 47 lines
BR,
Jani.Quoted 42 lines
drivers/gpu/drm/Makefile +0
-0touch $@ $(obj)/%.hdrtest: $(src)/%.h FORCEdrivers/gpu/drm/i915/Makefile +1
-1@@ -408,7 +408,7 @@ obj-$(CONFIG_DRM_I915_GVT_KVMGT) += kvmgt.o # # Enable locally for CONFIG_DRM_I915_WERROR=y. See also scripts/Makefile.build ifdef CONFIG_DRM_I915_WERROR - cmd_checkdoc = $(srctree)/scripts/kernel-doc -none -Werror $< + cmd_checkdoc = $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none -Werror $< endif # header testinclude/drm/Makefile +1
-1@@ -11,7 +11,7 @@ always-$(CONFIG_DRM_HEADER_TEST) += \ quiet_cmd_hdrtest = HDRTEST $(patsubst %.hdrtest,%.h,$@) cmd_hdrtest = \ $(CC) $(c_flags) -fsyntax-only -x c /dev/null -include $< -include $<; \ - $(srctree)/scripts/kernel-doc -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ + $(KERNELDOC) PYTHONDONTWRITEBYTECODE=1 -none $(if $(CONFIG_WERROR)$(CONFIG_DRM_WERROR),-Werror) $<; \ touch $@ $(obj)/%.hdrtest: $(src)/%.h FORCEscripts/find-unused-docs.sh +1
-1@@ -54,7 +54,7 @@ for file in `find $1 -name '*.c'`; do if [[ ${FILES_INCLUDED[$file]+_} ]]; then continue; fi - str=$(scripts/kernel-doc -export "$file" 2>/dev/null) + str=$(PYTHONDONTWRITEBYTECODE=1 scripts/kernel-doc -export "$file" 2>/dev/null) if [[ -n "$str" ]]; then echo "$file" fi
Signature · 1 line
Jani Nikula, Intel
PATCH v2 2/2 .gitignore: ignore Python compiled bytecode
In reply to Mauro Carvalho ChehabWhile the building system doesn't create any Python JIT bytecode, if one manually runs kernel-doc.py or get_abi.py, Python will, by default, create a bytecode and store it under scripts/lib/*.
This is normal, and not controlled by the Kernel itself. So, add *.pyc as an extension to be ignored.
--- .gitignore | 1 + 1 file changed, 1 insertion(+)
.gitignore +1 -0
@@ -39,6 +39,7 @@ *.mod.c *.o *.o.* +*.pyc *.patch *.rmeta *.rpm
Signature · 1 line
2.49.0
Re: [PATCH v2 2/2] .gitignore: ignore Python compiled bytecode
Quoted 6 lines · Mauro Carvalho Chehab
While the building system doesn't create any Python JIT bytecode, if one manually runs
kernel-doc.pyorget_abi.py, Python will, by default, create a bytecode and store it underscripts/lib/*.This is normal, and not controlled by the Kernel itself. So, add *.pyc as an extension to be ignored.
...
Quoted 4 lines
*.mod.c
*.o
*.o.*
+*.pyc
Shouldn't we prevent the order? (This should go after the "patch")
Quoted 3 lines
*.patch
*.rmeta
*.rpm
...
P.S. I'm going to test the first patch (at least) soon.
Signature · 2 lines
With Best Regards, Andy Shevchenko