Repository: mesonbuild/meson

Issue:
gnome.generate_gir: cryptic error when the name of the library contains an underscore.
Given this meson.build:

```
project('Hotdoc-Test', 'c')
gnome = import ('gnome')

glib_dep = dependency('glib-2.0')
gobject_dep = dependency('gobject-2.0')

sources = ['test-greeter.c']
headers = ['test-greeter.h']

testlib = shared_library('test_lib',
                          sources,
                          install: true,
                          dependencies: [glib_dep, gobject_dep],
                          c_args: ['-Wno-pedantic'],
                         )

gir = gnome.generate_gir(testlib,
  sources : sources + headers,
  nsversion : '1.0',
  namespace : 'Test',
  symbol_prefix : 'test_',
  identifier_prefix : 'Test',
  export_packages : 'test',
  includes : ['GObject-2.0'],
  install : true,
)
```

Assuming the two source files exist, `mkdir build && cd build && meson ..` raises:

```
(dev_env)[meh@meh-host build]$ rm -rf * && meson.py ..
The Meson build system
Version: 0.30.0.dev1
Source dir: /home/meh/devel/hotdoc/test_hotdoc
Build dir: /home/meh/devel/hotdoc/test_hotdoc/build
Build type: native build
Build machine cpu family: x86_64
Build machine cpu: x86_64
Project name: Hotdoc-Test
Native c compiler: cc (gcc 5.3.1)
Warning, glib compiled dependencies will not work reliably until this upstream issue is fixed: https://bugzilla.gnome.org/show_bug.cgi?id=745754
Found pkg-config: /usr/bin/pkg-config (0.28)
Native dependency glib-2.0 found: YES 2.44.1
Native dependency gobject-2.0 found: YES 2.44.1
Build targets in project: 3
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/mesonmain.py", line 266, in run
    app.generate()
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/mesonmain.py", line 170, in generate
    g.generate(intr)
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/backend/ninjabackend.py", line 176, in generate
    [self.generate_target(t, outfile) for t in self.build.get_targets().values()]
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/backend/ninjabackend.py", line 176, in <listcomp>
    [self.generate_target(t, outfile) for t in self.build.get_targets().values()]
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/backend/ninjabackend.py", line 219, in generate_target
    self.generate_custom_target(target, outfile)
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/backend/ninjabackend.py", line 338, in generate_custom_target
    (srcs, ofilenames, cmd) = self.eval_custom_target_command(target)
  File "/usr/lib/python3.4/site-packages/meson-0.30.0.dev1-py3.4.egg/mesonbuild/backend/backends.py", line 427, in eval_custom_target_command
    source = match.group(0)
AttributeError: 'NoneType' object has no attribute 'group'
(dev_env)[meh@meh-host build]$
```

This is with latest git master, I know this project used to build some time ago, so this must be a regression.

I had a quick look at the regex in question and figured out the issue, I'm not sure why regular expressions would be at all needed for these internal operations, it seems quite prone to breakage.



Discussion:
This is a pretty bad error, and I don't think resolution should be overly hard, @jpakkane not interested?

Jussi is low on bandwidth these days, but he will review any patches you provide. :)

Thanks @nirbheek , low on time here though and I don't depend on meson in production so don't expect too much ;)

@MathieuDuponchelle 

> I had a quick look at the regex in question and figured out the issue

Perhaps you could share your findings? :)

Ah well I read:

```
match = re.search('@PRIVATE_OUTDIR_(ABS_)?([-a-zA-Z0-9.@:]*)@', i)
source = match.group(0)
```

and guessed as "_" was not listed in there it was my problem, but I haven't tried to understand the context so that's not really helpful here :)

As I said, at first glance I'm a bit surprised regexes are even used in that context, looks pretty fragile to me, but maybe there's a very good reason.


Resolving patch:
diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py
--- a/mesonbuild/backend/backends.py
+++ b/mesonbuild/backend/backends.py
@@ -598,11 +598,17 @@ def eval_custom_target_command(self, target, absolute_paths=False):
                 i = i.replace('@OUTDIR@', outdir)
             elif '@DEPFILE@' in i:
                 if target.depfile is None:
-                    raise MesonException('Custom target %s has @DEPFILE@ but no depfile keyword argument.' % target.name)
+                    msg = 'Custom target {!r} has @DEPFILE@ but no depfile ' \
+                          'keyword argument.'.format(target.name)
+                    raise MesonException(msg)
                 dfilename = os.path.join(outdir, target.depfile)
                 i = i.replace('@DEPFILE@', dfilename)
             elif '@PRIVATE_OUTDIR_' in i:
-                match = re.search('@PRIVATE_OUTDIR_(ABS_)?([-a-zA-Z0-9.@:]*)@', i)
+                match = re.search('@PRIVATE_OUTDIR_(ABS_)?([^\/\s*]*)@', i)
+                if not match:
+                    msg = 'Custom target {!r} has an invalid argument {!r}' \
+                          ''.format(target.name, i)
+                    raise MesonException(msg)
                 source = match.group(0)
                 if match.group(1) is None and not absolute_paths:
                     lead_dir = ''


Test patch:
diff --git a/test cases/frameworks/7 gnome/gir/meson.build b/test cases/frameworks/7 gnome/gir/meson.build
--- a/test cases/frameworks/7 gnome/gir/meson.build	
+++ b/test cases/frameworks/7 gnome/gir/meson.build	
@@ -1,7 +1,7 @@
 libsources = ['meson-sample.c', 'meson-sample.h']
 
 girlib = shared_library(
-  'girlib',
+  'gir_lib',
   sources : libsources,
   dependencies : gobj,
   install : true
diff --git a/test cases/frameworks/7 gnome/installed_files.txt b/test cases/frameworks/7 gnome/installed_files.txt
--- a/test cases/frameworks/7 gnome/installed_files.txt	
+++ b/test cases/frameworks/7 gnome/installed_files.txt	
@@ -3,6 +3,6 @@ usr/include/enums2.h
 usr/include/enums3.h
 usr/include/marshaller.h
 usr/lib/girepository-1.0/Meson-1.0.typelib
-usr/lib/libgirlib.so
+usr/lib/libgir_lib.so
 usr/share/gir-1.0/Meson-1.0.gir
 usr/share/glib-2.0/schemas/com.github.meson.gschema.xml