| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
"""Provides a mixin for shared code between C and C++ Emscripten compilers.""" |
| 7 |
|
| 8 |
import os.path |
| 9 |
import typing as T |
| 10 |
|
| 11 |
from ... import options |
| 12 |
from ... import mesonlib |
| 13 |
from ...options import OptionKey |
| 14 |
from ...mesonlib import LibType |
| 15 |
from mesonbuild.compilers.compilers import CompileCheckMode |
| 16 |
|
| 17 |
if T.TYPE_CHECKING: |
| 18 |
from ...compilers.compilers import Compiler |
| 19 |
from ...dependencies import Dependency |
| 20 |
else: |
| 21 |
# This is a bit clever, for mypy we pretend that these mixins descend from |
| 22 |
# Compiler, so we get all of the methods and attributes defined for us, but |
| 23 |
# for runtime we make them descend from object (which all classes normally |
| 24 |
# do). This gives up DRYer type checking, with no runtime impact |
| 25 |
Compiler = object |
| 26 |
|
| 27 |
|
| 28 |
def wrap_js_includes(args: T.List[str]) -> T.List[str]: |
| 29 |
final_args: T.List[str] = [] |
| 30 |
for i in args: |
| 31 |
if i.endswith('.js') and not i.startswith('-'): |
| 32 |
final_args += ['--js-library', i] |
| 33 |
else: |
| 34 |
final_args += [i] |
| 35 |
return final_args |
| 36 |
|
| 37 |
class EmscriptenMixin(Compiler): |
| 38 |
|
| 39 |
def _get_compile_output(self, dirname: str, mode: CompileCheckMode) -> str: |
| 40 |
assert mode != CompileCheckMode.PREPROCESS, 'In pre-processor mode, the output is sent to stdout and discarded' |
| 41 |
# Unlike sane toolchains, emcc infers the kind of output from its name. |
| 42 |
# This is the only reason why this method is overridden; compiler tests |
| 43 |
# do not work well with the default exe/obj suffices. |
| 44 |
if mode == CompileCheckMode.LINK: |
| 45 |
suffix = 'js' |
| 46 |
else: |
| 47 |
suffix = 'o' |
| 48 |
return os.path.join(dirname, 'output.' + suffix) |
| 49 |
|
| 50 |
def thread_link_flags(self) -> T.List[str]: |
| 51 |
args = ['-pthread'] |
| 52 |
count = self.environment.coredata.optstore.get_value_for(OptionKey(f'{self.language}_thread_count', machine=self.for_machine)) |
| 53 |
assert isinstance(count, int) |
| 54 |
if count: |
| 55 |
args.append(f'-sPTHREAD_POOL_SIZE={count}') |
| 56 |
return args |
| 57 |
|
| 58 |
def get_options(self) -> options.MutableKeyedOptionDictType: |
| 59 |
opts = super().get_options() |
| 60 |
|
| 61 |
key = OptionKey(f'{self.language}_thread_count', machine=self.for_machine) |
| 62 |
opts[key] = options.UserIntegerOption( |
| 63 |
self.make_option_name(key), |
| 64 |
'Number of threads to use in web assembly, set to 0 to disable', |
| 65 |
4, # Default was picked at random |
| 66 |
min_value=0) |
| 67 |
|
| 68 |
return opts |
| 69 |
|
| 70 |
@classmethod |
| 71 |
def native_args_to_unix(cls, args: T.List[str]) -> T.List[str]: |
| 72 |
return wrap_js_includes(super().native_args_to_unix(args)) |
| 73 |
|
| 74 |
def get_dependency_link_args(self, dep: 'Dependency') -> T.List[str]: |
| 75 |
return wrap_js_includes(super().get_dependency_link_args(dep)) |
| 76 |
|
| 77 |
def find_library(self, libname: str, extra_dirs: T.List[str], libtype: LibType = LibType.PREFER_SHARED, |
| 78 |
lib_prefix_warning: bool = True, ignore_system_dirs: bool = False, |
| 79 |
skip_link_check: bool = False) -> T.Optional[T.List[str]]: |
| 80 |
if not libname.endswith('.js'): |
| 81 |
return super().find_library(libname, extra_dirs, libtype, lib_prefix_warning, ignore_system_dirs, skip_link_check) |
| 82 |
if os.path.isabs(libname): |
| 83 |
if os.path.exists(libname): |
| 84 |
return [libname] |
| 85 |
if len(extra_dirs) == 0: |
| 86 |
raise mesonlib.EnvironmentException('Looking up Emscripten JS libraries requires either an absolute path or specifying extra_dirs.') |
| 87 |
for d in extra_dirs: |
| 88 |
abs_path = os.path.join(d, libname) |
| 89 |
if os.path.exists(abs_path): |
| 90 |
return [abs_path] |
| 91 |
return None |