# -*- Mode: makefile -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

include $(topsrcdir)/config/rules.mk

ifdef NM
# check_vanilla_allocations.py is tailored to Linux, so only run it there.
# That should be enough to catch any problems.
check-vanilla-allocations:
	$(PYTHON3) $(topsrcdir)/config/check_vanilla_allocations.py $(REAL_LIBRARY)

# The "aggressive" variant will likely fail on some compiler/platform
# combinations, but is worth running by hand every once in a while.
check-vanilla-allocations-aggressive:
	$(PYTHON3) $(topsrcdir)/config/check_vanilla_allocations.py --aggressive $(REAL_LIBRARY)

check:: check-vanilla-allocations
endif

$(LIBRARY_NAME).pc: js.pc
	cp $^ $@

# Install versioned file, for parallel installability in Linux distributions
install:: $(LIBRARY_NAME).pc
	cp $^ $(JS_LIBRARY_NAME).pc
	$(SYSINSTALL) $(JS_LIBRARY_NAME).pc $(DESTDIR)$(libdir)/pkgconfig

install:: ../js-config.h
	$(SYSINSTALL) $^ $(DESTDIR)$(includedir)/$(JS_LIBRARY_NAME)

install:: ../../../mozglue/baseprofiler/public/ProfilingCategoryList.h
	$(SYSINSTALL) $^ $(DESTDIR)$(includedir)/$(JS_LIBRARY_NAME)/js

######################################################
# BEGIN SpiderMonkey header installation
#
# Mozilla/Gecko/Firefox mostly doesn't concern itself with defining a sensible
# install target, because it's shipping primarily packaged builds.  And even if
# it did, those builds wouldn't really have reason to include header files.  So
# we have to install public headers ourselves, rather than using something from
# config/rules.mk or similar.
#
# The overall directory structure of the installed headers looks like so:
#
#   $(includedir)/
#      $(JS_LIBRARY_NAME)/
#        jsapi.h, jspubtd.h, etc. (all of EXPORTS)
#        js/
#          js/public/* headers (all are public)
#        ds/
#          js/src/ds/* public headers
#        gc/
#          js/src/gc/* public headers
#        mozilla/
#          mfbt headers
#

install::
	$(call py_action,process_install_manifest,$(if $(filter copy,$(NSDISTMODE)),--no-symlinks )--track install_dist_include.track --no-symlinks $(DESTDIR)$(includedir)/$(JS_LIBRARY_NAME) $(DEPTH)/_build_manifests/install/dist_include)

#
# END SpiderMonkey header installation
#############################################

# Install versioned script, for parallel installability in Linux distributions
install:: js-config
	cp $^ js$(MOZJS_MAJOR_VERSION)-config
	$(SYSINSTALL) js$(MOZJS_MAJOR_VERSION)-config $(DESTDIR)$(bindir)

# Use install_name_tool to set the install_name properly for standalone
# installed libraries on macOS
install:: $(REAL_LIBRARY) $(SHARED_LIBRARY) $(IMPORT_LIBRARY)
ifneq (,$(REAL_LIBRARY))
	$(SYSINSTALL) $(REAL_LIBRARY) $(DESTDIR)$(libdir)
	mv -f $(DESTDIR)$(libdir)/$(REAL_LIBRARY) $(subst $(STATIC_LIBRARY_NAME),$(LIBRARY_NAME),$(DESTDIR)$(libdir)/$(REAL_LIBRARY))
endif
ifneq (,$(SHARED_LIBRARY))
	$(SYSINSTALL) $(SHARED_LIBRARY) $(DESTDIR)$(libdir)
ifeq ($(OS_ARCH),Darwin)
	$(INSTALL_NAME_TOOL) -id $(abspath $(libdir)/$(notdir $(SHARED_LIBRARY))) $(DESTDIR)$(libdir)/$(notdir $(SHARED_LIBRARY))
endif
endif
ifneq (,$(IMPORT_LIBRARY))
ifneq ($(IMPORT_LIBRARY),$(SHARED_LIBRARY))
	$(SYSINSTALL) $(IMPORT_LIBRARY) $(DESTDIR)$(libdir)
endif
endif
