Meson: Install glib tests

Fixes: #1444.
diff --git a/glib/tests/meson.build b/glib/tests/meson.build
index e684af4..f5c0fbe 100644
--- a/glib/tests/meson.build
+++ b/glib/tests/meson.build
@@ -30,11 +30,13 @@
   },
   'gwakeup' : {
     'source' : ['gwakeuptest.c', '../gwakeup.c'],
+    'install' : false,
   },
   'gwakeup-fallback' : {
     'skip' : not glib_conf.has('HAVE_EVENTFD'),
     'source' : ['gwakeuptest.c', '../gwakeup.c'],
     'c_args' : ['-DTEST_EVENTFD_FALLBACK'],
+    'install' : false,
   },
   'hash' : {},
   'hmac' : {},
@@ -115,6 +117,7 @@
   '1bit-emufutex' : {
     'source' : '1bit-mutex.c',
     'c_args' : ['-DTEST_EMULATED_FUTEX'],
+    'install' : false,
   },
   '642026' : {
     'suite' : ['slow'],
@@ -145,11 +148,25 @@
   # mutable to dictionaries (https://github.com/mesonbuild/meson/pull/3820).
   if not extra_args.get('skip', false)
     source = extra_args.get('source', test_name + '.c')
+    install = installed_tests_enabled and extra_args.get('install', true)
+
+    if install
+      test_conf = configuration_data()
+      test_conf.set('installed_tests_dir', installed_tests_execdir)
+      test_conf.set('program', test_name)
+      configure_file(
+        input: installed_tests_template,
+        output: test_name + '.test',
+        install_dir: installed_tests_metadir,
+        configuration: test_conf
+      )
+    endif
 
     exe = executable(test_name, source,
       c_args : test_cargs + extra_args.get('c_args', []),
       dependencies : test_deps + extra_args.get('dependencies', []),
-      install: false,
+      install_dir: installed_tests_execdir,
+      install: install,
     )
 
     suite = ['glib'] + extra_args.get('suite', [])
@@ -162,7 +179,8 @@
 executable('test-spawn-echo', 'test-spawn-echo.c',
   c_args : test_cargs,
   dependencies : test_deps,
-  install : false,
+  install_dir: installed_tests_execdir,
+  install: installed_tests_enabled,
 )
 
 # some testing of gtester functionality
diff --git a/meson.build b/meson.build
index fa7357d..15f895e 100644
--- a/meson.build
+++ b/meson.build
@@ -56,6 +56,7 @@
 glib_prefix = get_option('prefix')
 glib_bindir = join_paths(glib_prefix, get_option('bindir'))
 glib_libdir = join_paths(glib_prefix, get_option('libdir'))
+glib_libexecdir = join_paths(glib_prefix, get_option('libexecdir'))
 glib_datadir = join_paths(glib_prefix, get_option('datadir'))
 glib_pkgdatadir = join_paths(glib_datadir, 'glib-2.0')
 glib_includedir = join_paths(glib_prefix, get_option('includedir'))
@@ -66,6 +67,11 @@
 
 glib_pkgconfigreldir = join_paths(glib_libdir, 'pkgconfig')
 
+installed_tests_metadir = join_paths(glib_datadir, 'installed-tests', meson.project_name())
+installed_tests_execdir = join_paths(glib_libexecdir, 'installed-tests', meson.project_name())
+installed_tests_enabled = get_option('installed_tests')
+installed_tests_template = files('template.test.in')
+
 add_project_arguments('-D_GNU_SOURCE', language: 'c')
 
 # Disable strict aliasing;
diff --git a/meson_options.txt b/meson_options.txt
index 5245e0a..64accf6 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -78,3 +78,8 @@
        type : 'boolean',
        value : false,
        description : 'Use fam for file system monitoring')
+
+option('installed_tests',
+       type : 'boolean',
+       value : false,
+       description : 'enable installed tests')
diff --git a/template.test.in b/template.test.in
new file mode 100644
index 0000000..f701627
--- /dev/null
+++ b/template.test.in
@@ -0,0 +1,3 @@
+[Test]
+Type=session
+Exec=@installed_tests_dir@/@program@