forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
198 lines (159 loc) · 7 KB
/
Makefile
File metadata and controls
198 lines (159 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
UNAME = $(shell uname)
THIS_MAKEFILE = $(realpath $(filter %Makefile, $(MAKEFILE_LIST)))
ROOT_DIR = $(strip $(shell dirname $(THIS_MAKEFILE)))
# These are set by Halide's Makefile when built via that path.
HALIDE_PATH ?= $(ROOT_DIR)/..
HALIDE_DISTRIB_PATH ?= $(HALIDE_PATH)/distrib
BIN ?= $(ROOT_DIR)/bin
# 'python' or 'python3'
PYTHON ?= python3
TEST_TMP ?= $(BIN)/tmp
ifeq ($(OS), Windows_NT)
# assume we are building for the MinGW environment
FPIC=
SHARED_EXT=dll
else
FPIC=-fPIC
ifeq ($(UNAME), Darwin)
SHARED_EXT=dylib
# Note that Python on OSX won't load .dylib; it requires .so
SUFFIX=.so
else
SHARED_EXT=so
endif
endif
LIBHALIDE ?= $(HALIDE_DISTRIB_PATH)/bin/libHalide.$(SHARED_EXT)
SUFFIX ?= .$(SHARED_EXT)
ifeq ($(PYTHON), python3)
SUFFIX = $(shell $(PYTHON)-config --extension-suffix)
endif
# Discover PyBind path from `python3 -m pybind11 --includes`
# if it is pip/conda installed, which is a common case.
# Cf. https://github.com/pybind/pybind11/blob/master/docs/compiling.rst#building-manually
PYBIND11_CFLAGS = $(shell $(PYTHON) -m pybind11 --includes)
ifeq ($(PYBIND11_CFLAGS),)
ifndef PYBIND11_PATH
$(error PYBIND11_PATH is undefined)
endif
PYBIND11_PATH ?= /path/to/pybind11
PYBIND11_CFLAGS = -I $(PYBIND11_PATH)/include
endif
OPTIMIZE ?= -O3
# defining DEBUG + undefining NDEBUG gives extra debug info in PyBind11
# OPTIMIZE ?= -g -DDEBUG=1 -UNDEBUG
# Compiling with -fvisibility=hidden saves ~80k on optimized x64 builds
CCFLAGS=$(shell $(PYTHON)-config --cflags) $(PYBIND11_CFLAGS) -I $(HALIDE_DISTRIB_PATH)/include -I $(ROOT_DIR) -std=c++11 $(FPIC) -fvisibility=hidden -fvisibility-inlines-hidden $(OPTIMIZE) $(CXXFLAGS)
# Filter out a pointless warning present in some Python installs
CCFLAGS := $(filter-out -Wstrict-prototypes,$(CCFLAGS))
# DON'T link libpython* - leave those symbols to lazily resolve at load time
# Cf. https://github.com/pybind/pybind11/blob/master/docs/compiling.rst#building-manually
LDFLAGS=-lz
ifeq ($(UNAME), Darwin)
# Keep OS X builds from complaining about missing libpython symbols
LDFLAGS += -undefined dynamic_lookup
endif
PY_SRCS=$(shell ls $(ROOT_DIR)/src/*.cpp)
PY_OBJS=$(PY_SRCS:$(ROOT_DIR)/src/%.cpp=$(BIN)/src/%.o)
MODULE=$(BIN)/halide$(SUFFIX)
$(MODULE): $(PY_OBJS) $(LIBHALIDE)
@mkdir -p $(@D)
$(CXX) $^ $(LDFLAGS) -shared -o $@
# We don't want any of this auto-deleted
.SECONDARY:
$(BIN)/src/%.o: $(ROOT_DIR)/src/%.cpp
@mkdir -p $(@D)
$(CXX) $(CCFLAGS) -c $< -o $@
$(BIN)/%_generator.o: $(ROOT_DIR)/correctness/%_generator.cpp $(HALIDE_DISTRIB_PATH)/include/Halide.h
@mkdir -p $(@D)
$(CXX) $(CCFLAGS) -c $< -o $@
$(BIN)/PyStubImpl.o: $(ROOT_DIR)/stub/PyStubImpl.cpp $(HALIDE_DISTRIB_PATH)/include/Halide.h
@mkdir -p $(@D)
$(CXX) $(CCFLAGS) -c $< -o $@
# Produce a Python extension for the generator by compiling PyStub.cpp
# (with HALIDE_PYSTUB_GENERATOR_NAME defined to the Generator's build name),
# and linking with the generator's .o file, PyStubImpl.o, plus the same libHalide
# being used by halide.so.
#
# You can optionally also define HALIDE_PYSTUB_MODULE_NAME if you want the Python
# module name to be something other than the Generator build name.
$(BIN)/%_PyStub.o: $(ROOT_DIR)/stub/PyStub.cpp
@mkdir -p $(@D)
$(CXX) $(CCFLAGS) -DHALIDE_PYSTUB_GENERATOR_NAME=$* -c $< -o $@
$(BIN)/%.so: $(BIN)/%_PyStub.o $(BIN)/PyStubImpl.o $(BIN)/%_generator.o $(LIBHALIDE)
@mkdir -p $(@D)
$(CXX) $^ $(LDFLAGS) -shared -o $@
# Compile the generators:
$(BIN)/%.gen: $(HALIDE_DISTRIB_PATH)/tools/GenGen.cpp correctness/%_generator.cpp $(LIBHALIDE)
@mkdir -p $(@D)
$(CXX) $(CCFLAGS) $(LDFLAGS) $^ -o $@
# Special generator for generating a runtime:
$(BIN)/runtime.gen: $(HALIDE_DISTRIB_PATH)/tools/GenGen.cpp $(LIBHALIDE)
@mkdir -p $(@D)
$(CXX) $(CCFLAGS) $(LDFLAGS) $^ -o $@
# Generate a runtime:
$(BIN)/runtime.a: $(BIN)/runtime.gen
@mkdir -p $(@D)
$< -r runtime -o $(BIN) target=host
# Which target features to use for which test targets.
target_features_addconstant=-legacy_buffer_wrappers-no_runtime
target_features_bit=-no_runtime
target_features_user_context=-user_context-legacy_buffer_wrappers-no_runtime
# Make the generator generate a Python extension:
$(BIN)/%.py.c $(BIN)/%.cpp $(BIN)/%.h: $(BIN)/%.gen
LD_LIBRARY_PATH=$(HALIDE_DISTRIB_PATH)/bin $< -e h,cpp,py.c \
-g $(notdir $(basename $<)) -o $(BIN) \
target=host$(target_features_$(notdir $(basename $<)))
# Compile the generated Python extension(s):
$(BIN)/%.o: $(BIN)/%.cpp
$(CXX) -c $(FPIC) $(CCFLAGS) $^ -o $@
$(BIN)/%.py.o: $(BIN)/%.py.c
$(CXX) -c $(FPIC) $(CCFLAGS) $^ -o $@
# The Python extension of the generator is already in $(BIN), and is named
# the same, so put the Python extension of the function into ext/.
# TODO: Python extensions of generators should have a _generator suffix.
$(BIN)/ext/%.so: $(BIN)/%.py.o $(BIN)/%.o $(BIN)/runtime.a
@mkdir -p $(BIN)/ext
$(CXX) $(LDFLAGS) $^ -shared -o $@
# Run the Python extension(s):
%.run: $(ROOT_DIR)/correctness/%_test.py $(BIN)/ext/%.so
PYTHONPATH="$(BIN)/ext:$$PYTHONPATH" $(PYTHON) $<
# TODO: In the optimal case, we'd do %.run on all our generators. Unfortunately,
# every generator needs its own settings. See https://github.com/halide/Halide/issues/2977.
.PHONY: test_correctness_bit_test test_correctness_addconstant_test test_correctness_pystub test_correctness_user_context
test_correctness_addconstant_test: addconstant.run ;
test_correctness_bit_test: bit.run ;
test_correctness_user_context_test: user_context.run ;
test_correctness_pystub: $(BIN)/simplestub.so $(BIN)/complexstub.so $(BIN)/partialbuildmethod.so $(BIN)/nobuildmethod.so
APPS = $(shell ls $(ROOT_DIR)/apps/*.py)
CORRECTNESS = $(shell ls $(ROOT_DIR)/correctness/*.py)
TUTORIAL = $(shell ls $(ROOT_DIR)/tutorial/*.py)
.PHONY: test_apps
test_apps: $(APPS:$(ROOT_DIR)/apps/%.py=test_apps_%)
test_apps_%: $(ROOT_DIR)/apps/%.py $(MODULE)
@echo Testing $*...
@mkdir -p $(TEST_TMP)
@# Send stdout (but not stderr) from these to /dev/null to reduce noise
@cd $(TEST_TMP); PYTHONPATH="$(BIN):$$PYTHONPATH" $(PYTHON) $< >/dev/null
.PHONY: test_correctness
test_correctness: $(CORRECTNESS:$(ROOT_DIR)/correctness/%.py=test_correctness_%)
test_correctness_%: $(ROOT_DIR)/correctness/%.py $(MODULE)
@echo Testing $*...
@mkdir -p $(TEST_TMP)
@cd $(TEST_TMP); PYTHONPATH="$(BIN):$$PYTHONPATH" $(PYTHON) $<
.PHONY: test_tutorial
test_tutorial: $(TUTORIAL:$(ROOT_DIR)/tutorial/%.py=test_tutorial_%)
test_tutorial_%: $(ROOT_DIR)/tutorial/%.py $(MODULE)
@echo Testing $*...
@mkdir -p $(TEST_TMP)
@# Send stdout (but not stderr) from these to /dev/null to reduce noise
@# We need "." in the PYTHONPATH for lesson_10_halide.so.
@cd $(TEST_TMP); PYTHONPATH=".:$(BIN):$$PYTHONPATH" $(PYTHON) $< >/dev/null
test_tutorial_lesson_10_aot_compilation_run: $(TEST_TMP)/lesson_10_halide.so
$(TEST_TMP)/lesson_10_halide.so: test_tutorial_lesson_10_aot_compilation_generate
$(CXX) $(CCFLAGS) $(LDFLAGS) $(FPIC) -shared \
$(TEST_TMP)/lesson_10_halide.py.c \
$(TEST_TMP)/lesson_10_halide.o \
-I $(TEST_TMP) -o $@
clean:
rm -rf $(BIN)
test: test_correctness test_apps test_tutorial