3

I am trying to use CMAKE to run the Arduino compiler in Windows via CMAKE. I created a little .cmake file that can locate and override a few toolchain variables such as CMAKE_C_COMPILER. This works great on Linux. Everything is getting found and built. In Linux, I am using the UNIX Makefiles generator.

However, when I come over to Windows, I have problems. I would like to have both the NMake and Visual Studio generators working. If I had to pick one, I will go with least complex (NMake)

  • The NMake generator insists on adding command line switches for cl.exe (/nologo, /DWIN32, etc) These create errors and stop the build.

  • The NMake generator does use the path to the arduino compiler. (good)

  • The Visual Studio generator insists on using cl.exe (bad) and it puts into the command line switches for cl.exe (bad)

How do I stop it from adding the cl.exe command line switches?

The code looks something like this:

set(TOOLCHAIN_ROOT "C:\Program Files (x86)\Arduino\hardware\tools\arm")

if(WIN32)
    set(TOOL_OS_SUFFIX .exe)
else(WIN32)
    set(TOOL_OS_SUFFIX )
endif(WIN32)

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_CROSSCOMPILING 1)

set(CMAKE_C_COMPILER "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-gcc${TOOL_OS_SUFFIX}")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-g++${TOOL_OS_SUFFIX}")
set(CMAKE_AR "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-ar${TOOL_OS_SUFFIX}")
set(CMAKE_LINKER "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-ld${TOOL_OS_SUFFIX}")
set(CMAKE_NM "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-nm${TOOL_OS_SUFFIX}")
set(CMAKE_OBJCOPY "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-objcopy${TOOL_OS_SUFFIX}")
set(CMAKE_OBJDUMP "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-objdump${TOOL_OS_SUFFIX}")
set(CMAKE_STRIP "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-strip${TOOL_OS_SUFFIX}")
set(CMAKE_RANLIB "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-ranlib${TOOL_OS_SUFFIX}")

Here is what the output looks like:

Scanning dependencies of target MyProject
[  2%] Building C object CMakeFiles/MyProject.dir/main.c.obj
arm-none-eabi-gcc.exe: error: /nologo: No such file or directory
arm-none-eabi-gcc.exe: error: /DWIN32: No such file or directory
arm-none-eabi-gcc.exe: error: /D_WINDOWS: No such file or directory
arm-none-eabi-gcc.exe: error: /W3: No such file or directory
arm-none-eabi-gcc.exe: error: /D_DEBUG: No such file or directory
arm-none-eabi-gcc.exe: error: /MDd: No such file or directory
arm-none-eabi-gcc.exe: error: /Zi: No such file or directory
arm-none-eabi-gcc.exe: error: /Ob0: No such file or directory
arm-none-eabi-gcc.exe: error: /Od: No such file or directory
arm-none-eabi-gcc.exe: error: /RTC1: No such file or directory
arm-none-eabi-gcc.exe: error: /FoCMakeFilesMyProject.dirmain.c.obj: No such file or directory
arm-none-eabi-gcc.exe: error: /FdCMakeFilesMyProject.dir -c: No such
file or directory
arm-none-eabi-gcc.exe: error: D:projectsexamplecode.c: No such file or directory
arm-none-eabi-gcc.exe: fatal error: no input files
compilation terminated.
101010
  • 14,866
  • 30
  • 95
  • 172
  • How does `cmake` with Visual Studio generator **indentify** your compiler? Usually, when set compiler manually, you need to set its identificator too, via [CMAKE_C_COMPILER_ID](https://cmake.org/cmake/help/v3.0/variable/CMAKE_LANG_COMPILER_ID.html) variable. – Tsyvarev Mar 08 '17 at 18:54
  • Also, variable `CMAKE_CROSSCOMPILING` is set by CMake **automatically**, no need to set it manually. – Tsyvarev Mar 08 '17 at 18:59
  • You can't just change the compiler for Visual Studio. It would need an additional plug-in or toolset installed (which would need CMake awareness or needs to be supported by CMake). The just released Visual Studio 2017 does have some build-in support for Linux systems and there were already [some queries](https://cmake.org/pipermail/cmake/2016-October/064369.html) to support those project types by CMake, but I don't think that [there is something implemented yet](https://github.com/Kitware/CMake/blob/master/Source/cmVisualStudio10TargetGenerator.cxx#L3505). – Florian Mar 08 '17 at 21:12

1 Answers1

0

Turning my comment into an answer

I've installed Arduino Software (IDE) for Windows PCs version 1.8.1 and have given it a try with my Visual Studio 2015 Community Edition installation.

Here is my test code (but maybe you just search on the Internet for some ready to use toolchains):

toolchain.txt

set(TOOLCHAIN_ROOT "C:/Program Files (x86)/Arduino/hardware/tools/avr")

if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
    set(TOOL_OS_SUFFIX ".exe")
else()
    set(TOOL_OS_SUFFIX "")
endif()

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR avr)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(TRIPLE avr)
set(CMAKE_C_COMPILER "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-gcc${TOOL_OS_SUFFIX}")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_ROOT}/bin/${TRIPLE}-g++${TOOL_OS_SUFFIX}")

Since the download hasn't come with an arm compiler toolchain, I used avr (which should be replaceable). And you don't have to give all the tools, CMake will find them based on the naming scheme of your compilers.

And - as @Tsyvarev commented - variable CMAKE_CROSSCOMPILING is set by CMake automatically, no need to set it manually.


Here is what you can/can't do for Arduino with an out-of-the-box Visual Studio:

  1. For nmake I was running on the cmd prompt:

    > "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"
    > mkdir NMake
    > cd NMake
    > NMake>cmake -G "NMake Makefiles" -DCMAKE_TOOLCHAIN_FILE:PATH=..\toolchain.txt ..
    -- The C compiler identification is GNU 4.9.2
    -- The CXX compiler identification is GNU 4.9.2
    -- Check for working C compiler: C:/Program Files (x86)/Arduino/hardware/tools/avr/bin/avr-gcc.exe
    -- Check for working C compiler: C:/Program Files (x86)/Arduino/hardware/tools/avr/bin/avr-gcc.exe -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: C:/Program Files (x86)/Arduino/hardware/tools/avr/bin/avr-g++.exe
    -- Check for working CXX compiler: C:/Program Files (x86)/Arduino/hardware/tools/avr/bin/avr-g++.exe -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    
  2. For Visual Studio you can't just change the compiler. It would need an additional plug-in or toolset installed (which would need CMake awareness or needs to be supported by CMake). The just released Visual Studio 2017 does have some build-in support for Linux systems, but I don't think that there is something implemented yet.

References

Florian
  • 39,996
  • 9
  • 133
  • 149