Build with Arduino-Makefile

1.0.0

By alxm, 4 months ago

This is a short tutorial for building games with Arduino-Makefile from the command line, instead of with the Arduino IDE. I ran these steps on 64bit Linux, but Arduino-Makefile is compatible with Mac and Windows too!

Why? I was already using an external code editor, so why not bypass the IDE all the way? Building this way is a lot faster too, I'm able to compile example sketches in 4 sec vs 14 sec with the IDE!

I first wrote this article on my website, check it out for a bit of extra content like customizing your project's structure.

1. Install Arduino

Install the Gamebuino META dev environment as normal, so you can build your game with the IDE.

2. Install Arduino-Makefile

I cloned the latest version to my home folder:

$ cd ~
$ git clone https://github.com/sudar/Arduino-Makefile

3. Choose a Project

Pick a sketch you want to build, and create a new file called Makefile in its source folder (where the .ino file is). I'll use the Gamebuino META a_Hello example here:

$ cd ~/Arduino/libraries/Gamebuino_META/examples/1.Basics/a_Hello
$ touch Makefile
$ ls
a_Hello.ino Makefile

4. Edit Makefile

Note that the ARDUINO_DIR, ARDMK_DIR, and ALTERNATE_CORE_PATH paths below may be different on your system. You can check what they are by building with the IDE and looking at the output (remember to turn Verbose On from File > Preferences).

# Paths to Arduino IDE and cloned Arduino-Makefile
ARDUINO_DIR = $(HOME)/arduino
ARDMK_DIR = $(HOME)/Arduino-Makefile

# Specify the custom Gamebuino board and where it's installed BOARD_TAG = gamebuino_meta_native ALTERNATE_CORE_PATH =
$(HOME)/.arduino15/packages/gamebuino/hardware/samd/1.2.1

# The Arduino libraries the game uses ARDUINO_LIBS = Gamebuino_META SPI

# The META lib expects SKETCH_NAME to be defined CFLAGS += -D__SKETCH_NAME__="$(TARGET).ino" CXXFLAGS += -D__SKETCH_NAME__="$(TARGET).ino"

# Now that everything is configured, include Arduino-Makefile include $(ARDMK_DIR)/Sam.mk

5. Build & Upload

$ make # Compile code and build .BIN file $ make upload # Flash to Gamebuino $ make clean # Remove all build files

All of these just work, even upload! You can build even faster with make -j, which runs commands in parallel.

Last comments

avatar
Steph
1 month ago

Thank you very much Alex for sharing your technical tips with us. The community has a great toolkit with your contribution and that of bfx.

avatar
alxm
1 month ago

Hi,

I just published an update that makes this all work again after the big Arduino SAMD update this summer. Hereโ€™s the core Makefile you can drop in the source folder to build with just make:

# Where you unzipped the Arduino IDE and cloned Arduino-Makefile
ARDUINO_DIR = $(HOME)/arduino
ARDMK_DIR = $(HOME)/Arduino-Makefile

# Specify the custom Gamebuino board and where it's installed
BOARD_TAG = gamebuino_meta_native
ALTERNATE_CORE_PATH = \
    $(wildcard $(HOME)/.arduino15/packages/gamebuino/hardware/samd/1.*)

# The Arduino libraries the game uses
ARDUINO_LIBS = Gamebuino_META SPI

# Now that everything is configured, include Arduino-Makefile
include $(ARDMK_DIR)/Sam.mk

# The META lib expects __SKETCH_NAME__ to be defined,
# and doesn't build well with Link Time Optimization
CFLAGS += -D__SKETCH_NAME__="$(TARGET).ino" -fno-lto
CXXFLAGS += -D__SKETCH_NAME__="$(TARGET).ino" -fno-lto
LDFLAGS += -fno-lto

Aside from fixing the hardcoded boards version, the -fno-lto flags at the end are also necessary after the update to GCC 7 (Arduino-Makefile uses -flto if GCC >4.9, but that seems to have some problems on ARM targets).

Hope others find this useful, happy building :slight_smile:

avatar
gamebuino-admin
4 months ago
Author :

ย Sorunome


This is cool! Just saw that you hard-coded the version of the arduino boards in your description which was just updated to 1.2.2 though, to work with the newest samd board definitions. so yeah ^^