diff --git a/Makefile b/Makefile index 0938f67..2aafa8e 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,25 @@ # RRFLibraries Master Makefile # Builds RRFLibraries for various MCU configurations -# Cross-compiler toolchain (relative to project root) -#CROSS_COMPILE ?= ../arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi- -CROSS_COMPILE ?= ../arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi- +# Cross-compiler toolchain. +# RepRapFirmware exports CROSS_COMPILE when building this as a submodule. +# When building RRFLibraries standalone, fall back to a toolchain on PATH. +ARM_GNU_TOOLCHAIN_VERSION ?= 15.2.rel1 +HOST_ARCH_RAW := $(shell uname -m) + +ifeq ($(HOST_ARCH_RAW),aarch64) +ARM_GNU_TOOLCHAIN_HOST_ARCH := aarch64 +else ifeq ($(HOST_ARCH_RAW),arm64) +ARM_GNU_TOOLCHAIN_HOST_ARCH := aarch64 +else ifeq ($(HOST_ARCH_RAW),x86_64) +ARM_GNU_TOOLCHAIN_HOST_ARCH := x86_64 +else ifeq ($(HOST_ARCH_RAW),amd64) +ARM_GNU_TOOLCHAIN_HOST_ARCH := x86_64 +else +ARM_GNU_TOOLCHAIN_HOST_ARCH := $(HOST_ARCH_RAW) +endif + +CROSS_COMPILE ?= $(abspath ../arm-gnu-toolchain-$(ARM_GNU_TOOLCHAIN_VERSION)-$(ARM_GNU_TOOLCHAIN_HOST_ARCH)-arm-none-eabi/bin/arm-none-eabi-) export CROSS_COMPILE # Toolchain commands diff --git a/Makefiles/RP2040_RTOS.mk b/Makefiles/RP2040_RTOS.mk index 07479ba..3080f13 100644 --- a/Makefiles/RP2040_RTOS.mk +++ b/Makefiles/RP2040_RTOS.mk @@ -45,6 +45,8 @@ CXXFLAGS := -c -std=c++20 \ $(INCLUDES) \ $(DEFINES) +CXXFLAGS += $(DEBUG_FLAGS) + OBJS := $(CPP_SRCS:%.cpp=$(BUILD_DIR)/%.o) DEPS := $(OBJS:.o=.d) diff --git a/Makefiles/SAMC21.mk b/Makefiles/SAMC21.mk index ba59175..d299ac1 100644 --- a/Makefiles/SAMC21.mk +++ b/Makefiles/SAMC21.mk @@ -36,6 +36,8 @@ CXXFLAGS := -c -std=c++20 \ $(INCLUDES) \ $(DEFINES) +CXXFLAGS += $(DEBUG_FLAGS) + OBJS := $(CPP_SRCS:%.cpp=$(BUILD_DIR)/%.o) DEPS := $(OBJS:.o=.d) diff --git a/Makefiles/SAMC21_RTOS.mk b/Makefiles/SAMC21_RTOS.mk index cb623d5..1878cb8 100644 --- a/Makefiles/SAMC21_RTOS.mk +++ b/Makefiles/SAMC21_RTOS.mk @@ -47,6 +47,8 @@ CXXFLAGS := -c -std=c++20 \ $(INCLUDES) \ $(DEFINES) +CXXFLAGS += $(DEBUG_FLAGS) + OBJS := $(CPP_SRCS:%.cpp=$(BUILD_DIR)/%.o) DEPS := $(OBJS:.o=.d) diff --git a/Makefiles/SAME51.mk b/Makefiles/SAME51.mk index 5650396..5a37802 100644 --- a/Makefiles/SAME51.mk +++ b/Makefiles/SAME51.mk @@ -35,6 +35,8 @@ CXXFLAGS := -c -std=c++20 \ $(INCLUDES) \ $(DEFINES) +CXXFLAGS += $(DEBUG_FLAGS) + OBJS := $(CPP_SRCS:%.cpp=$(BUILD_DIR)/%.o) DEPS := $(OBJS:.o=.d) diff --git a/Makefiles/SAME51_RTOS.mk b/Makefiles/SAME51_RTOS.mk index 471259b..b5dbd42 100644 --- a/Makefiles/SAME51_RTOS.mk +++ b/Makefiles/SAME51_RTOS.mk @@ -36,15 +36,11 @@ SAME51_RTOS_CXXFLAGS := -c -std=c++20 \ -fstack-usage \ -fdump-rtl-expand \ -Wall \ + -O2 \ $(SAME51_RTOS_INCLUDES) \ $(SAME51_RTOS_DEFINES) -# Add debug flags if DEBUG=1 -ifeq ($(DEBUG),1) -SAME51_RTOS_CXXFLAGS += -O0 -g3 -else -SAME51_RTOS_CXXFLAGS += -O2 -endif +SAME51_RTOS_CXXFLAGS += $(DEBUG_FLAGS) SAME51_RTOS_OBJS := $(SAME51_RTOS_CPP_SRCS:%.cpp=$(SAME51_RTOS_BUILD_DIR)/%.o) SAME51_RTOS_DEPS := $(SAME51_RTOS_OBJS:.o=.d) diff --git a/Makefiles/SAME70.mk b/Makefiles/SAME70.mk index 0d3b33e..a9966b1 100644 --- a/Makefiles/SAME70.mk +++ b/Makefiles/SAME70.mk @@ -32,15 +32,11 @@ SAME70_CXXFLAGS := -c -std=c++20 \ -Wdouble-promotion \ -Werror -Wnoexcept -Wshadow -Wsign-promo \ -fsingle-precision-constant \ + -Os \ $(SAME70_INCLUDES) \ $(SAME70_DEFINES) -# Add debug flags if DEBUG=1 -ifeq ($(DEBUG),1) - SAME70_CXXFLAGS += -O0 -g3 -else - SAME70_CXXFLAGS += -Os -endif +SAME70_CXXFLAGS += $(DEBUG_FLAGS) SAME70_OBJS := $(SAME70_CPP_SRCS:%.cpp=$(SAME70_BUILD_DIR)/%.o) SAME70_DEPS := $(SAME70_OBJS:.o=.d) diff --git a/Makefiles/SAME70_RTOS.mk b/Makefiles/SAME70_RTOS.mk index 6c6f045..8a15531 100644 --- a/Makefiles/SAME70_RTOS.mk +++ b/Makefiles/SAME70_RTOS.mk @@ -39,6 +39,8 @@ SAME70_RTOS_CXXFLAGS := -c -std=c++20 \ $(SAME70_RTOS_INCLUDES) \ $(SAME70_RTOS_DEFINES) +SAME70_RTOS_CXXFLAGS += $(DEBUG_FLAGS) + SAME70_RTOS_OBJS := $(SAME70_RTOS_CPP_SRCS:%.cpp=$(SAME70_RTOS_BUILD_DIR)/%.o) SAME70_RTOS_DEPS := $(SAME70_RTOS_OBJS:.o=.d) diff --git a/Makefiles/STM32H5_RTOS.mk b/Makefiles/STM32H5_RTOS.mk index 5b9925e..b2f34ee 100644 --- a/Makefiles/STM32H5_RTOS.mk +++ b/Makefiles/STM32H5_RTOS.mk @@ -37,15 +37,11 @@ STM32H5_RTOS_CXXFLAGS := -c -std=c++20 \ -fstack-usage \ -fdump-rtl-expand \ -Wall \ + -O2 \ $(STM32H5_RTOS_INCLUDES) \ $(STM32H5_RTOS_DEFINES) -# Add debug flags if DEBUG=1 -ifeq ($(DEBUG),1) -STM32H5_RTOS_CXXFLAGS += -O0 -g3 -else -STM32H5_RTOS_CXXFLAGS += -O2 -endif +STM32H5_RTOS_CXXFLAGS += $(DEBUG_FLAGS) STM32H5_RTOS_OBJS := $(STM32H5_RTOS_CPP_SRCS:%.cpp=$(STM32H5_RTOS_BUILD_DIR)/%.o) STM32H5_RTOS_DEPS := $(STM32H5_RTOS_OBJS:.o=.d) diff --git a/Makefiles/STM32H7_RTOS.mk b/Makefiles/STM32H7_RTOS.mk index 8f885f8..0a447db 100644 --- a/Makefiles/STM32H7_RTOS.mk +++ b/Makefiles/STM32H7_RTOS.mk @@ -37,15 +37,11 @@ STM32H7_RTOS_CXXFLAGS := -c -std=c++20 \ -fstack-usage \ -fdump-rtl-expand \ -Wall \ + -O2 \ $(STM32H7_RTOS_INCLUDES) \ $(STM32H7_RTOS_DEFINES) -# Add debug flags if DEBUG=1 -ifeq ($(DEBUG),1) -STM32H7_RTOS_CXXFLAGS += -O0 -g3 -else -STM32H7_RTOS_CXXFLAGS += -O2 -endif +STM32H7_RTOS_CXXFLAGS += $(DEBUG_FLAGS) STM32H7_RTOS_OBJS := $(STM32H7_RTOS_CPP_SRCS:%.cpp=$(STM32H7_RTOS_BUILD_DIR)/%.o) STM32H7_RTOS_DEPS := $(STM32H7_RTOS_OBJS:.o=.d) diff --git a/RRFLibraries.cmake b/RRFLibraries.cmake new file mode 100644 index 0000000..fbe0485 --- /dev/null +++ b/RRFLibraries.cmake @@ -0,0 +1,139 @@ +# RRFLibraries as a reusable CMake component. See lib/FreeRTOS/FreeRTOS.cmake for the rationale +# behind the include()-a-module-and-call-a-function pattern. +# +# rrflibraries_add_library( +# TARGET +# MCU +# ARCH +# [RTOS] # build the RTOS variant (adds -DRTOS and the FreeRTOS include paths) +# [EXTRA_COMPILE_OPTIONS ] +# ) +# +# MCU HOST builds for an ordinary hosted target (Linux) rather than a Duet MCU. It exists for the +# SBC-side code, which reuses the portable parts of this library - Bitmap, StringRef, SimpleMath, +# Isqrt and the eCv annotation headers - and must not pull in anything freestanding. Differences +# from an MCU build: +# - RTOSIface/ is excluded; it needs FreeRTOS, and a hosted consumer brings its own threading. +# - the freestanding-only compile options (-nostdlib, -fsingle-precision-constant) are dropped, +# as is -Werror: this is upstream source built for a target it was not written for, so a new +# warning here must not break a downstream project's build. +# - the objects are position-independent, because they get linked into a shared library. +# +# EXTRA_COMPILE_OPTIONS is for options the consumer must impose on this library's own translation +# units, not just on its usage requirements - a forced include being the motivating case (see +# float16_t in DuetSbcInterface/src/Compat). + +set(RRFLIBRARIES_DIR "${CMAKE_CURRENT_LIST_DIR}") +set(RRFLIBRARIES_LIBRARY_FLAGS + "RTOS" +) +set(RRFLIBRARIES_LIBRARY_ARGS + "FREERTOS_INTERFACE" # interface target for FreeRTOS, if RTOS is enabled +) +set(RRFLIBRARIES_LIBRARY_MULTI_ARGS + "EXTRA_COMPILE_OPTIONS" # options applied to this library's own sources +) + +include("${LIBRARIES_DIR}/LibraryUtils.cmake") + +function(rrflibraries_add_interface OUT_TARGET) + cmake_parse_arguments(PARSE_ARGV 1 ARG "${RRFLIBRARIES_LIBRARY_FLAGS}" "${DEFAULT_INTERFACE_ARGS}" "") + if(ARG_UNPARSED_ARGUMENTS) + message(FATAL_ERROR "rrflibraries_add_interface: unknown arguments: ${ARG_UNPARSED_ARGUMENTS}") + endif() + + get_enabled_features(_enabled_features ${RRFLIBRARIES_LIBRARY_FLAGS}) + make_library_name(_target "RRFLibraries" INTERFACE ${ARG_MCU} ${_enabled_features}) + set(${OUT_TARGET} "${_target}" PARENT_SCOPE) + if(TARGET ${_target}) + return() # already built for this MCU and feature set + endif() + + add_library(${_target} INTERFACE) + target_include_directories(${_target} INTERFACE "${RRFLIBRARIES_DIR}/src") +endfunction() + +function(rrflibraries_add_library OUT_TARGET) + cmake_parse_arguments(PARSE_ARGV 1 ARG "${RRFLIBRARIES_LIBRARY_FLAGS}" "${DEFAULT_LIBRARY_ARGS};${RRFLIBRARIES_LIBRARY_ARGS}" "${RRFLIBRARIES_LIBRARY_MULTI_ARGS}") + if(ARG_UNPARSED_ARGUMENTS) + message(FATAL_ERROR "rrflibraries_add_library: unknown arguments: ${ARG_UNPARSED_ARGUMENTS}") + endif() + + get_enabled_features(_enabled_features ${RRFLIBRARIES_LIBRARY_FLAGS}) + make_library_name(_target "RRFLibraries" STATIC ${ARG_MCU} ${_enabled_features}) + set(${OUT_TARGET} "${_target}" PARENT_SCOPE) + if(TARGET ${_target}) + return() # already built for this MCU and feature set + endif() + + set(_src "${RRFLIBRARIES_DIR}/src") + set(_freertos "${RRFLIBRARIES_DIR}/../FreeRTOS/src") + + if(ARG_MCU STREQUAL "HOST") + # RTOSIface is the FreeRTOS shim; a hosted consumer supplies its own synchronisation. + set(_excludes "/RP2040/" "/SAME5x_C21/" "/RTOSIface/") + elseif(ARG_MCU STREQUAL "SAME70") + set(_excludes "/RP2040/" "/SAME5x_C21/") + elseif(ARG_MCU STREQUAL "SAME51") + set(_excludes "/RP2040/") + elseif(ARG_MCU STREQUAL "SAMC21") + set(_excludes "/RP2040/") + else() + message(FATAL_ERROR "rrflibraries_add_library: unsupported MCU '${ARG_MCU}'") + endif() + + file(GLOB_RECURSE _srcs CONFIGURE_DEPENDS "${_src}/*.cpp") + foreach(_ex IN LISTS _excludes) + list(FILTER _srcs EXCLUDE REGEX "${_ex}") + endforeach() + + add_library(${_target} STATIC ${_srcs}) + + target_link_libraries(${_target} PUBLIC I_${_target}) # link own interface target + + if(ARG_RTOS) + target_link_libraries(${_target} PRIVATE ${ARG_FREERTOS_INTERFACE}) + endif() + + # Options that only make sense freestanding, or that turn an upstream warning into a downstream + # build failure. A HOST build is this source compiled for a target it was not written for, so it + # opts out of both groups. + # + # -fsingle-precision-constant is deliberately NOT in this group: it decides whether a literal + # like 0.0 is float or double, so dropping it would silently change the results of the motion + # maths relative to the firmware. It stays PRIVATE to this library's own sources either way. + set(_freestanding_options + -nostdlib + -Werror) + if(ARG_MCU STREQUAL "HOST") + set(_freestanding_options) + # C++20 for this library's own sources (SafeVsnprintf.cpp uses std::bit_cast), which is what + # the firmware projects already compile it as. PRIVATE, so a C++17 consumer stays C++17 - + # nothing in the headers needs more than C++17. + set_target_properties(${_target} PROPERTIES + POSITION_INDEPENDENT_CODE ON + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON) + endif() + + target_compile_options(${_target} PRIVATE + -ffunction-sections + -fdata-sections + -fno-threadsafe-statics + -fno-rtti + -fno-exceptions + -Wall + -Wundef + -Wdouble-promotion + -Werror=return-type + -Wnoexcept + -Wshadow + -Wsign-promo + -fsingle-precision-constant + ${_freestanding_options} + ${ARG_EXTRA_COMPILE_OPTIONS} + $<$>:-O2> + $<$:-Og;-g3>) + + target_link_libraries(${_target} PRIVATE ${ARG_ARCH}) +endfunction() diff --git a/src/General/FreelistManager.h b/src/General/FreelistManager.h index 0793faf..aa2c9c1 100644 --- a/src/General/FreelistManager.h +++ b/src/General/FreelistManager.h @@ -78,7 +78,7 @@ namespace FreelistManager // Call this macro within a class public section to use freelist new and delete #define DECLARE_FREELIST_NEW_DELETE(_Type) \ -void* operator new(size_t sz) noexcept { return FreelistManager::Allocate<_Type>(); } \ +void* operator new(size_t /* sz */) noexcept { return FreelistManager::Allocate<_Type>(); } \ void operator delete(void* p) noexcept { FreelistManager::Release<_Type>(p); } #endif /* SRC_LIBRARIES_GENERAL_FREELISTMANAGER_H_ */ diff --git a/src/General/NamedEnum.h b/src/General/NamedEnum.h index e8ae91b..533aeb6 100644 --- a/src/General/NamedEnum.h +++ b/src/General/NamedEnum.h @@ -94,6 +94,8 @@ static inline constexpr c_string SkipLeadingUnderscore(c_string s) noexcept // If any of the names is a C++ reserved word or starts with a digit, prefix it with a single underscore // BaseType must be unsigned for IsValid and ToString to work correctly. +// TODO: determine if these linting checks are false positives or not +// NOLINTBEGIN(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator,performance-unnecessary-value-param,cppcoreguidelines-special-member-functions) #define NamedEnum(_typename, _baseType, _v1, ...) \ static_assert((_baseType)0 < (_baseType)-1, "base type must be unsigned"); \ class _typename final { \ @@ -101,11 +103,13 @@ public: \ typedef _baseType BaseType; /* alias for the base type */ \ enum RawType : BaseType { _v1 = 0, __VA_ARGS__ }; /* underlying enumeration */ \ static constexpr unsigned int NumValues = VA_SIZE(__VA_ARGS__) + 1; /* count of members */ \ - _typename(RawType arg) noexcept { v = arg; } /* constructor - cannot be declared 'explicit' because we need the conversion */ \ - explicit _typename(BaseType arg) noexcept { v = static_cast(arg); } /* constructor */ \ - explicit _typename(c_string s) noexcept { v = static_cast(NamedEnumLookup(s, _names, NumValues)); } /* constructor from string */ \ - _typename(const _typename& arg) noexcept { v = arg.v; } /* copy constructor */ \ - _typename(const volatile _typename& arg) noexcept { v = arg.v; } /* copy constructor */ \ + _typename(RawType arg) noexcept : v(arg) {} /* constructor - cannot be declared 'explicit' because we need the conversion */ \ + explicit _typename(BaseType arg) noexcept : v(static_cast(arg)) {} /* constructor */ \ + explicit _typename(c_string s) noexcept : v(static_cast(NamedEnumLookup(s, _names, NumValues))) {} /* constructor from string */ \ + _typename(const _typename& arg) noexcept : v(arg.v) {} /* copy constructor */ \ + _typename(const volatile _typename& arg) noexcept : v(arg.v) {} /* copy constructor */ \ + _typename(_typename&& arg) noexcept : v(arg.v) {} /* move constructor */ \ + ~_typename() = default; /* destructor */ \ bool operator==(_typename arg) const noexcept { return v == arg.v; } /* equality operator */ \ bool operator!=(_typename arg) const noexcept { return v != arg.v; } /* inequality operator */ \ bool operator>(_typename arg) const noexcept { return v > arg.v; } /* greater-than operator */ \ @@ -133,5 +137,6 @@ private: \ RawType v; \ static constexpr c_string _names[NumValues] = { STRINGLIST(_v1, __VA_ARGS__) }; \ } +// NOLINTEND(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator,performance-unnecessary-value-param,cppcoreguidelines-special-member-functions) #endif /* SRC_NAMEDENUM_H_ */ diff --git a/src/General/SimpleMath.h b/src/General/SimpleMath.h index f5ae53a..95e08cd 100644 --- a/src/General/SimpleMath.h +++ b/src/General/SimpleMath.h @@ -92,7 +92,9 @@ inline unsigned int LowestSetBitNumber(unsigned int val) noexcept return (unsigned int)__builtin_ctz(val); } -static_assert(sizeof(uint32_t) == sizeof(unsigned long)); +// Each overload forwards to the builtin for its own parameter type, so the set is correct whether +// uint32_t/uint64_t map onto unsigned long (ILP32, every Duet MCU) or not (LP64 hosts). +static_assert(sizeof(unsigned long) >= sizeof(uint32_t)); inline unsigned int LowestSetBitNumber(unsigned long val) noexcept { return (unsigned int)__builtin_ctzl(val);