Repository: mesonbuild/meson

Issue:
pulling subprojects fails if the build directory is outside the source directory
I am using meson 0.40.0
To reproduce,
git clone git://git.gnome.org/nautilus
mkdir build
cd build
meson --prefix=/usr --buildtype=debug --sysconfdir=/etc --libexecdir=/usr/lib/ -Denable-gtk-doc=false -Denable-exif=true -Denable-xmp=true -Denable-selinux=false ../nautilus

This results in the following error:

Meson encountered an error in file meson.build, line 107, column 0:
Subproject directory 'subprojects/libgd' does not exist and cannot be downloaded:
No libgd.wrap found for 'subprojects/libgd'

The directory subprojects/libgd exists but is empty.

Next I tried:
cd nautilus
mkdir build
cd build
meson --prefix=/usr --buildtype=debug --sysconfdir=/etc --libexecdir=/usr/lib/ -Denable-gtk-doc=false -Denable-exif=true -Denable-xmp=true -Denable-selinux=false ../
said:
Submodule path 'subprojects/libgd' not initialized
 Maybe you want to use 'update --init'?

A Gnome developer suggested I file a bug report here.
Thank you.


Discussion:
This describes two related issues:

- submodules aren’t initialized on checkout, which can be fixed by adding `--init` to the command line [here](https://github.com/mesonbuild/meson/blob/7dc747ea54480c452b913e4bfe682ec67061c9bf/mesonbuild/wrap/wrap.py#L162), as the error so kindly suggests
- [Meson doesn’t try to update submodules outside the repo](https://github.com/mesonbuild/meson/blob/7dc747ea54480c452b913e4bfe682ec67061c9bf/mesonbuild/wrap/wrap.py#L152)
Let's fix this and get it into 0.40.1. This bug makes submodule subprojects unusable in many cases.

Resolving patch:
diff --git a/mesonbuild/wrap/wrap.py b/mesonbuild/wrap/wrap.py
--- a/mesonbuild/wrap/wrap.py
+++ b/mesonbuild/wrap/wrap.py
@@ -38,8 +38,9 @@ def build_ssl_context():
     ctx.load_default_certs()
     return ctx
 
-def quiet_git(cmd):
-    pc = subprocess.Popen(['git'] + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+def quiet_git(cmd, workingdir):
+    pc = subprocess.Popen(['git', '-C', workingdir] + cmd,
+                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     out, err = pc.communicate()
     if pc.returncode != 0:
         return False, err
@@ -150,16 +151,16 @@ def resolve(self, packagename):
 
     def resolve_git_submodule(self, dirname):
         # Are we in a git repository?
-        ret, out = quiet_git(['rev-parse'])
+        ret, out = quiet_git(['rev-parse'], self.subdir_root)
         if not ret:
             return False
         # Is `dirname` a submodule?
-        ret, out = quiet_git(['submodule', 'status', dirname])
+        ret, out = quiet_git(['submodule', 'status', dirname], self.subdir_root)
         if not ret:
             return False
         # Submodule has not been added, add it
         if out.startswith(b'-'):
-            if subprocess.call(['git', 'submodule', 'update', dirname]) != 0:
+            if subprocess.call(['git', '-C', self.subdir_root, 'submodule', 'update', '--init', dirname]) != 0:
                 return False
         # Submodule was added already, but it wasn't populated. Do a checkout.
         elif out.startswith(b' '):


Test patch: