Repository: mesonbuild/meson

Issue:
Mixed source Fortran C project fails to build with 0.57
**Describe the bug**

Meson 0.57 fails to build mixed source Fortran/C projects.

**To Reproduce**

Minimal project producing the error: [mwe.tar.gz](https://github.com/mesonbuild/meson/files/5995234/mwe.tar.gz)

```
> meson setup _build
The Meson build system
Version: 0.57.0
Source dir: /home/awvwgk/projects/src/git/bugs/meson-0.57
Build dir: /home/awvwgk/projects/src/git/bugs/meson-0.57/_build
Build type: native build
Project name: mwe
Project version: undefined
Fortran compiler for the host machine: gfortran (gcc 10.2.0 "GNU Fortran (GCC) 10.2.0")
Fortran linker for the host machine: gfortran ld.bfd 2.36
C compiler for the host machine: cc (gcc 10.2.0 "cc (GCC) 10.2.0")
C linker for the host machine: cc ld.bfd 2.36
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1

Found ninja-1.10.2 at /home/awvwgk/miniforge3/envs/mesonbuild/bin/ninja
> ninja -C _build
ninja: Entering directory `_build'
[1/4] Module scanner.
ninja: build stopped: 'mwe.p/file.c.o' not mentioned in its dyndep file 'mwe.p/depscan.dd'.
```

Meson log file: [meson-log.txt](https://github.com/mesonbuild/meson/files/5995252/meson-log.txt)

Let me know if you need any additional information.

**Expected behavior**

Correctly build mixed Fortran/C project.

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)?
*native build*
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.)
```
> conda info
conda info

     active environment : mesonbuild
    active env location : /home/awvwgk/miniforge3/envs/mesonbuild
            shell level : 2
       user config file : /home/awvwgk/.condarc
 populated config files : /home/awvwgk/miniforge3/.condarc
          conda version : 4.9.2
    conda-build version : not installed
         python version : 3.7.8.final.0
       virtual packages : __glibc=2.33=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /home/awvwgk/miniforge3  (writable)
           channel URLs : https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
          package cache : /home/awvwgk/miniforge3/pkgs
                          /home/awvwgk/.conda/pkgs
       envs directories : /home/awvwgk/miniforge3/envs
                          /home/awvwgk/.conda/envs
               platform : linux-64
             user-agent : conda/4.9.2 requests/2.24.0 CPython/3.7.8 Linux/5.9.16-1-MANJARO manjaro/20.2.1 glibc/2.33
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False
> uname -a
Linux saw8203 5.9.16-1-MANJARO #1 SMP PREEMPT Mon Dec 21 22:00:46 UTC 2020 x86_64 GNU/Linux
```
* what Python version are you using e.g. 3.8.0
* what `meson --version`
* what `ninja --version` if it's a Ninja build
```
> conda list
# packages in environment at /home/awvwgk/miniforge3/envs/mesonbuild:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       1_gnu    conda-forge
ca-certificates           2020.12.5            ha878542_0    conda-forge
certifi                   2020.12.5        py37h89c1867_1    conda-forge
ld_impl_linux-64          2.35.1               hea4e1c9_2    conda-forge
libffi                    3.2.1             he1b5a44_1007    conda-forge
libgcc-ng                 9.3.0               h2828fa1_18    conda-forge
libgomp                   9.3.0               h2828fa1_18    conda-forge
libstdcxx-ng              9.3.0               h6de172a_18    conda-forge
meson                     0.57.0             pyhd8ed1ab_0    conda-forge
ncurses                   6.2                  h58526e2_4    conda-forge
ninja                     1.10.2               h4bd325d_0    conda-forge
openssl                   1.1.1j               h7f98852_0    conda-forge
pip                       21.0.1             pyhd8ed1ab_0    conda-forge
python                    3.7.8           h6f2ec95_0_cpython    conda-forge
python_abi                3.7                     1_cp37m    conda-forge
readline                  8.0                  he28a2e2_2    conda-forge
setuptools                49.6.0           py37h89c1867_3    conda-forge
sqlite                    3.34.0               h74cdb3f_0    conda-forge
tk                        8.6.10               hed695b0_1    conda-forge
wheel                     0.36.2             pyhd3deb0d_0    conda-forge
xz                        5.2.5                h516909a_1    conda-forge
zlib                      1.2.11            h516909a_1010    conda-forge
> python --version
Python 3.7.8
> meson --version
0.57.0
> ninja --version
1.10.2
```


Discussion:

Resolving patch:
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -2513,15 +2513,18 @@ def generate_single_compile(self, target, src, is_generated=False, header_deps=N
         element.add_item('DEPFILE', dep_file)
         element.add_item('ARGS', commands)
 
-        self.add_dependency_scanner_entries_to_element(target, compiler, element)
+        self.add_dependency_scanner_entries_to_element(target, compiler, element, src)
         self.add_build(element)
         assert(isinstance(rel_obj, str))
         assert(isinstance(rel_src, str))
         return (rel_obj, rel_src.replace('\\', '/'))
 
-    def add_dependency_scanner_entries_to_element(self, target, compiler, element):
+    def add_dependency_scanner_entries_to_element(self, target, compiler, element, src):
         if not self.should_use_dyndeps_for_target(target):
             return
+        extension = os.path.splitext(src.fname)[1][1:]
+        if not (extension in compilers.lang_suffixes['fortran'] or extension in compilers.lang_suffixes['cpp']):
+            return
         dep_scan_file = self.get_dep_scan_file_for(target)
         element.add_item('dyndep', dep_scan_file)
         element.add_orderdep(dep_scan_file)


Test patch:
diff --git a/test cases/fortran/9 cpp/main.c b/test cases/fortran/9 cpp/main.c
new file mode 100644
--- /dev/null
+++ b/test cases/fortran/9 cpp/main.c	
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+double fortran(void);
+
+int main(void) {
+    printf("FORTRAN gave us this number: %lf.\n", fortran());
+    return 0;
+}
diff --git a/test cases/fortran/9 cpp/meson.build b/test cases/fortran/9 cpp/meson.build
--- a/test cases/fortran/9 cpp/meson.build	
+++ b/test cases/fortran/9 cpp/meson.build	
@@ -1,4 +1,4 @@
-project('C++ and FORTRAN', 'cpp', 'fortran')
+project('C, C++ and Fortran', 'c', 'cpp', 'fortran')
 
 cpp = meson.get_compiler('cpp')
 fc = meson.get_compiler('fortran')
@@ -17,9 +17,17 @@ if fc.get_id() == 'intel'
 endif
 
 e = executable(
+  'cfort',
+  ['main.c', 'fortran.f'],
+  dependencies : link_with,
+)
+
+test('C and Fortran', e)
+
+e2 = executable(
   'cppfort',
   ['main.cpp', 'fortran.f'],
   dependencies : link_with,
 )
 
-test('C++ FORTRAN', e)
+test('C++ and Fortran', e2)