Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit b730a44

Browse files
authored
Add support for non-standard Python install in make (#88)
1 parent 3adaa1a commit b730a44

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ GOOS ?= $(word 1,$(GO_ENV))
2121
GOARCH ?= $(word 2,$(GO_ENV))
2222
ROOT_DIR := $(realpath .)
2323
PKG_DIR := build/pkg/$(GOOS)_$(GOARCH)
24+
PYTHON ?= python
25+
PYTHON_BIN := $(shell which $(PYTHON))
26+
PYTHON_VER := $(word 2,$(shell $(PYTHON) -V 2>&1))
2427
PY_DIR := build/lib/python2.7/site-packages
2528

29+
ifeq ($(filter 2.7.%,$(PYTHON_VER)),)
30+
$(error unsupported Python version $(PYTHON_VER), Grumpy only supports 2.7.x. To use a different python binary such as python2, run: 'make PYTHON=python2 ...')
31+
endif
32+
2633
export GOPATH := $(ROOT_DIR)/build
2734
export PYTHONPATH := $(ROOT_DIR)/$(PY_DIR)
2835
export PATH := $(ROOT_DIR)/build/bin:$(PATH)
@@ -90,30 +97,32 @@ precommit: cover gofmt lint test
9097
$(COMPILER_BIN) $(RUNNER_BIN) $(TOOL_BINS): build/bin/%: tools/%
9198
@mkdir -p build/bin
9299
@cp -f $< $@
100+
@sed -i.bak -e '1s@/usr/bin/env python@$(PYTHON_BIN)@' $@
101+
@rm -f $@.bak
93102

94103
$(COMPILER_SRCS) $(COMPILER_TEST_SRCS) $(COMPILER_SHARDED_TEST_SRCS): $(PY_DIR)/grumpy/%.py: %.py
95104
@mkdir -p $(PY_DIR)/grumpy/compiler
96105
@cp -f $< $@
97106

98107
$(COMPILER_PASS_FILES): %.pass: %.py $(COMPILER)
99-
@python $< -q
108+
@$(PYTHON) $< -q
100109
@touch $@
101110
@echo compiler/`basename $*` PASS
102111

103112
$(COMPILER_D_FILES): $(PY_DIR)/%.d: $(PY_DIR)/%.py $(COMPILER_SRCS)
104-
@python -m modulefinder $< | awk '{if (match($$2, /^grumpy\>/)) { print "$(PY_DIR)/$*.pass: " substr($$3, length("$(ROOT_DIR)/") + 1) }}' > $@
113+
@$(PYTHON) -m modulefinder $< | awk '{if (match($$2, /^grumpy\>/)) { print "$(PY_DIR)/$*.pass: " substr($$3, length("$(ROOT_DIR)/") + 1) }}' > $@
105114

106115
-include $(COMPILER_D_FILES)
107116

108117
# Does not depend on stdlibs since it makes minimal use of them.
109118
$(COMPILER_EXPR_VISITOR_PASS_FILES): $(PY_DIR)/grumpy/compiler/expr_visitor_test.%.pass: $(PY_DIR)/grumpy/compiler/expr_visitor_test.py $(RUNNER_BIN) $(COMPILER) $(RUNTIME)
110-
@python $< --shard=$*
119+
@$(PYTHON) $< --shard=$*
111120
@touch $@
112121
@echo 'compiler/expr_visitor_test $* PASS'
113122

114123
# Does not depend on stdlibs since it makes minimal use of them.
115124
$(COMPILER_STMT_PASS_FILES): $(PY_DIR)/grumpy/compiler/stmt_test.%.pass: $(PY_DIR)/grumpy/compiler/stmt_test.py $(RUNNER_BIN) $(COMPILER) $(RUNTIME)
116-
@python $< --shard=$*
125+
@$(PYTHON) $< --shard=$*
117126
@touch $@
118127
@echo 'compiler/stmt_test $* PASS'
119128

@@ -179,7 +188,7 @@ build/src/grumpy/lib/$(2)/module.go: $(1) $(COMPILER)
179188

180189
build/src/grumpy/lib/$(2)/module.d: $(1)
181190
@mkdir -p build/src/grumpy/lib/$(2)
182-
@python -m modulefinder -p $(ROOT_DIR)/lib:$(ROOT_DIR)/third_party/stdlib $$< | awk '{if (($$$$1 == "m" || $$$$1 == "P") && $$$$2 != "__main__" && $$$$2 != "$(2)") {gsub(/\./, "/", $$$$2); print "$(PKG_DIR)/grumpy/lib/$(2).a: $(PKG_DIR)/grumpy/lib/" $$$$2 ".a"}}' > $$@
191+
@$(PYTHON) -m modulefinder -p $(ROOT_DIR)/lib:$(ROOT_DIR)/third_party/stdlib $$< | awk '{if (($$$$1 == "m" || $$$$1 == "P") && $$$$2 != "__main__" && $$$$2 != "$(2)") {gsub(/\./, "/", $$$$2); print "$(PKG_DIR)/grumpy/lib/$(2).a: $(PKG_DIR)/grumpy/lib/" $$$$2 ".a"}}' > $$@
183192

184193
$(PKG_DIR)/grumpy/lib/$(2).a: build/src/grumpy/lib/$(2)/module.go $(RUNTIME)
185194
@mkdir -p $(PKG_DIR)/grumpy/lib/$(dir $(2))

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export PYTHONPATH=$PWD/build/lib/python2.7/site-packages
8585
Finally, compile the Python script and build a binary from it:
8686

8787
```
88-
tools/grumpc hello.py > hello.go
88+
build/bin/grumpc hello.py > hello.go
8989
go build -o hello hello.go
9090
```
9191

0 commit comments

Comments
 (0)