SourceRevision.gmk revision 2343:84669e1b01fe
1112758Ssam#
2112758Ssam# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3112758Ssam# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4112758Ssam#
5112758Ssam# This code is free software; you can redistribute it and/or modify it
6112758Ssam# under the terms of the GNU General Public License version 2 only, as
7112758Ssam# published by the Free Software Foundation.  Oracle designates this
8112758Ssam# particular file as subject to the "Classpath" exception as provided
9112758Ssam# by Oracle in the LICENSE file that accompanied this code.
10112758Ssam#
11112758Ssam# This code is distributed in the hope that it will be useful, but WITHOUT
12112758Ssam# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13112758Ssam# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14112758Ssam# version 2 for more details (a copy is included in the LICENSE file that
15112758Ssam# accompanied this code).
16112758Ssam#
17112758Ssam# You should have received a copy of the GNU General Public License version
18112758Ssam# 2 along with this work; if not, write to the Free Software Foundation,
19112758Ssam# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20112758Ssam#
21112758Ssam# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22112758Ssam# or visit www.oracle.com if you need additional information or have any
23112758Ssam# questions.
24112758Ssam#
25112758Ssam
26112758Ssamdefault: all
27112758Ssam
28105197Ssaminclude $(SPEC)
29105197Ssaminclude MakeBase.gmk
30105197Ssam
31105197Ssam################################################################################
32105197Ssam# Keep track of what source revision is used to create the build, by creating
33105197Ssam# a tracker file in the output directory. This tracker file is included in the
34105197Ssam# image, and can be used to recreate the source revision used.
35159965Sthompsa#
36105197Ssam# We're either building directly from a mercurial forest, and if so, use the
37105197Ssam# current revision from mercurial. Otherwise, we are building from a source
38105197Ssam# bundle. As a part of creating this source bundle, the current mercurial
39105197Ssam# revisions of all repos will be stored in a file in the top dir, which is then
40105197Ssam# used when creating the tracker file.
41105197Ssam
42105197SsamSTORED_SOURCE_REVISION := $(TOPDIR)/.src-rev
43105197Ssam
44105197Ssam# Are we using mercurial?
45105197Ssamifneq ($(and $(HG), $(wildcard $(TOPDIR)/.hg)), )
46105197Ssam
47257176Sglebius  # Verify that the entire forest is consistent
48171497Sbz  $(foreach repo, $(call FindAllReposRel), \
49105197Ssam    $(if $(wildcard $(TOPDIR)/$(repo)/.hg),, \
50195699Srwatson        $(error Inconsistent revision control: $(repo) is missing .hg directory)) \
51105197Ssam  )
52105197Ssam
53105197Ssam  # Replace "." with "_top" and "/" with "-"
54105197Ssam  MakeFilenameFromRepo = \
55105197Ssam      $(strip $(subst .,top, $(subst /,-, $1)))
56105197Ssam
57105197Ssam  ################################################################################
58105197Ssam  # SetupGetRevisionForRepo defines a make rule for creating a file containing
59105197Ssam  # the name of the repository and the output of "hg id" for that repository.
60105197Ssam  # Argument 1 is the relative path to the repository from the top dir.
61105197Ssam  #
62105197Ssam  SetupGetRevisionForRepo = $(NamedParamsMacroTemplate)
63105197Ssam  define SetupGetRevisionForRepoBody
64105197Ssam    $1_REPO_PATH :=  $$(TOPDIR)/$$(strip $1)
65105197Ssam    $1_FILENAME := $$(call MakeFilenameFromRepo, $1)
66105197Ssam
67105197Ssam    $$(SUPPORT_OUTPUTDIR)/src-rev/$$($1_FILENAME): FRC
68105197Ssam	$$(call MakeDir, $$(@D))
69105197Ssam	$$(ECHO) $$(strip $1):`$$(HG) id -i --repository $$($1_REPO_PATH)` > $$@
70105197Ssam
71105197Ssam    REPO_REVISIONS += $$(SUPPORT_OUTPUTDIR)/src-rev/$$($1_FILENAME)
72105197Ssam  endef
73105197Ssam
74105197Ssam  # Setup rules for all repos. This makes sure all the "hg id" calls are made
75105197Ssam  # in parallel.
76105197Ssam  $(foreach repo, $(call FindAllReposRel), \
77105197Ssam    $(eval $(call SetupGetRevisionForRepo, $(repo))) \
78105197Ssam  )
79105197Ssam
80105197Ssam  # Create a complete source revision output file from all repos
81105197Ssam  # Param 1: The output file
82105197Ssam  define CreateSourceRevisionFile
83105197Ssam    $1: $$(REPO_REVISIONS)
84105197Ssam	$$(call MakeDir, $$(@D))
85105197Ssam	$$(ECHO) `$$(CAT) $$(REPO_REVISIONS)` > $$@.tmp
86105197Ssam	if [ ! -f $$@ ] || [ "`$$(CAT) $$@`" != "`$$(CAT) $$@.tmp`" ]; then \
87194062Svanhu	  $$(MV) $$@.tmp $$@ ; \
88194062Svanhu	else \
89194062Svanhu	  $$(RM) $$@.tmp ; \
90194062Svanhu	fi
91181627Svanhu  endef
92181627Svanhu
93181627Svanhu  $(eval $(call CreateSourceRevisionFile, $(STORED_SOURCE_REVISION)))
94181627Svanhu
95181627Svanhu  store-source-revision: $(STORED_SOURCE_REVISION)
96105197Ssam
97105197Ssam  $(eval $(call CreateSourceRevisionFile, $(SOURCE_REVISION_TRACKER)))
98105197Ssam
99105197Ssam  create-source-revision-tracker: $(SOURCE_REVISION_TRACKER)
100105197Ssam
101105197Ssamelse
102105197Ssam  # Not using HG
103105197Ssam
104105197Ssam  ifneq ($(wildcard $(STORED_SOURCE_REVISION)), )
105120585Ssam    # We have a stored source revision (.src-rev)
106120585Ssam
107105197Ssam    store-source-revision:
108120585Ssam	$(call LogWarn, Warning: No mercurial configuration present, not updating .src-rev)
109120585Ssam
110105197Ssam    $(SOURCE_REVISION_TRACKER): $(STORED_SOURCE_REVISION)
111105197Ssam	$(install-file)
112105197Ssam
113105197Ssam    create-source-revision-tracker: $(SOURCE_REVISION_TRACKER)
114105197Ssam  else
115105197Ssam    # We don't have a stored source revision. Can't do anything, really.
116105197Ssam
117105197Ssam    store-source-revision:
118105197Ssam	$(call LogWarn, Error: No mercurial configuration present, cannot create .src-rev)
119105197Ssam	exit 2
120105197Ssam
121105197Ssam    create-source-revision-tracker:
122105197Ssam	$(call LogWarn, Error: No mercurial configuration present and no .src-rev)
123105197Ssam	exit 2
124105197Ssam  endif
125105197Ssam
126105197Ssamendif
127105197Ssam
128105197Ssamall: store-source-revision create-source-revision-tracker
129105197Ssam
130105197SsamFRC: # Force target
131105197Ssam
132105197Ssam.PHONY: all store-source-revision create-source-revision-tracker
133105197Ssam