Учитывая ответ на этот вопрос, Внедрить Python в Makefile, чтобы установить make-переменные , который работает!
define NEWLINE
endef
define PYTHON_SCRIPT_CODE
import sys
print("hi")
endef
SDK_PATH := $(shell echo \
'$(subst $(NEWLINE),@NEWLINE@,${PYTHON_SCRIPT_CODE})' | \
sed 's/@NEWLINE@/\n/g' | python -)
default:
@echo 'SDK Path Detected: $(SDK_PATH)'
Как передать данные на стандартный Python?Например:
define PYTHON_SCRIPT_CODE
import sys
print("hi")
print(sys.stdin.read())
endef
# pseudocode
SDK_PATH := $(shell bash --version | PYTHON_SCRIPT_CODE)
default:
@echo 'SDK Path Detected: $(SDK_PATH)'
Будет выводить:
SDK Path Detected: hi
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Сейчас я делаю это:
define NEWLINE
endef
BASH_VERSION := $(shell bash --version)
define PYTHON_SCRIPT_CODE
import sys
print("Hi")
print("${BASH_VERSION}")
endef
SDK_PATH := $(shell echo \
'$(subst $(NEWLINE),@NEWLINE@,${PYTHON_SCRIPT_CODE})' | \
sed 's/@NEWLINE@/\n/g' | python -)
default:
@echo 'SDK Path Detected: $(SDK_PATH)'
Результаты: (Нет новых строк)
Смежные вопросы:
- Можно ли создать многострочную строкупеременная в Makefile
- https://unix.stackexchange.com/questions/222911/using-embedded-python-script-in-makefile
Новый пример, без добавления объектов в сценарий Python:
#!/usr/bin/make -f
ECHOCMD:=/bin/echo -e
SHELL := /bin/bash
define NEWLINE
endef
VERSION := $(shell bash --version)
# With this, you cannot use single quotes inside your python code
define PYTHON_VERSION_CODE
import re, sys;
program_version = """${VERSION}"""
match = re.search("Copyright[^\d]+(\d+)", program_version);
if match:
if int( match.group(1) ) >= 2018:
sys.stdout.write("1")
else:
sys.stdout.write( match.group(1) )
else:
sys.stdout.write("0")
endef
# Due to this, you cannot use single quotes inside your python code
PYTHON_SCRIPT_RESULTS := $(shell echo \
'$(subst $(NEWLINE),@NEWLINE@,${PYTHON_VERSION_CODE})' | \
sed 's/@NEWLINE@/\n/g' | python -)
all:
printf 'Results: %s\n' "${PYTHON_SCRIPT_RESULTS}"
Результаты:
- Makefile как исполняемый скрипт с шебангом?