From 03c7f98355646937044595d6096b82334624eb72 Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Thu, 5 Jan 2023 23:05:43 -0500 Subject: [PATCH 1/5] build: Add support for building mcu code on MacOS I restored some of the deleted files from the bossac dependancy and made some tweaks to the build scripts to adapt to the different environment. Signed-off-by: Alan Everett --- Makefile | 18 ++++- lib/bossac/src/OSXPortFactory.cpp | 106 ++++++++++++++++++++++++++++++ lib/bossac/src/OSXPortFactory.h | 60 +++++++++++++++++ scripts/dev-install-macos.sh | 35 ++++++++++ scripts/flash_usb.py | 6 ++ 5 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 lib/bossac/src/OSXPortFactory.cpp create mode 100644 lib/bossac/src/OSXPortFactory.h create mode 100644 scripts/dev-install-macos.sh diff --git a/Makefile b/Makefile index 106157265478..975275eb9c82 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,16 @@ LD=$(CROSS_PREFIX)ld OBJCOPY=$(CROSS_PREFIX)objcopy OBJDUMP=$(CROSS_PREFIX)objdump STRIP=$(CROSS_PREFIX)strip -CPP=cpp PYTHON=python3 +UNAME_S := $(shell uname -s) +ifeq ($(UNAME_S),Darwin) +# cpp on MacOS forces a specific input directory, so directly use clang instead +CPP=clang -E +else +CPP=cpp +endif + # Source files src-y = dirs-y = src @@ -65,7 +72,7 @@ $(OUT)%.o: %.c $(OUT)autoconf.h $(OUT)%.ld: %.lds.S $(OUT)autoconf.h @echo " Preprocessing $@" - $(Q)$(CPP) -I$(OUT) -P -MD -MT $@ $< -o $@ + $(Q)$(CPP) -I $(OUT) -P -MD -MT $@ $< -o $@ $(OUT)klipper.elf: $(OBJS_klipper.elf) @echo " Linking $@" @@ -118,7 +125,7 @@ menuconfig: ################ Generic rules # Make definitions -.PHONY : all clean distclean olddefconfig menuconfig create-board-link FORCE +.PHONY : all clean libclean distclean olddefconfig menuconfig create-board-link FORCE .DELETE_ON_ERROR: all: $(target-y) @@ -126,6 +133,11 @@ all: $(target-y) clean: $(Q)rm -rf $(OUT) +libclean: + $(Q)make -C lib/bossac/ clean + $(Q)make -C lib/rp2040_flash/ clean + $(Q)make -C lib/hidflash/ clean + distclean: clean $(Q)rm -f .config .config.old diff --git a/lib/bossac/src/OSXPortFactory.cpp b/lib/bossac/src/OSXPortFactory.cpp new file mode 100644 index 000000000000..bde8459cba5f --- /dev/null +++ b/lib/bossac/src/OSXPortFactory.cpp @@ -0,0 +1,106 @@ +/////////////////////////////////////////////////////////////////////////////// +// BOSSA +// +// Copyright (c) 2011-2018, ShumaTech +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of the nor the +// names of its contributors may be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/////////////////////////////////////////////////////////////////////////////// +#include "OSXPortFactory.h" +#include "PosixSerialPort.h" + +#include +#include + +#include + +OSXPortFactory::OSXPortFactory() +{ + _dir = opendir("/dev"); +} + +OSXPortFactory::~OSXPortFactory() +{ + if (_dir) + closedir(_dir); +} + +SerialPort::Ptr +OSXPortFactory::create(const std::string& name) +{ + bool isUsb = false; + + if (name.find("usb") != std::string::npos) + isUsb = true; + + return create(name, isUsb); +} + +SerialPort::Ptr +OSXPortFactory::create(const std::string& name, bool isUsb) +{ + PosixSerialPort *p = new PosixSerialPort(name, isUsb); + // Needed to avoid upload errors + p->setAutoFlush(true); + return SerialPort::Ptr(p); +} + +std::string +OSXPortFactory::begin() +{ + if (!_dir) + return end(); + + rewinddir(_dir); + + return next(); +} + +std::string +OSXPortFactory::next() +{ + struct dirent* entry; + + if (!_dir) + return end(); + + while ((entry = readdir(_dir))) + { + if (strncmp("cu.", entry->d_name, sizeof("cu.") - 1) == 0) + return std::string(entry->d_name); + } + + return end(); +} + +std::string +OSXPortFactory::end() +{ + return std::string(); +} + +std::string +OSXPortFactory::def() +{ + return begin(); +} diff --git a/lib/bossac/src/OSXPortFactory.h b/lib/bossac/src/OSXPortFactory.h new file mode 100644 index 000000000000..52b3bfdbcbfb --- /dev/null +++ b/lib/bossac/src/OSXPortFactory.h @@ -0,0 +1,60 @@ +/////////////////////////////////////////////////////////////////////////////// +// BOSSA +// +// Copyright (c) 2011-2018, ShumaTech +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// * Neither the name of the nor the +// names of its contributors may be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/////////////////////////////////////////////////////////////////////////////// +#ifndef _OSXPORTFACTORY_H +#define _OSXPORTFACTORY_H + +class OSXPortFactory; +#include "PortFactory.h" + +#include +#include + +#include + + +class OSXPortFactory : public PortFactoryBase +{ +public: + OSXPortFactory(); + virtual ~OSXPortFactory(); + + virtual std::string begin(); + virtual std::string end(); + virtual std::string next(); + virtual std::string def(); + + virtual SerialPort::Ptr create(const std::string& name); + virtual SerialPort::Ptr create(const std::string& name, bool isUsb); + +private: + std::string _empty; + DIR* _dir; +}; + +#endif // _OSXPORTFACTORY_H \ No newline at end of file diff --git a/scripts/dev-install-macos.sh b/scripts/dev-install-macos.sh new file mode 100644 index 000000000000..bc682f9b32c9 --- /dev/null +++ b/scripts/dev-install-macos.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# This script installs the necessary build tools for the MCU code on MacOS + +install_packages() +{ + PKGLIST="" + TAPLIST="osx-cross/avr osx-cross/arm" + # AVR chip installation and building + PKGLIST="${PKGLIST} avrdude avr-gcc" + # ARM chip installation and building + PKGLIST="${PKGLIST} stm32flash wxmac arm-gcc-bin" + + # Tap necessary package repos + report_status "Tapping formula repositories..." + brew tap ${TAPLIST} + brew update + + # Install desired packages + report_status "Installing packages..." + brew install ${PKGLIST} +} + +verify_ready() +{ + if [ "$EUID" -eq 0 ]; then + echo "This script must not run as root" + exit -1 + fi +} + +# Force script to exit if an error occurs +set -e + +verify_ready +install_packages \ No newline at end of file diff --git a/scripts/flash_usb.py b/scripts/flash_usb.py index e43c457c5cff..f69ee294dcb2 100755 --- a/scripts/flash_usb.py +++ b/scripts/flash_usb.py @@ -28,6 +28,12 @@ def enter_bootloader(device): def translate_serial_to_tty(device): ttyname = os.path.realpath(device) if not os.path.exists('/dev/serial/by-path/'): + if os.path.basename(ttyname).startswith("tty."): + fname = os.path.dirname(ttyname) + "/cu." + os.path.basename(ttyname).split(".", 1)[1] + if os.path.exists(fname): + return ttyname, fname + elif os.path.basename(ttyname).startswith("cu."): + return ttyname, ttyname raise error("Unable to find serial 'by-path' folder") for fname in os.listdir('/dev/serial/by-path/'): fname = '/dev/serial/by-path/' + fname From d5eb07de4378aac2b7c3c4a600037ca521c9dccc Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Fri, 8 Sep 2023 16:18:33 -0400 Subject: [PATCH 2/5] build: Fix whitespace errors Signed-off-by: Alan Everett --- scripts/dev-install-macos.sh | 2 +- scripts/flash_usb.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/dev-install-macos.sh b/scripts/dev-install-macos.sh index bc682f9b32c9..8637ee118e5f 100644 --- a/scripts/dev-install-macos.sh +++ b/scripts/dev-install-macos.sh @@ -32,4 +32,4 @@ verify_ready() set -e verify_ready -install_packages \ No newline at end of file +install_packages diff --git a/scripts/flash_usb.py b/scripts/flash_usb.py index f69ee294dcb2..ee233653b284 100755 --- a/scripts/flash_usb.py +++ b/scripts/flash_usb.py @@ -29,7 +29,8 @@ def translate_serial_to_tty(device): ttyname = os.path.realpath(device) if not os.path.exists('/dev/serial/by-path/'): if os.path.basename(ttyname).startswith("tty."): - fname = os.path.dirname(ttyname) + "/cu." + os.path.basename(ttyname).split(".", 1)[1] + fname = (os.path.dirname(ttyname) + "/cu." + + os.path.basename(ttyname).split(".", 1)[1]) if os.path.exists(fname): return ttyname, fname elif os.path.basename(ttyname).startswith("cu."): From 80e17f14fd5291941a0eb47b05b8b70af821d57b Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Fri, 8 Sep 2023 16:45:31 -0400 Subject: [PATCH 3/5] build: Fix errors in MacOS dev-install script The new install script had a few errors since I had installed the packages directly, and several of the packages underwent name changes since I originally wrote it. Signed-off-by: Alan Everett --- scripts/dev-install-macos.sh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/dev-install-macos.sh b/scripts/dev-install-macos.sh index 8637ee118e5f..beab1f0f30c6 100644 --- a/scripts/dev-install-macos.sh +++ b/scripts/dev-install-macos.sh @@ -4,15 +4,18 @@ install_packages() { PKGLIST="" - TAPLIST="osx-cross/avr osx-cross/arm" + TAPLIST="osx-cross/homebrew-avr osx-cross/homebrew-arm" # AVR chip installation and building PKGLIST="${PKGLIST} avrdude avr-gcc" # ARM chip installation and building - PKGLIST="${PKGLIST} stm32flash wxmac arm-gcc-bin" + PKGLIST="${PKGLIST} stm32flash wxwidgets arm-gcc-bin" # Tap necessary package repos report_status "Tapping formula repositories..." - brew tap ${TAPLIST} + for TAP in $TAPLIST + do + brew tap $TAP + done brew update # Install desired packages @@ -20,6 +23,12 @@ install_packages() brew install ${PKGLIST} } +# Helper functions +report_status() +{ + echo -e "\n\n###### $1" +} + verify_ready() { if [ "$EUID" -eq 0 ]; then From 288560a993467b428e6047196024a930ba205681 Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Fri, 8 Sep 2023 16:54:20 -0400 Subject: [PATCH 4/5] build: Add a few comments to MacOS install script Signed-off-by: Alan Everett --- scripts/dev-install-macos.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/dev-install-macos.sh b/scripts/dev-install-macos.sh index beab1f0f30c6..5db8a3d9966f 100644 --- a/scripts/dev-install-macos.sh +++ b/scripts/dev-install-macos.sh @@ -1,6 +1,8 @@ #!/bin/bash # This script installs the necessary build tools for the MCU code on MacOS +# Unlike other install scripts, it does not install the klippy host +# Step 1: Install system packages install_packages() { PKGLIST="" @@ -40,5 +42,6 @@ verify_ready() # Force script to exit if an error occurs set -e +# Run installation steps defined above verify_ready install_packages From 166180f682a910daa2fe7205ae620f5f936bdff1 Mon Sep 17 00:00:00 2001 From: thatcomputerguy0101 Date: Sun, 15 Oct 2023 16:59:29 -0400 Subject: [PATCH 5/5] build: Edit clang condition to utilize cpp version instead of OS version Rather than detecting if the host is using Darwin, the cpp version script now checks if it's version report includes clang, at which point it will replace cpp with clang (in preprocessor mode). This is slightly less intrusive then the previous iteration as the regular flow of the script is maintained for most operating systems and `CPP` is only changed when needed. Signed-off-by: Alan Everett --- Makefile | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 975275eb9c82..700c200914df 100644 --- a/Makefile +++ b/Makefile @@ -18,14 +18,13 @@ LD=$(CROSS_PREFIX)ld OBJCOPY=$(CROSS_PREFIX)objcopy OBJDUMP=$(CROSS_PREFIX)objdump STRIP=$(CROSS_PREFIX)strip +CPP=cpp PYTHON=python3 -UNAME_S := $(shell uname -s) -ifeq ($(UNAME_S),Darwin) -# cpp on MacOS forces a specific input directory, so directly use clang instead +CPP_VERSION := $(shell $(CPP) --version) +ifeq ($(findstring clang,$(CPP_VERSION)),clang) +# The clang version of cpp forces a specific input directory, so directly use clang instead CPP=clang -E -else -CPP=cpp endif # Source files