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

build: Add support for building mcu code on MacOS #6337

Closed
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
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ STRIP=$(CROSS_PREFIX)strip
CPP=cpp
PYTHON=python3

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
endif

# Source files
src-y =
dirs-y = src
Expand Down Expand Up @@ -65,7 +71,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 $@"
Expand Down Expand Up @@ -118,14 +124,19 @@ 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)

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

Expand Down
106 changes: 106 additions & 0 deletions lib/bossac/src/OSXPortFactory.cpp
Original file line number Diff line number Diff line change
@@ -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 <organization> 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 <COPYRIGHT HOLDER> 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 <string.h>
#include <stdio.h>

#include <string>

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();
}
60 changes: 60 additions & 0 deletions lib/bossac/src/OSXPortFactory.h
Original file line number Diff line number Diff line change
@@ -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 <organization> 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 <COPYRIGHT HOLDER> 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 <sys/types.h>
#include <dirent.h>

#include <string>


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
47 changes: 47 additions & 0 deletions scripts/dev-install-macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/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=""
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 wxwidgets arm-gcc-bin"

# Tap necessary package repos
report_status "Tapping formula repositories..."
for TAP in $TAPLIST
do
brew tap $TAP
done
brew update

# Install desired packages
report_status "Installing packages..."
brew install ${PKGLIST}
}

# Helper functions
report_status()
{
echo -e "\n\n###### $1"
}

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

# Run installation steps defined above
verify_ready
install_packages
7 changes: 7 additions & 0 deletions scripts/flash_usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ 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
Expand Down