1 # builds opencv using want_var to create bld from src
2 # want_var set as: "" (empty) or "bld=typ" or "bld=typ,src=typ"
3 # empty: defaults to bld=sys
4 # bld=typ sta,dyn,sys: sta=static, dyn=shared, sys=system libraries
5 # src=typ git,tar,git=url,tar=url: git (default git_url), tar (default: tar_url)
6 # once thirdparty/opencv is built, it will be reused.
7 # use target "mrclean" to remove thirdparty/opencv*
9 ifeq (no,$(shell [ -d $(THIRDPARTY) ] && echo yes || echo no))
10 $(error THIRDPARTY is not a directory: '$(THIRDPARTY)')
13 blddir:=$(shell cd $(THIRDPARTY) && pwd)
15 opencv:=$(blddir)/opencv
16 opencv_prefix:=$(opencv)/build/usr/local
18 # if previous build stamped, use that bld
19 ifneq (,$(wildcard $(opencv).sta))
21 else ifneq (,$(wildcard $(opencv).dyn))
23 else ifneq (,$(wildcard $(opencv).sys))
28 params:=$(subst $(comma), ,$(want_var))
30 # code: src=tar/tar=tar_url;git/git=git_url, build: bld=sta/dyn/sys
31 $(foreach p,$(params),\
32 $(if $(bld),,$(eval var:=$(filter sta dyn sys,$(p)))\
33 $(if $(var),$(eval bld:=$(var))))\
34 $(if $(src),,$(eval var:=$(filter git tar,$(p)))\
35 $(if $(var),$(eval src:=$(var)),\
36 $(if $(filter git=%,$(p)),$(eval src:=git)$(eval git_url=$(patsubst git=%,%,$(p))),\
37 $(if $(filter tar=%,$(p)),$(eval src:=tar)$(eval tar_url=$(patsubst tar=%,%,$(p))))))))
39 # defaults: src=git, bld=sys
42 git_url?=https://github.com/opencv
43 tar_url?=https://cinelerra-cv.org/five/src/opencv-20170916.tgz
44 opencv_tar:=$(notdir $(tar_url))
46 cpus:=$(shell grep -c "^proc" /proc/cpuinfo)
47 jobs:=-j$(shell echo $$(($(cpus) + $(cpus)/2 +2)))
49 CFLAGS += -I$(opencv_prefix)/include
53 cd $(blddir) && rm -rf opencv opencv_contrib opencv.*
54 git -C $(blddir) clone --depth 1 $(git_url)/opencv.git
55 git -C $(blddir) clone --depth 1 $(git_url)/opencv_contrib.git
57 else ifeq ($(src),tar)
58 $(srcdir)/$(opencv_tar):
59 cd $(srcdir) && rm -f $(opencv_tar)
60 wget -P $(srcdir) "$(tar_url)"
62 $(opencv).src: $(srcdir)/$(opencv_tar)
63 cd $(blddir) && rm -rf opencv opencv_contrib opencv.*
64 tar -C $(blddir) -xzf $(srcdir)/$(opencv_tar);
67 $(error src not git/tar)
71 $(opencv)/build: $(opencv).src
74 -DCMAKE_BUILD_TYPE=RELEASE \
76 -DBUILD_SHARED_LIBS=OFF \
77 -DINSTALL_C_EXAMPLES=OFF \
78 -DINSTALL_PYTHON_EXAMPLES=OFF \
79 -DBUILD_EXAMPLES=OFF .. \
80 -DCMAKE_INSTALL_PREFIX=/usr/local \
81 -DOPENCV_EXTRA_MODULES_PATH="$(opencv)_contrib/modules/"
83 LFLAGS += -Wl,--start-group
84 LFLAGS += $(shell find $(opencv_prefix)/lib* -name "libopencv_*.a" 2> /dev/null)
85 LFLAGS += $(shell find $(opencv_prefix)/share/OpenCV/3rdparty/lib* -name "lib*.a" 2> /dev/null)
86 LFLAGS += -Wl,--end-group
87 else ifeq ($(bld),dyn)
88 $(opencv)/build: $(opencv).src
91 -DCMAKE_BUILD_TYPE=RELEASE \
93 -DBUILD_SHARED_LIBS=ON \
94 -DINSTALL_C_EXAMPLES=ON \
95 -DINSTALL_PYTHON_EXAMPLES=ON \
96 -DBUILD_EXAMPLES=ON .. \
97 -DCMAKE_INSTALL_PREFIX=/usr/local \
98 -DOPENCV_EXTRA_MODULES_PATH="$(opencv)_contrib/modules/"
100 SYSLIB := $(lastword $(wildcard /usr/lib /usrlib32 /usr/lib64))
101 CVLIBS := $(dir (shell find $(opencv_prefix) -name libopencv_core.a))
102 LFLAGS += -L$(CVLIBS) $(patsubst $(CVLIBS)/lib%.so,-l%,$(wildcard $(CVLIBS)/libopencv_*.so))
103 LFLAGS += $(patsubst $(SYSLIB)/lib%.so,-l%,$(wildcard $(SYSLIB)/lib{Half,Imath,Ilm,Iex}*.so))
106 else ifeq ($(bld),sys)
109 SYSLIB := $(lastword $(wildcard /usr/lib /usrlib32 /usr/lib64))
110 LFLAGS += $(patsubst $(SYSLIB)/lib%.so,-l%,$(wildcard $(SYSLIB)/libopencv_*.so))
111 LFLAGS += $(patsubst $(SYSLIB)/lib%.so,-l%,$(wildcard $(SYSLIB)/lib{Half,Imath,Ilm,Iex}*.so))
115 $(error bld not sta/dyn/sys)
118 $(opencv).sta $(opencv).dyn: $(opencv)/build
119 cd $(opencv)/build && make $(jobs) 2>&1 | tee log
120 cd $(opencv)/build && make install DESTDIR=`pwd`
127 rm -rf $(blddir)/opencv* $(srcdir)/$(opencv_tar)
129 opencv: $(opencv).$(bld)