Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

[Foxy] - colcon build C++ with Visual Studio 2022 - change CMake version #440

Open
anion0278 opened this issue Feb 7, 2023 · 6 comments
Assignees
Labels
bug Something isn't working Documentation enhancement New feature or request Foxy

Comments

@anion0278
Copy link

anion0278 commented Feb 7, 2023

We are setting up ROS2 in our labs, with Visual Studio 2022 already installed. I try to build Cpp examples with colcon, however, it seems like the version of Cmake that is provided along with Foxy does not yet know the VS2022, so the build fails. I know that according to the official docs, only VS up to 2019 are supported, however in our case it would be beneficial to avoid installing another VS.
ROS2 Foxy was installed from binaries with Chocolately according to: https://ms-iot.github.io/ROSOnWindows/GettingStarted/SetupRos2.html
Could please suggest any option (colcon argument or env variable) to change the Cmake.exe path during the colcon build?

My steps:
The initial error pointed me to the C:\opt\ros\foxy\x64\Lib\site-packages\colcon_cmake\task\cmake\build.py script:

…
--- stderr: cpp_pubsub
Traceback (most recent call last):
  File "c:\opt\ros\foxy\x64\lib\site-packages\colcon_core\executor\__init__.py", line 91, in __call__
    rc = await self.task(*args, **kwargs)
  File "c:\opt\ros\foxy\x64\lib\site-packages\colcon_core\task\__init__.py", line 93, in __call__
    return await task_method(*args, **kwargs)
  File "c:\opt\ros\foxy\x64\lib\site-packages\colcon_ros\task\ament_cmake\build.py", line 59, in build
    rc = await extension.build(
  File "c:\opt\ros\foxy\x64\lib\site-packages\colcon_cmake\task\cmake\build.py", line 87, in build
    rc = await self._reconfigure(args, env)
  File "c:\opt\ros\foxy\x64\lib\site-packages\colcon_cmake\task\cmake\build.py", line 163, in _reconfigure
    raise RuntimeError(
RuntimeError: Unknown / unsupported VS version '17.0'
…

Which was easily "fixed" by appending '17.0': 'Visual Studio 17 2022' to the dictionary:

supported_vsv = {
                '17.0': 'Visual Studio 17 2022',
                '16.0': 'Visual Studio 16 2019',
                '15.0': 'Visual Studio 15 2017',
                '14.0': 'Visual Studio 14 2015',
            }

A new attempt to build now suggests that the Cmake provided with the Foxy does not recognize the VS 2022 C++ compiler:

…
--- stderr: cpp_pubsub
CMake Error: Could not create named generator Visual Studio 17 2022

Generators
  Visual Studio 16 2019        = Generates Visual Studio 2019 project files.
                                 Use -A option to specify architecture.
  Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Visual Studio 9 2008 [arch]  = Generates Visual Studio 2008 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Borland Makefiles            = Generates Borland makefiles.
…

By checking the version C:\opt\ros\foxy\x64\Scripts\cmake.exe --version I could see that the default vesion of CMake is 3.18.2. I hoped, that by installing a newer version of Cmake (3.23.3) and by prepending the PATH env var with its corresponding path (C:\Program Files\CMake\bin) would be sufficient to force Colcon to utilize the different version of Cmake. But it wasn't :) .

Could please suggest any option (colcon argument or env variable) to change the Cmake.exe path during the colcon build?

@anion0278
Copy link
Author

anion0278 commented Feb 7, 2023

I guess I have found a solution, though it was so close that I should have checked it before :/ .
Looking at C:/opt/ros/foxy/x64/Lib/site-packages/colcon_cmake/task/cmake/__init__.py pointed me in the right direction.

"""Environment variable to override the CMake executable"""
CMAKE_COMMAND_ENVIRONMENT_VARIABLE = EnvironmentVariable(
    'CMAKE_COMMAND', 'The full path to the CMake executable')

So by setting env var CMAKE_COMMAND I could change the path to CMake executable:

set CMAKE_COMMAND=C:/Program Files/CMake/bin/cmake.exe

colcon now succesfully builds my Cpp packages.
At least I hope this information might be of use to someone else :) .
(I guess its partially related to #356 )

I wonder if it is possible to update the build configuration to incorporate a newer version of CMake into Foxy distribution?

@gavanderhoorn
Copy link
Contributor

gavanderhoorn commented Feb 7, 2023

CMAKE_COMMAND is also mentioned in the colcon usage output:

colcon --help
usage: colcon [-h] [--log-base LOG_BASE] [--log-level LOG_LEVEL]
              {build,extension-points,extensions,graph,info,list,metadata,mixin,test,test-result,version-check} ...

options:
...

colcon verbs:
...

Environment variables:
  CMAKE_COMMAND         The full path to the CMake executable
  ...

The documentation suggests placing the directory with your preferred CMake binary on the %PATH% before any other should also work.

@anion0278
Copy link
Author

🤦 I should have read this more carefully :D . thanks!
However, I wonder if it is possible to update the build configuration to incorporate a newer version of CMake into Foxy distribution so that it works out of the box for everyone?

@ooeygui
Copy link
Member

ooeygui commented Feb 7, 2023

@anion0278 ROS on Windows has a toolchain affinity, instead of ROS on Linux which has a distro affinity. Our binaries are compiled with the default toolchain for Visual Studio 2019, so it is required to be installed to build nodes for Noetic and Foxy. We can't change the toolchain for foxy; otherwise we would break existing deployments.

With Humble, we are moving to VS2022. Both can be installed in parallel.

@ooeygui ooeygui closed this as completed Feb 7, 2023
@ooeygui ooeygui reopened this Feb 7, 2023
@ooeygui
Copy link
Member

ooeygui commented Feb 7, 2023

Wrong button. Leaving open for consideration since installing VS2019 is getting difficult.

@ooeygui ooeygui self-assigned this Feb 7, 2023
@ooeygui ooeygui added bug Something isn't working enhancement New feature or request Foxy Documentation labels Feb 7, 2023
@ooeygui ooeygui changed the title ROS2 Foxy - colcon build C++ with Visual Studio 2022 - change CMake version [Foxy] - colcon build C++ with Visual Studio 2022 - change CMake version Mar 9, 2023
@anion0278
Copy link
Author

anion0278 commented Jun 8, 2023

I'd like to mention that if anyone intends to utilize the VS 22 compiler with a newer version of CMake, it's crucial to select versions prior to 3.24. Specifically, I would recommend using version 3.23.3 (refer to the related issue: dusty-nv/jetson-containers#181 (comment)). The newer versions of CMake introduce ament-related issues when building Turtlebot3 from source (Failed to find exported target names). Alternatively, it seems to be possible to apply a fix for ament: moveit/moveit2#1570 (comment).
To avoid confusion, I have edited CMake version in my initial message.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working Documentation enhancement New feature or request Foxy
Projects
None yet
Development

No branches or pull requests

3 participants