Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang compatibility] Added Clang CMake definitions. #76

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions utils/cmake/toolchains/CLANG/bin-generator.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_custom_command(
OUTPUT "${PROJECT_SOURCE_DIR}/${codal.output_folder}/${device.device}.bin"
COMMAND "${LLVM_OBJCOPY}" -O binary "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${device.device}" "${PROJECT_SOURCE_DIR}/${codal.output_folder}/${device.device}.bin"
DEPENDS ${device.device}
COMMENT "converting to bin file."
)

#specify a dependency on the elf file so that bin is automatically rebuilt when elf is changed.
add_custom_target(${device.device}_bin ALL DEPENDS "${PROJECT_SOURCE_DIR}/${codal.output_folder}/${device.device}.bin")
64 changes: 64 additions & 0 deletions utils/cmake/toolchains/CLANG/compiler-flags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# See toolchain.cmake before modifying this.

# This file is a copy from ../ARM_GCC, building with Clang/LLVM however will not immediatly work,
# some flags are not accepted by both compilers (e.g -Wl,--no-wchar-size-warning, defined in target.json), handle conflicts,
# and Clang will require the --target=arm-none-eabi flag. We also need Clang to include extra header files:
Copy link
Collaborator

@microbit-carlos microbit-carlos Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the --target=arm-none-eabi flag be added to this file?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other flags added to lancaster-university/codal-microbit-v2@master...Johnn333:codal-microbit-v2-clang:master could also be include in this file instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the --target=arm-none-eabi flag be added to this file?

Possibly, although I'm not sure where it would be added. "cpu_opts" in target.json feels like a more correct spot for it? Although we can't easily have a separate target.json.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other flags added to lancaster-university/[email protected]:codal-microbit-v2-clang:master could also be include in this file instead?

The main flags added here is for include paths which annoyingly have a version number in their name. Probably not the best thing to push as depending on arm-none-eabi version they may not have the directory? As for the others flags it was mostly to suppress warnings.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is more -W flags added to the target-lock.json file. I assume those might not be required and could be removed to reduce the diff?
What was the reason they were added? To help catch issues with the toolchain porting?

Copy link
Author

@Johnn333 Johnn333 Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From memory it was all just to reduce warnings aswell. Clang would give a warning about unused linker inputs so I got rid of them. It still builds but now I think about it may have something to do with the hex size diff? Might be worth looking into.

# "-I/lib/arm-none-eabi/include -I/etc/alternatives/gcc-arm-none-eabi-include -I/usr/include/newlib/c++/10.3.1 -I/usr/include/newlib/c++/10.3.1/arm-none-eabi"
# Note: The version of newlib may have changed. These changes *should* allow you too build upto the final link.

# The arm-embedded linker is auto configured, and specifically searches for corresponding libraries/startup files, given
# the architecture on the command line, Clang does not do this and as such linking is a much more difficult step, it remains easiest
# to run the arm-embedded linker, view its command using "$ python(3) build.py -v", further verbose this command, copy it and invoke lld directly.

# This file hasn't been changed to reflect any of this, modifying target.json is likely the easier route.
# Note: The above build process assumes that arm-embedded is already installed, which brings in all of the dependencies.

set(EXPLICIT_INCLUDES "")
if((CMAKE_VERSION VERSION_GREATER "3.4.0") OR (CMAKE_VERSION VERSION_EQUAL "3.4.0"))
# from CMake 3.4 <INCLUDES> are separate to <FLAGS> in the
# CMAKE_<LANG>_COMPILE_OBJECT, CMAKE_<LANG>_CREATE_ASSEMBLY_SOURCE, and
# CMAKE_<LANG>_CREATE_PREPROCESSED_SOURCE commands
set(EXPLICIT_INCLUDES "<INCLUDES> ")
endif()

# Override the link rules:
set(CMAKE_C_CREATE_SHARED_LIBRARY "echo 'shared libraries not supported' && 1")
set(CMAKE_C_CREATE_SHARED_MODULE "echo 'shared modules not supported' && 1")
set(CMAKE_C_CREATE_STATIC_LIBRARY "<CMAKE_AR> -cr <LINK_FLAGS> <TARGET> <OBJECTS>")
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${EXPLICIT_INCLUDES}<FLAGS> -o <OBJECT> -c <SOURCE>")

set(CMAKE_C_LINK_EXECUTABLE "<CMAKE_C_COMPILER> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> -Wl,-Map,<TARGET>.map -Wl,--start-group <OBJECTS> <LINK_LIBRARIES> -lm -lc -lgcc -lm -lc -lgcc -Wl,--end-group --specs=nano.specs -o <TARGET>")

set(CMAKE_CXX_OUTPUT_EXTENSION ".o")
set(CMAKE_DEPFILE_FLAGS_CXX "-MMD -MT <OBJECT> -MF <DEPFILE>")
set(CMAKE_C_OUTPUT_EXTENSION ".o")
set(CMAKE_DEPFILE_FLAGS_C "-MMD -MT <OBJECT> -MF <DEPFILE>")

set(CMAKE_C_FLAGS_DEBUG_INIT "-g -gdwarf-3")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "-Os -DNDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-Os -g -gdwarf-3 -DNDEBUG")
set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")


set(CMAKE_ASM_FLAGS_DEBUG_INIT "-g -gdwarf-3")
set(CMAKE_ASM_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
set(CMAKE_ASM_FLAGS_RELEASE_INIT "-Os -DNDEBUG")
set(CMAKE_ASM_FLAGS_RELWITHDEBINFO_INIT "-Os -g -gdwarf-3 -DNDEBUG")
set(CMAKE_INCLUDE_SYSTEM_FLAG_ASM "-isystem ")

set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_AR> -cr <LINK_FLAGS> <TARGET> <OBJECTS>")

set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> -Wl,-Map,<TARGET>.map -Wl,--start-group <OBJECTS> <LINK_LIBRARIES> -lnosys -lstdc++ -lsupc++ -lm -lc -lgcc -lstdc++ -lsupc++ -lm -lc -lgcc -Wl,--end-group --specs=nano.specs -o <TARGET>")

set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g -gdwarf-3")
set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-Os -g -gdwarf-3 -DNDEBUG")
set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-isystem ")

if (CMAKE_C_COMPILER_VERSION VERSION_GREATER "7.1.0" OR CMAKE_C_COMPILER_VERSION VERSION_EQUAL "7.1.0")
message("${BoldRed}Supressing -Wexpansion-to-defined.${ColourReset}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-expansion-to-defined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-expansion-to-defined")
endif ()
9 changes: 9 additions & 0 deletions utils/cmake/toolchains/CLANG/hex-generator.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_custom_command(
OUTPUT "${PROJECT_SOURCE_DIR}/${codal.output_folder}/${device.device}.hex"
COMMAND "${LLVM_OBJCOPY}" -O ihex "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${device.device}" "${PROJECT_SOURCE_DIR}/${codal.output_folder}/${device.device}.hex"
DEPENDS ${device.device}
COMMENT "converting to hex file."
)

#specify a dependency on the elf file so that hex is automatically rebuilt when elf is changed.
add_custom_target(${device.device}_hex ALL DEPENDS "${PROJECT_SOURCE_DIR}/${codal.output_folder}/${device.device}.hex")
10 changes: 10 additions & 0 deletions utils/cmake/toolchains/CLANG/platform_includes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef PLATFORM_INCLUDES
#define PLATFORM_INCLUDES

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>

#endif
35 changes: 35 additions & 0 deletions utils/cmake/toolchains/CLANG/toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Clang/LLVM is NOT the officialy supported toolchain for building Micro:Bit V2 CODAL applications.
# This file serves to define an alternative compiler of which work has been put into making
# compatible, but milage may vary in getting a working build. It is recommended to use
# ARM_GCC where possible, with that being said complete Clang are possible with some tweaking.
# See compiler-flags.cmake

find_program(LLVM_RANLIB llvm-ranlib)
find_program(LLVM_AR llvm-ar)
find_program(CLANG clang)
find_program(CLANG++ clang++)
find_program(LLVM_OBJCOPY llvm-objcopy)

set(CMAKE_OSX_SYSROOT "/")
set(CMAKE_OSX_DEPLOYMENT_TARGET "")

set(CMAKE_SYSTEM_NAME "Generic")
set(CMAKE_SYSTEM_VERSION "2.0.0")

set(CODAL_TOOLCHAIN "CLANG")

if(CMAKE_VERSION VERSION_LESS "3.5.0")
include(CMakeForceCompiler)
cmake_force_c_compiler("${CLANG}" GNU)
cmake_force_cxx_compiler("${CLANG++}" GNU)
else()
# from 3.5 the force_compiler macro is deprecated: CMake can detect
# llvm-gcc as being a GNU compiler automatically
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
set(CMAKE_C_COMPILER "${CLANG}")
set(CMAKE_CXX_COMPILER "${CLANG++}")
endif()

SET(CMAKE_AR "${LLVM_AR}" CACHE FILEPATH "Archiver")
SET(CMAKE_RANLIB "${LLVM_RANLIB}" CACHE FILEPATH "rlib")
set(CMAKE_CXX_OUTPUT_EXTENSION ".o")