meson: utils: Provide environment for Python scripts

Python scripts run as part of the build process need to take a few
actions specific to the environment in which they operate. One of those
is disabling the Python bytecode cache, to avoid writing .pyc files to
the source tree. This is done manually in the IPC generate.py and
parser.py scripts.

The current implementation is not ideal because it hardcodes in the
scripts information related to the environment in which they operate. As
those scripts are part of libcamera this is more of a theoretical issue
than a practical one. A second issue is that future Python scripts will
need to duplicate similar mechanisms, resulting in a higher maintenance
burden.

Address the issue with a different approach, by creating a meson
environment for the Python scripts, and passing it to the
custom_target() functions. The environment only disables the bytecode
cache for now.

The diffstat shows an increase in code size. This is expected to be
offset by usage of the environment for more Python scripts, as well as
support of more variables in the environment.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2024-08-08 17:30:35 +03:00
parent 50c92cc7e2
commit a5f12d2eb3
8 changed files with 30 additions and 19 deletions

View file

@ -26,7 +26,8 @@ ipa_mojom_core = custom_target(core_mojom_file.split('.')[0] + '_mojom_module',
'--output-root', meson.project_build_root(), '--output-root', meson.project_build_root(),
'--input-root', meson.project_source_root(), '--input-root', meson.project_source_root(),
'--mojoms', '@INPUT@' '--mojoms', '@INPUT@'
]) ],
env : py_build_env)
# core_ipa_interface.h # core_ipa_interface.h
libcamera_ipa_headers += custom_target('core_ipa_interface_h', libcamera_ipa_headers += custom_target('core_ipa_interface_h',
@ -42,7 +43,8 @@ libcamera_ipa_headers += custom_target('core_ipa_interface_h',
'--libcamera_generate_core_header', '--libcamera_generate_core_header',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)
# core_ipa_serializer.h # core_ipa_serializer.h
libcamera_ipa_headers += custom_target('core_ipa_serializer_h', libcamera_ipa_headers += custom_target('core_ipa_serializer_h',
@ -56,7 +58,8 @@ libcamera_ipa_headers += custom_target('core_ipa_serializer_h',
'--libcamera_generate_core_serializer', '--libcamera_generate_core_serializer',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)
# Mapping from pipeline handler name to mojom file # Mapping from pipeline handler name to mojom file
pipeline_ipa_mojom_mapping = { pipeline_ipa_mojom_mapping = {
@ -99,7 +102,8 @@ foreach pipeline, file : pipeline_ipa_mojom_mapping
'--output-root', meson.project_build_root(), '--output-root', meson.project_build_root(),
'--input-root', meson.project_source_root(), '--input-root', meson.project_source_root(),
'--mojoms', '@INPUT@' '--mojoms', '@INPUT@'
]) ],
env : py_build_env)
# {interface}_ipa_interface.h # {interface}_ipa_interface.h
header = custom_target(name + '_ipa_interface_h', header = custom_target(name + '_ipa_interface_h',
@ -115,7 +119,8 @@ foreach pipeline, file : pipeline_ipa_mojom_mapping
'--libcamera_generate_header', '--libcamera_generate_header',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)
# {interface}_ipa_serializer.h # {interface}_ipa_serializer.h
serializer = custom_target(name + '_ipa_serializer_h', serializer = custom_target(name + '_ipa_serializer_h',
@ -129,7 +134,8 @@ foreach pipeline, file : pipeline_ipa_mojom_mapping
'--libcamera_generate_serializer', '--libcamera_generate_serializer',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)
# {interface}_ipa_proxy.h # {interface}_ipa_proxy.h
proxy_header = custom_target(name + '_proxy_h', proxy_header = custom_target(name + '_proxy_h',
@ -143,7 +149,8 @@ foreach pipeline, file : pipeline_ipa_mojom_mapping
'--libcamera_generate_proxy_h', '--libcamera_generate_proxy_h',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)
ipa_mojoms += { ipa_mojoms += {
'name': name, 'name': name,

View file

@ -13,7 +13,8 @@ foreach mojom : ipa_mojoms
'--libcamera_generate_proxy_cpp', '--libcamera_generate_proxy_cpp',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' + '@INPUT@' './' + '@INPUT@'
]) ],
env : py_build_env)
libcamera_internal_sources += proxy libcamera_internal_sources += proxy
endforeach endforeach

View file

@ -15,7 +15,8 @@ foreach mojom : ipa_mojoms
'--libcamera_generate_proxy_worker', '--libcamera_generate_proxy_worker',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' + '@INPUT@' './' + '@INPUT@'
]) ],
env : py_build_env)
proxy = executable(mojom['name'] + '_ipa_proxy', worker, proxy = executable(mojom['name'] + '_ipa_proxy', worker,
install : true, install : true,

View file

@ -9,7 +9,8 @@ mojom = custom_target('test_mojom_module',
'--output-root', meson.project_build_root(), '--output-root', meson.project_build_root(),
'--input-root', meson.project_source_root(), '--input-root', meson.project_source_root(),
'--mojoms', '@INPUT@' '--mojoms', '@INPUT@'
]) ],
env : py_build_env)
# test_ipa_interface.h # test_ipa_interface.h
generated_test_header = custom_target('test_ipa_interface_h', generated_test_header = custom_target('test_ipa_interface_h',
@ -23,7 +24,8 @@ generated_test_header = custom_target('test_ipa_interface_h',
'--libcamera_generate_header', '--libcamera_generate_header',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)
# test_ipa_serializer.h # test_ipa_serializer.h
generated_test_serializer = custom_target('test_ipa_serializer_h', generated_test_serializer = custom_target('test_ipa_serializer_h',
@ -37,4 +39,5 @@ generated_test_serializer = custom_target('test_ipa_serializer_h',
'--libcamera_generate_serializer', '--libcamera_generate_serializer',
'--libcamera_output_path=@OUTPUT@', '--libcamera_output_path=@OUTPUT@',
'./' +'@INPUT@' './' +'@INPUT@'
]) ],
env : py_build_env)

View file

@ -9,9 +9,6 @@
import os import os
import sys import sys
# TODO set sys.pycache_prefix for >= python3.8
sys.dont_write_bytecode = True
sys.path.insert(0, f'{os.path.dirname(__file__)}/mojo/public/tools/bindings') sys.path.insert(0, f'{os.path.dirname(__file__)}/mojo/public/tools/bindings')
import mojo.public.tools.bindings.mojom_bindings_generator as generator import mojo.public.tools.bindings.mojom_bindings_generator as generator

View file

@ -13,6 +13,7 @@ mojom_docs_extractor = find_program('./extract-docs.py')
mojom_templates = custom_target('mojom_templates', mojom_templates = custom_target('mojom_templates',
input : mojom_template_files, input : mojom_template_files,
output : 'libcamera_templates.zip', output : 'libcamera_templates.zip',
command : [mojom_generator, '-o', '@OUTDIR@', 'precompile']) command : [mojom_generator, '-o', '@OUTDIR@', 'precompile'],
env : py_build_env)
mojom_templates_dir = meson.current_build_dir() mojom_templates_dir = meson.current_build_dir()

View file

@ -9,9 +9,6 @@
import os import os
import sys import sys
# TODO set sys.pycache_prefix for >= python3.8
sys.dont_write_bytecode = True
# Make sure that mojom_parser.py can import mojom # Make sure that mojom_parser.py can import mojom
sys.path.insert(0, f'{os.path.dirname(__file__)}/mojo/public/tools/mojom') sys.path.insert(0, f'{os.path.dirname(__file__)}/mojo/public/tools/mojom')

View file

@ -2,6 +2,10 @@
## Code generation ## Code generation
py_build_env = environment()
# \todo Investigate usage of PYTHONPYCACHEPREFIX for Python >= 3.8
py_build_env.set('PYTHONDONTWRITEBYTECODE', '1')
py_modules += ['jinja2', 'yaml'] py_modules += ['jinja2', 'yaml']
gen_controls = files('gen-controls.py') gen_controls = files('gen-controls.py')