Newer
Older
mbed-os / tools / export / cmake / CMakeLists.txt.tmpl
# This file was automagically generated by mbed.org.
# If you would like to add your own targets, create a
# project.cmake file locally in your project directory.

CMAKE_MINIMUM_REQUIRED(VERSION 3.9)
SET(CMAKE_SYSTEM_NAME Generic)
#SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_CROSSCOMPILING TRUE)

# force compiler settings
SET(CMAKE_C_COMPILER_WORKS TRUE)
SET(CMAKE_CXX_COMPILER_WORKS TRUE)

# force cmake compilers
SET(CMAKE_ASM_COMPILER    "{{asm}}")
SET(CMAKE_C_COMPILER      "{{cc}}")
SET(CMAKE_CXX_COMPILER    "{{cxx}}")
SET(ELF2BIN               "{{elf2bin}}")
{% if hex_files %}
SET(SREC_CAT "srec_cat")
{%- endif %}

# if the environment does not specify build type, set to Debug
IF(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Debug"
        CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
        FORCE)
ENDIF()

# here starts the project
PROJECT({{name}} C CXX ASM)

# uncomment below to have a verbose build process
#SET(CMAKE_VERBOSE_MAKEFILE ON)

SET(LD_SYS_LIBS "{%- block sys_libs -%} -Wl,--start-group {{ld_sys_libs|join(" ")}} {{libraries|join(" ")}} -Wl,--end-group {%- endblock -%}")

SET(CMAKE_C_FLAGS "{{cc_flags}} -include mbed_config.h")
SET(CMAKE_CXX_FLAGS "{{cxx_flags}}  -include mbed_config.h")
SET(CMAKE_ASM_FLAGS "{{asm_flags}}  -include mbed_config.h")
SET(CMAKE_CXX_LINK_FLAGS "{{ld_flags}} {% for p in library_paths %} {{user_library_flag}}${CMAKE_CURRENT_SOURCE_DIR}/{{p}} {% endfor %}")
{% if pp -%}
SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} ${LD_SYS_LIBS} {{link_script_option}} ${CMAKE_BINARY_DIR}/{{name}}_pp.link_script.ld")
{%- endif %}

ADD_DEFINITIONS(
  {% for d in symbols %}-D{{d}}
  {% endfor %})
INCLUDE_DIRECTORIES(
  {% for p in include_paths %}{{p}}
  {% endfor %})

# executable {{name}}
ADD_EXECUTABLE({{name}}
  {% for src in sources %}{{src}}
  {% endfor %})
SET_TARGET_PROPERTIES({{name}} PROPERTIES ENABLE_EXPORTS 1)
# add syslibs dependencies to create the correct linker order
TARGET_LINK_LIBRARIES({{name}} {{ld_sys_libs|join(" ")}})

{% if pp -%}
add_custom_command(TARGET {{name}} PRE_LINK
                   COMMAND "{{pp}}" {{pp_flags}} {{linker_script}} -o ${CMAKE_CURRENT_BINARY_DIR}/{{name}}_pp.link_script.ld
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                   BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/{{name}}_pp.link_script.ld"
                   )
{%- endif %}

add_custom_command(TARGET {{name}} POST_BUILD
                   COMMAND ${ELF2BIN} -O ihex $<TARGET_FILE:{{name}}> $<TARGET_FILE:{{name}}>.hex
                   COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:{{name}}>.hex"
                  )

{% if hex_files %}
add_custom_command(TARGET {{name}} POST_BUILD
                   COMMAND ${SREC_CAT}
                        {% for f in hex_files %}${CMAKE_CURRENT_SOURCE_DIR}/{{f}} {% endfor %}
                        -intel $<TARGET_FILE:{{name}}>.hex
                        -intel -o $<TARGET_FILE:{{name}}>-combined.hex -intel --line-length=44
                   COMMAND ${CMAKE_COMMAND} -E echo "-- built: $<TARGET_FILE:{{name}}>-combined.hex"
                   )
{% endif %}


##########################################################################
# mbed-cli specific targets
##########################################################################

# detect the build type and select the corresponding cli profile
SET(MBED_BUILD_PROFILE "")
STRING(TOLOWER ${CMAKE_BUILD_TYPE} LOWERCASE_CMAKE_BUILD_TYPE)
IF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES debug)
    SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/debug.json")
ELSEIF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES relwithdebinfo)
    SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/develop.json")
ELSEIF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES release)
    SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/release.json")
ELSEIF(LOWERCASE_CMAKE_BUILD_TYPE MATCHES minsizerel)
    SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/release.json")
ELSE()
  MESSAGE(WARNING "Build type '${CMAKE_BUILD_TYPE}' is unknown, using debug profile")
  SET(MBED_BUILD_PROFILE "mbed-os/tools/profiles/debug.json")
ENDIF()

# optional custom target to build via mbed-cli
ADD_CUSTOM_TARGET(mbed-cli-build
        COMMAND ${CMAKE_COMMAND} -E echo "mbed compile --build BUILD/${CMAKE_BUILD_TYPE} --profile ${MBED_BUILD_PROFILE}"
        COMMAND mbed compile --build BUILD/${CMAKE_BUILD_TYPE} --profile ${MBED_BUILD_PROFILE}
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        SOURCES ${SOURCE_FILES} ${SYS_SOURCE_FILES})

IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/project.cmake)
  INCLUDE(${CMAKE_CURRENT_SOURCE_DIR}/project.cmake)
ELSE()
  MESSAGE(STATUS "Add a local project.cmake file to add your own targets.")
ENDIF()

# this will take care of binary directories generated by cmake/clion not to confuse the cli build
FILE(GENERATE OUTPUT "${CMAKE_BINARY_DIR}/.mbedignore" CONTENT "*")