Skip to content

CMake: Allow providing a custom toolchain file #14446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
boraozgen opened this issue Mar 18, 2021 · 13 comments
Open

CMake: Allow providing a custom toolchain file #14446

boraozgen opened this issue Mar 18, 2021 · 13 comments

Comments

@boraozgen
Copy link
Contributor

Previously mentioned here: ARMmbed/mbed-tools#153

The following lines force CMake to use the toolchain from PATH.

set(CMAKE_ASM_COMPILER "arm-none-eabi-gcc")
set(CMAKE_C_COMPILER "arm-none-eabi-gcc")
set(CMAKE_CXX_COMPILER "arm-none-eabi-g++")
set(GCC_ELF2BIN "arm-none-eabi-objcopy")

This should instead be provided by the user (IDEs like VSCode and CLion do this). Otherwise it leads to configuration issues when using multiple toolchains in the same project. I have such a use case, where I build another target to run my GUI library on the host.

Is there a case against removing these lines? GCC_ELF2BIN could then be derived from the provided compiler location.

@ciarmcom
Copy link
Member

@boraozgen This issue has an incomplete or old issue template.For future reference please use an up to date clone of the repository before raising issues. Many thanks.

@ciarmcom
Copy link
Member

Thank you for raising this detailed GitHub issue. I am now notifying our internal issue triagers.
Internal Jira reference: https://jira.arm.com/browse/IOTOSM-3676

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 19, 2021

We can use find program if not set. A user should supply this via -D to provide own paths.

This should instead be provided by the user (IDEs like VSCode and CLion do this). Otherwise it leads to configuration issues when using multiple toolchains in the same project. I have such a use case, where I build another target to run my GUI library on the host.

Is it expected to do -DCMAKE_ASM_COMPILER=path ?

@boraozgen
Copy link
Contributor Author

boraozgen commented Mar 19, 2021

I don't understand your proposition. Let me clarify my point.

In my opinion, CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables should be set by the IDE or by the command line with -D, instead of by Mbed.

What do you mean by find program? Do you mean the find_program function of CMake? What is the purpose of using it?

PS: Another use case that occured to me: You use multiple toolchain versions and want to build the same project with them. Adding the toolchain to PATH isn't an option for such an development environment.

@ladislas
Copy link
Contributor

The user can also provide those in their main CMakeLists.txt. If not provided, a warning could be emitted and the default could be set. This way it works as before but lets user customize those.

I'm not sure about the find_program either for something as important as the compiler. For ccache or cppcheck and other tools that are not mandatory, it's okay, but you must provide a compiler for this to work.

I have such a use case, where I build another target to run my GUI library on the host.

@boraozgen can you tell me more about that? what IDE do you use? How did you set it up? With VSCode and CMake Tools, it's not possible to have two toolchains live side by side for the same project :(

@boraozgen
Copy link
Contributor Author

boraozgen commented Mar 19, 2021

I use vscode + cmake-tools. cmake-tools has the option to change the toolchains (called kits in the tool). It currently does not work with Mbed as it overrides this variable. You can PM me in the forums for further discussion.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 19, 2021

In my opinion, CMAKE_C_COMPILER and CMAKE_CXX_COMPILER variables should be set by the IDE or by the command line with -D, instead of by Mbed.

👍 I've got what I was looking for. We will triage and fix. it should be provided via -D, not being in CMakelists.txt

@boraozgen
Copy link
Contributor Author

boraozgen commented Mar 19, 2021

This also might apply to other toolchain related stuff like compiler options. How about a possibility for the user to supply an optional toolchain file, and using the current GCC_ARM file by default?

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 22, 2021

This also might apply to other toolchain related stuff like compiler options. How about a possibility for the user to supply an optional toolchain file, and using the current GCC_ARM file by default?

That is the idea. if CMAKE_TOOLCHAIN_FILE is not provided, we supply the default Mbed one.

@boraozgen
Copy link
Contributor Author

boraozgen commented Mar 22, 2021

Exactly. Apparently that conditional is already there:

# Load toolchain file
if(NOT CMAKE_TOOLCHAIN_FILE OR MBED_TOOLCHAIN_FILE_USED)
set(MBED_TOOLCHAIN_FILE_USED TRUE CACHE INTERNAL "")
include(${MBED_PATH}/tools/cmake/toolchain.cmake)
endif()

There are two issues with the current state:

  1. toolchain.cmake uses variables like MBED_PATH and MBED_TOOLCHAIN, which are not defined when toolchain files are processed. Therefore it is not possible to provide the default toolchain.cmake as CMAKE_TOOLCHAIN_FILE. In short, toolchain.cmake is not a valid toolchain file.
  2. toolchain.cmake includes both Mbed-specific and user-specific settings. These should be separated in order to avoid adding too much boilerplate code to the user-specified toolchain file.

@boraozgen boraozgen changed the title CMake: Mbed is forcing toolchain from PATH CMake: Allow providing a custom toolchain file Mar 22, 2021
@0xc0170
Copy link
Contributor

0xc0170 commented Mar 22, 2021

#14385 - MBED_PATH removal issue

Can you create an issue for 2nd?

@boraozgen
Copy link
Contributor Author

I edited the issue title to reflect the updated info. #14385 refers to only MBED_PATH but there are other constructs in toolchain.cmake which don't work as a real toolchain file. I think the goal here is to be able to provide a minimal custom toolchain file without the need to copy all of the functions and Mbed related settings etc. from toolchain.cmake. We should define the exact requirements, such as compiler flags, etc. I am not very experienced with toolchain files so maybe someone could help us out here.

@0xc0170
Copy link
Contributor

0xc0170 commented Mar 22, 2021

OK, I'll link here the proposal from Rob: ARMmbed/mbed-tools#225 (this should be tracking issue for section "Improve toolchain handling"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants