Refactor crt targets.

Instead of implicitly include the assembly code and gloss implementation
in `kelvin_binary`, create targets in `crt/` so binaries can include the
proper dependency explicitly.

Change-Id: I80aec18ca48d2ee9d735165f83939aee47ad44a3
diff --git a/build_tools/bazel/kelvin.bzl b/build_tools/bazel/kelvin.bzl
index 000d985..5b93729 100644
--- a/build_tools/bazel/kelvin.bzl
+++ b/build_tools/bazel/kelvin.bzl
@@ -145,7 +145,6 @@
 def kelvin_binary(
         name,
         srcs,
-        is_riscv_test = False,
         tags = [],
         **kwargs):
     """A helper macro for generating binary artifacts for the kelvin core.
@@ -156,8 +155,7 @@
     Args:
       name: The name of this rule.
       srcs: The c source files.
-      is_riscv_test: A bool flag to decide if a custom _start asm is included.
-        It is used by riscv-tests.
+      tags: build tags.
       **kwargs: Additional arguments forward to cc_binary.
     Emits rules:
       filegroup              named: <name>.bin
@@ -165,17 +163,11 @@
       filegroup              named: <name>.elf
         Containing all elf output for the target.
     """
-    srcs.append("//crt:kelvin_gloss.cc")
-    if not is_riscv_test:
-        srcs += [
-            "//crt:crt.S",
-            "//crt:kelvin_start.S",
-        ]
 
     kelvin_binary_impl(
         name = name,
         srcs = srcs,
-        linker_script = "//crt:kelvin.ld",
+        linker_script = "@kelvin_sw//crt:kelvin_linker",
         tags = tags,
         **kwargs
     )
diff --git a/crt/BUILD b/crt/BUILD
index acb27d3..8689baf 100644
--- a/crt/BUILD
+++ b/crt/BUILD
@@ -1,12 +1,34 @@
 package(default_visibility = ["//visibility:public"])
 
-# Exports the kelvin files to be used by the kelvin_binary()
-exports_files([
-    "crt.S",
-    "kelvin.ld",
-    "kelvin_gloss.cc",
-    "kelvin_start.S",
-])
+filegroup(
+    name = "kelvin_linker",
+    srcs = ["kelvin.ld"],
+)
+
+cc_library(
+    name = "crt",
+    srcs = [
+        "crt.S",
+        "kelvin_gloss.cc",
+        "kelvin_start.S",
+    ],
+    target_compatible_with = ["@kelvin_sw//platforms/cpu:kelvin"],
+    deps = [
+        ":crt_header",
+    ],
+)
+
+# Used by binaries with their own starting asm.
+cc_library(
+    name = "crt_gloss_only",
+    srcs = [
+        "kelvin_gloss.cc",
+    ],
+    target_compatible_with = ["@kelvin_sw//platforms/cpu:kelvin"],
+    deps = [
+        ":crt_header",
+    ],
+)
 
 cc_library(
     name = "crt_header",
diff --git a/examples/hello_world/BUILD b/examples/hello_world/BUILD
index 5e7afc3..2e67534 100644
--- a/examples/hello_world/BUILD
+++ b/examples/hello_world/BUILD
@@ -6,6 +6,6 @@
         "hello_world.c",
     ],
     deps = [
-        "//crt:crt_header",
+        "//crt",
     ]
 )
diff --git a/examples/tflm/person_detection/BUILD b/examples/tflm/person_detection/BUILD
index 4dcbb9a..3228be0 100644
--- a/examples/tflm/person_detection/BUILD
+++ b/examples/tflm/person_detection/BUILD
@@ -13,7 +13,7 @@
         "person_detect_tflite.h",
     ],
     deps = [
-        "//crt:crt_header",
+        "//crt:crt",
         "@tflite-micro//tensorflow/lite/micro:micro_framework",
         "@tflite-micro//tensorflow/lite/micro:system_setup",
     ],
diff --git a/examples/tflm/soundstream/BUILD b/examples/tflm/soundstream/BUILD
index 2cb2d2d..56917ee 100644
--- a/examples/tflm/soundstream/BUILD
+++ b/examples/tflm/soundstream/BUILD
@@ -35,7 +35,7 @@
     tags = ["manual"],
     deps = [
         ":soundstream",
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
@@ -48,7 +48,7 @@
     tags = ["manual"],
     deps = [
         ":soundstream",
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
@@ -60,7 +60,7 @@
     tags = ["manual"],
     deps = [
         ":soundstream",
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
@@ -73,7 +73,7 @@
     tags = ["manual"],
     deps = [
         ":soundstream",
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
@@ -93,7 +93,7 @@
     tags = ["manual"],
     deps = [
         ":soundstream",
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
@@ -114,7 +114,7 @@
     tags = ["manual"],
     deps = [
         ":soundstream",
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
diff --git a/tests/cv/BUILD b/tests/cv/BUILD
index 5b8652e..b9e5832 100644
--- a/tests/cv/BUILD
+++ b/tests/cv/BUILD
@@ -10,6 +10,9 @@
     hdrs = [
         "test_helper.h",
     ],
+    deps = [
+        "//crt",
+    ],
 )
 
 kelvin_test(
@@ -23,7 +26,6 @@
     ],
     deps = [
         ":test_helper",
-        "//crt:crt_header",
     ],
 )
 
@@ -39,7 +41,6 @@
     hw_test_size = "small",
     deps = [
         ":test_helper",
-        "//crt:crt_header",
     ],
 )
 
@@ -54,7 +55,6 @@
     ],
     deps = [
         ":test_helper",
-        "//crt:crt_header",
     ],
 )
 
@@ -69,7 +69,6 @@
     ],
     deps = [
         ":test_helper",
-        "//crt:crt_header",
     ],
 )
 
@@ -84,7 +83,6 @@
     ],
     deps = [
         ":test_helper",
-        "//crt:crt_header",
     ],
 )
 
@@ -100,6 +98,5 @@
     hw_test_size = "small",
     deps = [
         ":test_helper",
-        "//crt:crt_header",
     ],
 )
diff --git a/tests/kelvin_isa/BUILD b/tests/kelvin_isa/BUILD
index 115b286..25930b4 100644
--- a/tests/kelvin_isa/BUILD
+++ b/tests/kelvin_isa/BUILD
@@ -6,7 +6,7 @@
         "kelvin_test.h",
     ],
     deps = [
-        "//crt:crt_header",
+        "//crt:crt",
     ],
 )
 
diff --git a/tests/riscv-tests/BUILD b/tests/riscv-tests/BUILD
index aa188bd..2139115 100644
--- a/tests/riscv-tests/BUILD
+++ b/tests/riscv-tests/BUILD
@@ -11,8 +11,10 @@
         "riscv_test.h",
         "@riscv-tests//:isa/macros/scalar/test_macros.h",
     ],
+    target_compatible_with = ["@kelvin_sw//platforms/cpu:kelvin"],
     deps = [
-        "//crt:crt_header",
+        # only include the header since riscv tests have their own start.S.
+        "//crt:crt_gloss_only",
     ],
 )
 
@@ -75,7 +77,6 @@
         "RVTEST_RV64U=RVTEST_RV32U",
     ],
     hw_test_size = "small",
-    is_riscv_test = True,
     deps = [
         ":riscv_tests_base",
     ],
@@ -103,7 +104,6 @@
         "-Wno-variadic-macros",
     ],
     hw_test_size = "small",
-    is_riscv_test = True,
     deps = [
         ":riscv_tests_base",
     ],
@@ -116,7 +116,7 @@
     ],
     hw_test_size = "small",
     deps = [
-        "//crt:crt_header",
+        "//crt",
     ],
 )
 
@@ -127,6 +127,6 @@
     ],
     hw_test_size = "small",
     deps = [
-        "//crt:crt_header",
+        "//crt",
     ],
 )
diff --git a/tests/tflm/BUILD b/tests/tflm/BUILD
index c8320f4..e58ac93 100644
--- a/tests/tflm/BUILD
+++ b/tests/tflm/BUILD
@@ -9,7 +9,7 @@
     ],
     hw_test_size = "large",
     deps = [
-        "//crt:crt_header",
+        "//crt",
         "@tflite-micro//tensorflow/lite/c:common",
         "@tflite-micro//tensorflow/lite/kernels/internal:tensor",
         "@tflite-micro//tensorflow/lite/micro:micro_utils",
@@ -25,7 +25,7 @@
         "@tflite-micro//tensorflow/lite/micro/kernels:leaky_relu_test.cc",
     ],
     deps = [
-        "//crt:crt_header",
+        "//crt",
         "@tflite-micro//tensorflow/lite/c:common",
         "@tflite-micro//tensorflow/lite/kernels/internal:tensor",
         "@tflite-micro//tensorflow/lite/micro:micro_utils",
@@ -42,7 +42,7 @@
     ],
     hw_test_size = "large",
     deps = [
-        "//crt:crt_header",
+        "//crt",
         "@tflite-micro//tensorflow/lite/c:common",
         "@tflite-micro//tensorflow/lite/kernels/internal:tensor",
         "@tflite-micro//tensorflow/lite/micro:micro_utils",
diff --git a/tflm/opt/BUILD b/tflm/opt/BUILD
index 8da2593..a6722df 100644
--- a/tflm/opt/BUILD
+++ b/tflm/opt/BUILD
@@ -16,7 +16,7 @@
     ],
     target_compatible_with = ["@kelvin_sw//platforms/cpu:kelvin"],
     deps = [
-        "//crt:crt_header",
+        "//crt:crt",
     ],
     alwayslink = True,
 )