From 5695dc0f1674211049b3952f8005cccc78411cdd Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Sun, 15 Dec 2019 07:40:43 -0500 Subject: add compiler.get_linker_id() method this can be useful for if/elif where linker behaviors must be considered. For example, clang with "link" vs gcc with "ld.bfd" etc. ci for compiler.get_linker_id() method doc add @FeatureNew check Co-Authored-By: Daniel Mensinger --- docs/markdown/Compiler-properties.md | 47 ++++++++++++++-------------- docs/markdown/Reference-manual.md | 4 ++- docs/markdown/Reference-tables.md | 19 ++++++++--- docs/markdown/snippets/get_linker_id.md | 5 +++ mesonbuild/compilers/compilers.py | 3 ++ mesonbuild/interpreter.py | 7 +++++ test cases/common/31 compiler id/meson.build | 18 ++++++++--- 7 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 docs/markdown/snippets/get_linker_id.md diff --git a/docs/markdown/Compiler-properties.md b/docs/markdown/Compiler-properties.md index 4f5ebdb..5d29dd1 100644 --- a/docs/markdown/Compiler-properties.md +++ b/docs/markdown/Compiler-properties.md @@ -25,13 +25,16 @@ know the operating system your code will run on, issue this command: host_machine.system() ``` -Compiler id -== +## Compiler id -The compiler object has a method called `get_id`, which returns a -lower case string describing the "family" of the compiler. +The compiler object method `get_id` returns a +lower case string describing the "family" of the compiler. Since 0.53.0 +`get_linker_id` returns a lower case string with the linker name. Since +compilers can often choose from multiple linkers depending on operating +system, `get_linker_id` can be useful for handling or mitigating effects +of particular linkers. -The compiler object also has a method `get_argument_syntax` which +The compiler object also has a method `get_argument_syntax` which returns a lower case string of `gcc`, `msvc`, or another undefined string value; identifying whether the compiler arguments use the same syntax as either `gcc` or `msvc`, or that its arguments are not like either. This should @@ -41,11 +44,14 @@ with `has_argument`. See [reference tables](Reference-tables.md#compiler-ids) for a list of supported compiler ids and their argument type. -Does code compile? -== +## Does code compile? Sometimes the only way to test the system is to try to compile some -sample code and see if it works. This is a two-phase operation. First +sample code and see if it works. For example, this can test that a +"C++17" compiler actually supports a particular C++17 feature, +without resorting to maintaining a feature list vs. compiler vendor, +compiler version and operating system. +Testing that a code snippet runs is a two-phase operation. First we define some code using the multiline string operator: ```meson @@ -65,8 +71,7 @@ depending on whether the compilation succeeded or not. The keyword argument `name` is optional. If it is specified, Meson will write the result of the check to its log. -Does code compile and link? -== +## Does code compile and link? Sometimes it is necessary to check whether a certain code fragment not only compiles, but also links successfully, e.g. to check if a symbol @@ -90,11 +95,11 @@ depending on whether the compilation and linking succeeded or not. The keyword argument `name` is optional. If it is specified, Meson will write the result of the check to its log. - -Compile and run test application -== +## Compile and run test application Here is how you would compile and run a small test application. +Testing if a code snippets **runs** versus merely that it links +is particularly important for some dependencies such as MPI. ```meson code = '''#include @@ -126,9 +131,7 @@ if result.stdout().strip() == 'some_value' endif ``` - -Does a header exist? -== +## Does a header exist? Header files provided by different platforms vary quite a lot. Meson has functionality to detect whether a given header file is available @@ -142,8 +145,7 @@ if compiler.has_header('sys/fstat.h') endif ``` -Expression size -== +## Expression size Often you need to determine the size of a particular element (such as `int`, `wchar_t` or `char*`). Using the `compiler` variable mentioned @@ -163,8 +165,7 @@ In older versions (<= 0.30) meson would error out if the size could not be determined. Since version 0.31 it returns -1 if the size could not be determined. -Does a function exist? -== +## Does a function exist? Just having a header doesn't say anything about its contents. Sometimes you need to explicitly check if some function @@ -192,8 +193,7 @@ report the function as missing. Without the header however, it would lack the necessary availability information and incorrectly report the function as available. -Does a structure contain a member? -== +## Does a structure contain a member? Some platforms have different standard structures. Here's how one would check if a struct called `mystruct` from header `myheader.h` @@ -205,8 +205,7 @@ if compiler.has_member('struct mystruct', 'some_member', prefix : '#include str: return self.id + def get_linker_id(self) -> str: + return self.linker.id + def get_version_string(self) -> str: details = [self.id, self.version] if self.full_version: diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 758c461..1a9b5e6 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -1007,6 +1007,7 @@ class CompilerHolder(InterpreterObject): self.methods.update({'compiles': self.compiles_method, 'links': self.links_method, 'get_id': self.get_id_method, + 'get_linker_id': self.get_linker_id_method, 'compute_int': self.compute_int_method, 'sizeof': self.sizeof_method, 'get_define': self.get_define_method, @@ -1163,6 +1164,12 @@ class CompilerHolder(InterpreterObject): @noPosargs @permittedKwargs({}) + @FeatureNew('compiler.get_linker_id', '0.53.0') + def get_linker_id_method(self, args, kwargs): + return self.compiler.get_linker_id() + + @noPosargs + @permittedKwargs({}) def symbols_have_underscore_prefix_method(self, args, kwargs): ''' Check if the compiler prefixes _ (underscore) to global C symbols diff --git a/test cases/common/31 compiler id/meson.build b/test cases/common/31 compiler id/meson.build index 2b5c445..280d4f7 100644 --- a/test cases/common/31 compiler id/meson.build +++ b/test cases/common/31 compiler id/meson.build @@ -1,7 +1,15 @@ -project('compiler id', 'c') +project('compiler_id') -comp = meson.get_compiler('c') -str = comp.get_id() +foreach lang : ['c', 'cpp', 'fortran', 'objc', 'objcpp'] -message('Compiler name is:') -message(str) + if not add_languages(lang, required: false) + continue + endif + + comp = meson.get_compiler(lang) + + message(lang + ' compiler name is: ' + comp.get_id()) + + message(lang + ' linker name is: ' + comp.get_linker_id()) + +endforeach \ No newline at end of file -- cgit v1.1