Turn on remote tracking of submodules
[fuel.git] / build / f_repos / Makefile
1 ##############################################################################
2 # Copyright (c) 2015,2016 Ericsson AB, Enea AB and others.
3 # stefan.k.berg@ericsson.com
4 # jonas.bjurel@ericsson.com
5 # Alexandru.Avadanii@enea.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 SHELL = /bin/sh
13 REPOINFO = $(shell readlink -e ../repo_info.sh)
14 FREPODIR = $(shell pwd)
15 FPATCHES = $(shell find ${F_PATCH_DIR} -name '*.patch')
16
17 include ../config.mk
18
19 # NOTE: Mechanism overview is presented in ./README.md.
20
21 # Submodule consistent states:
22 #   - NOT initialized (submodule trees are not populated at all);
23 #   - initialized, bound to saved commits;
24 #   - initialized, tracking remote origin (only for FUEL_TRACK_REMOTES);
25 #   - patched (local patches are applied);
26
27 # In order to keep things sort of separate, we should only pass up (to main
28 # Makefile) the fully-pactched repos, and gather any fingerprinting info here.
29
30 # Fuel@OPNFV relies on upstream git repos (one per component) in 1 of 2 ways:
31 #   - pinned down to tag objects (e.g. "9.0.1")
32 #   - tracking upstream remote HEAD on a stable or master branch
33 # FIXME(alav): Should we support mixed cases? (e.g. pin down only fuel-main)
34
35 # To enable remote tracking, set the following var to any non-empty string.
36 # Leaving this var empty will bind each git submodule to its saved commit.
37 # NOTE: For non-stable branches, unless overriden, always track remotes
38 FUEL_TRACK_REMOTES ?= $(shell git symbolic-ref --short HEAD | grep -v stable)
39
40 .PHONY: all
41 all: release
42
43 ##############################################################################
44 # git submodule operations - to be used stand-alone or from parent Makefile
45 ##############################################################################
46
47 # Fetch & update git submodules, checkout remote HEAD or saved commit
48 # Also gather fingerprints for parent gitinfo_fuel.txt.
49 .PHONY: sub
50 sub: .cachefuelinfo
51
52 .cachefuelinfo:
53         @if [ -n "${FUEL_TRACK_REMOTES}" ]; then \
54                 git submodule update --init --remote 2>/dev/null; \
55         else \
56                 git submodule update --init 2>/dev/null; \
57         fi
58         @rm -f $@
59         @git submodule -q foreach '${REPOINFO} . >> ${FREPODIR}/$@'
60
61 # Generate patches from submodules
62 .PHONY: patches-export
63 patches-export: sub
64         @git submodule -q foreach ' \
65                 SUB_DIR=${F_PATCH_DIR}/$$name; \
66                 git tag | awk "!/root/ && /${F_OPNFV_TAG}-fuel/" | while read F_TAG; do \
67                         SUB_FEATURE=`dirname $${F_TAG#${F_OPNFV_TAG}-fuel/}`; \
68                         echo "`tput setaf 2`-- exporting $$name ($$F_TAG)`tput sgr0`"; \
69                         mkdir -p $$SUB_DIR/$${SUB_FEATURE} && \
70                         git format-patch --no-signature --ignore-space-at-eol \
71                                 -o $$SUB_DIR/$$SUB_FEATURE -N $$F_TAG-root..$$F_TAG; \
72                         sed -i -e "1{/From: /!d}" -e "s/[[:space:]]*$$//" \
73                                 $$SUB_DIR/$$SUB_FEATURE/*.patch; \
74                 done'
75
76 # Apply patches from patch/* to respective submodules
77 # We rely on `make sub` and/or `make clean` to checkout correct base
78 .PHONY: patches-import
79 patches-import: sub .cachepatched
80
81 .cachepatched: ${FPATCHES}
82         @$(MAKE) clean
83         @git submodule -q foreach ' \
84                 SUB_DIR=${F_PATCH_DIR}/$$name; mkdir -p $$SUB_DIR && \
85                 git tag ${F_OPNFV_TAG}-root && \
86                 git checkout -q -b opnfv-fuel && \
87                 find $$SUB_DIR -type d | sort | while read p_dir; do \
88                         SUB_PATCHES=$$(ls $$p_dir/*.patch 2>/dev/null); \
89                         if [ -n "$$SUB_PATCHES" ]; then \
90                                 SUB_FEATURE=$${p_dir#$$SUB_DIR}; \
91                                 SUB_TAG=${F_OPNFV_TAG}-fuel$$SUB_FEATURE/patch; \
92                                 echo "`tput setaf 2`-- patching $$name ($$SUB_TAG)`tput sgr0`";\
93                                 git tag $$SUB_TAG-root && git am -3 --whitespace=nowarn \
94                                         --committer-date-is-author-date $$SUB_PATCHES && \
95                                 git tag $$SUB_TAG || exit 1; \
96                         fi \
97                 done && \
98                 git tag ${F_OPNFV_TAG}'
99         @touch $@
100
101 # Clean any changes made to submodules, checkout upstream Fuel root commit
102 .PHONY: clean
103 clean:
104         @cd ${F_GIT_ROOT} && git submodule -q foreach ' \
105                 git am -q --abort > /dev/null 2>&1; \
106                 git checkout -q -f ${F_OPNFV_TAG}-root > /dev/null 2>&1; \
107                 git branch -q -D opnfv-fuel > /dev/null 2>&1; \
108                 git tag | grep ${F_OPNFV_TAG} | xargs git tag -d > /dev/null 2>&1; \
109                 git reset -q --hard HEAD; \
110                 git clean -xdff'
111         @rm -f .cachepatched
112
113 .PHONY: deepclean
114 deepclean: clean clean-cache
115         @git submodule deinit -f .
116         @rm -f .cache*
117
118 .PHONY: release
119 release: sub
120         # Store artifact in cache straight away if caching is enabled
121         # (no .cacheid will be present unless this is a cached build)
122         test -f .cacheid && $(MAKE) -f Makefile put-cache || exit 0
123         # NOTE: Patches are not included in cache
124         $(MAKE) -f Makefile patches-import
125
126 ##############################################################################
127 # Cache operations - only used when building through ci/build.sh
128 ##############################################################################
129
130 # NOTE: Current method of collecting submodule refs requires submodules to be
131 #       NOT initialized <OR> NO patches applied.
132 # NOTE: Querying `git submodule status` from parent will show the patched tree.
133 # Create a unique hash to be used for getting and putting cache, based on:
134 #   - git submodule SHAs, collected with `git submodule status`
135 #   - The contents of this Makefile
136 .cacheid:
137         @$(MAKE) clean
138         sha1sum Makefile > .cachedata
139         git submodule status | cut -c2-41 >> .cachedata
140         cat .cachedata | $(CACHETOOL) getid > .cacheid
141
142 # Clean local data related to caching - called prior to ordinary build
143 .PHONY: clean-cache
144 clean-cache: clean
145         rm -f .cachedata .cacheid
146
147 # Try to download cache - called prior to ordinary build
148 .PHONY: get-cache
149 get-cache: .cacheid
150         @if $(CACHETOOL) check $(shell cat .cacheid); then \
151                 $(MAKE) clean && \
152                 $(CACHETOOL) get $(shell cat .cacheid) | \
153                         tar xf - -C ${F_GIT_DIR}; \
154         else \
155                 echo "No cache item found for $(shell cat .cacheid)" ;\
156                 exit 0;\
157         fi
158
159 # Store cache if not already stored - called after ordinary build
160 .PHONY: put-cache
161 put-cache: .cacheid
162         @if ! $(CACHETOOL) check $(shell cat .cacheid); then \
163                 $(MAKE) clean && \
164                 tar cf - -C ${F_GIT_DIR} modules | \
165                         $(CACHETOOL) put $(shell cat .cacheid); \
166         fi