Repository: mesonbuild/meson

Issue:
KeyError: 'install_dir'
**Describe the bug**
AUR package [msitools](https://aur.archlinux.org/packages/msitools/) fails to build with meson 0.60.0. The meson output looks like this:
```
+ exec meson setup --prefix /usr --libexecdir lib --sbindir bin --buildtype plain --auto-features enabled --wrap-mode nodownload -D b_lto=true -D b_pie=true . build
The Meson build system
Version: 0.60.0
Source dir: /home/yes/git-repos/AUR/msitools/src/msitools-0.101
Build dir: /home/yes/git-repos/AUR/msitools/src/msitools-0.101/build
Build type: native build
Project name: msitools
Project version: 0.101
C compiler for the host machine: cc (gcc 11.1.0 "cc (GCC) 11.1.0")
C linker for the host machine: cc ld.bfd 2.36.1
Vala compiler for the host machine: valac (valac 0.54.2)
Host machine cpu family: x86_64
Host machine cpu: x86_64
Program build-aux/meson-dist found: YES (/home/yes/git-repos/AUR/msitools/src/msitools-0.101/build-aux/meson-dist)
Program perl found: YES (/usr/bin/perl)
Program bison found: YES (/usr/bin/bison)
Program subprojects/bats-core/bin/bats found: YES (/home/yes/git-repos/AUR/msitools/src/msitools-0.101/subprojects/bats-core/bin/bats)
Found pkg-config: /usr/bin/pkg-config (1.8.0)
Run-time dependency glib-2.0 found: YES 2.70.0
Run-time dependency gobject-2.0 found: YES 2.70.0
Run-time dependency gio-2.0 found: YES 2.70.0
Run-time dependency libgsf-1 found: YES 1.14.47
Run-time dependency libgcab-1.0 found: YES 1.4
Run-time dependency libxml-2.0 found: YES 2.9.12
Configuring config.h using configuration
Program glib-mkenums found: YES (/usr/bin/glib-mkenums)
Program glib-mkenums found: YES (/usr/bin/glib-mkenums)
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/mesonbuild/mesonmain.py", line 138, in run
    return options.run_func(options)
  File "/usr/lib/python3.9/site-packages/mesonbuild/msetup.py", line 294, in run
    app.generate()
  File "/usr/lib/python3.9/site-packages/mesonbuild/msetup.py", line 185, in generate
    self._generate(env)
  File "/usr/lib/python3.9/site-packages/mesonbuild/msetup.py", line 229, in _generate
    intr.run()
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreter/interpreter.py", line 2484, in run
    super().run()
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 150, in run
    self.evaluate_codeblock(self.ast, start=1)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 175, in evaluate_codeblock
    raise e
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 168, in evaluate_codeblock
    self.evaluate_statement(cur)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 181, in evaluate_statement
    return self.function_call(cur)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 456, in function_call
    res = func(node, func_args, kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/decorators.py", line 713, in wrapped
    return f(*wrapped_args, **wrapped_kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/decorators.py", line 115, in wrapped
    return f(*wrapped_args, **wrapped_kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/decorators.py", line 276, in wrapper
    return f(*nargs, **wrapped_kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreter/interpreter.py", line 1969, in func_subdir
    self.evaluate_codeblock(codeblock)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 175, in evaluate_codeblock
    raise e
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 168, in evaluate_codeblock
    self.evaluate_statement(cur)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 183, in evaluate_statement
    self.assignment(cur)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 568, in assignment
    value = self.evaluate_statement(node.value)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 185, in evaluate_statement
    return self.method_call(cur)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 484, in method_call
    res = obj.method_call(method_name, args, kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreter/interpreterobjects.py", line 754, in method_call
    ret = method(state, args, kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/interpreterbase/decorators.py", line 115, in wrapped
    return f(*wrapped_args, **wrapped_kwargs)
  File "/usr/lib/python3.9/site-packages/mesonbuild/modules/gnome.py", line 1459, in mkenums
    c_target = self._make_mkenum_custom_target(state, c_sources,
  File "/usr/lib/python3.9/site-packages/mesonbuild/modules/gnome.py", line 1596, in _make_mkenum_custom_target
    return build.CustomTarget(output, state.subdir, state.subproject, custom_kwargs,
  File "/usr/lib/python3.9/site-packages/mesonbuild/build.py", line 2330, in __init__
    self.process_kwargs(kwargs, backend)
  File "/usr/lib/python3.9/site-packages/mesonbuild/build.py", line 2441, in process_kwargs
    if isinstance(kwargs['install_dir'], list):
KeyError: 'install_dir'
```

According to https://github.com/mesonbuild/meson/issues/9365#issuecomment-937724433, it appears to be a meson bug that is not fully fixed.

**To Reproduce**
```bash
$ git clone https://aur.archlinux.org/msitools.git
$ cd msitools/
$ makepkg
```

**Expected behavior**
The package builds without issue.

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)?
  * native build
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.)
  * Arch Linux
* what Python version are you using e.g. 3.8.0
  * 3.9.7
* what `meson --version`
  * 0.60.0
* what `ninja --version` if it's a Ninja build
  * 1.10.2


Discussion:
@dcbaker this seems to be the same kind of issue as you fixed in https://github.com/mesonbuild/meson/pull/9369.
Sigh, yup. I'll get CustomTarget completely converted for 0.61...

I might need to send out the gnome module cleanups piecemeal as they're rather draining to work on.

Resolving patch:
diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py
--- a/mesonbuild/modules/gnome.py
+++ b/mesonbuild/modules/gnome.py
@@ -1434,13 +1434,16 @@ def mkenums(self, state, args, kwargs):
             # so --template consumes it.
             h_cmd = cmd + ['--template', '@INPUT@']
             h_sources = [h_template] + sources
-            custom_kwargs['install'] = install_header
-            if 'install_dir' not in custom_kwargs:
-                custom_kwargs['install_dir'] = \
+
+            # Copy so we don't mutate the arguments for the c_template
+            h_kwargs = custom_kwargs.copy()
+            h_kwargs['install'] = install_header
+            if 'install_dir' not in h_kwargs:
+                h_kwargs['install_dir'] = \
                     state.environment.coredata.get_option(mesonlib.OptionKey('includedir'))
             h_target = self._make_mkenum_custom_target(state, h_sources,
                                                        h_output, h_cmd,
-                                                       custom_kwargs)
+                                                       h_kwargs)
             targets.append(h_target)
 
         if c_template is not None:
@@ -1449,16 +1452,19 @@ def mkenums(self, state, args, kwargs):
             # so --template consumes it.
             c_cmd = cmd + ['--template', '@INPUT@']
             c_sources = [c_template] + sources
+
+            c_kwargs = custom_kwargs.copy()
             # Never install the C file. Complain on bug tracker if you need it.
-            custom_kwargs['install'] = False
+            c_kwargs['install'] = False
+            c_kwargs['install_dir'] = False
             if h_template is not None:
                 if 'depends' in custom_kwargs:
-                    custom_kwargs['depends'] += [h_target]
+                    c_kwargs['depends'] += [h_target]
                 else:
-                    custom_kwargs['depends'] = h_target
+                    c_kwargs['depends'] = h_target
             c_target = self._make_mkenum_custom_target(state, c_sources,
                                                        c_output, c_cmd,
-                                                       custom_kwargs)
+                                                       c_kwargs)
             targets.insert(0, c_target)
 
         if c_template is None and h_template is None:


Test patch:
diff --git a/test cases/frameworks/7 gnome/mkenums/meson.build b/test cases/frameworks/7 gnome/mkenums/meson.build
--- a/test cases/frameworks/7 gnome/mkenums/meson.build	
+++ b/test cases/frameworks/7 gnome/mkenums/meson.build	
@@ -34,9 +34,9 @@ enums_c2 = gnome.mkenums('abc2',
   sources : 'meson-sample.h',
   depends : [enums_h1, enums_h2],
   c_template : 'enums2.c.in',
-  ftail : '/* trailing source file info */',
-  install_header : true,
-  install_dir : get_option('includedir'))
+  ftail : '/* trailing source file info */')
+# explicitly don't set install_dir here, for bug testing
+# See https://github.com/mesonbuild/meson/issues/9472
 
 conf = configuration_data()
 conf.set('ENUM_FILE', 'enums2.h')