Skip to content

Commit 9f96c5e

Browse files
JoppeJoppe
authored andcommitted
[sh] REFACTOR: Consolidate OS specifics
1 parent 0efbf0f commit 9f96c5e

1 file changed

Lines changed: 147 additions & 124 deletions

File tree

make.mk

Lines changed: 147 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ _NAME := $(notdir $(realpath $/.))
104104
#### Recommended dependencies
105105

106106
# `bringup`: All code in the project is executable as bare shell commands, including .py files.
107-
# `build/*.py.bringup`: *.py is executable by the venv/bin/python3 next to it.
108-
# `build/*.py.shebang`: *.py has the local python shebang #!venv/bin/python3
107+
# `build/*.py.bringup`: *.py is executable by the venv python next to it.
108+
# `build/*.py.shebang`: *.py has a local shebang suitable for the OS (#!venv/bin/python3 or #!venv/Scripts/python.exe)
109109
# `build/directory-name.bringup`: directory-name is an executable that contains all compilable sources.
110110
# `build/*.tested`: * is self-tested without failure, and the test output is here.
111111
# `build/*.md`: * is documented here. Make *.gfm *.pdf *.html to translate it to your desired pandoc format.
@@ -149,7 +149,6 @@ $/python_executable_shell_example:
149149
# * `touch __init__.py` on the complete path to it. Now you can import and use it.
150150

151151
touch example/__init__.py
152-
venv/bin/python3 -c "import example; open('greeter-from-py.txt).write(example.greeter.hello())"
153152

154153

155154
## Parameters
@@ -185,18 +184,16 @@ ifeq ($~,/Users/$I)
185184
~ := /home/$I
186185
endif
187186

188-
# A base PYTHON to use. It can be one of:
189-
# - `python3` on Ubuntu or Mac,
190-
# - `python.exe` in Windows from WSL Ubuntu, or
191-
# - `python` on Windows.
192-
# - or any given PYTHON.
193-
ifeq ($~,/home/$I)
194-
SHELL ?= Linux
195-
PYTHON ?= python3
196-
VENV_PYTHON ?= bin/python3
187+
# A base PYTHON & OS manager. It can be one of:
188+
# - python3 & apt on Ubuntu
189+
# - python & choco in Windows
190+
# - python3 & brew on MacOS
191+
# - or any given PYTHON=python-interpreter !=OS-install-command ?=OS-install-directory
192+
ifeq ($~,/home/$I) # Probably Bash on Ubuntu
193+
CPU = $(shell uname -m)
194+
ifneq (,$(shell echo $$WSL_DISTRO_NAME)) # Probably Bash on Windows WSL Ubuntu
195+
OS ?= WSL
197196

198-
ifneq (,$(shell echo $$WSL_DISTRO_NAME))
199-
SHELL := WSL
200197
# Clone Windows home git and ssh settings
201198
ifndef H
202199
H := $(shell wslpath "$(cmd.exe /C echo '%USERPROFILE%' | head -c -2)")
@@ -209,78 +206,140 @@ ifeq ($~,/home/$I)
209206
# Workaround Windows WSL bridge bug: Timeout on ipv6 internet routes - slows down pip.
210207
SPEEDUP_WSL_DNS ?= $~/use_windows_dns.sh
211208
SPEEDUP_WSL_PIP ?= DISPLAY= #
209+
$~/use_windows_dns.sh:
210+
echo "# Fixing DNS issue in WSL https://gist.github.com/ThePlenkov/6ecf2a43e2b3898e8cd4986d277b5ecf#file-boot-sh" > $@
211+
echo -n "sed -i '/nameserver/d' /etc/resolv.conf && " >> $@
212+
echo -n "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command " >> $@
213+
echo -n "'(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | " >> $@
214+
echo -n "ForEach-Object { \"nameserver \$$_\" }' | tr -d '\\r' | " >> $@
215+
echo "tee -a /etc/resolv.conf > /dev/null" >> $@
216+
sudo sed -i '\|command=$@|d' /etc/wsl.conf
217+
echo "command=$@" | sudo tee -a /etc/wsl.conf > /dev/null
218+
sudo sh $@
219+
else
220+
OS ?= Linux
212221
endif
213-
else ifeq ($~,/c/Users/$I)
214-
SHELL ?= Git Bash
215-
PYTHON ?= python.exe
216-
VENV_PYTHON ?= Scripts/python.exe
217-
else
218-
SHELL ?= cmd
219-
PYTHON ?= python
220-
VENV_PYTHON ?= python.exe
221-
endif
222+
%-on-Linux-path: ~/.profile
223+
echo 'export PATH="$*:$$PATH"' >> $<
224+
false # Please `source $<` or open a new shell to get $* on PATH, and retry `make $(MAKECMDGOALS)`.
222225

223-
# Turn PYTHON into an explicit path
224-
override PYTHON := $(shell which $(PYTHON))
225-
PIPS = $(shell $(PYTHON) -m make --pips)
226-
$/venv/pip/ := $/venv/$(PIPS)
226+
# Default to an installable Conda python interpreter
227+
CONDA_DIR ?= ~/miniconda3/
228+
PYTHON ?= $(CONDA_DIR)bin/python3
229+
VENV_PYTHON ?= bin/python3
227230

228-
# A package manager for the PYTHON OS
229-
ifndef !
230-
UNAME ?= $(shell which uname)
231-
ifeq (,$(UNAME))
232-
UNAME := $(PYTHON) -m make
231+
# Default to an existing Apt package manager
232+
! ?= sudo apt update && sudo apt install -y
233+
? ?= /usr/bin
234+
235+
# Default to installable Pandoc fonts
236+
COUSINE ?= /usr/share/fonts/truetype/cousine
237+
CARLITO ?= /usr/share/fonts/truetype/crosextra
238+
239+
# Installable python interpreter, document compiler and fonts on Ubuntu
240+
$(CONDA_DIR)bin/python3: Miniconda3-latest-Linux-$(CPU).sh | $(CONDA_DIR)
241+
bash $< -bfup $| && \
242+
rm $<
243+
$?/xetex:
244+
# Need a document compiler: Texlive
245+
$! texlive-xetex
246+
/usr/share/fonts/truetype/crosextra/Carlito-Regular.ttf:
247+
# Need a more screen-readable normal font: Carlito
248+
sudo apt-get install fonts-crosextra-carlito
249+
/usr/share/fonts/truetype/cousine/Cousine-Regular.ttf:
250+
# Need a more screen-readable fixed-size font: Cousine
251+
( sudo mkdir -p $(dir $@) && cd $(dir $@) && \
252+
fonts=https://raw.githubusercontent.com/google/fonts/main/apache && \
253+
sudo wget $$fonts/cousine/DESCRIPTION.en_us.html && \
254+
sudo wget $$fonts/cousine/Cousine-Bold.ttf && \
255+
sudo wget $$fonts/cousine/Cousine-BoldItalic.ttf && \
256+
sudo wget $$fonts/cousine/Cousine-Italic.ttf && \
257+
sudo wget $$fonts/cousine/Cousine-Regular.ttf )
258+
259+
else ifeq ($~,/c/Users/$I) # Probably Git Bash in Windows
260+
OS ?= Windows
261+
CPU ?= $(shell echo $$PROCESSOR_ARCHITECTURE)
262+
ifeq (AMD64,$(CPU))
263+
CPU := x86_64
233264
endif
265+
%-on-Windows_NT-path:
266+
$(call ps1,[System.Environment]::SetEnvironmentVariable('Path', '$*;' + [System.Environment]::GetEnvironmentVariable('Path', 'User'), 'User'))
234267

235-
CPU ?= $(shell $(UNAME) -m)
236-
OS ?= $(shell $(UNAME) -s)
237-
WINDOWS_OS := $(filter Windows_NT MINGW% MSYS%,$(OS))
238-
ifneq (,$(WINDOWS_OS))
239-
# Expand $($(PYTHON)) to a Windows mixed-mode path when used as a dependency
240-
$(PYTHON) := $(shell cygpath -m $(PYTHON))
241-
! ?= choco install -y
242-
? ?= $(or $(ChocolateyInstall),C:/ProgramData/Chocolatey)/bin
243-
else ifeq (MacOSX,$(OS))
244-
$(PYTHON) = $(PYTHON)
245-
! ?= brew install
246-
? ?= /opt/homebrew/bin
247-
FONTS ?= ~/Library/Fonts
248-
COUSINE := $(FONTS)
249-
CARLITO := $(FONTS)
250-
else
251-
$(PYTHON) = $(PYTHON)
252-
CXX := /usr/bin/clang++
253-
CC := /usr/bin/clang
254-
! ?= sudo apt update && sudo apt install -y
255-
? ?= /usr/bin
256-
COUSINE ?= /usr/share/fonts/truetype/cousine
257-
CARLITO ?= /usr/share/fonts/truetype/crosextra
258-
/usr/share/fonts/truetype/crosextra/Carlito-Regular.ttf:
259-
# Need a more screen-readable normal font: carlito
260-
sudo apt-get install fonts-crosextra-carlito
261-
/usr/share/fonts/truetype/cousine/Cousine-Regular.ttf:
262-
# Need a more screen-readable fixed-size font: cousine
263-
( sudo mkdir -p $(dir $@) && cd $(dir $@) && \
264-
fonts=https://raw.githubusercontent.com/google/fonts/main/apache && \
265-
sudo wget $$fonts/cousine/DESCRIPTION.en_us.html && \
266-
sudo wget $$fonts/cousine/Cousine-Bold.ttf && \
267-
sudo wget $$fonts/cousine/Cousine-BoldItalic.ttf && \
268-
sudo wget $$fonts/cousine/Cousine-Italic.ttf && \
269-
sudo wget $$fonts/cousine/Cousine-Regular.ttf )
270-
endif
268+
# Default to an installable Conda python interpreter
269+
CONDA_DIR ?= $~/AppData/Local/Miniconda/
270+
$(info 1 $(PYTHON))
271+
PYTHON ?= $(CONDA_DIR)python.exe
272+
VENV_PYTHON ?= Scripts/python.exe
271273

272-
# Install a normal package
273-
$?/%:; $! $*
274+
# Default to an installable Chocolatey package manager
275+
? ?= ~/AppData/Local/Chocolatey/bin
276+
! ?= $?/choco.exe install -y
277+
278+
# Use C\: prefix for all global files so that make.exe finds them
279+
override ? := $(subst :,\:,$(shell cygpath -m $?))
280+
override CONDA_DIR := $(subst :,\:,$(shell cygpath -m $(CONDA_DIR)))
281+
override PYTHON := $(subst :,\:,$(shell cygpath -m $(PYTHON)))
282+
$(info 3 $(PYTHON))
283+
284+
# Installable package manager, python interpreter and document compiler on Windows
285+
"$?/choco.exe":
286+
powershell -NoProfile -ExecutionPolicy Bypass -Command \
287+
"[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol \
288+
-bor 3072; iex ((New-Object System.Net.WebClient).DownloadString(\
289+
'https://community.chocolatey.org/install.ps1'))" && \
290+
rm install.ps1
291+
$(info 4 $(CONDA_DIR)python.exe: Miniconda3-latest-Windows-$(CPU).exe | $(CONDA_DIR))
292+
$(CONDA_DIR)python.exe: Miniconda3-latest-Windows-$(CPU).exe | $(CONDA_DIR)
293+
powershell.exe -NoProfile -Command \
294+
"Start-Process -FilePath '$<' -Wait -NoNewWindow -ArgumentList \
295+
'/InstallationType=JustMe','/AddToPath=1','/RegisterPython=1','/S','/D=""$|""'" && \
296+
rm $<
297+
"$?/xetex":
298+
$! miktex
299+
300+
else # Probably Zsh on MacOSX
301+
OS ?= MacOS
302+
CONDA_DIR ?= ~/miniconda3/
303+
PYTHON ?= python3
304+
VENV_PYTHON ?= bin/python3
305+
%-on-MacOSX-path: ~/.zshrc
306+
echo 'export PATH="$*:$$PATH"' >> $<
307+
false # Please `source $<` or open a new shell to get $* on PATH, and retry `make $(MAKECMDGOALS)`.
274308

275-
# Custom packages
276-
$?/clang++: $?/clang
277-
ifneq (,$(WINDOWS_OS))
278-
$?/xetex:; $! miktex
279-
else
280-
$?/xetex:; $! texlive-xetex
281-
endif
309+
! ?= brew install
310+
? ?= /opt/homebrew/bin
311+
FONTS ?= ~/Library/Fonts
312+
COUSINE := $(FONTS)
313+
CARLITO := $(FONTS)
314+
$?/xetex:
315+
$! texlive-xetex
316+
317+
endif # Package manager, python interpreter, document compiler and fonts for the OS
318+
$/make: $(PYTHON)
319+
320+
# Installable Conda installer and directory
321+
Miniconda3-latest-%:
322+
curl -sL "https://repo.anaconda.com/miniconda/$@" -O
323+
$(CONDA_DIR):
324+
mkdir -p $@
325+
326+
# Relative path to pips within a $(PYTHON) venv
327+
VENV_PIPS = $(shell $(PYTHON) -m make --pips)
328+
329+
# Install a normal package
330+
$?/%:
331+
$! $*
332+
333+
# Default to an installable Clang ASM/C/C++ compiler
334+
CXX := $?/clang++
335+
CC := $?/clang
282336

283-
.PRECIOUS: $?/jq $?/pandoc $?/xetex
337+
# Custom packages
338+
$?/clang++: $?/clang
339+
340+
# Make sure dependent packages remain on the OS
341+
PRECIOUS += jq pandoc xetex clang++
342+
.PRECIOUS: $(PRECIOUS:%=$?/%)
284343

285344
# Notify the user if new rules were built and included, and make therefore restarted
286345
ifeq (2,$(MAKE_RESTARTS))
@@ -290,7 +349,7 @@ ifeq (2,$(MAKE_RESTARTS))
290349

291350
MAKER := $(shell $(MAKE) -v))
292351
INCLUDING ?= $/build/
293-
$(info # $(PWD) $(filter-out $(INCLUDING)%,$(subst $(INCLUDED),,$(MAKEFILE_LIST))) in $(word 6,$(MAKER)) $(wordlist 2,3,$(MAKER)) building $I `$(MAKE) $(MAKECMDGOALS)` on $(OS)-$(CPU) $(PYTHON) $($/venv/bin/python3))
352+
$(info # $(PWD) $(filter-out $(INCLUDING)%,$(subst $(INCLUDED),,$(MAKEFILE_LIST))) in $(word 6,$(MAKER)) $(wordlist 2,3,$(MAKER)) building $I `$(MAKE) $(MAKECMDGOALS)` on $(OS)-$(CPU) $(PYTHON) $/venv/$(VENV_PYTHON))
294353
INCLUDED := $(MAKEFILE_LIST)
295354
INCLUDING := $/build/
296355
ifneq (,$($/_SUBPROJECTS))
@@ -304,43 +363,6 @@ ifeq (2,$(MAKE_RESTARTS))
304363
endif
305364
endif
306365

307-
# Install conda python
308-
ifndef CONDA
309-
CONDA := ~/miniconda3/bin/conda
310-
$~/miniconda3/bin/conda:
311-
curl -sL "https://repo.anaconda.com/miniconda/Miniconda3-latest-$(OS)-$(CPU).sh" -o miniconda.sh
312-
bash miniconda.sh -bfup $~/miniconda3
313-
echo 'TODO: $~/miniconda3/bin/conda init | grep modified | (read _ rc && echo "TODO: source $$rc && conda activate")'
314-
rm miniconda.sh
315-
endif
316-
317-
# Install local commands before other commands
318-
ifndef .-ON-PATH_TARGETS
319-
.-ON-PATH_TARGETS = 1
320-
%-on-Linux-path: ~/.profile
321-
echo 'export PATH="$*:$$PATH"' >> $<
322-
false # Please `source $<` or open a new shell to get $* on PATH, and retry `make $(MAKECMDGOALS)`.
323-
%-on-MacOSX-path: ~/.zshrc
324-
echo 'export PATH="$*:$$PATH"' >> $<
325-
false # Please `source $<` or open a new shell to get $* on PATH, and retry `make $(MAKECMDGOALS)`.
326-
%-on-Windows_NT-path:
327-
$(call ps1,[System.Environment]::SetEnvironmentVariable('Path', '$*;' + [System.Environment]::GetEnvironmentVariable('Path', 'User'), 'User'))
328-
endif
329-
330-
ifndef SPEEDUP_WSL_DNS_TARGET
331-
SPEEDUP_WSL_DNS_TARGET = 1
332-
$~/use_windows_dns.sh:
333-
echo "# Fixing DNS issue in WSL https://gist.github.com/ThePlenkov/6ecf2a43e2b3898e8cd4986d277b5ecf#file-boot-sh" > $@
334-
echo -n "sed -i '/nameserver/d' /etc/resolv.conf && " >> $@
335-
echo -n "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command " >> $@
336-
echo -n "'(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses | " >> $@
337-
echo -n "ForEach-Object { \"nameserver \$$_\" }' | tr -d '\\r' | " >> $@
338-
echo "tee -a /etc/resolv.conf > /dev/null" >> $@
339-
sudo sed -i '\|command=$@|d' /etc/wsl.conf
340-
echo "command=$@" | sudo tee -a /etc/wsl.conf > /dev/null
341-
sudo sh $@
342-
endif
343-
344366
endif ### Generics ###
345367

346368

@@ -616,34 +638,35 @@ $/build/%.bringup: $/%
616638
mkdir -p $(dir $@) && touch $@
617639

618640
# Make a Python executable
619-
$/build/%.py.shebang: $($/venv/bin/python3) $/%.py
641+
$/build/%.py.shebang: $/venv/$(VENV_PYTHON) $/%.py
620642
$^ --shebang > $@
621643

622644
# Build a recipy for $/build/%.py.bringup
623645
$/build/%.py.mk: $/%.py | $/make.py
624646
rm -f $@ && ( cd $(dir $<). && $(PYTHON) $*.py --generic --dep build/$*.py.mk ) ; [ -e $@ ] || echo "\$$/build/$*.py.bringup:; touch \$$@" > $@
625647

626648
# Check Python 3.9 syntax
627-
$/build/%.py.syntax: $($/venv/bin/python3) $/%.py | $($/venv/pip/)ruff
649+
$/build/%.py.syntax: $/venv/$(VENV_PYTHON) $/%.py | $/venv/$(VENV_PIPS)ruff
628650
$< -m ruff check --select=E9,F63,F7,F82 --target-version=py39 $(lastword,$^) > $@ || (cat $@ && false)
629651

630652
# Install pip package in the local python:
631-
$($/venv/pip/)%: $($/venv/bin/python3)
653+
$(info 6 $/venv/$(VENV_PIPS)%: $/venv/$(VENV_PYTHON))
654+
$/venv/$(VENV_PIPS)%: $/venv/$(VENV_PYTHON)
632655
$< -m pip install --prefer-binary $*
633656

634657
# Setup a local shebang python
635-
$/%/$(VENV_PYTHON): | $($(PYTHON)) $(PYTHON_DEP) $(SPEEDUP_WSL_DNS)
658+
$/%/$(VENV_PYTHON): | $(PYTHON) $(PYTHON_DEP) $(SPEEDUP_WSL_DNS)
636659
$(SPEEDUP_WSL_PIP)$(PYTHON) -m venv --upgrade-deps $* && \
637660
$(SPEEDUP_WSL_PIP)$@ -m pip install requests # Needed by -m make --prompt
638661

639662

640663
# Check Python 3.9 style
641-
$/build/%.py.style: $/%.py $/build/%.py.syntax $($/venv/bin/python3)
664+
$/build/%.py.style: $/%.py $/build/%.py.syntax $/venv/$(VENV_PYTHON)
642665
$(word 3,$^) -m ruff check --fix --target-version=py39 $< > $@ || (cat $@ && false)
643666

644667
define META
645668
# Check Python and command line usage examples in .py files
646-
$/build/%.py.tested: $/%.py $/build/%.py.mk $/build/%.py.style $/build/%.py.bringup $/build/%.py.shebang $($/_EXE_TESTED) | $($/venv/bin/python3)
669+
$/build/%.py.tested: $/%.py $/build/%.py.mk $/build/%.py.style $/build/%.py.bringup $/build/%.py.shebang $($/_EXE_TESTED) | $/venv/$(VENV_PYTHON)
647670
( cd $/. && $$*.py --test ) > $$@ || (cat $$@ && false)
648671

649672
# Check command line usage examples in .md files
@@ -703,8 +726,8 @@ $/build/report-details.md:
703726
echo "$(_heading)# Source code, installation and test result" >> $@
704727

705728
# Use GPT for a release review.
706-
$/build/audit.diff: $/make.py $/build/prompt.diff $/build/make.py.bringup
707-
( cd $(dir $<) && venv/bin/python3 -m make --prompt build/prompt.diff $(GPT_MODEL) $(GPT_TEMPERATURE) $(GPT_BEARER_rot13) ) > $@ && cat $(word 2,$^) $@ || ( cat $@ && false )
729+
$/build/audit.diff: $/make.py $/build/prompt.diff $/build/make.py.bringup | $/venv/$(VENV_PYTHON)
730+
( cd $(dir $<) && $| -m make --prompt build/prompt.diff $(GPT_MODEL) $(GPT_TEMPERATURE) $(GPT_BEARER_rot13) ) > $@ && cat $(word 2,$^) $@ || ( cat $@ && false )
708731
$/build/prompt.diff: $/build/review.diff
709732
$(PYTHON) -m make -c 'print(REVIEW)' > $@
710733
echo "$$ $(MAKE) $(@:%build/prompt.diff=%)review" >> $@

0 commit comments

Comments
 (0)