8000 Makefile: Better numpy handling and make building more generic · hypercode-go/matplotlib-cpp@dbe8e4c · GitHub
[go: up one dir, main page]

Skip to content

Commit dbe8e4c

Browse files
alexdewarlava
authored andcommitted
Makefile: Better numpy handling and make building more generic
Changes: * Use system version of python by default (overridable with PYTHON_BIN) * Find where numpy headers live on system * Remove boilerplate from Makefile and just build all examples/*.cpp files with appropriate flags
1 parent d3137c5 commit dbe8e4c

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

Makefile

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,25 @@
1-
examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update imshow
21

3-
minimal: examples/minimal.cpp matplotlibcpp.h
4-
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
2+
# Use C++11
3+
CXXFLAGS += -std=c++11
54

6-
basic: examples/basic.cpp matplotlibcpp.h
7-
cd examples && g++ basic.cpp -I/usr/include/python2.7 -lpython2.7 -o basic -std=c++11
5+
# Default to using system's default version of python
6+
PYTHON_BIN ?= python
7+
PYTHON_CONFIG := $(PYTHON_BIN)-config
8+
PYTHON_INCLUDE ?= $(shell $(PYTHON_CONFIG) --includes)
9+
CXXFLAGS += $(PYTHON_INCLUDE)
10+
LDFLAGS += $(shell $(PYTHON_CONFIG) --libs)
811

9-
modern: examples/modern.cpp matplotlibcpp.h
10-
cd examples && g++ modern.cpp -I/usr/include/python2.7 -lpython2.7 -o modern -std=c++11
12+
# Either finds numpy or set -DWITHOUT_NUMPY
13+
CURRENT_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
14+
CXXFLAGS += $(shell $(PYTHON_BIN) $(CURRENT_DIR)/numpy_flags.py)
1115

12-
animation: examples/animation.cpp matplotlibcpp.h
13-
cd examples && g++ animation.cpp -I/usr/include/python2.7 -lpython2.7 -o animation -std=c++11
16+
# Assume every *.cpp file is a separate example
17+
SOURCES ?= $(wildcard examples/*.cpp)
18+
EXECUTABLES := $(foreach exec,$(basename $(SOURCES)),$(exec))
1419

15-
nonblock: examples/nonblock.cpp matplotlibcpp.h
16-
cd examples && g++ nonblock.cpp -I/usr/include/python2.7 -lpython2.7 -o nonblock -std=c++11
20+
.PHONY: examples
1721

18-
quiver: examples/quiver.cpp matplotlibcpp.h
19-
cd examples && g++ quiver.cpp -I/usr/include/python2.7 -lpython2.7 -o quiver -std=c++11
20-
21-
xkcd: examples/xkcd.cpp matplotlibcpp.h
22-
cd examples && g++ xkcd.cpp -I/usr/include/python2.7 -lpython2.7 -o xkcd -std=c++11
23-
24-
bar: examples/bar.cpp matplotlibcpp.h
25-
cd examples && g++ bar.cpp -I/usr/include/python2.7 -lpython2.7 -o bar -std=c++11
26-
27-
surface: examples/surface.cpp matplotlibcpp.h
28-
cd examples && g++ surface.cpp -I/usr/include/python2.7 -lpython2.7 -o surface -std=c++11
29-
30-
fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
31-
cd examples && g++ fill_inbetween.cpp -I/usr/include/python2.7 -lpython2.7 -o fill_inbetween -std=c++11
32-
33-
fill: examples/fill.cpp matplotlibcpp.h
34-
cd examples && g++ fill.cpp -I/usr/include/python2.7 -lpython2.7 -o fill -std=c++11
35-
36-
update: examples/update.cpp matplotlibcpp.h
37-
cd examples && g++ update.cpp -I/usr/include/python2.7 -lpython2.7 -o update -std=c++11
38-
39-
imshow: examples/imshow.cpp matplotlibcpp.h
40-
cd examples && g++ imshow.cpp -I/usr/include/python2.7 -lpython2.7 -o imshow -std=c++11
22+
examples: $(EXECUTABLES)
4123

4224
clean:
43-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update,imshow}
25+
rm -f ${EXECUTABLES}

numpy_flags.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from os import path
2+
3+
try:
4+
from numpy import __file__ as numpyloc
5+
6+
# Get numpy directory
7+
numpy_dir = path.dirname(numpyloc)
8+
9+
# Print the result of joining this to core and include
10+
print("-I" + path.join(numpy_dir, "core", "include"))
11+
except:
12+
print("-DWITHOUT_NUMPY")

0 commit comments

Comments
 (0)
0