#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
#

-include ../make-rules/shared-macros.mk

# for now, the assumption is we build any directory with a Makefile.  This may
# not always be the case. If we build a new make-like tool to drive the upper
# level build, it may make different decisions and will need to include
# appropriate dependency information.

# components.mk is auto-generated by the build tools.  It populates the
# COMPONENT_DIRS macro with a list of all directories in the workspace that
# contain a component.
COMPONENTS_IGNORE = components.ignore
$(WS_TOP)/components/$(ENCUMBERED)components.mk:	# Makefile # $(WS_TOP)/components
	@echo "Generating component list..."
	@$(BASS_O_MATIC) --workspace=$(WS_TOP) --components=paths \
		| sed -e 's;^$(shell pwd)/;;' \
		| sed -f $(COMPONENTS_IGNORE) \
		| sed -e 's;^;COMPONENT_DIRS += ;g' >$@

-include $(WS_TOP)/components/$(ENCUMBERED)components.mk

# mapping.json is auto-generated by the build tools. It provides a map
# between component path and component FMRI.
$(WS_TOP)/components/mapping.json:
	@echo "Generating component mapping..."
	$(WS_TOOLS)/userland-mapping --workspace=$(WS_TOP) \
	    --repo=$(PUBLISHER) \
	    --repo-map=encumbered/=$(PUBLISHER)-encumbered
mapping.json: $(WS_TOP)/components/mapping.json

# dependencies.json is auto-generate by the build tools. It provides information
# about component dependencies.
$(WS_TOP)/components/dependencies.json: mapping.json
	@echo "Generating component dependency list..."
	@$(BASS_O_MATIC) --workspace=$(WS_TOP) --components=dependencies >$@
dependencies.json: $(WS_TOP)/components/dependencies.json

rebuild-dependents: dependencies.json
	@echo "Bumping COMPONENT_REVISION for dependents of $(FMRI)"
	@$(WS_TOOLS)/userland-bump -v --rebuild-dependents --fmri $(FMRI)

print-dependents: dependencies.json
	@$(WS_TOOLS)/userland-bump -v --rebuild-dependents --dry-run --fmri $(FMRI)

publish-branch:
	@$(RM) $(WS_TOP)/components/$(ENCUMBERED)components.mk && \
		( env BASS_O_MATIC_MODE=incremental \
			GIT_PREVIOUS_SUCCESSFUL_COMMIT=origin/oi/hipster \
			$(GMAKE) -s publish )


download:		TARGET = download
update-metadata:	TARGET = update-metadata
prep:			TARGET = prep
build:			TARGET = build
install:		TARGET = install
pre-publish:		TARGET = pre-publish
publish:		TARGET = publish
clean:			TARGET = clean
clobber:		TARGET = clobber
test:			TARGET = test
zone-build:		TARGET = component-zone-build
zone-cleanup:		TARGET = component-zone-cleanup
component-hook:		TARGET = component-hook
zone-build zone-cleanup prep build install pre-publish publish test:	LOG = >$(WS_LOGS)/$(subst /,.,$@).$(TARGET).log 2>&1

# turn off pkglint for the individual component builds.
ifeq   ($(strip $(PKGLINT_COMPONENT)),)
publish:		MAKEFLAGS += PKGLINT=/bin/true
endif

.DEFAULT:	publish

zone-build zone-cleanup build install pre-publish publish test: $(COMPONENT_DIRS)

COMPONENT_DIRS.nosetup =	$(COMPONENT_DIRS:%=%.nosetup)
COMPONENT_DIRS.nolog =		$(COMPONENT_DIRS:%=%.nolog)

download prep:	$(COMPONENT_DIRS.nosetup)

update-metadata:	$(COMPONENT_DIRS.nolog)

component-hook:	$(COMPONENT_DIRS.nosetup)

clean:		$(COMPONENT_DIRS.nosetup)
	$(RM) $(WS_TOP)/components/mapping.json \
	.profile

clobber:	$(COMPONENT_DIRS.nosetup) clean
	$(RM) $(WS_TOP)/components/$(ENCUMBERED)components.mk
	@cd $(WS_TOP)/tools ; echo "clobbering tools..." ; $(GMAKE) clobber
	$(RM) -r $(WS_REPO) $(WS_LOGS) $(WS_LINT_CACHE) $(WS_MACH)/perl-meta-deps-cache


setup:	install_build_essential $(WS_LOGS) $(WS_REPO) tools $(WS_LINT_CACHE)

install_build_essential:
	@/usr/bin/pkg list -vH build-essential > /dev/null || \
		$(PFEXEC) /usr/bin/pkg install --accept build-essential

$(WS_LOGS):
	$(MKDIR) $@

$(WS_REPO):
	$(PKGREPO) create $(PKG_REPO)
	$(PKGREPO) add-publisher -s $(PKG_REPO) $(PUBLISHER)
	$(PKGREPO) rebuild -s $(PKG_REPO)
# Not required for OI
#	$(PKGREPO) add-publisher -s $(PKG_REPO) $(PUBLISHER_LOCALIZABLE)

DUMMYPKG =  "set name=fmri value=pkg:/dummy@0,$(BUILD_VERSION)\n"
DUMMYPKG += "set pkg.summary=dummy\n"
DUMMYPKG += "set org.opensolaris.consolidation=userland\n"
DUMMYPKG += "set info.classification=org.opensolaris.category.2008:System/Core"

$(WS_LINT_CACHE):	$(WS_LOGS) tools
ifdef CANONICAL_REPO
	@echo "Generating pkglint(1) cache from $(CANONICAL_REPO)..."
	@(echo $(DUMMYPKG) | $(PKGLINT) \
		-c $(WS_LINT_CACHE) -r $(CANONICAL_REPO) /dev/fd/0 \
		>$(WS_LOGS)/naughty-canonical-repo-actions 2>&1 ; exit 0)
else
	$(MKDIR) $(WS_LINT_CACHE)
endif

tools:
	@cd $(WS_TOP)/tools ; echo "building tools..." ; $(GMAKE) clean setup

# $(WS_COMPONENTS) is the home directory for the zone user, so create a profile
# to pass a few things on to zone based builds
.profile:
	echo "PATH=$(PATH)" >>$@
	echo "WS_TOP=$(WS_TOP)" >>$@
	echo "export PATH WS_TOP" >>$@

$(COMPONENT_DIRS):	$(WS_LOGS) setup FORCE
	@+cd $@ && echo "$(TARGET) $@" && \
	 $(BASS_O_MATIC) --make $(TARGET) $(LOG)

incorporation:
	$(eval COMPONENT_REVISION := $(shell git rev-list HEAD --count))
	$(WS_TOOLS)/userland-incorporator --repository $(PKG_REPO) \
	  --version=0.$(OS_VERSION),$(BUILD_VERSION) -p $(PUBLISHER) \
	  -c $(CONSOLIDATION) --destdir=$(WS_MACH) $(INCORPORATE_WERROR)
	for incorporation in $(WS_MACH)/*.p5m ; do \
	    $(PKGSEND) -s $(PKG_REPO) publish --fmri-in-manifest \
	               $${incorporation} ; \
	done


publish:
	$(PKGREPO) refresh -s $(PKG_REPO)
ifdef CANONICAL_REPO
	@echo 'pkglinting all package manifests...'
	@$(ENV) PYTHONPATH=$(WS_TOOLS)/python $(PKGLINT) \
			-c $(WS_LINT_CACHE) -f $(WS_TOOLS)/pkglintrc \
			$(shell find . -name 'manifest-*.published')
endif

$(COMPONENT_DIRS.nosetup):	$(WS_LOGS) FORCE
	@+cd $(@:%.nosetup=%) && echo "$(TARGET) $(@:%.nosetup=%)" && \
	 $(BASS_O_MATIC) --make $(TARGET) $(LOG)

.PHONY:
$(COMPONENT_DIRS.nolog):
	@$(MAKE) -s -C $(@:%.nolog=%) $(TARGET)

FORCE: