Linux makefile

Creations

Chimrod

5 years ago

Hello !


This is my first post on the forum :-) I didn't receive my gamebuino yet, but I've started to work with the emulator.


I'm here to share with you a Makefile for compiling a sketch without running arduino IDE. This makefile will automaticaly create the .bin and the .hex file, as arduino would have.


It assume that you already have configured correctly the arduino environment (actualy, the Makefile relies on the arduino binaries)

Makefile

LOCAL_PATH=~/.arduino15
ARDUINO_PATH=~/Téléchargements/arduino-1.8.5

SKETCHBOOK_PATH=$(shell grep sketchbook $(LOCAL_PATH)/preferences.txt | cut -d= -f2)

.PHONY: clean

tmp/%.deps: $(LOCAL_PATH)/package_%_index.json tmp
    python3 config.py $(LOCAL_PATH) < $< > $@

%.bin %.hex: %.ino *.h tmp/gamebuino.deps | tmp

    cp $< tmp/sketch/
    cp *.h tmp/sketch/

    $(ARDUINO_PATH)/arduino-builder \
        -compile \
        -logger=machine \
        -hardware $(ARDUINO_PATH)/hardware \
        -hardware $(LOCAL_PATH)/packages \
        -hardware $(SKETCHBOOK_PATH)/hardware \
        -tools $(ARDUINO_PATH)/tools-builder \
        -tools $(ARDUINO_PATH)/hardware/tools/avr \
        -tools $(LOCAL_PATH)/packages \
        -built-in-libraries $(ARDUINO_PATH)/libraries \
        -libraries $(SKETCHBOOK_PATH)/libraries \
        -fqbn=gamebuino:samd:gamebuino_meta_native \
        -ide-version=10805 \
        -build-path $$(realpath tmp/build) \
        -warnings=none \
        -build-cache $$(realpath tmp/cache) \
        -prefs=build.warn_data_percentage=75 \
        $$(cat tmp/gamebuino.deps) \
        -verbose \
        "tmp/sketch/$<"

    cp tmp/build/$<.bin $*.bin
    cp tmp/build/$<.hex $*.hex

    rm -r tmp/sketch/$<

tmp:
    mkdir -p tmp/sketch
    mkdir -p tmp/build
    mkdir -p tmp/cache

clean:
    rm -r tmp

You have to edit the two first line in the Makefile according with the arduino installation directory.


There is also a python script which extract all the dependancies from the gamebuino configuration file :

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, json

if __name__ == "__main__":
    deps = json.load(sys.stdin)['packages'][0]['platforms'][0]['toolsDependencies']
    for dep in deps:
        print("-prefs=runtime.tools.%s.path=%s/packages/%s/tools/%s/%s/" % \
                (dep['name'],\
                sys.argv[1], \
                dep['packager'],\
                dep['name'],\
                dep['version']), \
            end=' ')
        sys.stdout.flush()


Just put the two files in the same directory as the ino project. Now you can run make sketch.binor make sketch.hex in order the compile your project.

The Makefile run for me (debian stable), and my fail for you. Please tell me if you encountered some trouble (and better, how to make this script better :) )

View full creation

Sorunome

NEW 5 years ago

This looks nifty! Are you aware of current projects such as https://github.com/sudar/Arduino-Makefile ?

Chimrod

5 years ago

No, arduino-mk is in the Debian distribution, but the package is actualy so out-of-date that I didn't look to the source project (I may be wrong).


I've started with a simple bash shell, which became this Makefile : probably a lot simpler, but I exactly know what it does :)

Chimrod

NEW 5 years ago

Sorunome Sorunome

No, arduino-mk is in the Debian distribution, but the package is actualy so out-of-date that I didn't look to the source project (I may be wrong).


I've started with a simple bash shell, which became this Makefile : probably a lot simpler, but I exactly know what it does :)

josephscade

NEW 5 years ago

Hello,

Thanks for this tutorial, I think you forget to mention that you have to name the Python program "config.py".

Also, is there a way to use the version of GCC provided by your Distribution instead of the bundled one ? (Asking because Arduino IDE ships GCC 4.8 which is very old and not maintened, according to the GCC official website).

drummyfish

NEW 5 years ago

Thank you kind sir, this is exactly what I need :-)