# Copyright 2020-2026 Hewlett Packard Enterprise Development LP
# Copyright 2004-2019 Cray Inc.
# Other additional copyright holders may be indicated within.
#
# The entirety of this work is licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except
# in compliance with the License.
#
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

RUNTIME_ROOT = .
RUNTIME_SUBDIR = .

ifndef CHPL_MAKE_HOME
export CHPL_MAKE_HOME=$(shell pwd)/..
endif

include $(CHPL_MAKE_HOME)/make/Makefile.base


CHPL_ENV_HEADER_TEMPLATE=$(CHPL_MAKE_HOME)/runtime/etc/chpl-env-gen-template.h
CHPL_ENV_HEADER_DIR=$(RUNTIME_OBJDIR)/include/
CHPL_ENV_HEADER=$(CHPL_ENV_HEADER_DIR)/chpl-env-gen.h

all: $(CHPL_ENV_HEADER) third-party-pkgs all.helpme

depend:

clean: clean.helpme
	rm -f tags TAGS $(CHPL_ENV_HEADER)

cleanall: cleanall.helpme
	rm -f tags TAGS $(CHPL_ENV_HEADER)

cleandeps: cleandeps.helpme

clobber: clobber.helpme
	rm -f tags TAGS $(CHPL_ENV_HEADER)

%.helpme:
	@$(MAKE) -f Makefile.help MAKE_LAUNCHER=0 CHPL_MAKE_RULE=$* $*
ifneq ($(CHPL_MAKE_LAUNCHER),none)
	@$(MAKE) -f Makefile.help MAKE_LAUNCHER=1 CHPL_MAKE_RULE=$* $*
endif

# save output of printchplenv into #defines but munge away
# characters that can't be in #defines
$(CHPL_ENV_HEADER): $(CHPL_MAKE_HOME)/util/printchplenv $(CHPL_MAKE_HOME)/util/chplenv/*
	@mkdir -p $(CHPL_ENV_HEADER_DIR)
	@echo "Generating $(CHPL_ENV_HEADER)"
	@cp $(CHPL_ENV_HEADER_TEMPLATE) $(CHPL_ENV_HEADER)
	@echo "/* THIS FILE IS AUTOMATICALLY GENERATED */" >> $(CHPL_ENV_HEADER)
	@echo "#ifndef _CHPL_ENV_GEN_H_" >> $(CHPL_ENV_HEADER)
	@echo "#define _CHPL_ENV_GEN_H_" >> $(CHPL_ENV_HEADER)
	@$(CHPL_MAKE_HOME)/util/printchplenv --runtime --no-tidy --anonymize --simple | \
	  sed 's/^ *//;s/ *$$//' | \
	  sed 's/[^0-9A-Za-z]/_/g' | \
	  awk '{ print "#define " toupper($$1) }' >> $(CHPL_ENV_HEADER)
	@echo "#endif /* _CHPL_ENV_GEN_H_ */" >> $(CHPL_ENV_HEADER)

THIRD_PARTY_PKGS = $(shell $(CHPL_MAKE_PYTHON) $(CHPL_MAKE_HOME)/util/chplenv/third-party-pkgs)
# NOTE: If any of these builds fail, we won't stop building
third-party-pkgs:
ifneq (, $(THIRD_PARTY_PKGS))
# Qthreads may use hwloc and/or jemalloc. Ensure they are built first
ifneq (, $(filter $(THIRD_PARTY_PKGS),hwloc))
	@echo "Making hwloc..."
	@$(MAKE) -C $(THIRD_PARTY_DIR) hwloc
endif
ifneq (, $(filter $(THIRD_PARTY_PKGS),jemalloc))
	@echo "Making jemalloc..."
	@$(MAKE) -C $(THIRD_PARTY_DIR) CHPL_MAKE_HOST_TARGET=--target jemalloc
endif
ifneq (, $(filter $(THIRD_PARTY_PKGS),mimalloc))
	@echo "Making mimalloc..."
	@$(MAKE) -C $(THIRD_PARTY_DIR) CHPL_MAKE_HOST_TARGET=--target mimalloc
endif
	@echo "Making third-party-packages..."
	@$(MAKE) -C $(THIRD_PARTY_DIR) $(THIRD_PARTY_PKGS)
endif

.NOTPARALLEL:
