Repository: mesonbuild/meson

Issue:
All subproject  using wrap-file is broken since commit 68bd64c !!!
@jpakkane 

subproject using wrap-file is broken since commit (68bd64c Prevent projects from directly grabbing files from other subprojects. )

All my project depend on such type subproject is broken

# Reproduce step
```
mkdir test1
cd test1/
mkdir subprojects
wraptool install zlib

cat >> meson.build << EOF
project('mainproj', 'c')

subproject('zlib')
EOF

meson builddir

```

# log
```
meson builddir
The Meson build system
Version: 0.44.0.dev1
Source dir: /tmp/test1
Build dir: /tmp/test1/builddir
Build type: native build
Project name: mainproj
Native C compiler: ccache cc (gcc 6.3.0)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Downloading zlib from http://zlib.net/fossils/zlib-1.2.8.tar.gz
Download size: 571091
Downloading: ..........
Downloading patch from https://wrapdb.mesonbuild.com/v1/projects/zlib/1.2.8/8/get_zip
Download size: 363
Downloading: ..........

Executing subproject zlib.

Project name: zlib
Native C compiler: ccache cc (gcc 6.3.0)

Meson encountered an error in file subprojects/zlib-1.2.8/meson.build, line 7, column 0:
Sandbox violation: Tried to grab file adler32.c from a different subproject.

A full log can be found at /tmp/test1/builddir/meson-logs/meson-log.txt
```

Reason:

- **sproj_name** is `zlib-1.2.8` according to `directory = zlib-1.2.8` of zlib.wrap 
- but **self.subproject** `zlib`

```diff
git diff
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index f4bc11f8..448c5d22 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -2817,6 +2817,7 @@ different subdirectory.
             raise InterpreterException('Sandbox violation: Tried to grab file %s from a nested subproject.' % segments[-1])
         sproj_name = segments[segments.index(self.subproject_dir) + 1]
         if sproj_name != self.subproject:
+            mlog.log(sproj_name, self.subproject)
             raise InterpreterException('Sandbox violation: Tried to grab file %s from a different subproject.' % segments[-1])

     def source_strings_to_files(self, sources):
```




Discussion:
@jpakkane 

possible solution:

1. save directory name from xxx.wrap and compare with it
2. Using directory name from xxx.wrap to assign to self.subproject
Should do a mirror release 0.43.1 if this issue was fixed? because it broken lots of projects which using wrap-file

Resolving patch:
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -1373,6 +1373,9 @@ def __init__(self, build, backend, subproject='', subdir='', subproject_dir='sub
         self.coredata = self.environment.get_coredata()
         self.backend = backend
         self.subproject = subproject
+        # Subproject directory is usually the name of the subproject, but can
+        # be different for dependencies provided by wrap files.
+        self.subproject_directory_name = subdir.split(os.path.sep)[-1]
         self.subproject_dir = subproject_dir
         self.option_file = os.path.join(self.source_root, self.subdir, 'meson_options.txt')
         self.load_root_meson_file()
@@ -2785,7 +2788,6 @@ def run(self):
         super().run()
         mlog.log('Build targets in project:', mlog.bold(str(len(self.build.targets))))
 
-
     # Check that the indicated file is within the same subproject
     # as we currently are. This is to stop people doing
     # nasty things like:
@@ -2816,7 +2818,7 @@ def validate_within_subproject(self, subdir, fname):
         if num_sps > 1:
             raise InterpreterException('Sandbox violation: Tried to grab file %s from a nested subproject.' % segments[-1])
         sproj_name = segments[segments.index(self.subproject_dir) + 1]
-        if sproj_name != self.subproject:
+        if sproj_name != self.subproject_directory_name:
             raise InterpreterException('Sandbox violation: Tried to grab file %s from a different subproject.' % segments[-1])
 
     def source_strings_to_files(self, sources):


Test patch:
diff --git a/test cases/common/162 wrap file should not failed/meson.build b/test cases/common/162 wrap file should not failed/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/162 wrap file should not failed/meson.build	
@@ -0,0 +1,3 @@
+project('mainproj', 'c')
+
+subproject('zlib')
diff --git a/test cases/common/162 wrap file should not failed/subprojects/packagecache/zlib-1.2.8-8-wrap.zip b/test cases/common/162 wrap file should not failed/subprojects/packagecache/zlib-1.2.8-8-wrap.zip
new file mode 100644
--- /dev/null
+++ b/test cases/common/162 wrap file should not failed/subprojects/packagecache/zlib-1.2.8-8-wrap.zip	
@@ -0,0 +1 @@
+dummy
diff --git a/test cases/common/162 wrap file should not failed/subprojects/packagecache/zlib-1.2.8.tar.gz b/test cases/common/162 wrap file should not failed/subprojects/packagecache/zlib-1.2.8.tar.gz
new file mode 100644
--- /dev/null
+++ b/test cases/common/162 wrap file should not failed/subprojects/packagecache/zlib-1.2.8.tar.gz	
@@ -0,0 +1 @@
+dummy
diff --git a/test cases/common/162 wrap file should not failed/subprojects/zlib-1.2.8/foo.c b/test cases/common/162 wrap file should not failed/subprojects/zlib-1.2.8/foo.c
new file mode 100644
diff --git a/test cases/common/162 wrap file should not failed/subprojects/zlib-1.2.8/meson.build b/test cases/common/162 wrap file should not failed/subprojects/zlib-1.2.8/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/common/162 wrap file should not failed/subprojects/zlib-1.2.8/meson.build	
@@ -0,0 +1,2 @@
+project('shared lib', 'c')
+shared_library('foo', 'foo.c')
diff --git a/test cases/common/162 wrap file should not failed/subprojects/zlib.wrap b/test cases/common/162 wrap file should not failed/subprojects/zlib.wrap
new file mode 100644
--- /dev/null
+++ b/test cases/common/162 wrap file should not failed/subprojects/zlib.wrap	
@@ -0,0 +1,10 @@
+[wrap-file]
+directory = zlib-1.2.8
+
+source_url = http://zlib.net/fossils/zlib-1.2.8.tar.gz
+source_filename = zlib-1.2.8.tar.gz
+source_hash = 36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d
+
+patch_url = https://wrapdb.mesonbuild.com/v1/projects/zlib/1.2.8/8/get_zip
+patch_filename = zlib-1.2.8-8-wrap.zip
+patch_hash = 17c52a0e0c59ce926d3959005d5cd8178c6c7e2c9a4a1304279a8320c955ac60