2
# Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
3
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
# This code is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 2 only, as
7
# published by the Free Software Foundation. Oracle designates this
8
# particular file as subject to the "Classpath" exception as provided
9
# by Oracle in the LICENSE file that accompanied this code.
11
# This code is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
# version 2 for more details (a copy is included in the LICENSE file that
15
# accompanied this code).
17
# You should have received a copy of the GNU General Public License version
18
# 2 along with this work; if not, write to the Free Software Foundation,
19
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
# or visit www.oracle.com if you need additional information or have any
29
$(eval $(call IncludeCustomExtension, SourceRevision-pre.gmk))
31
################################################################################
32
# Keep track of what source revision is used to create the build, by creating
33
# a tracker file in the output directory. This tracker file is included in the
34
# source image, and can be used to recreate the source revision used.
36
# We're either building directly from an SCM repository, and if so, use the
37
# current revision from that SCM. Otherwise, we are building from a source
38
# bundle. As a part of creating this source bundle, the current SCM revisions of
39
# all repos will be stored in a file in the top dir, which is then used when
40
# creating the tracker file.
42
STORED_SOURCE_REVISION := $(TOPDIR)/.src-rev
44
# Locate all sourcecode repositories included in the forest, as absolute paths
46
$(strip $(sort $(dir $(filter-out $(TOPDIR)/build/%, $(wildcard \
47
$(addprefix $(TOPDIR)/, .git */.git */*/.git */*/*/.git */*/*/*/.git) \
50
# Locate all sourcecode repositories included in the forest, as relative paths
52
$(strip $(subst $(TOPDIR)/,.,$(patsubst $(TOPDIR)/%/, %, $(FindAllReposAbs))))
55
ifneq ($(and $(GIT), $(wildcard $(TOPDIR)/.git)), )
58
ID_COMMAND := $(PRINTF) "git:%s%s\n" \
59
"$$($(GIT) log -n1 --format=%H | cut -c1-12)" \
60
"$$(if test -n "$$($(GIT) status --porcelain)"; then printf '+'; fi)"
63
ifeq ($(USE_SCM), true)
65
# Verify that the entire forest is consistent
66
$(foreach repo, $(call FindAllReposRel), \
67
$(if $(wildcard $(TOPDIR)/$(repo)/$(SCM_DIR)),, \
68
$(error Inconsistent revision control: $(repo) is missing $(SCM_DIR) directory)) \
71
# Replace "." with "_top" and "/" with "-"
72
MakeFilenameFromRepo = \
73
$(strip $(subst .,top, $(subst /,-, $1)))
75
################################################################################
76
# SetupGetRevisionForRepo defines a make rule for creating a file containing
77
# the name of the repository and the output of the scm command for that
80
# Argument 1 is the relative path to the repository from the top dir.
82
SetupGetRevisionForRepo = $(NamedParamsMacroTemplate)
83
define SetupGetRevisionForRepoBody
84
$1_REPO_PATH := $$(TOPDIR)/$$(strip $1)
85
$1_FILENAME := $$(call MakeFilenameFromRepo, $1)
87
$$(SUPPORT_OUTPUTDIR)/src-rev/$$($1_FILENAME): FRC
88
$$(call MakeDir, $$(@D))
89
$$(ECHO) $$(strip $1):`$$(CD) $$($1_REPO_PATH) && $$(ID_COMMAND)` > $$@
91
REPO_REVISIONS += $$(SUPPORT_OUTPUTDIR)/src-rev/$$($1_FILENAME)
94
# Setup rules for all repos. This makes sure all the "git log" calls are made
96
$(foreach repo, $(call FindAllReposRel), \
97
$(eval $(call SetupGetRevisionForRepo, $(repo))) \
100
# Create a complete source revision output file from all repos
101
# Param 1: The output file
102
define CreateSourceRevisionFile
103
$1: $$(REPO_REVISIONS)
104
$$(call MakeDir, $$(@D))
105
$$(ECHO) `$$(CAT) $$(REPO_REVISIONS)` > $$@.tmp
106
if [ ! -f $$@ ] || [ "`$$(CAT) $$@`" != "`$$(CAT) $$@.tmp`" ]; then \
107
$$(MV) $$@.tmp $$@ ; \
113
$(eval $(call CreateSourceRevisionFile, $(STORED_SOURCE_REVISION)))
115
scm-store-source-revision: $(STORED_SOURCE_REVISION)
117
$(eval $(call CreateSourceRevisionFile, $(SOURCE_REVISION_TRACKER)))
119
scm-create-source-revision-tracker: $(SOURCE_REVISION_TRACKER)
121
STORE_SOURCE_REVISION_TARGET := scm-store-source-revision
122
CREATE_SOURCE_REVISION_TRACKER_TARGET := scm-create-source-revision-tracker
124
.PHONY: scm-store-source-revision scm-create-source-revision-tracker
129
ifneq ($(wildcard $(STORED_SOURCE_REVISION)), )
130
# We have a stored source revision (.src-rev)
132
src-store-source-revision:
133
$(call LogInfo, No SCM configuration present$(COMMA) not updating .src-rev)
135
$(SOURCE_REVISION_TRACKER): $(STORED_SOURCE_REVISION)
138
src-create-source-revision-tracker: $(SOURCE_REVISION_TRACKER)
140
# We don't have a stored source revision. Can't do anything, really.
142
src-store-source-revision:
143
$(call LogWarn, Error: No SCM configuration present$(COMMA) cannot create .src-rev)
146
src-create-source-revision-tracker:
147
$(call LogWarn, Warning: No SCM configuration present and no .src-rev)
150
STORE_SOURCE_REVISION_TARGET := src-store-source-revision
151
CREATE_SOURCE_REVISION_TRACKER_TARGET := src-create-source-revision-tracker
153
.PHONY: src-store-source-revision src-create-source-revision-tracker
157
################################################################################
159
$(eval $(call IncludeCustomExtension, SourceRevision-post.gmk))
161
################################################################################
163
store-source-revision: $(STORE_SOURCE_REVISION_TARGET)
165
create-source-revision-tracker: $(CREATE_SOURCE_REVISION_TRACKER_TARGET)
169
.PHONY: store-source-revision create-source-revision-tracker