浏览代码

esp32: make the arduino build environment self-contained

Don't rely on the global build environment (other than the existence
of arduino-cli); instead do a profile build. However, arduino-cli is
really annoying in the way it handles build properties, requiring a
long command line, so create a wrapper to pick up the properties from
a sketch-specific config file (this really should be in sketch.yaml.)

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
H. Peter Anvin 2 月之前
父节点
当前提交
fe09c8cc4c
共有 6 个文件被更改,包括 137 次插入31 次删除
  1. 1 1
      esp32/.gitignore
  2. 12 30
      esp32/Makefile
  3. 16 0
      esp32/arduino-cli.yaml
  4. 87 0
      esp32/arduino-wrapper
  5. 9 0
      esp32/max80/properties.txt
  6. 12 0
      esp32/max80/sketch.yaml

+ 1 - 1
esp32/.gitignore

@@ -5,6 +5,7 @@
 applet/
 build/
 cache/
+arduino/
 *.bootloader.bin
 *.partitions.bin
 *.elf
@@ -14,4 +15,3 @@ max80/src/common
 max80/debug.cfg
 max80/debug_custom.json
 max80/esp32s2.svd
-max80/sketch.yaml

+ 12 - 30
esp32/Makefile

@@ -1,6 +1,9 @@
 MAKEFLAGS    += -R -r
 
-ARDUINO_CLI   = arduino-cli
+dstdir	     := $(shell pwd)
+export dstdir
+
+ARDUINO       = $(PERL) $(realpath arduino-wrapper)
 ZIP	      = zip
 PERL	      = perl
 
@@ -10,37 +13,16 @@ GENFILES      = www.zip
 WWW	      = www
 PORT	     ?= /dev/ttyACM0
 
-build_defines = -DBOARD_HAS_PSRAM -I. -Isrc/common
-
-BOARD	      = esp32:esp32:esp32s2usb
-ARDUINO_OPTS  = -b $(BOARD) \
-		--warnings all \
-		--build-path       ../build \
-		--output-dir       ../output \
-		--build-cache-path ../cache \
-		--build-property 'build.flash_size=4MB' \
-		--build-property 'build.defines=$(build_defines)' \
-		--build-property 'build.cdc_on_boot=1' \
-		--build-property 'build.msc_on_boot=0' \
-		--build-property 'build.dfu_on_boot=0' \
-		--build-property 'build.partitions=min_spiffs'
-
 all: $(TARGET)
 
-properties:
-	cd $(SKETCH) && \
-		$(ARDUINO_CLI) compile --show-properties $(ARDUINO_OPTS)
-
 common = $(SKETCH)/src/common
 
-$(TARGET): $(shell find $(SKETCH) -type f) $(GENFILES) $(common)
-	mkdir -p build output cache
-	cd $(SKETCH) && \
-		$(ARDUINO_CLI) compile $(ARDUINO_OPTS)
-
-$(common):
-	mkdir -p $(@D)
-	cd $(@D) && ln -sf ../../../common .
+$(TARGET): $(shell find $(SKETCH) -type f) $(GENFILES)
+	if ! test -L '$(common)'; then	\
+		rm -rf '$(common)' ; \
+		cd '$(SKETCH)/src' && ln -sf ../../../common . ; \
+	fi
+	cd $(SKETCH) && $(ARDUINO) compile
 
 .PHONY: zip
 zip: zipexclude
@@ -60,10 +42,10 @@ www.zip: zip
 	fi
 
 upload: $(TARGET)
-	$(ARDUINO_CLI) upload -i $(TARGET) -p $(PORT) -b $(BOARD) $(SKETCH)
+	cd $(SKETCH) && $(ARDUINO) upload  -p $(PORT) $(SKETCH)
 
 clean:
 	rm -rf build cache zip $(GENFILES) $(common)
 
 spotless: clean
-	rm -rf output
+	rm -rf arduino output

+ 16 - 0
esp32/arduino-cli.yaml

@@ -0,0 +1,16 @@
+board_manager:
+  additional_urls:
+  - https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
+library:
+  enable_unsafe_install: false
+logging:
+  format: text
+  level: info
+metrics:
+  enabled: false
+output:
+  no_color: true
+sketch:
+  always_export_binaries: true
+updater:
+  enable_notification: false

+ 87 - 0
esp32/arduino-wrapper

@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+#
+# Wrapper for calling arduino-cli with a bunch of obligatory options...
+#
+
+use File::Spec;
+use Cwd qw(abs_path realpath);
+use strict;
+use integer;
+
+sub dirname($) {
+    my($path) = @_;
+    $path = File::Spec->rel2abs($path);
+    my($vol,$dir,$file) = File::Spec->splitpath($path);
+    return File::Spec->catpath($vol, $dir, '');
+}
+
+my $here = realpath(dirname($0));
+
+my $arduino = File::Spec->catdir($here, 'arduino');
+$ENV{'ARDUINO_DIRECTORIES_DATA'} = $arduino;
+$ENV{'ARDUINO_DIRECTORIES_USER'} = $arduino;
+$ENV{'ARDUINO_LOGGING_FILE'} = File::Spec->catfile($arduino, 'arduino.log');
+$ENV{'ARDUINO_CONFIG_FILE'} = File::Spec->catfile($here, 'arduino_cli.yaml');
+
+my $arduino_cli = $ENV{'ARDUINO_CLI'} || 'arduino-cli';
+
+mkdir($arduino);
+
+my $subcmd = shift(@ARGV);
+my @args = ($subcmd);
+my %props;
+
+if (open(my $prop, '<', 'properties.txt')) {
+    while (defined(my $p = <$prop>)) {
+	chomp $p;
+	next if ($p !~ /^\s*([\w.]+)\s*=\s*(.*)$/);
+	$props{$1} = $2;
+    }
+    close($prop);
+}
+
+if ($subcmd eq 'compile') {
+    my %named_opts = (
+	'build.fqbn' => '--fqbn',
+	'build.path' => '--build-path',
+	'build.output.path' => '--output-dir'
+	);
+
+    foreach my $pn ('build.path', 'build.output.path') {
+	# Make sure directories exist
+	my $pv = $props{$pn};
+	mkdir($pv) if (defined($pv));
+    }
+
+    foreach my $pn (sort(keys(%props))) {
+	my $pv = $props{$pn};
+	my $opt_name = $named_opts{$pn};
+	if ($opt_name) {
+	    push(@args, $opt_name, $pv);
+	} elsif ($pn =~ /^build\./) {
+	    push(@args, '--build-property', "$pn=$pv");
+	}
+    }
+} elsif ($subcmd eq 'upload') {
+    my %named_opts = (
+	'build.fqbn'        => '--fqbn',
+	'build.path'        => '--build-path',
+	'build.output.path' => '--input-dir'
+	);
+
+    foreach my $pn (sort(keys(%props))) {
+	my $pv = $props{$pn};
+	my $opt_name = $named_opts{$pn};
+	if ($opt_name) {
+	    push(@args, $opt_name, $pv);
+	} elsif ($pn =~ /^upload\./) {
+	    push(@args, '--upload-property', "$pn=$pv");
+	}
+    }
+}
+
+push(@args, @ARGV);
+
+exec($arduino_cli, @args);
+print STDERR "$0: failed to execute: $arduino_cli ", join(' ', @args), "\n";
+exit 1;

+ 9 - 0
esp32/max80/properties.txt

@@ -0,0 +1,9 @@
+build.fqbn=esp32:esp32:esp32s2usb
+build.path=../build
+build.output.path=../output
+build.flash_size=4MB
+build.defines=-DBOARD_HAS_PSRAM -I. -Isrc/common
+build.cdc_on_boot=1
+build.msc_on_boot=0
+build.dfu_on_boot=0
+build.partitions=min_spiffs

+ 12 - 0
esp32/max80/sketch.yaml

@@ -0,0 +1,12 @@
+profiles:
+  max80-esp32:
+    fqbn: esp32:esp32:esp32s2usb
+    notes: ESP32S2 build profile for the MAX80 card
+    platforms:
+      - platform: esp32:esp32 (2.0.14)
+        platform_index_url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
+    libraries:
+      - unzipLIB (1.0.0)
+      - incbin (0.1.2)
+
+default_profile: max80-esp32