Repository: mesonbuild/meson

Issue:
depend_files in subdir looks in parent
Not sure if this is by design, but if I have a subdir with the files `foo.in` and `file`, and:

~~~Meson
# meson.build
subdir(src)

# src/meson.build
custom_target('foo',
  build_by_default : true,
  depend_files : 'file',
  input : 'foo.in',
  output : 'foo.out',
  command : [
    ...
  ]
)
~~~

then building fails with

> ninja: error: '../file', needed by 'src/foo.out', missing and no known rule to make it

whereas if I use `files('file')` instead of `'file'` it works.

If I put `file` in the parent folder it also builds. I kind of assumed that referring to `'file'` inside the build file of the subdir would just work, like referring to `'foo.in'` does.


depend_files in subdir looks in parent
Not sure if this is by design, but if I have a subdir with the files `foo.in` and `file`, and:

~~~Meson
# meson.build
subdir(src)

# src/meson.build
custom_target('foo',
  build_by_default : true,
  depend_files : 'file',
  input : 'foo.in',
  output : 'foo.out',
  command : [
    ...
  ]
)
~~~

then building fails with

> ninja: error: '../file', needed by 'src/foo.out', missing and no known rule to make it

whereas if I use `files('file')` instead of `'file'` it works.

If I put `file` in the parent folder it also builds. I kind of assumed that referring to `'file'` inside the build file of the subdir would just work, like referring to `'foo.in'` does.




Discussion:

Resolving patch:
diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py
--- a/mesonbuild/backend/backends.py
+++ b/mesonbuild/backend/backends.py
@@ -738,9 +738,9 @@ def get_custom_target_depend_files(self, target, absolute_paths=False):
                     deps.append(i.rel_to_builddir(self.build_to_src))
             else:
                 if absolute_paths:
-                    deps.append(os.path.join(self.environment.get_build_dir(), i))
+                    deps.append(os.path.join(self.environment.get_source_dir(), target.subdir, i))
                 else:
-                    deps.append(os.path.join(self.build_to_src, i))
+                    deps.append(os.path.join(self.build_to_src, target.subdir, i))
         return deps
 
     def eval_custom_target_command(self, target, absolute_outputs=False):


Test patch:
diff --git a/test cases/common/166 custom target subdir depend files/copyfile.py b/test cases/common/166 custom target subdir depend files/copyfile.py
new file mode 100644
--- /dev/null
+++ b/test cases/common/166 custom target subdir depend files/copyfile.py	
@@ -0,0 +1,6 @@
+#!/usr/bin/env python3
+
+import sys
+import shutil
+
+shutil.copyfile(sys.argv[1], sys.argv[2])
diff --git a/test cases/common/166 custom target subdir depend files/meson.build b/test cases/common/166 custom target subdir depend files/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/166 custom target subdir depend files/meson.build	
@@ -0,0 +1,7 @@
+project('custom target subdir depend files', 'c')
+
+copy = find_program('copyfile.py')
+
+subdir('subdir')
+
+executable('foo', foo_src)
diff --git a/test cases/common/166 custom target subdir depend files/subdir/dep.dat b/test cases/common/166 custom target subdir depend files/subdir/dep.dat
new file mode 100644
--- /dev/null
+++ b/test cases/common/166 custom target subdir depend files/subdir/dep.dat	
@@ -0,0 +1 @@
+You can depend on this file.
\ No newline at end of file
diff --git a/test cases/common/166 custom target subdir depend files/subdir/foo.c.in b/test cases/common/166 custom target subdir depend files/subdir/foo.c.in
new file mode 100644
--- /dev/null
+++ b/test cases/common/166 custom target subdir depend files/subdir/foo.c.in	
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main() {
+    printf("foo is working.\n");
+    return 0;
+}
diff --git a/test cases/common/166 custom target subdir depend files/subdir/meson.build b/test cases/common/166 custom target subdir depend files/subdir/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/166 custom target subdir depend files/subdir/meson.build	
@@ -0,0 +1,6 @@
+foo_src = custom_target('foo_src',
+  depend_files : 'dep.dat',
+  input : 'foo.c.in',
+  output : 'foo.c',
+  command : [copy, '@INPUT@', '@OUTPUT@']
+)