SourceRevision.gmk revision 2344:326cbee3e265
144743Smarkm#
256977Sshin# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
344743Smarkm# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
444743Smarkm#
544743Smarkm# This code is free software; you can redistribute it and/or modify it
644743Smarkm# under the terms of the GNU General Public License version 2 only, as
744743Smarkm# published by the Free Software Foundation.  Oracle designates this
844743Smarkm# particular file as subject to the "Classpath" exception as provided
944743Smarkm# by Oracle in the LICENSE file that accompanied this code.
1044743Smarkm#
1144743Smarkm# This code is distributed in the hope that it will be useful, but WITHOUT
1244743Smarkm# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1344743Smarkm# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1444743Smarkm# version 2 for more details (a copy is included in the LICENSE file that
1544743Smarkm# accompanied this code).
1644743Smarkm#
1744743Smarkm# You should have received a copy of the GNU General Public License version
1844743Smarkm# 2 along with this work; if not, write to the Free Software Foundation,
1944743Smarkm# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2044743Smarkm#
2144743Smarkm# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2244743Smarkm# or visit www.oracle.com if you need additional information or have any
2344743Smarkm# questions.
2444743Smarkm#
2556977Sshin
2644743Smarkmdefault: all
2744743Smarkm
2844743Smarkminclude $(SPEC)
2944743Smarkminclude MakeBase.gmk
3044743Smarkm
3144743Smarkm################################################################################
3244743Smarkm# Keep track of what source revision is used to create the build, by creating
3344743Smarkm# a tracker file in the output directory. This tracker file is included in the
3444743Smarkm# image, and can be used to recreate the source revision used.
3544743Smarkm#
3644743Smarkm# We're either building directly from a mercurial forest, and if so, use the
3744743Smarkm# current revision from mercurial. Otherwise, we are building from a source
3844743Smarkm# bundle. As a part of creating this source bundle, the current mercurial
3944743Smarkm# revisions of all repos will be stored in a file in the top dir, which is then
4044743Smarkm# used when creating the tracker file.
4144743Smarkm
4244743SmarkmSTORED_SOURCE_REVISION := $(TOPDIR)/.src-rev
4344743Smarkm
4444743Smarkm# Are we using mercurial?
4544743Smarkmifneq ($(and $(HG), $(wildcard $(TOPDIR)/.hg)), )
4644743Smarkm
4744743Smarkm  # Verify that the entire forest is consistent
4844743Smarkm  $(foreach repo, $(call FindAllReposRel), \
4944743Smarkm    $(if $(wildcard $(TOPDIR)/$(repo)/.hg),, \
5044743Smarkm        $(error Inconsistent revision control: $(repo) is missing .hg directory)) \
5144743Smarkm  )
5244743Smarkm
5344743Smarkm  # Replace "." with "_top" and "/" with "-"
5444743Smarkm  MakeFilenameFromRepo = \
5544743Smarkm      $(strip $(subst .,top, $(subst /,-, $1)))
5644743Smarkm
5744743Smarkm  ################################################################################
5844743Smarkm  # SetupGetRevisionForRepo defines a make rule for creating a file containing
5944743Smarkm  # the name of the repository and the output of "hg id" for that repository.
6044743Smarkm  # Argument 1 is the relative path to the repository from the top dir.
6144743Smarkm  #
6244743Smarkm  SetupGetRevisionForRepo = $(NamedParamsMacroTemplate)
6344743Smarkm  define SetupGetRevisionForRepoBody
6444743Smarkm    $1_REPO_PATH :=  $$(TOPDIR)/$$(strip $1)
6544743Smarkm    $1_FILENAME := $$(call MakeFilenameFromRepo, $1)
6644743Smarkm
6744743Smarkm    $$(SUPPORT_OUTPUTDIR)/src-rev/$$($1_FILENAME): FRC
6844743Smarkm	$$(call MakeDir, $$(@D))
6944743Smarkm	$$(ECHO) $$(strip $1):`$$(HG) id -i --repository $$($1_REPO_PATH)` > $$@
7044743Smarkm
7144743Smarkm    REPO_REVISIONS += $$(SUPPORT_OUTPUTDIR)/src-rev/$$($1_FILENAME)
7244743Smarkm  endef
7344743Smarkm
7444743Smarkm  # Setup rules for all repos. This makes sure all the "hg id" calls are made
7544743Smarkm  # in parallel.
7644743Smarkm  $(foreach repo, $(call FindAllReposRel), \
7744743Smarkm    $(eval $(call SetupGetRevisionForRepo, $(repo))) \
7844743Smarkm  )
7944743Smarkm
8044743Smarkm  # Create a complete source revision output file from all repos
8144743Smarkm  # Param 1: The output file
8244743Smarkm  define CreateSourceRevisionFile
8344743Smarkm    $1: $$(REPO_REVISIONS)
8444743Smarkm	$$(call MakeDir, $$(@D))
8544743Smarkm	$$(ECHO) `$$(CAT) $$(REPO_REVISIONS)` > $$@.tmp
8644743Smarkm	if [ ! -f $$@ ] || [ "`$$(CAT) $$@`" != "`$$(CAT) $$@.tmp`" ]; then \
8744743Smarkm	  $$(MV) $$@.tmp $$@ ; \
8844743Smarkm	else \
8944743Smarkm	  $$(RM) $$@.tmp ; \
9044743Smarkm	fi
9144743Smarkm  endef
9244743Smarkm
9344743Smarkm  $(eval $(call CreateSourceRevisionFile, $(STORED_SOURCE_REVISION)))
9444743Smarkm
9544743Smarkm  store-source-revision: $(STORED_SOURCE_REVISION)
9644743Smarkm
9744743Smarkm  $(eval $(call CreateSourceRevisionFile, $(SOURCE_REVISION_TRACKER)))
9844743Smarkm
9944743Smarkm  create-source-revision-tracker: $(SOURCE_REVISION_TRACKER)
10044743Smarkm
10144743Smarkmelse
10244743Smarkm  # Not using HG
10344743Smarkm
10444743Smarkm  ifneq ($(wildcard $(STORED_SOURCE_REVISION)), )
10544743Smarkm    # We have a stored source revision (.src-rev)
10644743Smarkm
10744743Smarkm    store-source-revision:
10844743Smarkm	$(call LogWarn, Warning: No mercurial configuration present, not updating .src-rev)
10944743Smarkm
11044743Smarkm    $(SOURCE_REVISION_TRACKER): $(STORED_SOURCE_REVISION)
11144743Smarkm	$(install-file)
11244743Smarkm
11344743Smarkm    create-source-revision-tracker: $(SOURCE_REVISION_TRACKER)
11444743Smarkm  else
11544743Smarkm    # We don't have a stored source revision. Can't do anything, really.
11644743Smarkm
11744743Smarkm    store-source-revision:
11844743Smarkm	$(call LogWarn, Error: No mercurial configuration present, cannot create .src-rev)
11944743Smarkm	exit 2
12044743Smarkm
12144743Smarkm    create-source-revision-tracker:
12244743Smarkm	$(call LogWarn, Warning: No mercurial configuration present and no .src-rev)
12344743Smarkm  endif
12444743Smarkm
12544743Smarkmendif
12644743Smarkm
12744743Smarkmall: store-source-revision create-source-revision-tracker
12844743Smarkm
12944743SmarkmFRC: # Force target
13044743Smarkm
13144743Smarkm.PHONY: all store-source-revision create-source-revision-tracker
13244743Smarkm