Repository: mesonbuild/meson

Issue:
Assertion error when using libm from find_library()
Running Meson fails with Nautilus, as we do explicit linking with libm.

```
Traceback (most recent call last):
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/mesonmain.py", line 353, in run
    app.generate()
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/mesonmain.py", line 152, in generate
    self._generate(env)
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/mesonmain.py", line 209, in _generate
    g.generate(intr)
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/backend/ninjabackend.py", line 210, in generate
    self.generate_target(t, outfile)
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/backend/ninjabackend.py", line 461, in generate_target
    elem = self.generate_link(target, outfile, outname, obj_list, linker, pch_objects)
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/backend/ninjabackend.py", line 2465, in generate_link
    self.determine_rpath_dirs(target),
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/backend/backends.py", line 337, in determine_rpath_dirs
    for rp in self.rpaths_for_bundled_shared_libraries(target):
  File "/home/ernestas/Development/github.com/mesonbuild/meson/mesonbuild/backend/backends.py", line 326, in rpaths_for_bundled_shared_libraries
    assert(not os.path.isabs(rel_to_src))
AssertionError
```

The code in Meson checks if the path is a system path by looking at its prefix, but on my machine it seems to be a bit more exotic: `/usr/bin/../lib/gcc/x86_64-redhat-linux/7/../../../../lib64/libm.so`.


Discussion:
Same problem here building Nautilus in Arch Linux. Did you find a solution?
Same problem on windows:

```
Traceback (most recent call last):
  File "c:\program files\python36\lib\site-packages\mesonbuild\mesonmain.py", line 352, in run
    app.generate()
  File "c:\program files\python36\lib\site-packages\mesonbuild\mesonmain.py", line 147, in generate
    self._generate(env)
  File "c:\program files\python36\lib\site-packages\mesonbuild\mesonmain.py", line 197, in _generate
    g.generate(intr)
  File "c:\program files\python36\lib\site-packages\mesonbuild\backend\ninjabackend.py", line 210, in generate
    self.generate_target(t, outfile)
  File "c:\program files\python36\lib\site-packages\mesonbuild\backend\ninjabackend.py", line 461, in generate_target
    elem = self.generate_link(target, outfile, outname, obj_list, linker, pch_objects)
  File "c:\program files\python36\lib\site-packages\mesonbuild\backend\ninjabackend.py", line 2460, in generate_link
    self.determine_rpath_dirs(target),
  File "c:\program files\python36\lib\site-packages\mesonbuild\backend\backends.py", line 336, in determine_rpath_dirs
    for rp in self.rpaths_for_bundled_shared_libraries(target):
  File "c:\program files\python36\lib\site-packages\mesonbuild\backend\backends.py", line 325, in rpaths_for_bundled_shared_libraries
    assert(not os.path.isabs(rel_to_src))
AssertionError
```

`rel_to_src` seem to be `/<directory>` after adding some debug prints. Adding code to remove the leading `/` fixes the problem.

Reverting to 0.42.1 fixes it

Is this the proper fix? I thought python should have better ways to manipulate paths than strings..

```diff
-rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]
+rel_to_src = absdir[len(self.environment.get_source_dir()) + 2:]
```
I added the following lines, right before the assert.

```python
                    print(absdir)
                    print(self.environment.get_source_dir())
                    print(rel_to_src)
```

I can now see that `absdir` and `self.environment.get_source_dir()` are completely unrelated if you use:

```
find_library('foo', dirs:'C:\foo\lib')
```

`absdir` will be `c:\foo\lib` and `get_source_dir` will be something like `c:\users\niklas\git\myproj`.

The following line is therefore completely wrong. It subtracts the length of a completely unrelated path:

```python
rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]
```

I don't know what the real fix is here though..

Resolving patch:
diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py
--- a/mesonbuild/backend/backends.py
+++ b/mesonbuild/backend/backends.py
@@ -323,9 +323,12 @@ def rpaths_for_bundled_shared_libraries(self, target):
                     if os.path.splitext(libpath)[1] not in ['.dll', '.lib', '.so']:
                         continue
                     absdir = os.path.dirname(libpath)
-                    rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]
-                    assert(not os.path.isabs(rel_to_src))
-                    paths.append(os.path.join(self.build_to_src, rel_to_src))
+                    if absdir.startswith(self.environment.get_source_dir()):
+                        rel_to_src = absdir[len(self.environment.get_source_dir()) + 1:]
+                        assert not os.path.isabs(rel_to_src), 'rel_to_src: {} is absolute'.format(rel_to_src)
+                        paths.append(os.path.join(self.build_to_src, rel_to_src))
+                    else:
+                        paths.append(absdir)
         return paths
 
     def determine_rpath_dirs(self, target):


Test patch: