diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ad3aa19cc..ca07d18457 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,28 @@ if(POLICY CMP0076) cmake_policy(SET CMP0076 NEW) endif() +add_executable(${APP_TARGET}) + +# Create Mbed OS library +add_library(mbed-os OBJECT) + +include(${MBED_CONFIG_PATH}/mbed_config.cmake) +include(${MBED_ROOT}/cmake/toolchain.cmake) +include(${MBED_ROOT}/cmake/core.cmake) +include(${MBED_ROOT}/cmake/profile.cmake) +include(${MBED_ROOT}/cmake/util.cmake) + +set_target_properties(mbed-os PROPERTIES MBED_TARGET_LABELS "${MBED_TARGET_LABELS}") +target_compile_definitions(mbed-os PUBLIC ${MBED_TARGET_DEFINITIONS}) +target_compile_definitions(mbed-os PUBLIC ${MBED_CONFIG_DEFINITIONS}) + +# Specify a default build type +if(NOT CMAKE_BUILD_TYPE) +set(CMAKE_BUILD_TYPE "RelWithDebInfo" + CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." + FORCE) +endif() + # Include mbed.h and config from generate folder target_include_directories(mbed-os PUBLIC .) @@ -25,3 +47,69 @@ add_subdirectory(platform) add_subdirectory(rtos) add_subdirectory(storage) add_subdirectory(targets) + +# I have to leave this here as linker is processed after mbed-os added, and can't be in toolchain.cmake +# as its global symbol is empty at that stage, this needs more work +# TODO: This property + pre/post should be moved +get_property(mbed_target_startup GLOBAL PROPERTY MBED_TARGET_LINKER_FILE) + +# TODO: @mbed-os-tools These pre/post build commands should get details from target + profile. +# I have to leave this here as linker is processed after mbed-os added, and can't be in toolchain.cmake +# as its global symbol is empty at that stage, this needs more work. +# Link the Mbed target linker file +if(MBED_TOOLCHAIN STREQUAL "GCC_ARM") + set(CMAKE_PRE_BUILD_COMMAND + COMMAND "arm-none-eabi-cpp" -E -P + -Wl,--gc-sections -Wl,--wrap,main -Wl,--wrap,_malloc_r -Wl,--wrap,_free_r + -Wl,--wrap,_realloc_r -Wl,--wrap,_memalign_r -Wl,--wrap,_calloc_r + -Wl,--wrap,exit -Wl,--wrap,atexit -Wl,-n + -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=softfp + -DMBED_ROM_START=0x0 -DMBED_ROM_SIZE=0x100000 -DMBED_RAM_START=0x20000000 + -DMBED_RAM_SIZE=0x30000 -DMBED_RAM1_START=0x1fff0000 + -DMBED_RAM1_SIZE=0x10000 -DMBED_BOOT_STACK_SIZE=1024 + -DXIP_ENABLE=0 + ${mbed_target_startup} -o ${CMAKE_BINARY_DIR}/${APP_TARGET}.link_script.ld + + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + BYPRODUCTS "${CMAKE_BINARY_DIR}/${APP_TARGET}.link_script.ld" + ) + # Generate binary and hex file + set(CMAKE_POST_BUILD_COMMAND + COMMAND ${ELF2BIN} -O binary $ $.bin + COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.bin" + COMMAND ${ELF2BIN} -O ihex $ $.hex + COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.hex" + ) +elseif(MBED_TOOLCHAIN STREQUAL "ARM") + set(CMAKE_PRE_BUILD_COMMAND COMMAND "") + target_link_options(mbed-os + PUBLIC + "--scatter=${mbed_target_startup}" + ) + # Generate binary and hex file + set(CMAKE_POST_BUILD_COMMAND + COMMAND ${ELF2BIN} ${MBED_STUDIO_ARM_COMPILER} --bin -o $.bin $ + COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.bin" + COMMAND ${ELF2BIN} ${MBED_STUDIO_ARM_COMPILER} --i32combined -o $.hex $ + COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.hex" + ) +endif() + +add_custom_command( + TARGET + ${APP_TARGET} + PRE_LINK + ${CMAKE_PRE_BUILD_COMMAND} + COMMENT + "link line:" + VERBATIM +) +add_custom_command( + TARGET + ${APP_TARGET} + POST_BUILD + ${CMAKE_POST_BUILD_COMMAND} + COMMENT + "executable:" + VERBATIM +) diff --git a/cmake/app.cmake b/cmake/app.cmake deleted file mode 100644 index add76736c9..0000000000 --- a/cmake/app.cmake +++ /dev/null @@ -1,89 +0,0 @@ -# Copyright (c) 2020 ARM Limited. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -# Create Mbed OS library -add_library(mbed-os OBJECT) - -# Create application executable -add_executable(app) - -include(.mbedbuild/mbed_config.cmake) -include(${MBED_ROOT}/cmake/toolchain.cmake) -include(${MBED_ROOT}/cmake/core.cmake) -include(${MBED_ROOT}/cmake/profile.cmake) -include(${MBED_ROOT}/cmake/util.cmake) - -set_target_properties(mbed-os PROPERTIES MBED_TARGET_LABELS "${MBED_TARGET_LABELS}") -target_compile_definitions(mbed-os PUBLIC ${MBED_TARGET_DEFINITIONS}) -target_compile_definitions(mbed-os PUBLIC ${MBED_CONFIG_DEFINITIONS}) - -# Specify a default build type -if(NOT CMAKE_BUILD_TYPE) -set(CMAKE_BUILD_TYPE "RelWithDebInfo" - CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." - FORCE) -endif() - -# Include Mbed OS main cmake -add_subdirectory(mbed-os) - -# Link the example libs -target_link_libraries(app mbed-os) - -# I have to leave this here as linker is processed after mbed-os added, and can't be in toolchain.cmake -# as its global symbol is empty at that stage, this needs more work -# TODO: This property + pre/post should be moved -get_property(linkerfile GLOBAL PROPERTY MBED_TARGET_LINKER_FILE) - -# TODO: get project name to inject into ld -# TODO: @mbed-os-tools this pre/post build commands should get details from target + profile -if(MBED_TOOLCHAIN STREQUAL "GCC_ARM") - # I have to leave this here as linker is processed after mbed-os added, and can't be in toolchain.cmake - # as its global symbol is empty at that stage, this needs more work - # TODO: This property pre/post should be moved - set(CMAKE_PRE_BUILD_COMMAND - COMMAND "arm-none-eabi-cpp" -E -P - -Wl,--gc-sections -Wl,--wrap,main -Wl,--wrap,_malloc_r -Wl,--wrap,_free_r - -Wl,--wrap,_realloc_r -Wl,--wrap,_memalign_r -Wl,--wrap,_calloc_r - -Wl,--wrap,exit -Wl,--wrap,atexit -Wl,-n - -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=softfp - -DMBED_ROM_START=0x0 -DMBED_ROM_SIZE=0x100000 -DMBED_RAM_START=0x20000000 - -DMBED_RAM_SIZE=0x30000 -DMBED_RAM1_START=0x1fff0000 - -DMBED_RAM1_SIZE=0x10000 -DMBED_BOOT_STACK_SIZE=1024 - -DXIP_ENABLE=0 - ${linkerfile} -o ${CMAKE_CURRENT_BINARY_DIR}/app.link_script.ld - - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/app.link_script.ld" - ) -elseif(MBED_TOOLCHAIN STREQUAL "ARM") - set(CMAKE_PRE_BUILD_COMMAND COMMAND "") - - target_link_options(mbed-os - PUBLIC - "--scatter=${linkerfile}" - ) -endif() - -# TODO: @mbed-os-tools this pre/post build commands should get details from target + profile -if(MBED_TOOLCHAIN STREQUAL "GCC_ARM") - set(CMAKE_POST_BUILD_COMMAND - COMMAND ${ELF2BIN} -O binary $ $.bin - COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.bin" - COMMAND ${ELF2BIN} -O ihex $ $.hex - COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.hex" - ) -elseif(MBED_TOOLCHAIN STREQUAL "ARM") - set(CMAKE_POST_BUILD_COMMAND - COMMAND ${ELF2BIN} ${MBED_STUDIO_ARM_COMPILER} --bin -o $.bin $ - COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.bin" - COMMAND ${ELF2BIN} ${MBED_STUDIO_ARM_COMPILER} --i32combined -o $.hex $ - COMMAND ${CMAKE_COMMAND} -E echo "-- built: $.hex" - ) -endif() - - - -# Custom pre/post build steps -add_custom_command(TARGET app PRE_LINK ${CMAKE_PRE_BUILD_COMMAND}) -add_custom_command(TARGET app POST_BUILD ${CMAKE_POST_BUILD_COMMAND}) diff --git a/cmake/toolchains/GCC_ARM.cmake b/cmake/toolchains/GCC_ARM.cmake index 48fbc6bbe8..63127cd660 100644 --- a/cmake/toolchains/GCC_ARM.cmake +++ b/cmake/toolchains/GCC_ARM.cmake @@ -16,7 +16,7 @@ list(APPEND link_options "-lnosys" "-Wl,--end-group" "-T" - "${CMAKE_BINARY_DIR}/app.link_script.ld" + "${CMAKE_BINARY_DIR}/${APP_TARGET}.link_script.ld" ) list(APPEND common_options