#
# 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) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#

#
# This file sets up the standard, default options and base requirements for
# userland components.
#
.NOTPARALLEL:

# Assume components use a configure script-style build by default.
BUILD_STYLE ?= configure

# Some build styles might want to set some defaults before prep.mk is included.
-include $(WS_MAKE_RULES)/$(strip $(BUILD_STYLE))-defaults.mk

# The SINGLE_PERL_VERSION variable is used to select building a component for
# single or multiple Perl versions.  By default we build for single Perl
# version only unless the component is a Perl module.  If the default value of
# SINGLE_PERL_VERSION is not what is needed for a component it could be
# overridden in component's Makefile.
SINGLE_PERL_VERSION ?= yes
ifeq ($(strip $(SINGLE_PERL_VERSION)),yes)
PERL_VERSIONS = $(PERL_VERSION)
endif

# The SINGLE_PYTHON_VERSION variable is used to select building a component for
# single or multiple Python versions.  By default we build for single Python
# version only unless the component is a Python project.  If the default value
# of SINGLE_PYTHON_VERSION is not what is needed for a component it could be
# overridden in component's Makefile.
SINGLE_PYTHON_VERSION ?= yes
ifeq ($(strip $(SINGLE_PYTHON_VERSION)),yes)
PYTHON_VERSIONS = $(PYTHON_VERSION)
endif

include $(WS_MAKE_RULES)/prep.mk

# Override this to limit builds and publication to a single architecture.
BUILD_ARCH ?= $(MACH)
ifneq ($(strip $(BUILD_ARCH)),$(MACH))
PUBLISH_STAMP= 

target-na:
	@echo "Not available for $(MACH)"

BUILD_TARGET ?= target-na
INSTALL_TARGET ?= target-na
TEST_TARGET ?= target-na
SYSTEM_TEST_TARGET ?= target-na
endif

# If not an archive build (i.e. extract and copy) or pkg build (publish only),
# include relevant makefile.
ifneq ($(strip $(BUILD_STYLE)),archive)
ifneq ($(strip $(BUILD_STYLE)),pkg)
include $(WS_MAKE_RULES)/$(strip $(BUILD_STYLE)).mk

# Include common rules used by build styles that opted to use them
USE_COMMON_RULES ?= no
ifneq ($(strip $(USE_COMMON_RULES)),no)
include $(WS_MAKE_RULES)/common-rules.mk
endif
endif
endif

ifeq ($(strip $(BUILD_STYLE)),configure)
# Assume these items should always be set in the configure environment.  strip
# is used to work around potential problems with extra blank characters
# inserted into strings during nested configure invocations within builds for
# components such as gdb.
ifneq ($(strip $(CFLAGS)),)
# Only if provided.
CONFIGURE_ENV += CFLAGS="$(strip $(CFLAGS))"
endif
ifneq ($(strip $(CXXFLAGS)),)
# Only if provided.
CONFIGURE_ENV += CXXFLAGS="$(strip $(CXXFLAGS))"
endif
# Always process LDFLAGS.
CONFIGURE_ENV += LDFLAGS="$(strip $(LDFLAGS))"
endif

ifeq ($(strip $(BUILD_STYLE)),justmake)
# Assume these items should always be set in the build environment.
COMPONENT_BUILD_ENV += CC="$(CC)"
COMPONENT_BUILD_ENV += CXX="$(CXX)"
ifneq ($(strip $(CFLAGS)),)
# Only if provided.
COMPONENT_BUILD_ENV += CFLAGS="$(strip $(CFLAGS))"
endif
ifneq ($(strip $(CXXFLAGS)),)
# Only if provided.
COMPONENT_BUILD_ENV += CXXFLAGS="$(strip $(CXXFLAGS))"
endif
# Always process LDFLAGS.
COMPONENT_BUILD_ENV += LDFLAGS="$(strip $(LDFLAGS))"
endif

# Assume components built with ant do not need ASLR.
ifeq ($(strip $(BUILD_STYLE)),ant)
ASLR_MODE= $(ASLR_NOT_APPLICABLE)
else
ifeq ($(strip $(DEFAULT_ASLR_ENABLE)),yes)
# Enable ASLR by default unless target build is NO_ARCH.
ifneq ($(strip $(BUILD_BITS)),NO_ARCH)
ASLR_MODE= $(ASLR_ENABLE)
endif # DEFAULT_ASLR_ENABLE yes
endif # BUILD_BITS NO_ARCH
endif # BUILD_STYLE ant

# Default targets; to omit a specific target, explicitly set the related target
# variable to empty before including this file or set COMMON_TARGETS=no.
COMMON_TARGETS ?= yes

ifneq ($(strip $(COMMON_TARGETS)),no)
# For ant-based build style, assume that install is provided by simply building
# the component and that no tests are available.
ifeq ($(strip $(BUILD_STYLE)),ant)
INSTALL_TARGET ?= build
TEST_TARGET ?= $(NO_TESTS)
SYSTEM_TEST_TARGET ?= $(NO_TESTS)
endif

# For archive-based build style, assume that the build, install, and test
# targets will be provided.
ifeq ($(strip $(BUILD_STYLE)),archive)
BUILD_TARGET ?=
INSTALL_TARGET ?=
TEST_TARGET ?=
endif

# For pkg-based build style, assume there are no build, install, or test steps;
# just a package to be published.  However, 'gmake sample-manifest' requires
# proto dir.  Since sample-manifest generation depends on install target we
# will abuse it to get the required dir created.
ifeq ($(strip $(BUILD_STYLE)),pkg)
BUILD_TARGET=
INSTALL_TARGET = $(BUILD_DIR)/.installed
TEST_TARGET=
SYSTEM_TEST_TARGET=
build:
test system-test:	$(NO_TESTS)

$(BUILD_DIR)/.installed:
	$(RM) -r $(BUILD_DIR)
	$(MKDIR) $(PROTO_DIR)
	$(TOUCH) $@

clean::
	$(RM) -r $(BUILD_DIR)
endif

# If TEST_TARGET is NO_TESTS, assume no system tests by default.
ifeq ($(strip $(TEST_TARGET)),$(NO_TESTS))
SYSTEM_TEST_TARGET ?= $(NO_TESTS)
endif

# If TEST_TARGET is SKIP_TEST, assume system tests are skipped by default.
ifeq ($(strip $(TEST_TARGET)),$(SKIP_TEST))
SYSTEM_TEST_TARGET ?= $(SKIP_TEST)
endif

# Otherwise, attempt to define common targets assuming defaults.
BUILD_TARGET ?= $(BUILD_$(MK_BITS))
ifneq ($(strip $(BUILD_TARGET)),)
build:          $(BUILD_TARGET)
endif

INSTALL_TARGET ?= $(INSTALL_$(MK_BITS))
ifneq ($(strip $(INSTALL_TARGET)),)
install:        $(INSTALL_TARGET)
endif

TEST_TARGET ?= $(TEST_$(MK_BITS))
ifneq ($(strip $(TEST_TARGET)),)
test:           $(TEST_TARGET)
endif

# For the system-test target, assume that none have been implemented.
SYSTEM_TEST_TARGET ?= $(SYSTEM_TESTS_NOT_IMPLEMENTED)
ifneq ($(strip $(SYSTEM_TEST_TARGET)),)
system-test:    $(SYSTEM_TEST_TARGET)
endif
endif # COMMON_TARGETS

# Always needed; every component builds packages.
include $(WS_MAKE_RULES)/ips.mk

# Determine if we should automatically add files directory to PKG_PROTO_DIRS.
ifeq ($(wildcard files),files)
PKG_PROTO_DIRS +=	$(COMPONENT_DIR)/files
endif

# Add bit and mach-specific logic to general actions.
COMPONENT_PRE_CONFIGURE_ACTION += $(COMPONENT_PRE_CONFIGURE_ACTION.$(BITS))
COMPONENT_PRE_CONFIGURE_ACTION += $(COMPONENT_PRE_CONFIGURE_ACTION.$(MACH))
COMPONENT_POST_CONFIGURE_ACTION += $(COMPONENT_POST_CONFIGURE_ACTION.$(BITS))
COMPONENT_POST_CONFIGURE_ACTION += $(COMPONENT_POST_CONFIGURE_ACTION.$(MACH))
COMPONENT_PRE_BUILD_ACTION += $(COMPONENT_PRE_BUILD_ACTION.$(BITS))
COMPONENT_PRE_BUILD_ACTION += $(COMPONENT_PRE_BUILD_ACTION.$(MACH))
COMPONENT_POST_BUILD_ACTION += $(COMPONENT_POST_BUILD_ACTION.$(BITS))
COMPONENT_POST_BUILD_ACTION += $(COMPONENT_POST_BUILD_ACTION.$(MACH))
COMPONENT_PRE_INSTALL_ACTION += $(COMPONENT_PRE_INSTALL_ACTION.$(BITS))
COMPONENT_PRE_INSTALL_ACTION += $(COMPONENT_PRE_INSTALL_ACTION.$(MACH))
COMPONENT_POST_INSTALL_ACTION += $(COMPONENT_POST_INSTALL_ACTION.$(BITS))
COMPONENT_POST_INSTALL_ACTION += $(COMPONENT_POST_INSTALL_ACTION.$(MACH))
COMPONENT_PRE_TEST_ACTION += $(COMPONENT_PRE_TEST_ACTION.$(BITS))
COMPONENT_PRE_TEST_ACTION += $(COMPONENT_PRE_TEST_ACTION.$(MACH))
COMPONENT_POST_TEST_ACTION += $(COMPONENT_POST_TEST_ACTION.$(BITS))
COMPONENT_POST_TEST_ACTION += $(COMPONENT_POST_TEST_ACTION.$(MACH))

# In an ideal world all components should support parallel build but it is
# often not the case.  So by default we do not run parallel build and allow
# components to opt-in for parallel build by setting USE_PARALLEL_BUILD = yes.
PARALLEL_JOBS ?= $(shell /usr/sbin/psrinfo -t -c)
ifeq ($(strip $(USE_PARALLEL_BUILD)),yes)
COMPONENT_BUILD_CMAKE_ARGS += -j$(PARALLEL_JOBS)
COMPONENT_BUILD_GMAKE_ARGS += -j$(PARALLEL_JOBS)
COMPONENT_BUILD_SETUP_PY_ARGS += -j$(PARALLEL_JOBS)
endif