Skip to content

Commit

Permalink
Support for multi-os Python install
Browse files Browse the repository at this point in the history
`/usr/bin/env` being used in scripts at the moment is not acceptable in
certain distributions. macOS system Python is generally not used by
developers in favour of Homebrew Python - this installs to
`/usr/bin/local`.

One cannot support both install locations `/usr/bin` and
`/usr/bin/local` without using `/usr/bin/env` in the shebang. To remedy
this, the Makefile resolves the shell `python` (3 first, then any) executable to
`PYTHON_CMD` and calls the scripts with this binary.
  • Loading branch information
tuna-f1sh committed Dec 21, 2019
1 parent 5076bf4 commit 87d5241
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Arduino.mk
Expand Up @@ -853,15 +853,15 @@ endif
# Reset

ifndef RESET_CMD
ARD_RESET_ARDUINO := $(shell which ard-reset-arduino 2> /dev/null)
ARD_RESET_ARDUINO := $(PYTHON_CMD) $(shell which ard-reset-arduino 2> /dev/null)
ifndef ARD_RESET_ARDUINO
# same level as *.mk in bin directory when checked out from git
# or in $PATH when packaged
ARD_RESET_ARDUINO = $(ARDMK_DIR)/bin/ard-reset-arduino
ARD_RESET_ARDUINO = $(PYTHON_CMD) $(ARDMK_DIR)/bin/ard-reset-arduino
endif
ifneq (,$(findstring CYGWIN,$(shell uname -s)))
# confirm user is using default cygwin unix Python (which uses ttySx) and not Windows Python (which uses COMx)
ifeq ($(shell which python),/usr/bin/python)
ifeq ($(PYTHON_CMD),/usr/bin/python)
RESET_CMD = $(ARD_RESET_ARDUINO) $(ARD_RESET_OPTS) $(DEVICE_PATH)
else
RESET_CMD = $(ARD_RESET_ARDUINO) $(ARD_RESET_OPTS) $(call get_monitor_port)
Expand Down
21 changes: 21 additions & 0 deletions Common.mk
Expand Up @@ -98,3 +98,24 @@ ifeq ($(CURRENT_OS),WINDOWS)
echo $(error On Windows, ARDUINO_DIR and other defines must use forward slash and not contain spaces, special characters or be cygdrive relative)
endif
endif

########################################################################
# System Python

ifndef PYTHON_CMD
# try for Python 3 first
PYTHON_CMD := $(shell which python3 2> /dev/null)
ifdef PYTHON_CMD
$(call show_config_variable,PYTHON_CMD,[AUTODETECTED])
else
# fall-back to any Python
PYTHON_CMD := $(shell which python 2> /dev/null)
ifdef PYTHON_CMD
$(call show_config_variable,PYTHON_CMD,[AUTODETECTED])
else
echo $(error "Unable to find system Python! Utility scipts won't work. Override this error by defining PYTHON_CMD")
endif
endif
else
$(call show_config_variable,PYTHON_CMD,[USER])
endif
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -33,6 +33,7 @@ I tried to give credit whenever possible. If I have missed anyone, kindly add it
- New: Updated Arch instructions. (https://github.com/Akram-Chehaima)
- New: Add support for Robotis OpenCR 1.0 boards.
- New: Build the ArduinoCore API
- New: Support for Python 3 and multi-os Python installation using new PYTHON_CMD variable.

### 1.6.0 (2017-07-11)
- Fix: Allowed for SparkFun's weird usb pid/vid submenu shenanigans (issue #499). (https://github.com/sej7278)
Expand Down
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -123,10 +123,11 @@ On Arch:
sudo pacman -S python-pyserial
```

On Mac using MacPorts:
On macOS using Homebrew (one can install to System Python but this is not recommend or good practice):

```sh
sudo port install py27-serial
brew install python
pip3 install pyserial
```

On Windows:
Expand Down
16 changes: 16 additions & 0 deletions arduino-mk-vars.md
Expand Up @@ -134,6 +134,22 @@ RESET_CMD = $(HOME)/gertduino/reset

----

### PYTHON_CMD

**Description:**

Path to Python binary. Requires pyserial module installed. Makefile will error if unable to auto-find as utility scripts will not work. To override this, give it an empty define.

**Example:**

```Makefile
PYTHON_CMD = /usr/bin/python3
```

**Requirement:** *Optional*

----

## Arduino IDE variables

### ARDUINO_DIR
Expand Down
2 changes: 1 addition & 1 deletion bin/ard-reset-arduino
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/python3

from __future__ import print_function
import serial
Expand Down
2 changes: 1 addition & 1 deletion bin/ardmk-init
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/python3
"""
Arduino-mk Makefile and project initialiser
Expand Down
2 changes: 1 addition & 1 deletion bin/robotis-loader
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3

# This script sends a program on a robotis board (OpenCM9.04 or CM900)
# using the robotis bootloader (used in OpenCM IDE)
Expand Down

0 comments on commit 87d5241

Please sign in to comment.