sanitycheck: use overlays to enable coverage

Set COVERAGE option using an overlay instead of piping it through CMAKE
which breaks dependecies, i.e. if someone wants to disable that option
on the application level.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
diff --git a/scripts/sanitycheck b/scripts/sanitycheck
index d1b3c97..479ed84 100755
--- a/scripts/sanitycheck
+++ b/scripts/sanitycheck
@@ -1016,7 +1016,6 @@
 
         if options.enable_coverage:
             args += ["EXTRA_LDFLAGS=--coverage"]
-            args += ["CONFIG_COVERAGE=y"]
 
         if type == 'qemu':
             args.append("QEMU_PIPE=%s" % handler.get_fifo())
@@ -1042,10 +1041,13 @@
             by execute() will be keyed by its .name field.
         """
         args = ti.test.extra_args[:]
-        if len(ti.test.extra_configs) > 0:
+        if len(ti.test.extra_configs) > 0 or options.coverage:
             args.append("OVERLAY_CONFIG=%s" %
                         os.path.join(ti.outdir, "overlay.conf"))
 
+        if ti.test.type == "unit" and options.enable_coverage:
+            args.append("COVERAGE=1")
+
         args.append("BOARD={}".format(ti.platform.name))
         args.extend(extra_args)
 
@@ -1555,14 +1557,18 @@
         return build_only
 
     def create_overlay(self):
+        file = os.path.join(self.outdir, "overlay.conf")
+        os.makedirs(self.outdir, exist_ok=True)
+        f = open(file, "w")
+        content = ""
+
         if len(self.test.extra_configs) > 0:
-            file = os.path.join(self.outdir, "overlay.conf")
-            os.makedirs(self.outdir, exist_ok=True)
-            f = open(file, "w")
-            content = ""
             content = "\n".join(self.test.extra_configs)
-            f.write(content)
-            f.close()
+        if options.enable_coverage:
+            content = content + "\nCONFIG_COVERAGE=y"
+
+        f.write(content)
+        f.close()
 
     def calculate_sizes(self):
         """Get the RAM/ROM sizes of a test case.
@@ -1853,9 +1859,6 @@
                         # each other since they all try to build them
                         # simultaneously
 
-                        if options.enable_coverage:
-                            args.append("CONFIG_COVERAGE=y")
-
                         o = os.path.join(self.outdir, plat.name, tc.path)
                         dlist[tc, plat, tc.name.split("/")[-1]] = os.path.join(o, "zephyr", ".config")
                         goal = "_".join([plat.name, "_".join(tc.name.split("/")), "config-sanitycheck"])
@@ -2692,7 +2695,7 @@
                          "--output-file", ztestfile,
                          "--rc", "lcov_branch_coverage=1"], stdout=coveragelog)
 
-        if os.path.getsize(ztestfile) > 0:
+        if os.path.exists(ztestfile) and os.path.getsize(ztestfile) > 0:
             subprocess.call(["lcov", "--remove", ztestfile,
                              os.path.join(ZEPHYR_BASE, "tests/ztest/test/*"),
                              "--output-file", ztestfile,