Skip to content

Commit

Permalink
Fix compile.links for vala
Browse files Browse the repository at this point in the history
Fixes issue #12959
compiler.links command for vala crashes
  • Loading branch information
bcorby committed Apr 23, 2024
1 parent 9e3b3db commit 2938d9c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
40 changes: 40 additions & 0 deletions mesonbuild/compilers/vala.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import typing as T

from .. import mlog
from .. import mesonlib
from ..mesonlib import EnvironmentException, version_compare, LibType, OptionKey
from .compilers import CompileCheckMode, Compiler
from ..arglist import CompilerArgs

if T.TYPE_CHECKING:
from ..coredata import KeyedOptionDictType
from ..envconfig import MachineInfo
from ..environment import Environment
from ..mesonlib import MachineChoice
from ..dependencies import Dependency

class ValaCompiler(Compiler):

Expand All @@ -25,6 +29,7 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic
super().__init__([], exelist, version, for_machine, info, is_cross=is_cross)
self.version = version
self.base_options = {OptionKey('b_colorout')}
self.force_link = False

def needs_static_linker(self) -> bool:
return False # Because compiles into C.
Expand All @@ -41,6 +46,20 @@ def get_output_args(self, outputname: str) -> T.List[str]:
def get_compile_only_args(self) -> T.List[str]:
return [] # Because compiles into C.

def get_compiler_args_for_mode(self, mode: CompileCheckMode) -> T.List[str]:
args: T.List[str] = []
if mode is CompileCheckMode.LINK and self.force_link:
return args
args += self.get_always_args()
if mode is CompileCheckMode.COMPILE:
args += self.get_compile_only_args()
elif mode is CompileCheckMode.PREPROCESS:
args += self.get_preprocess_only_args()
return args

def get_preprocess_only_args(self) -> T.List[str]:
return []

def get_pic_args(self) -> T.List[str]:
return []

Expand Down Expand Up @@ -119,3 +138,24 @@ def thread_flags(self, env: 'Environment') -> T.List[str]:

def thread_link_flags(self, env: 'Environment') -> T.List[str]:
return []

def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
return []

def links(self, code: 'mesonlib.FileOrString', env: 'Environment', *,
compiler: T.Optional['Compiler'] = None,
extra_args: T.Union[None, T.List[str], CompilerArgs, T.Callable[[CompileCheckMode], T.List[str]]] = None,
dependencies: T.Optional[T.List['Dependency']] = None,
disable_cache: bool = False) -> T.Tuple[bool, bool]:
self.force_link = True
if compiler:
with compiler._build_wrapper(code, env, dependencies=dependencies, want_output=True) as r:
objfile = mesonlib.File.from_absolute_file(r.output_name)
result = self.compiles(objfile, env, extra_args=extra_args,
dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=True)
self.force_link = False
return result
result = self.compiles(code, env, extra_args=extra_args,
dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=disable_cache)
self.force_link = False
return result
24 changes: 24 additions & 0 deletions test cases/vala/29 compiler.links/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
project('link-test', ['c', 'vala'], version: '0.1')

valac = meson.get_compiler('vala')

code = '''void main() {
const double PI3 = 1.047197551196597746154214461093167628;
var a = GLib.Math.cos (PI3);
stdout.printf ("%f\n", a); }'''

# test 1; code should link
code_links = valac.links(
code,
args: '--Xcc=-lm',
name: 'links with math library? == YES',
)
assert (code_links, 'Math library should link successfully.')

# test 2; code should not link
code_links = valac.links(
code,
args: '--Xcc=-lfake_library_90DFE450330A',
name: 'links with fake library? == NO',
)
assert (not code_links, 'Fake library should not link successfully.')

0 comments on commit 2938d9c

Please sign in to comment.