#
# 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) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
# Copyright 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
# Copyright 2016, 2024 Andreas Wacknitz <a.wacknitz@gmx.de>
# Copyright 2023 Niklas Poslovski
#

USE_PARALLEL_BUILD= yes
USE_COMMON_TEST_MASTER= no
include ../../../make-rules/shared-macros.mk

COMPONENT_NAME=         cmake
COMPONENT_VERSION=	3.31.8
CMAKE_VERSION=		$(basename $(COMPONENT_VERSION))
COMPONENT_SUMMARY=      A cross-platform, open-source make system
COMPONENT_PROJECT_URL=  https://www.cmake.org
COMPONENT_SRC=          $(COMPONENT_NAME)-$(COMPONENT_VERSION)
COMPONENT_ARCHIVE=      $(COMPONENT_SRC).tar.gz
COMPONENT_ARCHIVE_HASH=	sha256:e3cde3ca83dc2d3212105326b8f1b565116be808394384007e7ef1c253af6caa
COMPONENT_ARCHIVE_URL=	$(COMPONENT_PROJECT_URL)/files/v$(CMAKE_VERSION)/$(COMPONENT_ARCHIVE)
COMPONENT_FMRI=         developer/build/cmake3
COMPONENT_CLASSIFICATION=Development/Distribution Tools
COMPONENT_LICENSE=      BSD 3 Clause, MIT, GPLv2, LGPLv2.1, LGPLv3, Curl, PD

include $(WS_MAKE_RULES)/common.mk

PKG_MACROS += CMAKE_VERSION=$(CMAKE_VERSION)

GENERATE_EXTRA_CMD += | \
	$(GSED) -e 's/$(subst .,\.,$(CMAKE_VERSION))/$$(CMAKE_VERSION)/g'

# Need to remove this option from LD_OPTIONS (only affects sparc build);
# it causes a SegFault failure in kwsys/testSystemInformation
# when testing on sparc
LD_MAP_NOEXBSS=

LDFLAGS += $($(COMPILER)_NORUNPATH)

CONFIGURE_ENV += MAKE="$(GMAKE)"

# Remove undesriable /opt/csw lines from the Modules/*.cmake files.
COMPONENT_PREP_ACTION += \
	cd $(COMPONENT_SRC)/Modules; $(GNU_GREP) -l '/opt/csw' *.cmake | \
	/usr/bin/xargs -I \{\} $(GSED) -i -e '/^[ ]*\/opt\/csw/d' \{\}

# Do not create gpatch backup files because they pollute the generated manifest
GPATCH_BACKUP =

CONFIGURE_OPTIONS  = --prefix=$(CONFIGURE_PREFIX)
CONFIGURE_OPTIONS += --bindir=cmake3/bin
CONFIGURE_OPTIONS += --docdir=share/doc/cmake-$(CMAKE_VERSION)

# Use system-installed third-party libraries, rather than the private
# copies in the cmake source.
CONFIGURE_OPTIONS += --system-bzip2
CONFIGURE_OPTIONS += --system-curl
CONFIGURE_OPTIONS += --system-expat
CONFIGURE_OPTIONS += --system-jsoncpp
CONFIGURE_OPTIONS += --system-libarchive
CONFIGURE_OPTIONS += --system-liblzma
CONFIGURE_OPTIONS += --system-libuv
CONFIGURE_OPTIONS += --system-nghttp2
CONFIGURE_OPTIONS += --system-zlib
CONFIGURE_OPTIONS += --system-zstd

# bootstrap cmake in parallel
CONFIGURE_OPTIONS += --parallel=$(PARALLEL_JOBS)
# Use system libs for bootstrap to make it faster
CONFIGURE_OPTIONS += --bootstrap-system-libuv
CONFIGURE_OPTIONS += --bootstrap-system-jsoncpp

COMPONENT_BUILD_ENV += CC="$(CC)"
COMPONENT_BUILD_ENV += CXX="$(CXX)"
COMPONENT_BUILD_ENV += FC="$(FC)"
COMPONENT_BUILD_ENV += CFLAGS="$(CFLAGS)"
COMPONENT_BUILD_ENV += CXXFLAGS="$(CXXFLAGS)"
COMPONENT_BUILD_ENV += CFLAGS="$(FCFLAGS)"
COMPONENT_BUILD_ENV += LDFLAGS="$(LDFLAGS)"
COMPONENT_BUILD_ENV += MAKE="$(GMAKE)"
COMPONENT_BUILD_TARGETS= all

ASLR_MODE = $(ASLR_ENABLE)

# Create the /usr/bin/cmake3 symlink
COMPONENT_POST_INSTALL_ACTION += \
	$(MKDIR) $(PROTOUSRBINDIR) ; \
	$(SYMLINK) ../cmake3/bin/cmake $(PROTOUSRBINDIR)/cmake3 ;

# Need C and C++ compiler locations for cmake tests
COMPONENT_TEST_ENV += LANG=C
COMPONENT_TEST_ENV += CC="$(CC)"
COMPONENT_TEST_ENV += CXX="$(CXX)"
COMPONENT_TEST_ENV += FC="$(FC)"
COMPONENT_TEST_ENV += CFLAGS="$(CFLAGS)"
COMPONENT_TEST_ENV += CXXFLAGS="$(CXXFLAGS)"
COMPONENT_TEST_ENV += FCFLAGS="$(FCFLAGS)"
COMPONENT_TEST_ENV += LDFLAGS="$(LDFLAGS)"
# Disable term colors to avoid test failures (actual and expected output mismatch).
COMPONENT_TEST_ENV += CLICOLOR=0

# Most Fortran tests fail because -m64 they do not pass compiler flags as they should.
# Results output is different on Intel vs. SPARC.
# The tests should run by a user with default privileges. Otherwise some tests will fail.
COMPONENT_TEST_MASTER = $(COMPONENT_TEST_RESULTS_DIR)/results-$(MACH).master
COMPONENT_TEST_TARGETS = test

# Normalize test results
COMPONENT_TEST_TRANSFORMS += "-e 's/ *[0-9]\{1,\}\.[0-9]\{1,\} sec//'"				# remove timing
COMPONENT_TEST_TRANSFORMS += "-e '/^ *Start/d'"							# drop Start lines
COMPONENT_TEST_TRANSFORMS += "-e 's/^ *[0-9]\{1,\}\/[0-9]\{1,\} Test *\#[0-9]\{1,\}:/Test/'"	# drop test numbers
COMPONENT_TEST_TRANSFORMS += "-e '/^make:/d'"							# drop make logs

# To get consistent test results we need following packages
TEST_REQUIRED_PACKAGES += $(GFORTRAN_RUNTIME_PKG)
TEST_REQUIRED_PACKAGES += developer/versioning/cvs
TEST_REQUIRED_PACKAGES += developer/versioning/mercurial
TEST_REQUIRED_PACKAGES += developer/versioning/subversion

# Auto-generated dependencies
REQUIRED_PACKAGES += $(GCC_RUNTIME_PKG)
REQUIRED_PACKAGES += $(GXX_RUNTIME_PKG)
REQUIRED_PACKAGES += library/expat
REQUIRED_PACKAGES += library/jsoncpp
REQUIRED_PACKAGES += library/libarchive
REQUIRED_PACKAGES += library/libuv
REQUIRED_PACKAGES += library/ncurses
REQUIRED_PACKAGES += library/zlib
REQUIRED_PACKAGES += system/library
REQUIRED_PACKAGES += system/library/math
REQUIRED_PACKAGES += web/curl
