Skip to content

Commit

Permalink
greenhills: add cmake support
Browse files Browse the repository at this point in the history
1. refactor the ghs/gcc/clang/armclang toolchain management in CMake
2. unify the cmake toolchain naming style
3. support greenhills build procedure with CMake
4. add protect build for greenhills and gnu toolchain with CMake

Signed-off-by: guoshichao <[email protected]>
  • Loading branch information
guoshichao committed Sep 28, 2024
1 parent 065046b commit f345fb4
Show file tree
Hide file tree
Showing 20 changed files with 1,216 additions and 373 deletions.
118 changes: 53 additions & 65 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/arch/${CONFIG_ARCH}/src/cmake)
set(CMAKE_TOOLCHAIN_FILE
"${CMAKE_SOURCE_DIR}/arch/${CONFIG_ARCH}/src/cmake/Toolchain.cmake")

# include common toolchain setting
include(nuttx_toolchain)

# Define project #############################################################
# This triggers configuration

Expand Down Expand Up @@ -466,15 +469,6 @@ else()
nuttx PRIVATE $<GENEX_EVAL:$<TARGET_PROPERTY:nuttx,NUTTX_COMPILE_OPTIONS>>)
endif()

# Compiler options TODO: move elsewhere

if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
# force color for gcc > 4.9
add_compile_options(-fdiagnostics-color=always)
endif()
endif()

if(MSVC)
add_compile_options(
-W2
Expand All @@ -493,21 +487,6 @@ elseif(NOT CONFIG_ARCH_TOOLCHAIN_TASKING)
$<$<COMPILE_LANGUAGE:ASM>:-D__ASSEMBLY__>)
endif()

if(NOT CONFIG_LIBCXXTOOLCHAIN)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
else()
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-D_STDLIB_H_>)
endif()

if(NOT CONFIG_CXX_EXCEPTION)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
endif()

if(CONFIG_STACK_CANARIES)
add_compile_options(-fstack-protector-all)
endif()

if(CONFIG_NDEBUG)
add_compile_options(-DNDEBUG)
endif()
Expand All @@ -530,6 +509,8 @@ endif()

add_definitions(-D__NuttX__)

add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-D__ASSEMBLY__>)

set_property(
TARGET nuttx
APPEND
Expand Down Expand Up @@ -593,16 +574,12 @@ endif()
get_property(ldscript GLOBAL PROPERTY LD_SCRIPT)

# Pre-compile linker script
if(DEFINED PREPROCESS)
if(NOT CONFIG_ARCH_SIM)
get_filename_component(LD_SCRIPT_NAME ${ldscript} NAME)
set(LD_SCRIPT_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_NAME}.tmp")

add_custom_command(
OUTPUT ${LD_SCRIPT_TMP}
DEPENDS ${ldscript}
COMMAND ${PREPROCESS} -I${CMAKE_BINARY_DIR}/include -I${NUTTX_CHIP_ABS_DIR}
${ldscript} > ${LD_SCRIPT_TMP}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
nuttx_generate_preproces_target(SOURCE_FILE ${ldscript} TARGET_FILE
${LD_SCRIPT_TMP})

add_custom_target(ldscript_tmp DEPENDS ${LD_SCRIPT_TMP})
add_dependencies(nuttx ldscript_tmp)
Expand Down Expand Up @@ -638,14 +615,18 @@ set(nuttx_libs ${nuttx_kernel_libs} ${nuttx_system_libs} ${nuttx_apps_libs}
${nuttx_extra_libs})

if(NOT CONFIG_ARCH_SIM)

# TODO: nostart/nodefault not applicable to nuttx toolchain
if(CONFIG_ARCH_TOOLCHAIN_TASKING)
target_link_libraries(nuttx PRIVATE --lsl-file=${ldscript} ${nuttx_libs})
else()
target_link_libraries(
nuttx PRIVATE ${NUTTX_EXTRA_FLAGS} -T${ldscript} -Wl,--start-group
${nuttx_libs} -Wl,--end-group)
nuttx
PRIVATE ${NUTTX_EXTRA_FLAGS}
-T
${ldscript}
$<$<NOT:$<BOOL:${DISABLE_LINK_GROUP}>>:-Wl,--start-group>
${nuttx_libs}
$<$<NOT:$<BOOL:${DISABLE_LINK_GROUP}>>:-Wl,--end-group>)
endif()

# generate binary outputs in different formats (.bin, .hex, etc)
Expand Down Expand Up @@ -750,48 +731,55 @@ if(CONFIG_BUILD_PROTECTED)

get_property(nuttx_apps_libs GLOBAL PROPERTY NUTTX_APPS_LIBRARIES)

get_property(nuttx_user_extra_libs GLOBAL PROPERTY NUTTX_USER_EXTRA_LIBRARIES)

get_property(user_ldscript GLOBAL PROPERTY LD_SCRIPT_USER)

if(DEFINED PREPROCESS)
get_filename_component(LD_SCRIPT_NAME ${user_ldscript} NAME)
set(LD_SCRIPT_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_NAME}.tmp")
add_custom_command(
OUTPUT ${LD_SCRIPT_TMP}
DEPENDS ${user_ldscript}
COMMAND ${PREPROCESS} -I${CMAKE_BINARY_DIR}/include
-I${NUTTX_CHIP_ABS_DIR} ${user_ldscript} > ${LD_SCRIPT_TMP}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(user_ldscript_tmp DEPENDS ${LD_SCRIPT_TMP})
add_dependencies(nuttx_user user_ldscript_tmp)
set(user_ldscript ${LD_SCRIPT_TMP})
# Pre-compile linker script
get_filename_component(LD_SCRIPT_USER_NAME ${user_ldscript} NAME)
set(LD_SCRIPT_USER_TMP "${CMAKE_BINARY_DIR}/${LD_SCRIPT_USER_NAME}.tmp")
nuttx_generate_preproces_target(SOURCE_FILE ${user_ldscript} TARGET_FILE
${LD_SCRIPT_USER_TMP})
add_custom_target(user_ldscript_tmp DEPENDS ${LD_SCRIPT_USER_TMP})
add_dependencies(nuttx_user user_ldscript_tmp)
set(user_ldscript ${LD_SCRIPT_USER_TMP})

# reset link options that don't fit userspace
get_target_property(nuttx_user_LINK_OPTIONS nuttx_user LINK_OPTIONS)
list(REMOVE_ITEM nuttx_user_LINK_OPTIONS "-Wl,--cref")
list(REMOVE_ITEM nuttx_user_LINK_OPTIONS "-Wl,-Map=nuttx.map")
if(CONFIG_ARCH_TOOLCHAIN_GHS)
list(REMOVE_ITEM nuttx_user_LINK_OPTIONS "-entry=__start")
list(REMOVE_ITEM nuttx_user_LINK_OPTIONS "-map=nuttx.map")
list(APPEND nuttx_user_LINK_OPTIONS "-map=nuttx_user.map")
else()
list(REMOVE_ITEM nuttx_user_LINK_OPTIONS "-Wl,--entry=__start")
list(APPEND nuttx_user_LINK_OPTIONS "-Wl,-Map=nuttx_user.map")
endif()

list(TRANSFORM user_ldscript PREPEND "-T")

execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS} ${NUTTX_EXTRA_FLAGS}
--print-libgcc-file-name
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE nuttx_user_libgcc)

# reset link options for userspace to prevent sections from being accidentally
# deleted
set_target_properties(nuttx_user PROPERTIES LINK_OPTIONS "")
set_target_properties(nuttx_user PROPERTIES LINK_OPTIONS
"${nuttx_user_LINK_OPTIONS}")

target_link_options(
nuttx_user PRIVATE -nostartfiles -nodefaultlibs
-Wl,--entry=${CONFIG_INIT_ENTRYPOINT}
-Wl,--undefined=${CONFIG_INIT_ENTRYPOINT})
if(CONFIG_ARCH_TOOLCHAIN_GHS)
target_link_options(nuttx_user PRIVATE -nostartfiles -minlib
-entry=${CONFIG_INIT_ENTRYPOINT})
else()
target_link_options(
nuttx_user PRIVATE -nostartfiles -nodefaultlibs
-Wl,--entry=${CONFIG_INIT_ENTRYPOINT}
-Wl,--undefined=${CONFIG_INIT_ENTRYPOINT})
endif()

target_link_libraries(
nuttx_user
PRIVATE ${user_ldscript}
$<$<NOT:$<BOOL:${APPLE}>>:-Wl,--start-group>
PRIVATE -T
${user_ldscript}
$<$<NOT:$<BOOL:${DISABLE_LINK_GROUP}>>:-Wl,--start-group>
${nuttx_system_libs}
${nuttx_apps_libs}
${nuttx_user_libgcc}
${nuttx_user_extra_libs}
$<$<BOOL:${CONFIG_HAVE_CXX}>:supc++>
$<$<NOT:$<BOOL:${APPLE}>>:-Wl,--end-group>)
$<$<NOT:$<BOOL:${DISABLE_LINK_GROUP}>>:-Wl,--end-group>)

target_include_directories(
nuttx_user SYSTEM
Expand Down
190 changes: 8 additions & 182 deletions arch/arm/src/cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,188 +43,14 @@ endif()

include(${ARCH_SUBDIR})

if(CONFIG_ARCH_TOOLCHAIN_CLANG)
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_STRIP llvm-strip --strip-unneeded)
set(CMAKE_OBJCOPY llvm-objcopy)
set(CMAKE_OBJDUMP llvm-objdump)
set(CMAKE_LINKER ld.lld)
set(CMAKE_LD ld.lld)
set(CMAKE_AR llvm-ar)
set(CMAKE_NM llvm-nm)
set(CMAKE_RANLIB llvm-ranlib)
set(TOOLCHAIN_FILE)

# Since the no_builtin attribute is not fully supported on Clang disable the
# built-in functions, refer:
# https://github.com/apache/incubator-nuttx/pull/5971

add_compile_options(-fno-builtin)
add_compile_options(-Wno-atomic-alignment)
add_compile_options(-Wno-atomic-alignment)
else()
set(TOOLCHAIN_PREFIX arm-none-eabi)
set(CMAKE_LIBRARY_ARCHITECTURE ${TOOLCHAIN_PREFIX})
set(CMAKE_C_COMPILER_TARGET ${TOOLCHAIN_PREFIX})
set(CMAKE_CXX_COMPILER_TARGET ${TOOLCHAIN_PREFIX})

set(CMAKE_ASM_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_C_COMPILER ${CMAKE_ASM_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_STRIP ${TOOLCHAIN_PREFIX}-strip --strip-unneeded)
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}-objcopy)
set(CMAKE_OBJDUMP ${TOOLCHAIN_PREFIX}-objdump)

if(CONFIG_LTO_FULL AND CONFIG_ARCH_TOOLCHAIN_GNU)
set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_LD ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_AR ${TOOLCHAIN_PREFIX}-gcc-ar)
set(CMAKE_NM ${TOOLCHAIN_PREFIX}-gcc-nm)
set(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}-gcc-ranlib)
else()
set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}-ld)
set(CMAKE_LD ${TOOLCHAIN_PREFIX}-ld)
set(CMAKE_AR ${TOOLCHAIN_PREFIX}-ar)
set(CMAKE_NM ${TOOLCHAIN_PREFIX}-nm)
set(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}-ranlib)
endif()
endif()

# override the ARCHIVE command
set(CMAKE_ARCHIVE_COMMAND "<CMAKE_AR> rcs <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_RANLIB_COMMAND "<CMAKE_RANLIB> <TARGET>")
set(CMAKE_C_ARCHIVE_CREATE ${CMAKE_ARCHIVE_COMMAND})
set(CMAKE_CXX_ARCHIVE_CREATE ${CMAKE_ARCHIVE_COMMAND})
set(CMAKE_ASM_ARCHIVE_CREATE ${CMAKE_ARCHIVE_COMMAND})

set(CMAKE_C_ARCHIVE_APPEND ${CMAKE_ARCHIVE_COMMAND})
set(CMAKE_CXX_ARCHIVE_APPEND ${CMAKE_ARCHIVE_COMMAND})
set(CMAKE_ASM_ARCHIVE_APPEND ${CMAKE_ARCHIVE_COMMAND})

set(CMAKE_C_ARCHIVE_FINISH ${CMAKE_RANLIB_COMMAND})
set(CMAKE_CXX_ARCHIVE_FINISH ${CMAKE_RANLIB_COMMAND})
set(CMAKE_ASM_ARCHIVE_FINISH ${CMAKE_RANLIB_COMMAND})

# Architecture flags

add_link_options(-Wl,--entry=__start)
add_link_options(-nostdlib)
add_compile_options(-fno-common)
add_compile_options(-Wall -Wshadow -Wundef)
add_compile_options(-nostdlib)

if(CONFIG_ARM_THUMB)
add_compile_options(-mthumb)

# GCC Manual: -mthumb ... If you want to force assembler files to be
# interpreted as Thumb code, either add a `.thumb' directive to the source or
# pass the -mthumb option directly to the assembler by prefixing it with -Wa.

add_compile_options(-Wa,-mthumb)

# Outputs an implicit IT block when there is a conditional instruction without
# an enclosing IT block.

add_compile_options(-Wa,-mimplicit-it=always)
endif()

if(CONFIG_UNWINDER_ARM)
add_compile_options(-funwind-tables -fasynchronous-unwind-tables)
endif()

if(CONFIG_DEBUG_CUSTOMOPT)
add_compile_options(${CONFIG_DEBUG_OPTLEVEL})
elseif(CONFIG_DEBUG_FULLOPT)
if(CONFIG_ARCH_TOOLCHAIN_CLANG)
add_compile_options(-Oz)
else()
add_compile_options(-Os)
endif()
endif()

if(NOT CONFIG_DEBUG_NOOPT)
add_compile_options(-fno-strict-aliasing)
endif()

if(CONFIG_FRAME_POINTER)
add_compile_options(-fno-omit-frame-pointer -fno-optimize-sibling-calls)
else()
add_compile_options(-fomit-frame-pointer)
endif()

if(CONFIG_STACK_CANARIES)
add_compile_options(-fstack-protector-all)
endif()

if(CONFIG_ARCH_COVERAGE)
add_compile_options(-fprofile-generate -ftest-coverage)
if(CONFIG_ARCH_TOOLCHAIN_CLANG) # clang
set(TOOLCHAIN_FILE clang)
elseif(CONFIG_ARCH_TOOLCHAIN_GHS) # greenhills
set(TOOLCHAIN_FILE ghs)
else() # gcc
set(TOOLCHAIN_FILE gcc)
endif()

# Optimization of unused sections

if(CONFIG_DEBUG_OPT_UNUSED_SECTIONS)
add_link_options(-Wl,--gc-sections)
add_compile_options(-ffunction-sections -fdata-sections)
endif()

# Debug --whole-archive

if(CONFIG_DEBUG_LINK_WHOLE_ARCHIVE)
add_link_options(-Wl,--whole-archive)
endif()

if(CONFIG_ENDIAN_BIG)
add_compile_options(-mbig-endian)
endif()

# Link Time Optimization

if(CONFIG_LTO_THIN)
add_compile_options(-flto=thin)
elseif(CONFIG_LTO_FULL)
add_compile_options(-flto)
if(CONFIG_ARCH_TOOLCHAIN_GNU)
add_compile_options(-fno-builtin)
add_compile_options(-fuse-linker-plugin)
endif()
endif()

# Debug link map

if(CONFIG_DEBUG_LINK_MAP)
add_link_options(-Wl,--cref -Wl,-Map=nuttx.map)
endif()

if(CONFIG_DEBUG_SYMBOLS)
add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL})
if(CONFIG_ARM_TOOLCHAIN_ARMCLANG)
add_link_options(-Wl,--debug)
endif()
endif()

add_compile_options(-Wno-attributes -Wno-unknown-pragmas
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)

if(CONFIG_CXX_STANDARD)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>)
endif()

if(NOT CONFIG_LIBCXXTOOLCHAIN)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
endif()

if(NOT CONFIG_CXX_EXCEPTION)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
endif()

if(NOT CONFIG_CXX_RTTI)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()

if(CONFIG_ARCH_TOOLCHAIN_CLANG)
set(CMAKE_EXE_LINKER_FLAGS_INIT "-c")
else()
set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")
endif()
include(${TOOLCHAIN_FILE})
Loading

0 comments on commit f345fb4

Please sign in to comment.