Repository: mesonbuild/meson

Issue:
New `add_project_dependencies` fails to include deps when combined with custom_target
**Describe the bug**

I'm using `add_project_dependencies` to add include directories to all targets in my build, like this:

```
common = declare_dependency(
  include_directories: include_directories('.'))
add_project_dependencies(common, language: 'cpp')
```

Prior to using `add_project_dependencies` I had added `common` explicitly to all target dependencies. After removing `common` from all targets, a few targets failed to build because they did not include the directories of the `common` target.

All of the targets that failed to build had one thing in common: they depended on generated headers via `custom_target`.

**To Reproduce**

Here is the minimal reproducer

```diff
diff --git a/d/meson.build b/d/meson.build
new file mode 100644
index 0000000..ddd9cd0
--- /dev/null
+++ b/d/meson.build
@@ -0,0 +1,6 @@
+gen_h = custom_target(
+  output: 'gen.h',
+  command: ['touch', '@OUTPUT@'])
+
+executable('with_common', 'test.cc', gen_h, dependencies: [common])
+executable('xxxx_common', 'test.cc', gen_h)
diff --git a/d/test.cc b/d/test.cc
new file mode 100644
index 0000000..9f1ab71
--- /dev/null
+++ b/d/test.cc
@@ -0,0 +1,2 @@
+#include "d/gen.h"
+int main() {}
diff --git a/meson.build b/meson.build
new file mode 100644
index 0000000..bfd65ce
--- /dev/null
+++ b/meson.build
@@ -0,0 +1,7 @@
+project('proj', 'cpp')
+
+common = declare_dependency(
+  include_directories: include_directories('.'))
+add_project_dependencies(common, language: 'cpp')
+
+subdir('d')
```

If I configure the builddir we can see that in the `xxxx_common` target the root directory is not included as it is in the `with_common` target (ie `-I..`).

```json
[nwatkins@fedora repo]$ cat builddir/compile_commands.json 
[
  {
    "directory": "/home/nwatkins/src/repo/builddir",
    "command": "ccache c++ -Id/with_common.p -Id -I../d -I. -I.. -I/home/nwatkins/src/repo/. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -O0 -g -MD -MQ d/with_common.p/test.cc.o -MF d/with_common.p/test.cc.o.d -o d/with_common.p/test.cc.o -c ../d/test.cc",
    "file": "../d/test.cc",
    "output": "d/with_common.p/test.cc.o"
  },
  {
    "directory": "/home/nwatkins/src/repo/builddir",
    "command": "ccache c++ -Id/xxxx_common.p -Id -I../d -I/home/nwatkins/src/repo/. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -O0 -g -MD -MQ d/xxxx_common.p/test.cc.o -MF d/xxxx_common.p/test.cc.o.d -o d/xxxx_common.p/test.cc.o -c ../d/test.cc",
    "file": "../d/test.cc",
    "output": "d/xxxx_common.p/test.cc.o"
  }
]
```

Example run where we cannot include paths now that are relative to the root directory.

```
[nwatkins@fedora repo]$ meson compile -C builddir with_common
ninja: Entering directory `/home/nwatkins/src/repo/builddir'
[3/3] Linking target d/with_common
[nwatkins@fedora repo]$ meson compile -C builddir xxxx_common
ninja: Entering directory `/home/nwatkins/src/repo/builddir'
[1/2] Compiling C++ object d/xxxx_common.p/test.cc.o
FAILED: d/xxxx_common.p/test.cc.o 
ccache c++ -Id/xxxx_common.p -Id -I../d -I/home/nwatkins/src/repo/. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -O0 -g -MD -MQ d/xxxx_common.p/test.cc.o -MF d/xxxx_common.p/test.cc.o.d -o d/xxxx_common.p/test.cc.o -c ../d/test.cc
../d/test.cc:1:10: fatal error: d/gen.h: No such file or directory
    1 | #include "d/gen.h"
      |          ^~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
```

**Expected behavior**

The targets include the directory configured in the `common` dependency. In the reproducer, that `xxxx_common` would also build.

**system parameters**

* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)? No
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.) Fedora 35
* what Python version are you using: 3.10.5
* what `meson --version`: 0.63.1
* what `ninja --version` if it's a Ninja build: 1.10.2



Discussion:
CC @bonzini 

Resolving patch:
diff --git a/mesonbuild/interpreter/interpreter.py b/mesonbuild/interpreter/interpreter.py
--- a/mesonbuild/interpreter/interpreter.py
+++ b/mesonbuild/interpreter/interpreter.py
@@ -2753,7 +2753,7 @@ def func_add_project_dependencies(self, node: mparser.FunctionNode, args: T.Tupl
             for i in d.get_include_dirs():
                 for lang in kwargs['language']:
                     comp = self.coredata.compilers[for_machine][lang]
-                    for idir in i.to_string_list(self.environment.get_source_dir()):
+                    for idir in i.to_string_list(self.environment.get_source_dir(), self.environment.get_build_dir()):
                         compile_args.extend(comp.get_include_args(idir, system_incdir))
 
             self._add_project_arguments(node, self.build.projects_args[for_machine], compile_args, kwargs)


Test patch: