Repository: conan-io/conan
Pull request: Fix for #16882: Generated CMake build when using tools.build.linker_scripts
The generated build otherwise appends the "-Tfoo.ld" argument directly without a preceding space before which can generate an invalid command line.

Changelog: Bugfix: Avoid ``CMakeToolchain`` error when both architecture flags and ``tools.build:linker_scripts`` are defined, due to missing space.
Docs: Omit

Close #16882

- [X] Refer to the issue that supports this Pull Request.
- [ ] If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
- [x] I've read the [Contributing guide](https://github.com/conan-io/conan/blob/develop2/.github/CONTRIBUTING.md).
- [x] I've followed the PEP8 style guides for Python code.
- [ ] I've opened another PR in the Conan docs repo to the ``develop`` branch, documenting this one.


Problem statement:
[bug] Specifying `tools.build.linker_scripts` in my profile results in an invalid command line when linking.
### Describe the bug

I'm using Fedora 40, with conan from the develop2 branch.

While attempting to move my linker_script setup out of cmake and into my conan profile I encountered the error that the way in which the linker command was appended doesn't put a space between the previous linker arguments (if any).

### How to reproduce it

If you create a new cmake_lib and use a profile such as:

```ini
[settings]
os=baremetal
arch=tc162
compiler=gcc
compiler.version=11
compiler.cppstd=20
compiler.libcxx=libstdc++11

[conf]
tools.build:compiler_executables={"c":"tricore-elf-gcc","cpp":"tricore-elf-g++"}
tools.build:linker_scripts=[ "${PROJECT_SOURCE_DIR}/Lcf_Gnuc_Tricore_Tc.ld" ]
tools.cmake.cmake_layout:build_folder=build/tc162
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config

# I have to do this, otherwise zlib tries to use fPIC which isn't supported by tricore.
[options]
*:shared=False
*:fPIC=False
```

Then you'll get an error during the build similar to this:

```
tricore-elf-g++: error: unrecognized argument in option ‘-mtc162-T/home/chipc/.conan2/p/b/mypkg6fa17d873b59e/b/Lcf_Gnuc_Tricore_Tc.ld’
    tricore-elf-g++: note: valid arguments to ‘-mtc’ are: 13 131 16 161 162 18
```

Hints from issue discussion:

Commit message: Avoids the warning regarding arguments not being separated by space.

```
CMake Warning (dev) at /home/chipc/.conan2/p/b/mypkgbc25b469ad9ce/b/build/generators/conan_toolchain.cmake:54:
  Syntax Warning in cmake code at column 43

  Argument not separated from preceding token by whitespace.
```

Patch to review:
diff --git a/conan/tools/cmake/toolchain/blocks.py b/conan/tools/cmake/toolchain/blocks.py
index fffa48aa273..2c7ee1ed0ae 100644
--- a/conan/tools/cmake/toolchain/blocks.py
+++ b/conan/tools/cmake/toolchain/blocks.py
@@ -254,7 +254,7 @@ class LinkerScriptsBlock(Block):
         # Add linker flags from tools.build:linker_scripts conf
 
         message(STATUS "Conan toolchain: Defining linker script flag: {{ linker_script_flags }}")
-        string(APPEND CONAN_EXE_LINKER_FLAGS {{ linker_script_flags }})
+        string(APPEND CONAN_EXE_LINKER_FLAGS " {{ linker_script_flags }}")
         """)
 
     def context(self):
@@ -265,7 +265,7 @@ def context(self):
         linker_scripts = [linker_script.replace('\\', '/') for linker_script in linker_scripts]
         linker_scripts = [relativize_path(p, self._conanfile, "${CMAKE_CURRENT_LIST_DIR}")
                           for p in linker_scripts]
-        linker_script_flags = ['-T"' + linker_script + '"' for linker_script in linker_scripts]
+        linker_script_flags = ['-T' + linker_script for linker_script in linker_scripts]
         return {"linker_script_flags": " ".join(linker_script_flags)}
 
 


Code review comments:
diff_hunk: @@ -265,7 +265,7 @@ def context(self):
         linker_scripts = [linker_script.replace('\\', '/') for linker_script in linker_scripts]
         linker_scripts = [relativize_path(p, self._conanfile, "${CMAKE_CURRENT_LIST_DIR}")
                           for p in linker_scripts]
-        linker_script_flags = ['-T"' + linker_script + '"' for linker_script in linker_scripts]
+        linker_script_flags = ['-T' + linker_script for linker_script in linker_scripts]
path: conan/tools/cmake/toolchain/blocks.py
text: @user1:
Are you sure these quotes are to be removed? They can be very necessary when there are path containing spaces, can you please try to keep them?

@author:
It was causing cmake warnings previously. But you’re right, there is perhaps another approach to fix those. 

@author:
You already found the fix apparently :D