d1126db74751af1a2138c14db81e7f4f1448f10c
[vpp.git] / build / external / packages.mk
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 define h1
15         @echo "--- $(1)"
16 endef
17
18 define package
19 $1_tarball_strip_dirs ?= 0
20 $1_src_dir ?= $(B)/src-$1
21 $1_patch_dir ?= $(CURDIR)/patches/$1_$($1_version)
22 $1_build_dir ?= $(B)/build-$1
23 $1_install_dir ?= $(I)
24 $1_config_log ?= $(B)/$1.config.log
25 $1_build_log ?= $(B)/$1.build.log
26 $1_install_log ?= $(B)/$1.install.log
27
28 ##############################################################################
29 # Download
30 ##############################################################################
31 downloads/$($1_tarball):
32         mkdir -p downloads
33         @if [ -e $(DL_CACHE_DIR)/$($1_tarball) ] ; \
34                 then cp $(DL_CACHE_DIR)/$($1_tarball) $$@ ; \
35         else \
36                 echo "Downloading $($1_url)" ; \
37                 curl -o $$@ -LO $($1_url) ; \
38         fi
39         @rm -f $(B)/.$1.download.ok
40
41 $(B)/.$1.download.ok: downloads/$($1_tarball)
42         @mkdir -p $(B)
43         $$(call h1,"validating $1 $($1_version) checksum")
44         @SUM=$$(shell openssl md5 $$< | cut -f 2 -d " " -) ; \
45         ([ "$$$${SUM}" = "$($1_tarball_md5sum)" ] || \
46         ( echo "==========================================================" && \
47           echo "Bad Checksum!" && \
48           echo "Expected:   $($1_tarball_md5sum)" && \
49           echo "Calculated: $$$${SUM}" && \
50           echo "Please remove $$< and retry" && \
51           echo "==========================================================" && \
52           false ))
53         @touch $$@
54
55 .PHONY: $1-download
56 $1-download: $(B)/.$1.download.ok
57
58 ##############################################################################
59 # Extract
60 ##############################################################################
61 $(B)/.$1.extract.ok: $(B)/.$1.download.ok
62         $$(call h1,"extracting $1 $($1_version)")
63         @mkdir -p $$($1_src_dir)
64         @tar \
65           --directory $$($1_src_dir) \
66           --extract \
67           --strip-components=$$($1_tarball_strip_dirs) \
68           --file downloads/$($1_tarball)
69         @touch $$@
70
71 .PHONY: $1-extract
72 $1-extract: $(B)/.$1.extract.ok
73
74 ##############################################################################
75 # Git clone & checkout
76 ##############################################################################
77
78 $(B)/.$1.clone.ok:
79         $$(call h1,"Cloning $1 $($1_repository)")
80         @mkdir -p $$($1_src_dir)
81         @git clone --recursive $$($1_repository) $$($1_src_dir)
82 ifneq ($$($1_version),)
83         $$(call h1,"Checking out $1 $($1_version)")
84         cd $$($1_src_dir) && git -c advice.detachedHead=false checkout $$($1_version)
85         cd $$($1_src_dir) && git submodule update --init
86 endif
87         @touch $$@
88
89 .PHONY: $1-clone
90 $1-clone: $(B)/.$1.clone.ok
91
92 ##############################################################################
93 # Fetch source : clone or extract
94 ##############################################################################
95
96 ifeq ($$($1_repository),)
97 $(B)/.$1.fetchsrc.ok: $(B)/.$1.extract.ok
98         @touch $$@
99 else
100 $(B)/.$1.fetchsrc.ok: $(B)/.$1.clone.ok
101         @touch $$@
102 endif
103
104 ##############################################################################
105 # Patch
106 ##############################################################################
107 $(B)/.$1.patch.ok: $(B)/.$1.fetchsrc.ok
108         $$(call h1,"patching $1 $($1_version)")
109 ifneq ($$(wildcard $$($1_patch_dir)/*.patch),)
110         @for f in $$($1_patch_dir)/*.patch ; do \
111                 echo "Applying patch: $$$$(basename $$$$f)" ; \
112                 patch -p1 -d $$($1_src_dir) < $$$$f ; \
113         done
114 endif
115         @touch $$@
116
117 .PHONY: $1-patch
118 $1-patch: $(B)/.$1.patch.ok
119
120 ##############################################################################
121 # Config
122 ##############################################################################
123
124 ifeq ($$(call $1_config_cmds),)
125 define $1_config_cmds
126         @cd $$($1_build_dir) && \
127           CFLAGS="$$($1_cflags)" \
128           $$($1_src_dir)/configure \
129             --prefix=$$($1_install_dir) \
130             $$($1_configure_args) > $$($1_config_log)
131 endef
132 endif
133
134 $(B)/.$1.config.ok: $(B)/.$1.patch.ok $(addsuffix -install,$($1_depends))
135         $$(call h1,"configuring $1 $($1_version) - log: $$($1_config_log)")
136         @mkdir -p $$($1_build_dir)
137         $$(call $1_config_cmds)
138         @touch $$@
139
140 .PHONY: $1-config
141 $1-config: $(B)/.$1.config.ok
142
143 ##############################################################################
144 # Build
145 ##############################################################################
146
147 ifeq ($$(call $1_build_cmds),)
148 define $1_build_cmds
149         @$(MAKE) $(MAKE_ARGS) -C $$($1_build_dir) > $$($1_build_log)
150 endef
151 endif
152
153 $(B)/.$1.build.ok: $(B)/.$1.config.ok
154         $$(call h1,"building $1 $($1_version) - log: $$($1_build_log)")
155         $$(call $1_build_cmds)
156         @touch $$@
157
158 .PHONY: $1-build
159 $1-build: $(B)/.$1.build.ok
160
161 ##############################################################################
162 # Install
163 ##############################################################################
164
165 ifeq ($$(call $1_install_cmds),)
166 define $1_install_cmds
167         @$(MAKE) $(MAKE_ARGS) -C $$($1_build_dir) install > $$($1_install_log)
168 endef
169 endif
170
171 $(B)/.$1.install.ok: $(B)/.$1.build.ok
172         $$(call h1,"installing $1 $($1_version) - log: $$($1_install_log)")
173         $$(call $1_install_cmds)
174         @touch $$@
175
176 .PHONY: $1-install
177 $1-install: $(B)/.$1.install.ok
178
179 ALL_TARGETS += $1-install
180 endef