title: CMake: How to specify a different link script for each target
body: <p>I have multiple targets that are being made in a CMake project.  Each target has a different linkscript (LD file).  How do I write the CMakeLists.txt file to make this happen?  This is for an embedded project with C/C++/ASM files.</p>
  
  <p>This is what I have so far.  The problem is that LINKER_SCRIPT is defined globally and not per-target.</p>
  
  <pre><code># add alpha target
  add_executable(alpha.elf ${SOURCES})
  target_compile_definitions(alpha.elf PUBLIC -DALPHA_DEFINED)
  set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Linker/alpha.ld)
  
  # add beta target
  add_executable(beta.elf ${SOURCES})
  target_compile_definitions(beta.elf PUBLIC -DBETA_DEFINED)
  set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Linker/beta.ld)
  
  set(CMAKE_EXE_LINKER_FLAGS
      "${CMAKE_EXE_LINKER_FLAGS} -T ${LINKER_SCRIPT}")
  </code></pre>
  
  <p>The problem is that LINKER_SCRIPT gets overwritten and the last definition is the one used.  How can I make this work?</p>
  
  <p>I have tried to define per-target variable using the following, however the output is not as expected.  The file gets compiled, however things are not where they should be.  For example, the generated HEX file does not start at 0x08000000 which is where it should be defined per the LD file.</p>
  
  <pre><code>set_target_properties(alpha.elf PROPERTIES
          CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_BINARY_DIR}/alpha.elf.map -T ${CMAKE_SOURCE_DIR}/Linker/alpha.ld")
  </code></pre>