Build: Default to add debug symbols with the flag -g/--debug

Currently the flag "-g" (--debug on IAR) to add debug symbols to the
elf file is opt-in, instead of opt-out.

This is quite annoying when developing as one often has to rebuild to
be able to debug.

This flag has no affect on the .hex or .bin file so there are no
security concerns with adding debug symbols to the .elf file.

As I see it, if you are concerned about leaking symbol information you
will not distribute the .elf file and only distribute the .bin and
.hex file.

If you are not concerned about leaking symbol information then it is
very useful and harmless to add debug symbols with -g.

NB: The -g flag does not affect the FLASH/RAM usage.

The reason we can not use RelWithDebInfo is that it does more than add
the -g flag so you don't get the same binary and risk not reproducing
the issue after changing the build type.

We add this flag by introducing the opt-out CMake variable
TFM_DEBUG_SYMBOLS. This variable has conflicting semantics with
TFM_CODE_COVERAGE so we change TFM_CODE_COVERAGE to no longer add the
-g flag and we require that if TFM_CODE_COVERAGE is enabled, then
TFM_DEBUG_SYMBOLS must also be enabled.

This is a breaking change to users of TFM_CODE_COVERAGE, but there is
no clean way of introducing TFM_DEBUG_SYMBOLS without breaking
TFM_CODE_COVERAGE.

Change-Id: I0a412426c40717e086c9bc4330cbb27a5cc810bd
Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
diff --git a/config/config_default.cmake b/config/config_default.cmake
index 055f0b2..b4d1e6b 100755
--- a/config/config_default.cmake
+++ b/config/config_default.cmake
@@ -42,7 +42,8 @@
 
 set(TFM_INSTALL_PATH                    ${CMAKE_BINARY_DIR}/install CACHE PATH "Path to which to install TF-M files")
 
-set(TFM_CODE_COVERAGE                   OFF         CACHE BOOL      "Whether to build the binary for lcov tools by adding -g")
+set(TFM_DEBUG_SYMBOLS                   ON          CACHE BOOL      "Add debug symbols. Note that setting CMAKE_BUILD_TYPE to Debug or RelWithDebInfo will also add debug symbols.")
+set(TFM_CODE_COVERAGE                   OFF         CACHE BOOL      "Whether to build the binary for lcov tools")
 
 set(TFM_SP_META_PTR_ENABLE              OFF         CACHE BOOL      "Use Partition Metadata Pointer")
 
diff --git a/docs/technical_references/instructions/tfm_build_instruction.rst b/docs/technical_references/instructions/tfm_build_instruction.rst
index 378b299..3000981 100644
--- a/docs/technical_references/instructions/tfm_build_instruction.rst
+++ b/docs/technical_references/instructions/tfm_build_instruction.rst
@@ -94,17 +94,20 @@
 types are:
 
  - ``Debug``
- - ``Relwithdebinfo``
+ - ``RelWithDebInfo``
  - ``Release``
- - ``Minsizerel``
+ - ``MinSizeRel``
 
 ``Release`` is default.
 
-Both ``Debug`` and ``Relwithdebinfo`` will include debug symbols in the output
-files. ``Relwithdebinfo``, ``Release`` and ``Minsizerel`` have optimization
-turned on and hence will produce smaller, faster code. ``Minsizerel`` will
-produce the smallest code, and hence is often a good idea on RAM or flash
-constrained systems.
+Debug symbols are added by default to all builds, but can be removed
+from ``Release`` and ``MinSizeRel`` builds by setting
+``TFM_DEBUG_SYMBOLS`` to ``OFF``.
+
+``RelWithDebInfo``, ``Release`` and ``MinSizeRel`` all have different
+optimizations turned on and hence will produce smaller, faster code
+than ``Debug``. ``MinSizeRel`` will produce the smallest code, and
+hence is often a good idea on RAM or flash constrained systems.
 
 Other cmake parameters
 ----------------------
diff --git a/toolchain_ARMCLANG.cmake b/toolchain_ARMCLANG.cmake
index c0e6a5c..1908bdc 100644
--- a/toolchain_ARMCLANG.cmake
+++ b/toolchain_ARMCLANG.cmake
@@ -48,6 +48,7 @@
         $<$<AND:$<COMPILE_LANGUAGE:C>,$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>>:-mfpu=none>
         $<$<AND:$<COMPILE_LANGUAGE:ASM>,$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>>:--fpu=none>
         $<$<COMPILE_LANGUAGE:ASM>:--cpu=${CMAKE_ASM_CPU_FLAG}>
+        $<$<AND:$<COMPILE_LANGUAGE:C>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-g>
     )
 endmacro()
 
diff --git a/toolchain_GNUARM.cmake b/toolchain_GNUARM.cmake
index 359f846..78705c4 100644
--- a/toolchain_GNUARM.cmake
+++ b/toolchain_GNUARM.cmake
@@ -48,7 +48,7 @@
         -mthumb
         -nostdlib
         -std=c99
-        $<$<BOOL:${TFM_CODE_COVERAGE}>:-g>
+        $<$<BOOL:${TFM_DEBUG_SYMBOLS}>:-g>
         $<$<NOT:$<BOOL:${TFM_SYSTEM_FP}>>:-msoft-float>
     )
 endmacro()
diff --git a/toolchain_IARARM.cmake b/toolchain_IARARM.cmake
index ec532f1..0984c8e 100644
--- a/toolchain_IARARM.cmake
+++ b/toolchain_IARARM.cmake
@@ -48,6 +48,8 @@
         $<$<COMPILE_LANGUAGE:C,CXX>:-DNO_TYPEOF>
         $<$<COMPILE_LANGUAGE:C,CXX>:-D_NO_DEFINITIONS_IN_HEADER_FILES>
         $<$<COMPILE_LANGUAGE:C,CXX>:--diag_suppress=Pe546,Pe940,Pa082,Pa084>
+        $<$<AND:$<COMPILE_LANGUAGE:C,CXX>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:--debug>
+        $<$<AND:$<COMPILE_LANGUAGE:ASM>,$<BOOL:${TFM_DEBUG_SYMBOLS}>>:-r>
     )
 endmacro()
 
@@ -189,4 +191,4 @@
 
 macro(compiler_link_shared_code TARGET SHARED_CODE_PATH ORIG_TARGET LIB_LIST)
     message(FATAL_ERROR "Code sharing support is not implemented by IAR.")
-endmacro()
\ No newline at end of file
+endmacro()