Skip to content

Commit 7d7d530

Browse files
committed
Use config dump for data directory + generate platform path
1 parent 6f2978d commit 7d7d530

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

build_platform.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,19 +479,54 @@ def main():
479479
# Inject boards.local.txt if requested
480480
if COPY_BOARDS_LOCAL_TXT and boards_local_txt:
481481
try:
482-
# Find platform path via `arduino-cli core list --format json`
482+
# Get arduino-cli data directory
483483
import json
484-
output = subprocess.check_output(["arduino-cli", "core", "list", "--format", "json"]).decode()
485-
core_list = json.loads(output)["platforms"]
486-
for core in core_list:
487-
if core["id"] == core_fqbn:
488-
platform_path = core["install_dir"]
484+
config_output = subprocess.check_output(["arduino-cli", "config", "dump", "--format", "json"]).decode()
485+
config = json.loads(config_output)
486+
ColorPrint.print_info(f"Using arduino-cli config: {config_output.strip()}")
487+
488+
# Extract data directory, with fallback to default
489+
data_dir = config.get("directories", {}).get("data", "")
490+
if not data_dir:
491+
ColorPrint.print_warn("No data directory found in arduino-cli config, using fallback locations.")
492+
# Fallback to common default locations
493+
if os.name == 'nt': # Windows
494+
data_dir = os.path.join(os.environ.get('LOCALAPPDATA', ''), 'Arduino15')
495+
else: # Linux/macOS
496+
data_dir = os.path.join(os.environ.get('HOME', ''), '.arduino15')
497+
ColorPrint.print_info(f"Using data directory: {data_dir}")
498+
499+
# Parse platform vendor and architecture from core_fqbn (e.g., "adafruit:samd")
500+
parts = core_fqbn.split(':')
501+
if len(parts) >= 2:
502+
vendor = parts[0]
503+
architecture = parts[1]
504+
else:
505+
vendor = core_fqbn
506+
architecture = vendor
507+
508+
ColorPrint.print_info(f"Using vendor: {vendor}, architecture: {architecture}")
509+
510+
# Construct base platform path
511+
platform_base = os.path.join(data_dir, "packages", vendor, "hardware", architecture)
512+
513+
# Find the latest version directory
514+
if os.path.exists(platform_base):
515+
versions = [d for d in os.listdir(platform_base) if os.path.isdir(os.path.join(platform_base, d))]
516+
ColorPrint.print_info(f"Found versions: {versions}")
517+
if versions:
518+
# Sort versions and take the latest (could be improved with proper version sorting)
519+
latest_version = sorted(versions)[-1]
520+
platform_path = os.path.join(platform_base, latest_version)
521+
489522
dest_path = os.path.join(platform_path, "boards.local.txt")
490523
shutil.copyfile(boards_local_txt, dest_path)
491524
ColorPrint.print_info(f"Copied boards.local.txt to {dest_path}")
492-
break
525+
else:
526+
ColorPrint.print_warn(f"No version directories found in {platform_base}")
493527
else:
494-
ColorPrint.print_warn(f"Could not find platform path for {core_fqbn} - not copying boards.local.txt")
528+
ColorPrint.print_warn(f"Platform path {platform_base} does not exist")
529+
495530
except Exception as e:
496531
ColorPrint.print_fail(f"Error injecting boards.local.txt for {core_fqbn}: {e}")
497532
print('#'*80)

0 commit comments

Comments
 (0)