[CMake] Fwd: Toolchains and CMAKE_FORCE_C_COMPILER in 3.5

Thibault Genessay tibogens at gmail.com
Wed Feb 10 09:28:43 EST 2016


[Note: this was erroneously sent off-list to Chuck only, sorry for the
noise]

Hi Chuck,

I can't really post my toolchain file for two reasons: 1) it contains
proprietary stuff and 2) I am ashamed of it.

OK, here you go. I just renamed whatever was proprietary. Please don't
shoot me, I promise my code and the other CMakeLists are better than that
:) When I read it again, I don't understand why I have implemented this
"flags reset" feature, and don't know if it actually works - but the rest
does. Anyway:

# Frobnicator 2
#
# Cross-compilation toolchain definition file using GNU Tools for ARM
Embedded Processors
# https://launchpad.net/gcc-arm-embedded
#
# This toolchain file assumes that the executable 'arm-none-eabi-gcc.exe'
is in the path.
# As you can see, it also assumes that you are running Windows. If not,
then please modify
# this file to make it smarter.
#
# It was written according to the CMake wiki page dedicated to this topic:
# http://www.vtk.org/Wiki/CMake_Cross_Compiling
# (especially important is the CMAKE_FORCE_C_COMPILER stuff, required
because
# the compiler is not able to spit out code without flags, breaking CMake's
# detection system otherwise)
#
# Copyright (c) 2015 Initrode SA

include(CMakeForceCompiler)

set(_should_set_flags FALSE)

if (NOT CMAKE_SYSTEM_NAME)

    set(CMAKE_SYSTEM_NAME Generic)
    set(CMAKE_SYSTEM_VERSION "frob2-r1")
    set(CMAKE_SYSTEM_PROCESSOR cortex-m3)

    CMAKE_FORCE_C_COMPILER(arm-none-eabi-gcc.exe GNU)
    CMAKE_FORCE_CXX_COMPILER(arm-none-eabi-g++.exe GNU)

    # To find the location of the toolchain, invoke the compiler asking for
the
    # path to libc.a. This path is then stripped off of the lib/libc.a part
- this
    # hopefully points to the root of the toolchain.
    execute_process(
        COMMAND ${CMAKE_C_COMPILER} -print-file-name=libc.a
        OUTPUT_VARIABLE _path_to_toolchain
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    # Get rid of the filename
    get_filename_component(_path_to_toolchain
        "${_path_to_toolchain}" PATH
    )
    # Get rid of the trailing lib/ (or whatever the dir is)
    get_filename_component(_path_to_toolchain
        "${_path_to_toolchain}/.." REALPATH
    )
    set(INITRODE_CROSSCOMPILE_TOOLCHAIN ${_path_to_toolchain} CACHE PATH
"Path to the cross compiler toolchain")

    set(_should_set_flags TRUE)
endif()

if (NOT _banner_already_printed)
    message(STATUS "Cross-compiling using the gcc-arm-embedded toolchain")
    message(STATUS "          Toolchain location: ${_path_to_toolchain}")
    message(STATUS "              C/C++ compiler: ${CMAKE_C_COMPILER}")
    message(STATUS "Frobnicator hardware version: ${CMAKE_SYSTEM_VERSION}")
    message(STATUS "                 MCU variant:
${CMAKE_SYSTEM_PROCESSOR}")
    set(_banner_already_printed TRUE)
endif ()

set(CMAKE_FIND_ROOT_PATH ${INITRODE_CROSSCOMPILE_TOOLCHAIN})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

set(_flags_reset_mode "")

# This should only be used on the command line (i.e. cmake
-DINITRODE_RESET_C_CXX_FLAGS=1)
# and never put in the cache, otherwise changes in the GUI would be erased
at each cmake
# run (which is annoying behaviour to say the least)
if (INITRODE_RESET_C_CXX_FLAGS)
    set(_should_set_flags TRUE)
    set(_flags_reset_mode "FORCE")
endif ()

if (_should_set_flags)

    # Generic flags
    set(_flags "-fno-common -ffunction-sections -fdata-sections -Wall")

    # CPU-specific flags
    # These could go into a separate file but since we have no other
hardware target
    # for the time being, it is kept here.
    set(_flags "${_flags} -mcpu=cortex-m3 -march=armv7-m -mthumb
-msoft-float")

    set(CMAKE_C_FLAGS "${_flags}" CACHE STRING "Flags used by the C
compiler during all build types." ${_flags_reset_mode})

    # C++ flags
    set(_cppflags "${_flags} -fno-rtti -fno-exceptions -std=c++11")
    set(CMAKE_CXX_FLAGS "${_cppflags}" CACHE STRING "Flags used by the C++
compiler during all build types." ${_flags_reset_mode})

    # Debugging flags
    set(CMAKE_C_FLAGS_DEBUG "-g -D_DEBUG" CACHE STRING "Flags used by the C
compiler during debug builds.")
    set(CMAKE_CXX_FLAGS_DEBUG "-g -D_DEBUG" CACHE STRING "Flags used by the
C++ compiler during debug builds.")
    set(CMAKE_ASM_FLAGS_DEBUG "-g -D_DEBUG" CACHE STRING "Flags used by the
assembler during debug builds.")

    # These are actually set by each target independently, until I manage
to understand
    # what is generic (i.e. specific to the toolchain) and what is
target-specific.
    ##set(CMAKE_EXE_LINKER_FLAGS "-lc -lm -lgcc" CACHE STRING "Flags used
by the linker.")
endif ()



Cheers,
Thibault
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160210/90eeb03e/attachment-0001.html>


More information about the CMake mailing list