diff options
Diffstat (limited to 'docs/markdown/Vala.md')
-rw-r--r-- | docs/markdown/Vala.md | 202 |
1 files changed, 112 insertions, 90 deletions
diff --git a/docs/markdown/Vala.md b/docs/markdown/Vala.md index c7d44b0..d3edce0 100644 --- a/docs/markdown/Vala.md +++ b/docs/markdown/Vala.md @@ -29,17 +29,18 @@ runtime type system. ## Using libraries -Meson uses the [`dependency()`](Reference-manual.md#dependency) function to find -the relevant VAPI, C headers and linker flags when it encounters a Vala source -file in a build target. Vala needs a VAPI file and a C header or headers to use -a library. The VAPI file helps map Vala code to the library's C programming -interface. It is the -[`pkg-config`](https://www.freedesktop.org/wiki/Software/pkg-config/) tool that -makes finding these installed files all work seamlessly behind the scenes. When -a `pkg-config` file doesn't exist for the library then the -[`find_library()`](Reference-manual.md#find_library) method of the [compiler -object](Reference-manual.md#compiler-object) needs to be used. Examples are -given later. + +Meson uses the [`dependency()`](Reference-manual.md#dependency) +function to find the relevant VAPI, C headers and linker flags when it +encounters a Vala source file in a build target. Vala needs a VAPI +file and a C header or headers to use a library. The VAPI file helps +map Vala code to the library's C programming interface. It is the +[`pkg-config`](https://www.freedesktop.org/wiki/Software/pkg-config/) +tool that makes finding these installed files all work seamlessly +behind the scenes. When a `pkg-config` file doesn't exist for the +library then the [`find_library()`](Reference-manual.md#find_library) +method of the [compiler object](Reference-manual.md#compiler-object) +needs to be used. Examples are given later. Note Vala uses libraries that follow the C Application Binary Interface (C ABI). The library, however, could be written in C, Vala, Rust, Go, C++ or any other @@ -72,26 +73,29 @@ sources = files('app.vala') executable('app_name', sources, dependencies: dependencies) ``` -GTK+ is the graphical toolkit used by GNOME, elementary OS and other desktop -environments. The binding to the library, the VAPI file, is distributed with -Vala. +GTK+ is the graphical toolkit used by GNOME, elementary OS and other +desktop environments. The binding to the library, the VAPI file, is +distributed with Vala. -Other libraries may have a VAPI that is distributed with the library itself. -Such libraries will have their VAPI file installed along with their other -development files. The VAPI is installed in Vala's standard search path and so -works just as seamlessly using the `dependency()` function. +Other libraries may have a VAPI that is distributed with the library +itself. Such libraries will have their VAPI file installed along with +their other development files. The VAPI is installed in Vala's +standard search path and so works just as seamlessly using the +`dependency()` function. ### Targeting a version of GLib -Meson's [`dependency()`](Reference-manual.md#dependency) function allows a -version check of a library. This is often used to check a minimum version is -installed. When setting a minimum version of GLib, Meson will also pass this to -the Vala compiler using the `--target-glib` option. -This is needed when using GTK+'s user interface definition files with Vala's -`[GtkTemplate]`, `[GtkChild]` and `[GtkCallback]` attributes. This requires -`--target-glib 2.38`, or a newer version, to be passed to Vala. With Meson this -is simply done with: +Meson's [`dependency()`](Reference-manual.md#dependency) function +allows a version check of a library. This is often used to check a +minimum version is installed. When setting a minimum version of GLib, +Meson will also pass this to the Vala compiler using the +`--target-glib` option. + +This is needed when using GTK+'s user interface definition files with +Vala's `[GtkTemplate]`, `[GtkChild]` and `[GtkCallback]` attributes. +This requires `--target-glib 2.38`, or a newer version, to be passed +to Vala. With Meson this is simply done with: ```meson project('vala app', 'vala', 'c') @@ -107,9 +111,9 @@ sources = files('app.vala') executable('app_name', sources, dependencies: dependencies) ``` -Using `[GtkTemplate]` also requires the GTK+ user interface definition files to -be built in to the binary as GResources. For completeness, the next example -shows this: +Using `[GtkTemplate]` also requires the GTK+ user interface definition +files to be built in to the binary as GResources. For completeness, +the next example shows this: ```meson project('vala app', 'vala', 'c') @@ -133,17 +137,19 @@ executable('app_name', sources, dependencies: dependencies) ### Adding to Vala's search path -So far we have covered the cases where the VAPI file is either distributed with -Vala or the library. A VAPI can also be included in the source files of your -project. The convention is to put it in the `vapi` directory of your project. -This is needed when a library does not have a VAPI or your project needs to link -to another component in the project that uses the C ABI. For example if part of -the project is written in C. +So far we have covered the cases where the VAPI file is either +distributed with Vala or the library. A VAPI can also be included in +the source files of your project. The convention is to put it in the +`vapi` directory of your project. + +This is needed when a library does not have a VAPI or your project +needs to link to another component in the project that uses the C ABI. +For example if part of the project is written in C. -The Vala compiler's `--vapidir` option is used to add the project directory to -the VAPI search path. In Meson this is done with the `add_project_arguments()` -function: +The Vala compiler's `--vapidir` option is used to add the project +directory to the VAPI search path. In Meson this is done with the +`add_project_arguments()` function: ```meson project('vala app', 'vala', 'c') @@ -163,30 +169,33 @@ sources = files('app.vala') executable('app_name', sources, dependencies: dependencies) ``` -If the VAPI is for an external library then make sure that the VAPI name -corresponds to the pkg-config file name. +If the VAPI is for an external library then make sure that the VAPI +name corresponds to the pkg-config file name. -The [`vala-extra-vapis` repository](https://gitlab.gnome.org/GNOME/vala-extra-vapis) -is a community maintained repository of VAPIs that are not distributed. +The [`vala-extra-vapis` +repository](https://gitlab.gnome.org/GNOME/vala-extra-vapis) is a +community maintained repository of VAPIs that are not distributed. Developers use the repository to share early work on new bindings and -improvements to existing bindings. So the VAPIs can frequently change. It is -recommended VAPIs from this repository are copied in to your project's source -files. +improvements to existing bindings. So the VAPIs can frequently change. +It is recommended VAPIs from this repository are copied in to your +project's source files. -This also works well for starting to write new bindings before they are shared -with the `vala-extra-vapis` repository. +This also works well for starting to write new bindings before they +are shared with the `vala-extra-vapis` repository. ### Libraries without pkg-config files + A library that does not have a corresponding pkg-config file may mean -`dependency()` is unsuitable for finding the C and Vala interface files. In this -case it is necessary to use the `find_library()` method of the compiler object. +`dependency()` is unsuitable for finding the C and Vala interface +files. In this case it is necessary to use the `find_library()` method +of the compiler object. -The first example uses Vala's POSIX binding. There is no pkg-config file because -POSIX includes the standard C library on Unix systems. All that is needed is the -VAPI file, `posix.vapi`. This is included with Vala and installed in Vala's -standard search path. Meson just needs to be told to only find the library for -the Vala compiler: +The first example uses Vala's POSIX binding. There is no pkg-config +file because POSIX includes the standard C library on Unix systems. +All that is needed is the VAPI file, `posix.vapi`. This is included +with Vala and installed in Vala's standard search path. Meson just +needs to be told to only find the library for the Vala compiler: ```meson project('vala app', 'vala', 'c') @@ -202,10 +211,11 @@ sources = files('app.vala') executable('app_name', sources, dependencies: dependencies) ``` -The next example shows how to link with a C library where no additional VAPI is -needed. The standard maths functions are already bound in `glib-2.0.vapi`, but -the GNU C library requires linking to the maths library separately. In this -example Meson is told to find the library only for the C compiler: +The next example shows how to link with a C library where no +additional VAPI is needed. The standard maths functions are already +bound in `glib-2.0.vapi`, but the GNU C library requires linking to +the maths library separately. In this example Meson is told to find +the library only for the C compiler: ```meson project('vala app', 'vala', 'c') @@ -220,12 +230,15 @@ sources = files('app.vala') executable('app_name', sources, dependencies: dependencies) ``` -The `required: false` means the build will continue when using another C library -that does not separate the maths library. See [Add math library (-lm) -portably](howtox.md#add-math-library-lm-portably). -The final example shows how to use a library that does not have a pkg-config -file and the VAPI is in the `vapi` directory of your project source files: +The `required: false` means the build will continue when using another +C library that does not separate the maths library. See [Add math +library (-lm) portably](howtox.md#add-math-library-lm-portably). + +The final example shows how to use a library that does not have a +pkg-config file and the VAPI is in the `vapi` directory of your +project source files: + ```meson project('vala app', 'vala', 'c') @@ -252,13 +265,18 @@ keyword added to include the project VAPI directory. This is not added automatically by `add_project_arguments()`. ### Working with the Vala Preprocessor -Passing arguments to [Vala's preprocessor](https://wiki.gnome.org/Projects/Vala/Manual/Preprocessor) requires specifying the language as `vala`. For example, the following statement sets the preprocessor symbol `USE_FUSE`: + +Passing arguments to [Vala's +preprocessor](https://wiki.gnome.org/Projects/Vala/Manual/Preprocessor) +requires specifying the language as `vala`. For example, the following +statement sets the preprocessor symbol `USE_FUSE`: ```meson add_project_arguments('-D', 'USE_FUSE', language: 'vala') ``` -If you need to pass an argument to the C pre-processor then specify the language as c. For example to set FUSE_USE_VERSION to 26 use: +If you need to pass an argument to the C pre-processor then specify +the language as c. For example to set FUSE_USE_VERSION to 26 use: ```meson add_project_arguments('-DFUSE_USE_VERSION=26', language: 'c') @@ -268,9 +286,10 @@ add_project_arguments('-DFUSE_USE_VERSION=26', language: 'c') ### Changing C header and VAPI names -Meson's [`library`](Reference-manual.md#library) target automatically outputs -the C header and the VAPI. They can be renamed by setting the `vala_header` and -`vala_vapi` arguments respectively: + +Meson's [`library`](Reference-manual.md#library) target automatically +outputs the C header and the VAPI. They can be renamed by setting the +`vala_header` and `vala_vapi` arguments respectively: ```meson foo_lib = shared_library('foo', 'foo.vala', @@ -280,26 +299,29 @@ foo_lib = shared_library('foo', 'foo.vala', install: true, install_dir: [true, true, true]) ``` -In this example, the second and third elements of the `install_dir` array -indicate the destination with `true` to use default directories (i.e. `include` -and `share/vala/vapi`). + +In this example, the second and third elements of the `install_dir` +array indicate the destination with `true` to use default directories +(i.e. `include` and `share/vala/vapi`). ### GObject Introspection and language bindings -A 'binding' allows another programming language to use a library written in -Vala. Because Vala uses the GObject type system as its runtime type system it is -very easy to use introspection to generate a binding. A Meson build of a Vala -library can generate the GObject introspection metadata. The metadata is then -used in separate projects with [language specific -tools](https://wiki.gnome.org/Projects/Vala/LibraryWritingBindings) to generate -a binding. - -The main form of metadata is a GObject Introspection Repository (GIR) XML file. -GIRs are mostly used by languages that generate bindings at compile time. -Languages that generate bindings at runtime mostly use a typelib file, which is -generated from the GIR. - -Meson can generate a GIR as part of the build. For a Vala library the + +A 'binding' allows another programming language to use a library +written in Vala. Because Vala uses the GObject type system as its +runtime type system it is very easy to use introspection to generate a +binding. A Meson build of a Vala library can generate the GObject +introspection metadata. The metadata is then used in separate projects +with [language specific +tools](https://wiki.gnome.org/Projects/Vala/LibraryWritingBindings) to +generate a binding. + +The main form of metadata is a GObject Introspection Repository (GIR) +XML file. GIRs are mostly used by languages that generate bindings at +compile time. Languages that generate bindings at runtime mostly use a +typelib file, which is generated from the GIR. + +Meson can generate a GIR as part of the build. For a Vala library the `vala_gir` option has to be set for the `library`: ```meson @@ -310,12 +332,12 @@ foo_lib = shared_library('foo', 'foo.vala', install_dir: [true, true, true, true]) ``` -The `true` value in `install_dir` tells Meson to use the default directory (i.e. -`share/gir-1.0` for GIRs). The fourth element in the `install_dir` array -indicates where the GIR file will be installed. +The `true` value in `install_dir` tells Meson to use the default +directory (i.e. `share/gir-1.0` for GIRs). The fourth element in the +`install_dir` array indicates where the GIR file will be installed. -To then generate a typelib file use a custom target with the `g-ir-compiler` -program and a dependency on the library: +To then generate a typelib file use a custom target with the +`g-ir-compiler` program and a dependency on the library: ```meson g_ir_compiler = find_program('g-ir-compiler') |