[build] Move sw/device-specific definitions to sw/device/meson.build.

Signed-off-by: Miguel Young de la Sota <mcyoung@google.com>
diff --git a/sw/device/boot_rom/meson.build b/sw/device/boot_rom/meson.build
index e053cef..49af33d 100644
--- a/sw/device/boot_rom/meson.build
+++ b/sw/device/boot_rom/meson.build
@@ -17,10 +17,11 @@
 
 custom_target(
   'boot_rom',
-  output: embedded_target_output,
+  command: make_embedded_target,
+  output: make_embedded_target_outputs,
   input: executable(
     'boot_rom',
-    [
+    sources: [
       chip_info_h,
       'boot_rom.c',
       'bootstrap.c',
@@ -39,6 +40,4 @@
       sw_lib_log,
     ],
   ),
-  command: embedded_target_args,
-  build_by_default: true,
 )
diff --git a/sw/device/examples/hello_usbdev/meson.build b/sw/device/examples/hello_usbdev/meson.build
index 7482181..c770c0b 100644
--- a/sw/device/examples/hello_usbdev/meson.build
+++ b/sw/device/examples/hello_usbdev/meson.build
@@ -4,20 +4,18 @@
 
 custom_target(
   'hello_usbdev',
-  output: embedded_target_output,
+  command: make_embedded_target,
+  output: make_embedded_target_outputs,
   input: executable(
     'hello_usbdev',
-    ['hello_usbdev.c', startup_files],
+    sources: ['hello_usbdev.c'],
     name_suffix: 'elf',
-    link_args: riscv_link_args,
-    link_depends: riscv_link_deps,
     dependencies: [
       sw_lib_gpio,
       sw_lib_irq,
       sw_lib_uart,
       sw_lib_usb,
+      riscv_crt,
     ],
   ),
-  command: embedded_target_args,
-  build_by_default: true,
 )
diff --git a/sw/device/examples/hello_world/meson.build b/sw/device/examples/hello_world/meson.build
index 2e62097..33b3b38 100644
--- a/sw/device/examples/hello_world/meson.build
+++ b/sw/device/examples/hello_world/meson.build
@@ -3,22 +3,20 @@
 # SPDX-License-Identifier: Apache-2.0
 
 custom_target(
-  'hello_world',
-  output: embedded_target_output,
+  'hello_world',  
+  command: make_embedded_target,
+  output: make_embedded_target_outputs,
   input: executable(
     'hello_world',
-    ['hello_world.c', startup_files],
+    sources: ['hello_world.c',],
     name_suffix: 'elf',
-    link_args: riscv_link_args,
-    link_depends: riscv_link_deps,
     dependencies: [
       sw_lib_pinmux,
       sw_lib_gpio,
       sw_lib_irq,
       sw_lib_spi_device,
       sw_lib_uart,
+      riscv_crt,
     ],
   ),
-  command: embedded_target_args,
-  build_by_default: true,
 )
diff --git a/sw/device/exts/common/meson.build b/sw/device/exts/common/meson.build
new file mode 100644
index 0000000..f91214d
--- /dev/null
+++ b/sw/device/exts/common/meson.build
@@ -0,0 +1,23 @@
+# Copyright lowRISC contributors.
+# Licensed under the Apache License, Version 2.0, see LICENSE for details.
+# SPDX-License-Identifier: Apache-2.0
+
+# RISC-V linker parameters.
+riscv_linker_script = '@0@/@1@'.format(meson.source_root(), files(['link.ld'])[0])
+riscv_linker_args = [
+  '-Wl,-T,@0@'.format(riscv_linker_script),
+  # Ensure we don't include a build-id attribute in the ELF.
+  '-Wl,--build-id=none',
+]
+
+# RISC-V startup library. Every RISC-V executable should depend on this target.
+riscv_crt = declare_dependency(
+  link_args: riscv_linker_args,
+  link_with: static_library(
+    'riscv_crt',
+    sources: ['_crt.c'],
+    # NOTE: This line is mostly to make sure that a change in link.ld causes a
+    # recompilation.
+    link_depends: [riscv_linker_script],
+  ),
+)
diff --git a/sw/device/exts/meson.build b/sw/device/exts/meson.build
new file mode 100644
index 0000000..e0a1ebe
--- /dev/null
+++ b/sw/device/exts/meson.build
@@ -0,0 +1,5 @@
+# Copyright lowRISC contributors.
+# Licensed under the Apache License, Version 2.0, see LICENSE for details.
+# SPDX-License-Identifier: Apache-2.0
+
+subdir('common')
diff --git a/sw/device/meson.build b/sw/device/meson.build
index 052b2db..5516a8a 100644
--- a/sw/device/meson.build
+++ b/sw/device/meson.build
@@ -3,6 +3,22 @@
 # SPDX-License-Identifier: Apache-2.0
 
 subdir('lib')
+subdir('exts')
+
+# Arguments for custom_target, for converting a linked .elf file into .bin and .vmem
+# files (plus a disassembled .dis file).
+#
+# These definitions should only be available to directories which define executables.
+make_embedded_target_outputs = ['@BASENAME@.bin', '@BASENAME@.dis', '@BASENAME@.vmem']
+make_embedded_target = [
+  prog_python, meson.source_root() + '/util/embedded_target.py',
+  '--objcopy', prog_objcopy,
+  '--srec_cat', prog_srec_cat,
+  '--objdump', prog_objdump,
+  '--input', '@INPUT@',
+  '--basename', '@BASENAME@',
+  '--outdir', '@OUTDIR@',
+]
 
 subdir('boot_rom')
 subdir('examples')
diff --git a/sw/device/tests/flash_ctrl/meson.build b/sw/device/tests/flash_ctrl/meson.build
index 01191bc..6b3fe90 100644
--- a/sw/device/tests/flash_ctrl/meson.build
+++ b/sw/device/tests/flash_ctrl/meson.build
@@ -4,20 +4,18 @@
 
 custom_target(
   'flash_test',
-  output: embedded_target_output,
+  command: make_embedded_target,
+  output: make_embedded_target_outputs,
   input: executable(
     'flash_test',
-    ['flash_test.c', startup_files],
+    sources: ['flash_test.c'],
     name_suffix: 'elf',
-    link_args: riscv_link_args,
-    link_depends: riscv_link_deps,
     dependencies: [
       sw_lib_flash_ctrl,
       sw_lib_gpio,
       sw_lib_irq,
       sw_lib_uart,
+      riscv_crt,
     ],
   ),
-  command: embedded_target_args,
-  build_by_default: true,
 )
diff --git a/sw/device/tests/hmac/meson.build b/sw/device/tests/hmac/meson.build
index 81f009b..3428292 100644
--- a/sw/device/tests/hmac/meson.build
+++ b/sw/device/tests/hmac/meson.build
@@ -4,20 +4,18 @@
 
 custom_target(
   'sha256_test',
-  output: embedded_target_output,
+  command: make_embedded_target,
+  output: make_embedded_target_outputs,
   input: executable(
     'sha256_test',
-    ['sha256_test.c', startup_files],
+    sources: ['sha256_test.c'],
     name_suffix: 'elf',
-    link_args: riscv_link_args,
-    link_depends: riscv_link_deps,
     dependencies: [
       sw_lib_flash_ctrl,
       sw_lib_hmac,
       sw_lib_irq,
       sw_lib_uart,
+      riscv_crt,
     ],
   ),
-  command: embedded_target_args,
-  build_by_default: true,
 )
diff --git a/sw/device/tests/rv_timer/meson.build b/sw/device/tests/rv_timer/meson.build
index 74a85ed..0026172 100644
--- a/sw/device/tests/rv_timer/meson.build
+++ b/sw/device/tests/rv_timer/meson.build
@@ -4,19 +4,17 @@
 
 custom_target(
   'rv_timer_test',
-  output: embedded_target_output,
+  command: make_embedded_target,
+  output: make_embedded_target_outputs,
   input: executable(
     'rv_timer_test',
-    ['rv_timer_test.c', startup_files],
+    sources: ['rv_timer_test.c'],
     name_suffix: 'elf',
-    link_args: riscv_link_args,
-    link_depends: riscv_link_deps,
     dependencies: [
       sw_lib_irq,
       sw_lib_rv_timer,
       sw_lib_uart,
+      riscv_crt,
     ],
   ),
-  command: embedded_target_args,
-  build_by_default: true,
 )