cmake: Fix dependencies for LD script generation

The dependencies relating to generating LD scripts are declared
incorrectly. This manifests itself as broken incremental builds as
shown in #13001.

This commit aligns the LD script generation with the Zephyr standard
for declaring custom commands.

For instance, custom commands should generate files that are wrapped
by targets. The custom commands should declare the dependencies, not
the targets.

Also, when using custom commands, the OUTPUT signature should be used,
not the TARGET signature, as app_smem was doing.

For details about how Zephyr uses custom commands see
https://samthursfield.wordpress.com/2015/11/21/cmake-dependencies-between-targets-and-files-and-custom-commands/

This fixes #13001.

Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7919b88..61e83ba 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -753,6 +753,11 @@
 construct_add_custom_command_for_linker_pass(
   linker
   custom_command
+  ${ALIGN_SIZING_DEP}
+  ${PRIV_STACK_DEP}
+  ${APP_SMEM_DEP}
+  ${CODE_RELOCATION_DEP}
+  ${OFFSETS_H_TARGET}
   )
 add_custom_command(
   ${custom_command}
@@ -761,11 +766,7 @@
 add_custom_target(
   ${LINKER_SCRIPT_TARGET}
   DEPENDS
-  ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
-  ${APP_SMEM_DEP}
-  ${CODE_RELOCATION_DEP}
   linker.cmd
-  ${OFFSETS_H_TARGET}
   )
 
 # Give the '${LINKER_SCRIPT_TARGET}' target all of the include directories so
@@ -1132,22 +1133,25 @@
   set(OBJ_FILE_DIR "${PROJECT_BINARY_DIR}/../")
 
   add_custom_target(
-    ${APP_SMEM_DEP} ALL
-    DEPENDS app
-    kernel ${ZEPHYR_LIBS_PROPERTY}
+    ${APP_SMEM_DEP}
+    DEPENDS
+    ${APP_SMEM_LD}
     )
 
   if(CONFIG_NEWLIB_LIBC)
     set(NEWLIB_PART -l libc.a z_newlib_partition)
   endif()
   add_custom_command(
-    TARGET ${APP_SMEM_DEP}
+    OUTPUT ${APP_SMEM_LD}
     COMMAND ${PYTHON_EXECUTABLE}
     ${ZEPHYR_BASE}/scripts/gen_app_partitions.py
     -d ${OBJ_FILE_DIR}
     -o ${APP_SMEM_LD}
     ${NEWLIB_PART}
     $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
+    DEPENDS
+    kernel
+    ${ZEPHYR_LIBS_PROPERTY}
     WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/
     COMMENT "Generating app_smem linker section"
     )
@@ -1157,6 +1161,11 @@
   construct_add_custom_command_for_linker_pass(
     linker_priv_stacks
     custom_command
+    ${ALIGN_SIZING_DEP}
+    ${CODE_RELOCATION_DEP}
+    ${APP_SMEM_DEP}
+    ${APP_SMEM_LD}
+    ${OFFSETS_H_TARGET}
     )
   add_custom_command(
     ${custom_command}
@@ -1165,10 +1174,7 @@
   add_custom_target(
     linker_priv_stacks_script
     DEPENDS
-    ${ALIGN_SIZING_DEP} ${APP_SMEM_DEP}
-    ${CODE_RELOCATION_DEP}
     linker_priv_stacks.cmd
-    ${OFFSETS_H_TARGET}
     )
 
   set_property(TARGET
@@ -1201,6 +1207,11 @@
   construct_add_custom_command_for_linker_pass(
     linker_pass_final
     custom_command
+    ${ALIGN_SIZING_DEP}
+    ${PRIV_STACK_DEP}
+    ${CODE_RELOCATION_DEP}
+    ${ZEPHYR_PREBUILT_EXECUTABLE}
+    ${OFFSETS_H_TARGET}
     )
   add_custom_command(
     ${custom_command}
@@ -1210,11 +1221,7 @@
   add_custom_target(
     ${LINKER_PASS_FINAL_SCRIPT_TARGET}
     DEPENDS
-    ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
-    ${CODE_RELOCATION_DEP}
-    ${ZEPHYR_PREBUILT_EXECUTABLE}
     linker_pass_final.cmd
-    ${OFFSETS_H_TARGET}
     )
   set_property(TARGET
     ${LINKER_PASS_FINAL_SCRIPT_TARGET}