From 4739c51447bd7a18a2e16fb10a33b2daa5ceb52e Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Thu, 22 Feb 2018 16:26:26 +0100 Subject: [PATCH] Rework Python Makefile - add help_python --- Makefile | 2 +- makefiles/Makefile.port.mk | 4 +- makefiles/Makefile.python.mk | 95 ++++++++++++++++++++---------------- 3 files changed, 56 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index eff1b12bd7..8ff7a918cb 100755 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ help: @echo Please define target: @echo " - Prerequisite: third_party third_party_check clean_third_party" @echo " - C++: cc test_cc clean_cc" - @echo " - Python: python test_python clean_python" + @echo " - Python: python help_python test_python clean_python" @echo " - Java: java test_java clean_java" @echo " - .NET (CSharp): csharp test_csharp clean_csharp" @echo " - .NET (FSharp): fsharp fsharp-help fsharp-clean" diff --git a/makefiles/Makefile.port.mk b/makefiles/Makefile.port.mk index cfce8b62e2..33c718da33 100755 --- a/makefiles/Makefile.port.mk +++ b/makefiles/Makefile.port.mk @@ -13,7 +13,7 @@ endif ifeq ($(SYSTEM),unix) OR_TOOLS_TOP ?= $(shell pwd) OS = $(shell uname -s) - ifeq ("$(UNIX_PYTHON_VER)",) + ifeq ($(UNIX_PYTHON_VER),) DETECTED_PYTHON_VERSION := $(shell python -c "from sys import version_info as v; print (str(v[0]) + '.' + str(v[1]))") else DETECTED_PYTHON_VERSION := $(UNIX_PYTHON_VER) @@ -186,7 +186,7 @@ ifeq ($(SYSTEM),win) DETECTED_PATH_TO_PYTHON = $(shell python -c "from sys import executable; from os.path import sep; print(sep.join(executable.split(sep)[:-1]).rstrip())") CANONIC_DETECTED_PATH_TO_PYTHON = $(subst $(SPACE),$(BACKSLASH_SPACE),$(subst \,/,$(subst \\,/,$(DETECTED_PATH_TO_PYTHON)))) ifeq ($(wildcard $(CANONIC_DETECTED_PATH_TO_PYTHON)),) - SELECTED_PATH_TO_PYTHON = WINDOWS_PATH_TO_PYTHON =\# python was not found. Set this variable to the path to python to build the python files. Don't include the name of the executable in the path! (ex: WINDOWS_PATH_TO_PYTHON = c:\\python27-64) + SELECTED_PATH_TO_PYTHON = WINDOWS_PATH_TO_PYTHON =\# python was not found. Set this variable to the path to python to build the python files. Don\'t include the name of the executable in the path! (ex: WINDOWS_PATH_TO_PYTHON = c:\\python27-64) else SELECTED_PATH_TO_PYTHON = WINDOWS_PATH_TO_PYTHON = $(DETECTED_PATH_TO_PYTHON) endif diff --git a/makefiles/Makefile.python.mk b/makefiles/Makefile.python.mk index 08b71e85d2..d4a734cd7f 100755 --- a/makefiles/Makefile.python.mk +++ b/makefiles/Makefile.python.mk @@ -1,34 +1,38 @@ -.PHONY : python install_python_modules pypi_archive pypi_archive_dir pyinit pycp pyalgorithms pygraph pylp pysat pydata - -# Python support using SWIG - -# Detect python3 +# ---------- Python support using SWIG ---------- +.PHONY: help_python # Generate list of targets with descriptions. +help_python: + @echo Use one of the following targets: + @grep "^.PHONY: .* #" $(CURDIR)/makefiles/Makefile.python.mk | sed "s/\.PHONY: \(.*\) # \(.*\)/\1\t\2/" | expand -t24 OR_TOOLS_PYTHONPATH = $(OR_ROOT_FULL)$(CPSEP)$(OR_ROOT_FULL)$Sdependencies$Ssources$Sprotobuf-$(PROTOBUF_TAG)$Spython +# Check for required build tools ifeq ($(SYSTEM),win) - PYTHON_EXECUTABLE = "$(WINDOWS_PATH_TO_PYTHON)$Spython.exe" - SET_PYTHONPATH = @set PYTHONPATH=$(OR_TOOLS_PYTHONPATH) && -else #UNIX - PYTHON_EXECUTABLE = $(shell which python$(UNIX_PYTHON_VER)) - SET_PYTHONPATH = @PYTHONPATH=$(OR_TOOLS_PYTHONPATH) -endif - -ifeq ($(shell $(PYTHON_EXECUTABLE) -c "from sys import version_info as v; print (str(v[0]))"),3) - PYTHON3 = true - SWIG_PYTHON3_FLAG=-py3 -DPY3 - PYTHON3_CFLAGS=-DPY3 -endif - -# Main target -CANONIC_PYTHON_EXECUTABLE = $(subst ",,$(subst $(SPACE),$(BACKSLASH_SPACE),$(subst \,/,$(subst \\,/,$(PYTHON_EXECUTABLE))))) -ifeq ($(wildcard $(CANONIC_PYTHON_EXECUTABLE)),) -python: - @echo CANONIC_PYTHON_EXECUTABLE = $(CANONIC_PYTHON_EXECUTABLE) - @echo "The python executable was not set properly. Check Makefile.local for more information." -test_python: python - +PYTHON_COMPILER ?= python.exe +ifneq ($(WINDOWS_PATH_TO_PYTHON),) +PYTHON_EXECUTABLE := $(shell where "$(WINDOWS_PATH_TO_PYTHON):$(PYTHON_COMPILER)") else +PYTHON_EXECUTABLE := $(shell where $(PYTHON_COMPILER)) +endif +SET_PYTHONPATH = @set PYTHONPATH=$(OR_TOOLS_PYTHONPATH) && +else # UNIX +PYTHON_COMPILER ?= python$(UNIX_PYTHON_VER) +PYTHON_EXECUTABLE := $(shell which $(PYTHON_COMPILER)) +SET_PYTHONPATH = @PYTHONPATH=$(OR_TOOLS_PYTHONPATH) +endif + +# Detect python3 +ifneq ($(PYTHON_EXECUTABLE),) +ifeq ($(shell $(PYTHON_EXECUTABLE) -c "from sys import version_info as v; print (str(v[0]))"),3) +PYTHON3 := true +SWIG_PYTHON3_FLAG := -py3 -DPY3 +PYTHON3_CFLAGS := -DPY3 +endif +endif + +.PHONY: python # Build Python OR-Tools. +.PHONY: test_python # Test Python OR-Tools using various examples. +ifneq ($(PYTHON_EXECUTABLE),) python: \ install_python_modules \ pyinit \ @@ -38,12 +42,16 @@ python: \ pylp \ pysat \ pyrcpsp - test_python: test_python_examples -BUILT_LANGUAGES +=, python +BUILT_LANGUAGES +=, Python$(PYTHON_VERSION) +else +python: + @echo PYTHON_EXECUTABLE = ${PYTHON_EXECUTABLE} + $(warning Cannot find '$(PYTHON_COMPILER)' command which is needed for build. Please make sure it is installed and in system path.) +test_python: python endif -# Clean target +.PHONY: clean_python # Clean Python output from previous build. clean_python: -$(DEL) $(GEN_DIR)$Sortools$S__init__.py -$(DEL) $(GEN_DIR)$Sortools$Salgorithms$S*python_wrap* @@ -78,11 +86,12 @@ clean_python: -$(DEL) $(OBJ_DIR)$Sswig$S*python_wrap.$O -$(DELREC) $(PYPI_ARCHIVE_TEMP_DIR) +.PHONY: install_python_modules pypi_archive pypi_archive_dir pyinit pycp pyalgorithms pygraph pylp pysat pydata install_python_modules: dependencies/sources/protobuf-$(PROTOBUF-TAG)/python/google/protobuf/descriptor_pb2.py dependencies/sources/protobuf-$(PROTOBUF-TAG)/python/google/protobuf/descriptor_pb2.py: \ dependencies/sources/protobuf-$(PROTOBUF_TAG)/python/setup.py -ifeq ("$(SYSTEM)", "win") +ifeq ($(SYSTEM),win) copy dependencies$Sinstall$Sbin$Sprotoc.exe dependencies$Ssources$Sprotobuf-$(PROTOBUF_TAG)$Ssrc else cp dependencies$Sinstall$Sbin$Sprotoc dependencies$Ssources$Sprotobuf-$(PROTOBUF_TAG)$Ssrc @@ -111,7 +120,7 @@ $(OBJ_DIR)/swig/knapsack_solver_python_wrap.$O: $(GEN_DIR)/ortools/algorithms/kn $(LIB_DIR)/_pywrapknapsack_solver.$(SWIG_LIB_SUFFIX): $(OBJ_DIR)/swig/knapsack_solver_python_wrap.$O $(OR_TOOLS_LIBS) $(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S_pywrapknapsack_solver.$(SWIG_LIB_SUFFIX) $(OBJ_DIR)$Sswig$Sknapsack_solver_python_wrap.$O $(OR_TOOLS_LNK) $(SYS_LNK) $(PYTHON_LNK) -ifeq "$(SYSTEM)" "win" +ifeq ($(SYSTEM),win) copy $(LIB_DIR)\\_pywrapknapsack_solver.dll $(GEN_DIR)\\ortools\\algorithms\\_pywrapknapsack_solver.pyd else cp $(LIB_DIR)/_pywrapknapsack_solver.$(SWIG_LIB_SUFFIX) $(GEN_DIR)/ortools/algorithms @@ -137,7 +146,7 @@ $(OBJ_DIR)/swig/graph_python_wrap.$O: $(GEN_DIR)/ortools/graph/graph_python_wrap $(LIB_DIR)/_pywrapgraph.$(SWIG_LIB_SUFFIX): $(OBJ_DIR)/swig/graph_python_wrap.$O $(OR_TOOLS_LIBS) $(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S_pywrapgraph.$(SWIG_LIB_SUFFIX) $(OBJ_DIR)$Sswig$Sgraph_python_wrap.$O $(OR_TOOLS_LNK) $(SYS_LNK) $(PYTHON_LNK) -ifeq "$(SYSTEM)" "win" +ifeq ($(SYSTEM),win) copy $(LIB_DIR)\\_pywrapgraph.dll $(GEN_DIR)\\ortools\\graph\\_pywrapgraph.pyd else cp $(LIB_DIR)/_pywrapgraph.$(SWIG_LIB_SUFFIX) $(GEN_DIR)/ortools/graph @@ -195,7 +204,7 @@ $(LIB_DIR)/_pywrapcp.$(SWIG_LIB_SUFFIX): \ $(OBJ_DIR)/swig/constraint_solver_python_wrap.$O \ $(OR_TOOLS_LIBS) $(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S_pywrapcp.$(SWIG_LIB_SUFFIX) $(OBJ_DIR)$Sswig$Sconstraint_solver_python_wrap.$O $(OR_TOOLS_LNK) $(SYS_LNK) $(PYTHON_LNK) -ifeq "$(SYSTEM)" "win" +ifeq ($(SYSTEM),win) copy $(LIB_DIR)\\_pywrapcp.dll $(GEN_DIR)\\ortools\\constraint_solver\\_pywrapcp.pyd else cp $(LIB_DIR)/_pywrapcp.$(SWIG_LIB_SUFFIX) $(GEN_DIR)/ortools/constraint_solver @@ -229,7 +238,7 @@ $(LIB_DIR)/_pywraplp.$(SWIG_LIB_SUFFIX): \ $(OBJ_DIR)/swig/linear_solver_python_wrap.$O \ $(OR_TOOLS_LIBS) $(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S_pywraplp.$(SWIG_LIB_SUFFIX) $(OBJ_DIR)$Sswig$Slinear_solver_python_wrap.$O $(OR_TOOLS_LNK) $(SYS_LNK) $(PYTHON_LNK) -ifeq "$(SYSTEM)" "win" +ifeq ($(SYSTEM),win) copy $(LIB_DIR)\\_pywraplp.dll $(GEN_DIR)\\ortools\\linear_solver\\_pywraplp.pyd else cp $(LIB_DIR)/_pywraplp.$(SWIG_LIB_SUFFIX) $(GEN_DIR)/ortools/linear_solver @@ -263,7 +272,7 @@ $(LIB_DIR)/_pywrapsat.$(SWIG_LIB_SUFFIX): \ $(OBJ_DIR)/swig/sat_python_wrap.$O \ $(OR_TOOLS_LIBS) $(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S_pywrapsat.$(SWIG_LIB_SUFFIX) $(OBJ_DIR)$Sswig$Ssat_python_wrap.$O $(OR_TOOLS_LNK) $(SYS_LNK) $(PYTHON_LNK) -ifeq "$(SYSTEM)" "win" +ifeq ($(SYSTEM),win) copy $(LIB_DIR)\\_pywrapsat.dll $(GEN_DIR)\\ortools\\sat\\_pywrapsat.pyd else cp $(LIB_DIR)/_pywrapsat.$(SWIG_LIB_SUFFIX) $(GEN_DIR)/ortools/sat @@ -293,7 +302,7 @@ $(LIB_DIR)/_pywraprcpsp.$(SWIG_LIB_SUFFIX): \ $(OBJ_DIR)/swig/rcpsp_python_wrap.$O \ $(OR_TOOLS_LIBS) $(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S_pywraprcpsp.$(SWIG_LIB_SUFFIX) $(OBJ_DIR)$Sswig$Srcpsp_python_wrap.$O $(OR_TOOLS_LNK) $(SYS_LNK) $(PYTHON_LNK) -ifeq "$(SYSTEM)" "win" +ifeq ($(SYSTEM),win) copy $(LIB_DIR)\\_pywraprcpsp.dll $(GEN_DIR)\\ortools\\data\\_pywraprcpsp.pyd else cp $(LIB_DIR)/_pywraprcpsp.$(SWIG_LIB_SUFFIX) $(GEN_DIR)/ortools/data @@ -305,8 +314,7 @@ rpy: $(LIB_DIR)/_pywraplp.$(SWIG_LIB_SUFFIX) $(LIB_DIR)/_pywrapcp.$(SWIG_LIB_SUF @echo Running $(EX) $(SET_PYTHONPATH) $(PYTHON_EXECUTABLE) $(EX) $(ARGS) -# Build stand-alone archive file for redistribution. - +.PHONY: python_examples_archive # Build stand-alone Python examples archive file for redistribution. python_examples_archive: -$(DELREC) temp $(MKDIR) temp @@ -428,13 +436,16 @@ else endif cd $(PYPI_ARCHIVE_TEMP_DIR)/ortools && twine upload dist/* +.PHONY: detect_python # Show variables used to build Python OR-Tools. detect_python: +ifeq ($(SYSTEM),win) + @echo WINDOWS_PATH_TO_PYTHON = $(WINDOWS_PATH_TO_PYTHON) +endif + @echo PYTHON_COMPILER = $(PYTHON_COMPILER) + @echo PYTHON_EXECUTABLE = $(PYTHON_EXECUTABLE) @echo PYTHON_VERSION = $(PYTHON_VERSION) @echo PYTHON3 = $(PYTHON3) - @echo PYTHON_EXECUTABLE = $(PYTHON_EXECUTABLE) + @echo SET_PYTHONPATH = "$(SET_PYTHONPATH)" @echo PYTHON_INC = $(PYTHON_INC) @echo PYTHON_LNK = $(PYTHON_LNK) @echo SWIG_PYTHON3_FLAG = $(SWIG_PYTHON3_FLAG) -ifeq ($(SYSTEM),unix) - @echo SET_PYTHONPATH = $(SET_PYTHONPATH) -endif