1313# limitations under the License.
1414from __future__ import annotations
1515
16- import copy
17- import os
18- import shutil
16+ import copy , json , os , shutil
1917import typing as T
2018
2119from . import ExtensionModule , ModuleInfo
4139 from typing_extensions import TypedDict
4240
4341 from . import ModuleState
44- from ..build import SharedModule , Data
42+ from ..build import Build , SharedModule , Data
4543 from ..dependencies import Dependency
4644 from ..interpreter import Interpreter
4745 from ..interpreter .kwargs import ExtractRequired
@@ -66,6 +64,12 @@ class FindInstallationKw(ExtractRequired):
6664
6765
6866class PythonExternalProgram (BasicPythonExternalProgram ):
67+
68+ # This is a ClassVar instead of an instance bool, because although an
69+ # installation is cached, we actually copy it, modify attributes such as pure,
70+ # and return a temporary one rather than the cached object.
71+ run_bytecompile : T .ClassVar [T .Dict [str , bool ]] = {}
72+
6973 def sanity (self , state : T .Optional ['ModuleState' ] = None ) -> bool :
7074 ret = super ().sanity ()
7175 if ret :
@@ -216,6 +220,7 @@ def dependency_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') ->
216220 )
217221 def install_sources_method (self , args : T .Tuple [T .List [T .Union [str , mesonlib .File ]]],
218222 kwargs : 'PyInstallKw' ) -> 'Data' :
223+ self .held_object .run_bytecompile [self .version ] = True
219224 tag = kwargs ['install_tag' ] or 'python-runtime'
220225 pure = kwargs ['pure' ] if kwargs ['pure' ] is not None else self .pure
221226 install_dir = self ._get_install_dir_impl (pure , kwargs ['subdir' ])
@@ -229,6 +234,7 @@ def install_sources_method(self, args: T.Tuple[T.List[T.Union[str, mesonlib.File
229234 @noPosargs
230235 @typed_kwargs ('python_installation.install_dir' , _PURE_KW , _SUBDIR_KW )
231236 def get_install_dir_method (self , args : T .List ['TYPE_var' ], kwargs : 'PyInstallKw' ) -> str :
237+ self .held_object .run_bytecompile [self .version ] = True
232238 pure = kwargs ['pure' ] if kwargs ['pure' ] is not None else self .pure
233239 return self ._get_install_dir_impl (pure , kwargs ['subdir' ])
234240
@@ -297,6 +303,56 @@ def __init__(self, interpreter: 'Interpreter') -> None:
297303 'find_installation' : self .find_installation ,
298304 })
299305
306+ def _get_install_scripts (self ) -> T .List [mesonlib .ExecutableSerialisation ]:
307+ backend = self .interpreter .backend
308+ ret = []
309+ optlevel = self .interpreter .environment .coredata .get_option (mesonlib .OptionKey ('bytecompile' , module = 'python' ))
310+ if optlevel == - 1 :
311+ return ret
312+ if not any (PythonExternalProgram .run_bytecompile .values ()):
313+ return ret
314+
315+ installdata = backend .create_install_data ()
316+ py_files = []
317+
318+ def should_append (f , isdir : bool = False ):
319+ # This uses the install_plan decorated names to see if the original source was propagated via
320+ # install_sources() or get_install_dir().
321+ return f .startswith (('{py_platlib}' , '{py_purelib}' )) and (f .endswith ('.py' ) or isdir )
322+
323+ for t in installdata .targets :
324+ if should_append (t .out_name ):
325+ py_files .append (os .path .join (installdata .prefix , t .outdir , os .path .basename (t .fname )))
326+ for d in installdata .data :
327+ if should_append (d .install_path_name ):
328+ py_files .append (os .path .join (installdata .prefix , d .install_path ))
329+ for d in installdata .install_subdirs :
330+ if should_append (d .install_path_name , True ):
331+ py_files .append (os .path .join (installdata .prefix , d .install_path ))
332+
333+ import importlib .resources
334+ pycompile = os .path .join (self .interpreter .environment .get_scratch_dir (), 'pycompile.py' )
335+ with open (pycompile , 'wb' ) as f :
336+ f .write (importlib .resources .read_binary ('mesonbuild.scripts' , 'pycompile.py' ))
337+
338+ for i in self .installations .values ():
339+ if isinstance (i , PythonExternalProgram ) and i .run_bytecompile [i .info ['version' ]]:
340+ i = T .cast (PythonExternalProgram , i )
341+ manifest = f'python-{ i .info ["version" ]} -installed.json'
342+ manifest_json = []
343+ for f in py_files :
344+ if f .startswith ((os .path .join (installdata .prefix , i .platlib ), os .path .join (installdata .prefix , i .purelib ))):
345+ manifest_json .append (f )
346+ with open (os .path .join (self .interpreter .environment .get_scratch_dir (), manifest ), 'w' , encoding = 'utf-8' ) as f :
347+ json .dump (manifest_json , f )
348+ cmd = i .command + [pycompile , manifest , str (optlevel )]
349+ script = backend .get_executable_serialisation (cmd , verbose = True )
350+ ret .append (script )
351+ return ret
352+
353+ def postconf_hook (self , b : Build ) -> None :
354+ b .install_scripts .extend (self ._get_install_scripts ())
355+
300356 # https://www.python.org/dev/peps/pep-0397/
301357 @staticmethod
302358 def _get_win_pythonpath (name_or_path : str ) -> T .Optional [str ]:
@@ -421,6 +477,7 @@ def find_installation(self, state: 'ModuleState', args: T.Tuple[T.Optional[str]]
421477 else :
422478 python = copy .copy (python )
423479 python .pure = kwargs ['pure' ]
480+ python .run_bytecompile .setdefault (python .info ['version' ], False )
424481 return python
425482
426483 raise mesonlib .MesonBugException ('Unreachable code was reached (PythonModule.find_installation).' )
0 commit comments