Repository: mesonbuild/meson

Issue:
CMake install target fails for capnproto-c++-0.7 cmake subproject
**Describe the bug**

When you run install and use capnproto in subprojects/, installation will fail.

**To Reproduce**

Use the attachment (attachment tested under windows) and:

1. meson build
2. ninja -C build install

[CapnpCMakeSubprojectInstallFails.zip](https://github.com/mesonbuild/meson/files/4292873/CapnpCMakeSubprojectInstallFails.zip)


**Expected behavior**

`ninja -C build install` should install correctly the dependencies.

**Actual output**

The installation step fails, I suspect because it is trying to install a dependency that has not been compiled but has been identified as a target when CMake configuration is done. 
It seems that version 0.53.0 lazily compiles only needed dependencies for cmake subprojects? I saw some generated meson.build from an AST and CMake server running to gather this information. I guess this was not what version 0.52.0 of Meson did.


**system parameters**

Tested in Ubuntu 19.10

* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)? Native

* what operating system -> Ubuntu 19.10, Windows 10


* what Python version are you using e.g. 3.8.0 -> pre-packaged python3 version (no access at this moment)
* what `meson --version` 0.53.2 (in fact, this bug does not happen in 0.52.0)

* what `ninja --version` if it's a Ninja build -> the one packaged with Ubuntu 19.10





Discussion:
Actually this is a general meson bug/problem. It is the combination of `install: true` and `build_by_default: false` that causes meson install to fail. You can reproduce this with a simple example:

```meson
project('test_project', 'c')

executable('exe1', 'main.c', build_by_default: false, install: true)
```

Resolving patch:
diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py
--- a/mesonbuild/cmake/interpreter.py
+++ b/mesonbuild/cmake/interpreter.py
@@ -1124,7 +1124,7 @@ def process_target(tgt: ConverterTarget):
 
             # Generate target kwargs
             tgt_kwargs = {
-                'build_by_default': False,
+                'build_by_default': tgt.install,
                 'link_args': tgt.link_flags + tgt.link_libraries,
                 'link_with': link_with,
                 'include_directories': id_node(inc_var),


Test patch:
diff --git a/test cases/cmake/3 advanced no dep/subprojects/cmMod/CMakeLists.txt b/test cases/cmake/3 advanced no dep/subprojects/cmMod/CMakeLists.txt
--- a/test cases/cmake/3 advanced no dep/subprojects/cmMod/CMakeLists.txt	
+++ b/test cases/cmake/3 advanced no dep/subprojects/cmMod/CMakeLists.txt	
@@ -16,9 +16,11 @@ generate_export_header(cmModLib)
 set_target_properties(cmModLib PROPERTIES VERSION 1.0.1)
 
 add_executable(testEXE main.cpp)
+add_executable(testEXE2 main.cpp)
 
 target_link_libraries(testEXE cmModLib)
+target_link_libraries(testEXE2 cmModLib)
 
 target_compile_definitions(cmModLibStatic PUBLIC CMMODLIB_STATIC_DEFINE)
 
-install(TARGETS cmModLib testEXE LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
+install(TARGETS cmModLib testEXE testEXE2 LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
diff --git a/test cases/cmake/3 advanced no dep/test.json b/test cases/cmake/3 advanced no dep/test.json
--- a/test cases/cmake/3 advanced no dep/test.json	
+++ b/test cases/cmake/3 advanced no dep/test.json	
@@ -5,6 +5,8 @@
     {"type": "implib", "platform": "!cygwin", "file": "usr/bin/libcm_cmModLib"},
     {"type": "pdb", "file": "usr/bin/cm_cmModLib"},
     {"type": "pdb", "file": "usr/bin/cm_testEXE"},
-    {"type": "exe", "file": "usr/bin/cm_testEXE"}
+    {"type": "exe", "file": "usr/bin/cm_testEXE"},
+    {"type": "pdb", "file": "usr/bin/cm_testEXE2"},
+    {"type": "exe", "file": "usr/bin/cm_testEXE2"}
   ]
 }