Repository: mesonbuild/meson

Issue:
rpm module doesn't generate template
When attempting to use the [RPM module](http://mesonbuild.com/RPM-module.html#rpm-module) with meson 0.41.2, it generates the following traceback:
```
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 307, in run
    app.generate()
  File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 180, in generate
    intr.run()
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreter.py", line 2544, in run
    super().run()
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 125, in run
    self.evaluate_codeblock(self.ast, start=1)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 146, in evaluate_codeblock
    raise e
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 140, in evaluate_codeblock
    self.evaluate_statement(cur)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 151, in evaluate_statement
    return self.function_call(cur)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 361, in function_call
    return self.funcs[func_name](node, self.flatten(posargs), kwargs)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 47, in wrapped
    return f(self, node, args, kwargs)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreter.py", line 2261, in func_subdir
    self.evaluate_codeblock(codeblock)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 146, in evaluate_codeblock
    raise e
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 140, in evaluate_codeblock
    self.evaluate_statement(cur)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 155, in evaluate_statement
    return self.method_call(cur)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreterbase.py", line 389, in method_call
    return obj.method_call(method_name, self.flatten(args), kwargs)
  File "/usr/lib/python3.6/site-packages/mesonbuild/interpreter.py", line 1060, in method_call
    value = fn(state, args, kwargs)
  File "/usr/lib/python3.6/site-packages/mesonbuild/modules/rpm.py", line 100, in generate_spec_template
    for lib in state.environment.coredata.ext_libs.values():
AttributeError: 'CoreData' object has no attribute 'ext_libs'
```


Discussion:
Pinging @ignatenkobrain 
Igor is vacationing, so he won't be able to look into this right now. Is it blocking something urgent for you?
@ignatenkobrain poke
This broke in 2015 and no one noticed:

```
commit 06c7cef26ebe8b34658791adf4d6e7fcd02b173b
Author: Jussi Pakkanen <jpakkane@gmail.com>
Date:   Tue Nov 24 17:49:12 2015 +0200
```

I don't think the rpm module is getting any maintenance at all. I have previously also found things in it that had been broken for 1-2 years. We should consider removing it or disabling it till we have automated tests for it.
I have version 0.43.0 and still see the same bug.
can I create an rpm in a different way ?
is there an estimation for fixing this bug ?
Bug is still present in 0.47.1.

Maybe the rpm module should be removed?

Resolving patch:
diff --git a/mesonbuild/modules/rpm.py b/mesonbuild/modules/rpm.py
--- a/mesonbuild/modules/rpm.py
+++ b/mesonbuild/modules/rpm.py
@@ -29,39 +29,16 @@
 class RPMModule(ExtensionModule):
 
     @noKwargs
-    def generate_spec_template(self, state, args, kwargs):
-        compiler_deps = set()
-        for compiler in state.compilers.values():
-            # Elbrus has one 'lcc' package for every compiler
-            if isinstance(compiler, compilers.GnuCCompiler):
-                compiler_deps.add('gcc')
-            elif isinstance(compiler, compilers.GnuCPPCompiler):
-                compiler_deps.add('gcc-c++')
-            elif isinstance(compiler, compilers.ElbrusCCompiler):
-                compiler_deps.add('lcc')
-            elif isinstance(compiler, compilers.ElbrusCPPCompiler):
-                compiler_deps.add('lcc')
-            elif isinstance(compiler, compilers.ElbrusFortranCompiler):
-                compiler_deps.add('lcc')
-            elif isinstance(compiler, compilers.ValaCompiler):
-                compiler_deps.add('vala')
-            elif isinstance(compiler, compilers.GnuFortranCompiler):
-                compiler_deps.add('gcc-gfortran')
-            elif isinstance(compiler, compilers.GnuObjCCompiler):
-                compiler_deps.add('gcc-objc')
-            elif compiler == compilers.GnuObjCPPCompiler:
-                compiler_deps.add('gcc-objc++')
-            else:
-                mlog.log('RPM spec file will not created, generating not allowed for:',
-                         mlog.bold(compiler.get_id()))
-                return
-        proj = state.project_name.replace(' ', '_').replace('\t', '_')
+    def generate_spec_template(self, coredata, args, kwargs):
+        self.coredata = coredata
+        required_compilers = self.__get_required_compilers()
+        proj = coredata.project_name.replace(' ', '_').replace('\t', '_')
         so_installed = False
         devel_subpkg = False
         files = set()
         files_devel = set()
         to_delete = set()
-        for target in state.targets.values():
+        for target in coredata.targets.values():
             if isinstance(target, build.Executable) and target.need_install:
                 files.add('%%{_bindir}/%s' % target.get_filename())
             elif isinstance(target, build.SharedLibrary) and target.need_install:
@@ -80,18 +57,19 @@ def generate_spec_template(self, state, args, kwargs):
                 files_devel.add('%%{_datadir}/gir-1.0/%s' % target.get_filename()[0])
             elif isinstance(target, TypelibTarget) and target.should_install():
                 files.add('%%{_libdir}/girepository-1.0/%s' % target.get_filename()[0])
-        for header in state.headers:
+        for header in coredata.headers:
             if len(header.get_install_subdir()) > 0:
                 files_devel.add('%%{_includedir}/%s/' % header.get_install_subdir())
             else:
                 for hdr_src in header.get_sources():
                     files_devel.add('%%{_includedir}/%s' % hdr_src)
-        for man in state.man:
+        for man in coredata.man:
             for man_file in man.get_sources():
                 files.add('%%{_mandir}/man%u/%s.*' % (int(man_file.split('.')[-1]), man_file))
         if len(files_devel) > 0:
             devel_subpkg = True
-        filename = os.path.join(state.environment.get_build_dir(),
+
+        filename = os.path.join(coredata.environment.get_build_dir(),
                                 '%s.spec' % proj)
         with open(filename, 'w+') as fn:
             fn.write('Name: %s\n' % proj)
@@ -102,24 +80,28 @@ def generate_spec_template(self, state, args, kwargs):
             fn.write('\n')
             fn.write('Source0: %{name}-%{version}.tar.xz # FIXME\n')
             fn.write('\n')
-            for compiler in compiler_deps:
+            fn.write('BuildRequires: meson\n')
+            for compiler in required_compilers:
                 fn.write('BuildRequires: %s\n' % compiler)
-            for dep in state.environment.coredata.deps:
+            for dep in coredata.environment.coredata.deps:
                 fn.write('BuildRequires: pkgconfig(%s)\n' % dep[0])
-            for lib in state.environment.coredata.ext_libs.values():
-                name = lib.get_name()
-                fn.write('BuildRequires: {} # FIXME\n'.format(name))
-                mlog.warning('replace', mlog.bold(name), 'with the real package.',
-                             'You can use following command to find package which '
-                             'contains this lib:',
-                             mlog.bold("dnf provides '*/lib{}.so'".format(name)))
-            for prog in state.environment.coredata.ext_progs.values():
-                if not prog.found():
-                    fn.write('BuildRequires: %%{_bindir}/%s # FIXME\n' %
-                             prog.get_name())
-                else:
-                    fn.write('BuildRequires: {}\n'.format(prog.get_path()))
-            fn.write('BuildRequires: meson\n')
+#   ext_libs and ext_progs have been removed from coredata so the following code
+#   no longer works. It is kept as a reminder of the idea should anyone wish
+#   to re-implement it.
+#
+#            for lib in state.environment.coredata.ext_libs.values():
+#                name = lib.get_name()
+#                fn.write('BuildRequires: {} # FIXME\n'.format(name))
+#                mlog.warning('replace', mlog.bold(name), 'with the real package.',
+#                             'You can use following command to find package which '
+#                             'contains this lib:',
+#                             mlog.bold("dnf provides '*/lib{}.so'".format(name)))
+#            for prog in state.environment.coredata.ext_progs.values():
+#                if not prog.found():
+#                    fn.write('BuildRequires: %%{_bindir}/%s # FIXME\n' %
+#                             prog.get_name())
+#                else:
+#                    fn.write('BuildRequires: {}\n'.format(prog.get_path()))
             fn.write('\n')
             fn.write('%description\n')
             fn.write('\n')
@@ -167,5 +149,33 @@ def generate_spec_template(self, state, args, kwargs):
         mlog.log('RPM spec template written to %s.spec.\n' % proj)
         return ModuleReturnValue(None, [])
 
+    def __get_required_compilers(self):
+        required_compilers = set()
+        for compiler in self.coredata.compilers.values():
+            # Elbrus has one 'lcc' package for every compiler
+            if isinstance(compiler, compilers.GnuCCompiler):
+                required_compilers.add('gcc')
+            elif isinstance(compiler, compilers.GnuCPPCompiler):
+                required_compilers.add('gcc-c++')
+            elif isinstance(compiler, compilers.ElbrusCCompiler):
+                required_compilers.add('lcc')
+            elif isinstance(compiler, compilers.ElbrusCPPCompiler):
+                required_compilers.add('lcc')
+            elif isinstance(compiler, compilers.ElbrusFortranCompiler):
+                required_compilers.add('lcc')
+            elif isinstance(compiler, compilers.ValaCompiler):
+                required_compilers.add('vala')
+            elif isinstance(compiler, compilers.GnuFortranCompiler):
+                required_compilers.add('gcc-gfortran')
+            elif isinstance(compiler, compilers.GnuObjCCompiler):
+                required_compilers.add('gcc-objc')
+            elif compiler == compilers.GnuObjCPPCompiler:
+                required_compilers.add('gcc-objc++')
+            else:
+                mlog.log('RPM spec file not created, generation not allowed for:',
+                         mlog.bold(compiler.get_id()))
+        return required_compilers
+
+
 def initialize(*args, **kwargs):
     return RPMModule(*args, **kwargs)


Test patch: