Repository: mesonbuild/meson

Issue:
ninja scan-build ignores buildoptions
I've tried to use scan-build on a project which uses some options and it seems like ninja scan-build ignores set options altogether. See attached example.

Running meson yields:

```
# meson .. -Dnecessary_opt="foo"
The Meson build system
Version: 0.36.0
Source dir: /home/czyba/meson3
Build dir: /home/czyba/meson3/bin
Build type: native build
Project name: a
Native c compiler: cc (gcc 4.9.3)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Build targets in project: 1
```

And ninja works fine as well, but as soon as I try to run ninja scan-build following issue occurs:

```
# ninja scan-build
[1/1] '/usr/lib/python-exec/python3.4/python3...thon-exec/python3.4/python3' '/usr/bin/meson'
scan-build: Using '/usr/bin/clang-3.9' for static analysis
The Meson build system
Version: 0.36.0
Source dir: /home/czyba/meson3
Build dir: /home/czyba/meson3/bin/meson-private/tmpuc72p4y9
Build type: native build
Project name: a
Native c compiler: /usr/bin/../libexec/ccc-analyzer (gcc 4.9.3)
Build machine cpu family: x86_64
Build machine cpu: x86_64

Meson encountered an error in file meson.build, line 6, column 2:
Error encountered: invalid is invalid value for necessary_opt
scan-build: Removing directory '/tmp/scan-build-2016-12-08-175410-3760-1' because it contains no reports.
scan-build: No bugs found.
FAILED: '/usr/lib/python-exec/python3.4/python3' '/usr/bin/meson' '--internal' 'scanbuild' '/home/czyba/meson3' '/home/czyba/meson3/bin' '/usr/lib/python-exec/python3.4/python3' '/usr/bin/meson'
ninja: build stopped: subcommand failed.
```

Sure, enforcing an option to be set may not be an elegant way of using meson, but I do believe that it shouldn't ignore options. Especially if they may affect the build. Or is there some way to circumvent this issue? Another question is why ninja scan-build tries to rebuild the file. From my understanding it shouldn't need to, or am I wrong on that account?

[scan-build_broken.zip](https://github.com/mesonbuild/meson/files/640053/scan-build_broken.zip)



Discussion:
This is due to the way scan-build works. It needs to do a full rebuild of the entire source with its own build directory. It can't be retrofitted inside an existing tree. For simplicity we just build with the default settings. It is highly recommended that you set up your project so that it can be built without setting any special command line arguments.
Ok, I shall do that. 

Resolving patch:
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -2132,11 +2132,17 @@ def generate_gcov_clean(self, outfile):
             gcda_elem.add_item('description', 'Deleting gcda files')
             gcda_elem.write(outfile)
 
+    def get_user_option_args(self):
+        cmds = []
+        for (k, v) in self.environment.coredata.user_options.items():
+            cmds.append('-D' + k + '=' + (v.value if isinstance(v.value, str) else str(v.value).lower()))
+        return cmds
+
     # For things like scan-build and other helper tools we might have.
     def generate_utils(self, outfile):
         cmd = [sys.executable, self.environment.get_build_command(),
                '--internal', 'scanbuild', self.environment.source_dir, self.environment.build_dir,
-               sys.executable, self.environment.get_build_command()]
+               sys.executable, self.environment.get_build_command()] + self.get_user_option_args()
         elem = NinjaBuildElement(self.all_outputs, 'scan-build', 'CUSTOM_COMMAND', 'PHONY')
         elem.add_item('COMMAND', cmd)
         elem.add_item('pool', 'console')


Test patch: