Makefile: Add `make upgrade` target
[armband.git] / Makefile
1 ##############################################################################
2 # Copyright (c) 2016,2017 Cavium, Enea AB and others.
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7 ##############################################################################
8
9 # NOTE: Armband patching consists of:
10 # - clone upstream repositories to git submodules, tracking remotes;
11 # - tag each submodule (before patching) with "${A_OPNFV_TAG}-root";
12 # - apply Armband patches for each submodule;
13 # - tag each submodule (after patching) with "${OPNFV_TAG}";
14 # - stage Fuel submodule patches by copying them to Fuel f_repos/patch dir;
15 # - pass updated repository info to Fuel@OPNFV build system
16 #   (e.g. FUEL_PLUGIN_ODL_CHANGE="${OPNFV_TAG}") via armband-fuel-config.mk;
17
18 # NOTE: Long-term goals (Armband repo should merge with Fuel@OPNFV):
19 # - all build related changes should affect Fuel@OPNFV, NOT Armband;
20 # - Armband make/build system should only handle patching non-Fuel submodules,
21 #   and then invoke Fuel@OPNFV's patch & build system;
22 # - Fuel@OPNFV is made aware of an Armband type build by passing
23 #   the "ARMBAND_BASE" env var;
24
25 SHELL = /bin/sh
26
27 export ARMBAND_BASE  := $(shell pwd)
28 export OPNFV_GIT_SHA := $(shell git rev-parse HEAD)
29 export REVSTATE
30
31 include armband-fuel-config.mk
32
33 all: upgrade
34
35 # Ignore release tag and upgrade Armband to latest change on <branch>/HEAD
36 .PHONY: upgrade
37 upgrade:
38         @git fetch origin ${A_BRANCH}:${A_BRANCH}
39         @git checkout ${A_BRANCH}
40         @$(MAKE) -e submodules-clean patches-import
41
42 # Fetch & update git submodules, checkout remote HEAD
43 .PHONY: submodules-init
44 submodules-init: .submodules-init
45
46 .submodules-init:
47         @if [ -n "${ARMBAND_TRACK_REMOTES}" ]; then \
48                 git submodule update --init --remote 2>/dev/null; \
49         else \
50                 git submodule update --init 2>/dev/null; \
51         fi
52         @touch $@
53
54 # Clean any changes made to submodules, checkout Armband root commit
55 .PHONY: submodules-clean
56 submodules-clean:
57         @git submodule -q foreach ' \
58                 git am -q --abort 2>/dev/null; \
59                 git checkout -q -f ${A_OPNFV_TAG}-root 2>/dev/null; \
60                 git branch -q -D opnfv-armband 2>/dev/null; \
61                 git tag | grep ${A_OPNFV_TAG} | xargs git tag -d > /dev/null 2>&1; \
62                 git reset -q --hard HEAD; \
63                 git clean -xdff'
64         @rm -f .submodules-*
65         @$(MAKE) -e submodules-init
66
67 # Generate patches from submodules
68 .PHONY: patches-export
69 patches-export: .submodules-init
70         @git submodule -q foreach ' \
71                 SUB_DIR=${A_PATCH_DIR}/$$name; \
72                 rm -rf $$SUB_DIR/*; \
73                 git tag | awk "!/root/ && /${A_OPNFV_TAG}-fuel/" | while read A_TAG; do \
74                         SUB_FEATURE=`dirname $${A_TAG#${A_OPNFV_TAG}-fuel/}`; \
75                         echo "`tput setaf 2`== exporting $$name ($$A_TAG)`tput sgr0`"; \
76                         mkdir -p $$SUB_DIR/$${SUB_FEATURE} && \
77                         git format-patch --no-signature --ignore-space-at-eol \
78                                 -o $$SUB_DIR/$$SUB_FEATURE -N $$A_TAG-root..$$A_TAG; \
79                         sed -i -e "1{/From: /!d}" -e "s/[[:space:]]*$$//" \
80                                 $$SUB_DIR/$$SUB_FEATURE/*.patch; \
81                 done'
82
83 # Apply patches from patches/* to respective submodules
84 .PHONY: patches-import
85 patches-import: .submodules-init .submodules-patched
86
87 .submodules-patched: ${A_PATCHES}
88         @$(MAKE) submodules-clean
89         @git submodule -q foreach ' \
90                 SUB_DIR=${A_PATCH_DIR}/$$name; mkdir -p $$SUB_DIR && \
91                 git tag ${A_OPNFV_TAG}-root && \
92                 git checkout -q -b opnfv-armband && \
93                 find $$SUB_DIR -type d | sort | while read p_dir; do \
94                         SUB_PATCHES=$$(ls $$p_dir/*.patch 2>/dev/null); \
95                         if [ -n "$$SUB_PATCHES" ]; then \
96                                 SUB_FEATURE=$${p_dir#$$SUB_DIR} \
97                                 SUB_TAG=${A_OPNFV_TAG}-fuel$$SUB_FEATURE/patch; \
98                                 echo "`tput setaf 2`== patching $$name ($$SUB_TAG)`tput sgr0`";\
99                                 git tag $$SUB_TAG-root && \
100                                 git am -3 --whitespace=nowarn --patch-format=mbox \
101                                         --committer-date-is-author-date $$SUB_PATCHES && \
102                                 git tag $$SUB_TAG || exit 1; \
103                         fi \
104                 done && \
105                 git tag ${A_OPNFV_TAG}'
106         # Staging Fuel@OPNFV patches
107         @ls -d ${F_PATCH_DIR}/* 2>/dev/null | while read p_sub_path; do \
108                 SUB_NAME=`basename $$p_sub_path`; \
109                 find ${A_PATCH_DIR}/$$SUB_NAME -name '*.patch' 2>/dev/null -exec sh -c '\
110                         A_PATCH={}; R_PATCH=$${A_PATCH#${A_PATCH_DIR}/}; \
111                         F_PATCH=${F_PATCH_DIR}/$${0}/armband/$${R_PATCH#$${0}/}; \
112                         if [ -f $$F_PATCH ]; then \
113                                 echo "`tput setaf 3`* WARN: $$R_PATCH upstream.`tput sgr0`"; \
114                         else \
115                                 if [ -h $$A_PATCH ]; then \
116                                         echo "`tput setaf 3`* PHONY: $$R_PATCH`tput sgr0`"; \
117                                 else \
118                                         echo "`tput setaf 6`* Staging $$R_PATCH`tput sgr0`"; \
119                                         mkdir -p `dirname $$F_PATCH` && cp $$A_PATCH $$F_PATCH; \
120                                 fi; \
121                         fi' "$$SUB_NAME" \; || true ; \
122         done
123         @touch $@
124
125 ##############################################################################
126 # Fuel@OPNFV patch operations - to be used only during development
127 ##############################################################################
128 # Apply all Fuel@OPNFV patches, including Armband patches
129 .PHONY: fuel-patches-import
130 fuel-patches-import: .submodules-patched fuel-patches-clean
131         $(MAKE) -e -C ${F_PATCH_DIR} patches-import
132
133 .PHONY: fuel-patches-clean
134 fuel-patches-clean:
135         $(MAKE) -e -C ${F_PATCH_DIR} clean
136
137 # Add copyright header to patch files if not already present
138 .PHONY: patches-copyright
139 patches-copyright:
140         @grep -e "Copyright (c)" -L ${A_PATCHES} | while read p_file; do \
141                 ptmp=`mktemp` && \
142                 cat armband-patch-copyright.template $$p_file > $$ptmp && \
143                 mv $$ptmp $$p_file; \
144         done