iceman1357
iceman1357

Reputation: 11

How to generate Map file using cmake

I am trying to cross-compile, host is wondows target is stm32(arm). I am trying to generate a map file from the arm-none-eabi-ld. My issues are twofold

  1. I am unable to generate a Map file. This is my primary problem.
  2. When I run build clean my .hex file & .bin files are not deleted.

Please find my toolchain cmake file below(arm-none-eabi.cmake).

MESSAGE("Running : arm-none-eabi.cmake")

SET(CMAKE_SYSTEM_PROCESSOR  arm)
SET(CMAKE_SYSTEM_NAME       Generic)

SET(CMAKE_C_COMPILER_WORKS  TRUE)
SET(CMAKE_CXX_COMPILER_WORKS  TRUE)

SET(CMAKE_TRY_COMPILE_TARGTE_TYPE   STATIC_LIBRARY)

SET(TARGET      STM32F030x4)
SET(ARCH        armv6-m)
SET(CPU         cortex-m0)
SET(ARM_ISA     mthumb)

SET(CMAKE_C_COMPILER    arm-none-eabi-gcc)
SET(CMAKE_CXX_COMPILER  arm-none-eabi-g++)
SET(CMAKE_ASM_COMPILER  arm-none-eabi-g++)
SET(CMAKE_SIZE          arm-none-eabi-size)
SET(CMAKE_OBJDUMP       arm-none-eabi-objdump)
SET(CMAKE_OBJCOPY       arm-none-eabi-objcopy)

SET(OPTIMISATION Og)
SET(DEBUG "ggdb")

SET(CMAKE_COMMON_FLAGS  "-march=${ARCH} -mcpu=${CPU} -${ARM_ISA} -D${TARGET} -${OPTIMISATION} -${DEBUG} -Wall -Wextra -ffunction-sections -fdata-sections -nostdlib -ffreestanding -fno-builtin --specs=nosys.specs -lc --entry main")
SET(CMAKE_ASM_FLAGS     "${CMAKE_COMMON_FLAGS}")
SET(CMAKE_C_FLAGS       "${CMAKE_COMMON_FLAGS} ") #-std=gnull
SET(CMAKE_CXX_FLAGS     "${CMAKE_COMMON_FLAGS} -fno-rtti -fno-exceptions -Wno-volatile -std=c++1z")
SET(CMAKE_LINKER_FLAGS  "${CMAKE_COMMON_FLAGS} -nostartfiles -Wl,-Map,\"${TARGET}.map\"  --specs-nosys.specs")

-Wl,-Map,"${TARGET}.map" command is not generating a map file.

My CmakeLists.txt file

CMAKE_MINIMUM_REQUIRED(VERSION 3.20)

INCLUDE("CMake/arm-none-eabi.cmake")

SET(CMAKE_C_STANDARD    11)
SET(CMAKE_CXX_STANDARD  17)



PROJECT(BLINKY VERSION 1.0.1 DESCRIPTION "Blinky Example")
MESSAGE("Building "${PROJECT_NAME})

FILE(GLOB_RECURSE
    LDSCRIPTS
    "ldscripts/*.ld"
)

FOREACH(file ${LDSCRIPTS})
    SET(CMAKE_LINKER_FLAGS "${CAMKE_LINKER_FLAGS} -T \"${file}\" ")
ENDFOREACH()

#Setup project headers
INCLUDE_DIRECTORIES(
    "Core/"
    "Drivers/"
)

#Setup porject sources
FILE(GLOB_RECURSE
    APPLICATION_SOURCE
    "Application/*.c"
    "Application/*.cpp"
    "Application/*.s"
    "Core/*.c"
    "Core/*cpp"
    "Core/*.s"
    "Drivers/*.c"
    "Drivers/*.cpp"
    "Drivers/*.s"
)

ADD_EXECUTABLE(${TARGET}.elf ${APPLICATION_SOURCE})
ADD_CUSTOM_TARGET(${TARGET}.bin ALL DEPENDS ${TARGET}.elf COMMAND ${CMAKE_OBJCOPY} -O binary ${TARGET}.elf ${TARGET}.bin)
ADD_CUSTOM_TARGET(${TARGET}.hex ALL DEPENDS ${TARGET}.elf COMMAND ${CMAKE_OBJCOPY} -O ihex ${TARGET}.elf ${TARGET}.hex)
ADD_CUSTOM_TARGET(SIZE ALL ${CMAKE_SIZE} ${TARGET}.elf DEPENDS ${TARGET}.elf)

Upvotes: 1

Views: 7039

Answers (2)

Lance E.T. Compte
Lance E.T. Compte

Reputation: 1209

For the other question, you should do something like this...

add_executable(foo ...)

target_link_options(foo
  PUBLIC
    LINKER:-Map=foo.map
)

This is discussed in the section Handling Compiler Driver Differences here.

Upvotes: 1

mydisplayname
mydisplayname

Reputation: 356

(I believe general guidance is one issue/question per posted question)

answer for:

When I run build clean my .hex file & .bin files are not deleted.

Per the docs for add_custom_target():

The target has no output file

Having no output file means that CMake doesn't know what it produces. What you probably want to do is use add_custom_command(), and then make the custom target depend on the custom command:

ADD_CUSTOM_COMMAND(OUTPUT ${TARGET}.bin DEPENDS ${TARGET}.elf COMMAND ${CMAKE_OBJCOPY} -O binary ${TARGET}.elf ${TARGET}.bin)
ADD_CUSTOM_COMMAND(OUTPUT ${TARGET}.hex DEPENDS ${TARGET}.elf COMMAND ${CMAKE_OBJCOPY} -O ihex ${TARGET}.elf ${TARGET}.hex)
ADD_CUSTOM_TARGET(${TARGET}_bin DEPENDS ${TARGET}.bin)
ADD_CUSTOM_TARGET(${TARGET}_hex DEPENDS ${TARGET}.hex)

I'm not sure if the ${TARGET}.bin would collide, but I modified it to reduce ambiguity for future maintainers.

Upvotes: 0

Related Questions