diff --git a/utils/cmake/toolchains/CLANG/bin-generator.cmake b/utils/cmake/toolchains/CLANG/bin-generator.cmake new file mode 100644 index 00000000..b7cfa7a8 --- /dev/null +++ b/utils/cmake/toolchains/CLANG/bin-generator.cmake @@ -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") diff --git a/utils/cmake/toolchains/CLANG/compiler-flags.cmake b/utils/cmake/toolchains/CLANG/compiler-flags.cmake new file mode 100644 index 00000000..65f70758 --- /dev/null +++ b/utils/cmake/toolchains/CLANG/compiler-flags.cmake @@ -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: +# "-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 are separate to in the + # CMAKE__COMPILE_OBJECT, CMAKE__CREATE_ASSEMBLY_SOURCE, and + # CMAKE__CREATE_PREPROCESSED_SOURCE commands + set(EXPLICIT_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 " -cr ") +set(CMAKE_C_COMPILE_OBJECT " ${EXPLICIT_INCLUDES} -o -c ") + +set(CMAKE_C_LINK_EXECUTABLE " -Wl,-Map,.map -Wl,--start-group -lm -lc -lgcc -lm -lc -lgcc -Wl,--end-group --specs=nano.specs -o ") + +set(CMAKE_CXX_OUTPUT_EXTENSION ".o") +set(CMAKE_DEPFILE_FLAGS_CXX "-MMD -MT -MF ") +set(CMAKE_C_OUTPUT_EXTENSION ".o") +set(CMAKE_DEPFILE_FLAGS_C "-MMD -MT -MF ") + +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 " -cr ") + +set(CMAKE_CXX_LINK_EXECUTABLE " -Wl,-Map,.map -Wl,--start-group -lnosys -lstdc++ -lsupc++ -lm -lc -lgcc -lstdc++ -lsupc++ -lm -lc -lgcc -Wl,--end-group --specs=nano.specs -o ") + +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 () \ No newline at end of file diff --git a/utils/cmake/toolchains/CLANG/hex-generator.cmake b/utils/cmake/toolchains/CLANG/hex-generator.cmake new file mode 100644 index 00000000..2eac9c9d --- /dev/null +++ b/utils/cmake/toolchains/CLANG/hex-generator.cmake @@ -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") diff --git a/utils/cmake/toolchains/CLANG/platform_includes.h b/utils/cmake/toolchains/CLANG/platform_includes.h new file mode 100644 index 00000000..3417ef3d --- /dev/null +++ b/utils/cmake/toolchains/CLANG/platform_includes.h @@ -0,0 +1,10 @@ +#ifndef PLATFORM_INCLUDES +#define PLATFORM_INCLUDES + +#include +#include +#include +#include +#include + +#endif diff --git a/utils/cmake/toolchains/CLANG/toolchain.cmake b/utils/cmake/toolchains/CLANG/toolchain.cmake new file mode 100644 index 00000000..7d94eac0 --- /dev/null +++ b/utils/cmake/toolchains/CLANG/toolchain.cmake @@ -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")