Skip to content

Instantly share code, notes, and snippets.

@pmcalabrese
Last active January 31, 2019 13:44
Show Gist options
  • Save pmcalabrese/19acf575a2f24cf22f396ccb2cb10c8c to your computer and use it in GitHub Desktop.
Save pmcalabrese/19acf575a2f24cf22f396ccb2cb10c8c to your computer and use it in GitHub Desktop.
Makefile for ZephyrOS Projects (only for mac)
# The intent of this Makefile is to facilitate the developing using ZephyrOS \
(on Mac), allows you to download the toolchain, compile and flash the project \
easly. \
\
Remember to source zephyr-env.sh from the zephyr folder or \
add . ~/dev/zephyr/zephyr-env.sh to your shall-resource file (.zshrc f exp.) \
\
The Makefile and the makefile.config need to be inside the project folder.\
You can download Makefile and the makefile.config with the following command:\
\
curl -Lo Makefile https://gist.github.com/pmcalabrese/19acf575a2f24cf22f396ccb2cb10c8c/raw/Makefile && curl -Lo makefile.config https://gist.github.com/pmcalabrese/19acf575a2f24cf22f396ccb2cb10c8c/raw/makefile.config
.SILENT: #don't echo commands as we run them.
-include makefile.config
init: ## Create a makefile.config file
if test "$(TARGET)" = "" ; then \
target_input_default="arduino_101"; \
else \
target_input_default="$(TARGET)"; \
fi && \
if test "$(TOOLCHAIN_URL)" = "" ; then \
toolchain_url_input_default="https://developer.arm.com/-/media/Files/downloads/gnu-rm/8-2018q4/gcc-arm-none-eabi-8-2018-q4-major-src.tar.bz2"; \
else \
toolchain_url_input_default="$(TOOLCHAIN_URL)"; \
fi && \
if test "$(TOOLCHAIN)" = "" ; then \
TOOLCHAIN_1=$${toolchain_url_input_default#\#*/} && \
toolchain_input_default="$${TOOLCHAIN_1/-src.tar.bz2/}"; \
else \
toolchain_input_default="$(TOOLCHAIN)"; \
fi && \
if test "$(TOOLCHAIN_FOLDER_PATH)" = "" ; then \
toolchain_folder_path_input_default="~/opt/"; \
else \
toolchain_folder_path_input_default="$(TOOLCHAIN_FOLDER_PATH)"; \
fi && \
if test "$(SERIAL_BOUD_RATE)" = "" ; then \
serial_boud_rate_input_default="1152000"; \
else \
serial_boud_rate_input_default="$(SERIAL_BOUD_RATE)"; \
fi && \
if test "$(SERIAL_DEVICE_PATH)" = "" ; then \
serial_device_path_input_default="/Volumes/MICROBIT"; \
else \
serial_device_path_input_default="$(SERIAL_DEVICE_PATH)"; \
fi && \
read -p "TARGET [$$target_input_default]: " target_input; \
if [ -z $${target_input} ]; then \
target_input=$$target_input_default; \
fi && \
read -p "TOOLCHAIN_URL [$$toolchain_url_input_default]:" toolchain_url_input; \
if [ -z $${toolchain_url_input} ]; then \
toolchain_url_input=$$toolchain_url_input_default; \
fi && \
read -p "TOOLCHAIN [$$toolchain_input_default]:" toolchain_input; \
if [ -z $${toolchain_input} ]; then \
toolchain_input=$$toolchain_input_default; \
fi && \
read -p "TOOLCHAIN_FOLDER_PATH [$$toolchain_folder_path_input_default]:" toolchain_folder_path_input; \
if [ -z $${toolchain_folder_path_input} ]; then \
toolchain_folder_path_input=$$toolchain_folder_path_input_default; \
fi && \
read -p "SERIAL_BOUD_RATE [$$serial_boud_rate_input_default]:" serial_boud_rate_input; \
if [ -z $${serial_boud_rate_input} ]; then \
serial_boud_rate_input=$$serial_boud_rate_input_default; \
fi && \
read -p "SERIAL_DEVICE_PATH [$$serial_device_path_input_default]:" serial_device_path_input; \
if [ -z $${serial_device_path_input} ]; then \
serial_device_path_input=$$serial_device_path_input_default; \
fi && \
echo ""; \
echo "\t$$target_input"; \
echo "\t$$toolchain_url_input"; \
echo "\t$$toolchain_input"; \
echo "\t$$toolchain_folder_path_input"; \
echo "\t$$serial_boud_rate_input"; \
echo "\t$$serial_device_path_input"; \
> ./makefile.config \
echo "TARGET = $$target_input" >> ./makefile.config && \
echo "TOOLCHAIN_URL = $$toolchain_url_input" >> ./makefile.config && \
echo "TOOLCHAIN = $$toolchain_input" >> ./makefile.config && \
echo "TOOLCHAIN_FOLDER_PATH = $$toolchain_folder_path_input" >> ./makefile.config && \
echo "SERIAL_BOUD_RATE = $$serial_boud_rate_input" >> ./makefile.config && \
echo "SERIAL_DEVICE_PATH = $$serial_device_path_input" >> ./makefile.config
check_compile:
if test "$(TOOLCHAIN)" = "" ; then \
echo "\n\n ERROR: TOOLCHAIN not set, run make init.\n\n"; \
exit 1; \
fi && \
if test "$(TARGET)" = "" ; then \
echo "\n\n ERROR: TARGET not set, run make init.\n\n"; \
exit 1; \
fi
compile: check_compile ## Compile
if [ ! -d ./build ]; then mkdir -p build; fi
echo "\n\nCompiling with toolchain $(TOOLCHAIN) for the board $(TARGET)\n\n"
cd build && \
export GNUARMEMB_TOOLCHAIN_PATH=$(TOOLCHAIN_FOLDER_PATH)$(TOOLCHAIN) && \
export ZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb && \
cmake -GNinja -DBOARD=$(TARGET) .. && \
ninja
clean: ## Clean by removing the build folder
printf "\nCleaning..." && \
rm -fr build && \
printf "done\n"
check_download_gcc_arm:
if test "$(TOOLCHAIN_URL)" = "" ; then \
echo "\n\n ERROR: TOOLCHAIN_URL not set, run make init.\n\n"; \
exit 1; \
fi && \
if test "$(TOOLCHAIN)" = "" ; then \
echo "\n\n ERROR: TOOLCHAIN not set, run make init.\n\n"; \
exit 1; \
fi
download_gcc_arm: check_download_gcc_arm ## Download toolchian GCC ARM
echo "\nDownloading the toolchain from $(TOOLCHAIN_URL)\n" && \
curl -L -o '$(TOOLCHAIN)-mac' '$(TOOLCHAIN_URL)'
printf "\nDownloading...done\n\nThe toolchain will be installed in the folder: $(TOOLCHAIN_FOLDER_PATH)$(TOOLCHAIN) ... " && \
tar xjf $(TOOLCHAIN)-mac
rm $(TOOLCHAIN)-mac
mv $(TOOLCHAIN) $(TOOLCHAIN_FOLDER_PATH)
chmod -R -w $(TOOLCHAIN_FOLDER_PATH)
printf "done\n"
flash: ## Flash target
cp ./build/zephyr/zephyr.hex $(SERIAL_DEVICE_PATH)
serial: ## Open serial (use screen)
echo "Open serial at: $(SERIAL_DEVICE_PATH) with $(SERIAL_BOUD_RATE) boud rate" \
screen $(SERIAL_DEVICE_PATH) $(SERIAL_BOUD_RATE)
.PHONY: help
help:
IFS=$$'\n' ; \
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
printf "%-26s %-66s %s\n" "target" "help" "usage" ; \
printf "%-26s %-66s %s\n" "------" "----" "-----" ; \
for help_line in $${help_lines[@]}; do \
IFS=$$':' ; \
help_split=($$help_line) ; \
help_command=`echo $${help_split[0]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
help_info=`echo $${help_split[2]} | sed -e 's/^ *//' -e 's/ *$$//'` ; \
help_usage="$${help_command}_usage" ; \
printf '\033[36m'; \
printf "%-26s %s" $$help_command ; \
printf '\033[0m'; \
printf "%-66s %s" $$help_info; \
printf "%s \n" $${!help_usage}; \
done
.DEFAULT_GOAL := help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment