# Top level -*- makefile -*- fragment for libgccjit.so.
#   Copyright (C) 2013-2025 Free Software Foundation, Inc.

#This file is part of GCC.

#GCC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 3, or (at your option)
#any later version.

#GCC is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

# This file provides the language dependent support in the main Makefile.
# Each language makefile fragment must provide the following targets:
#
# foo.all.cross, foo.start.encap, foo.rest.encap,
# foo.install-common, foo.install-man, foo.install-info, foo.install-pdf,
# foo.install-html, foo.info, foo.dvi, foo.pdf, foo.html, foo.uninstall,
# foo.mostlyclean, foo.clean, foo.distclean,
# foo.maintainer-clean, foo.stage1, foo.stage2, foo.stage3, foo.stage4
#
# where `foo' is the name of the language.
#
# It should also provide rules for:
#
# - making any compiler driver (eg: g++)
# - the compiler proper (eg: cc1plus)
# - define the names for selecting the language in LANGUAGES.

#
# Define the names for selecting jit in LANGUAGES.
# Note that it would be nice to move the dependency on g++
# into the jit rule, but that needs a little bit of work
# to do the right thing within all.cross.

# LIBGCCJIT_VERSION_NUM is set in ../Makefile.in
LIBGCCJIT_MINOR_NUM = 0
LIBGCCJIT_RELEASE_NUM = 1

ifneq (,$(findstring mingw,$(target)))
LIBGCCJIT_FILENAME = libgccjit-$(LIBGCCJIT_VERSION_NUM).dll
LIBGCCJIT_IMPORT_LIB = libgccjit.dll.a

jit: $(LIBGCCJIT_FILENAME) \
	$(FULL_DRIVER_NAME)

else

ifneq (,$(findstring darwin,$(host)))

# NOTE that we are building here for the host, and so tests for target-
# specific functionality will only work when host == target.  This causes
# fails when building cross-compilers with different object formats (at
# least when the respective linkers do not accept the same command line
# options).  Fortunately, for Darwin we can safely hard-code the relevant
# host options, since all usable linkers support them).

LIBGCCJIT_CURRENT = 26
LIBGCCJIT_REVISION = 0
LIBGCCJIT_AGE = 26
LIBGCCJIT_COMPAT = 0
LIBGCCJIT_BASENAME = libgccjit

LIBGCCJIT_SONAME = \
  $(DARWIN_RPATH)/$(LIBGCCJIT_BASENAME).$(LIBGCCJIT_VERSION_NUM).dylib
LIBGCCJIT_FILENAME = $(LIBGCCJIT_BASENAME).$(LIBGCCJIT_VERSION_NUM).dylib
LIBGCCJIT_LINKER_NAME = $(LIBGCCJIT_BASENAME).dylib

# Darwin does not have a version script option. Exported symbols are controlled
# by the following, and library versioning is done using libtool.
# We have to define a COMMA here, otherwise the commas are treated as
# separators.
COMMA := ,
LIBGCCJIT_VERSION_SCRIPT_OPTION = \
  -Wl$(COMMA)-exported_symbols_list$(COMMA)$(srcdir)/jit/libgccjit.exports

# For Darwin host, we need a l64 or ld64-compatible linker, that uses
# -install_name to introduce this.
LIBGCCJIT_SONAME_OPTION = \
  -Wl$(COMMA)-install_name$(COMMA)$(LIBGCCJIT_SONAME)

LIBGCCJIT_SONAME_SYMLINK = $(LIBGCCJIT_FILENAME)
LIBGCCJIT_LINKER_NAME_SYMLINK = $(LIBGCCJIT_LINKER_NAME)

jit: $(LIBGCCJIT_FILENAME) \
	$(LIBGCCJIT_SYMLINK) \
	$(LIBGCCJIT_LINKER_NAME_SYMLINK) \
	$(FULL_DRIVER_NAME)

else

LIBGCCJIT_LINKER_NAME = libgccjit.so
LIBGCCJIT_SONAME = $(LIBGCCJIT_LINKER_NAME).$(LIBGCCJIT_VERSION_NUM)
LIBGCCJIT_FILENAME = \
  $(LIBGCCJIT_SONAME).$(LIBGCCJIT_MINOR_NUM).$(LIBGCCJIT_RELEASE_NUM)

LIBGCCJIT_LINKER_NAME_SYMLINK = $(LIBGCCJIT_LINKER_NAME)
LIBGCCJIT_SONAME_SYMLINK = $(LIBGCCJIT_SONAME)

# Conditionalize the use of the LD_VERSION_SCRIPT_OPTION and
# LD_SONAME_OPTION depending if configure found them, using $(if)
# We have to define a COMMA here, otherwise the commas in the "true"
# result are treated as separators by the $(if).
COMMA := ,
LIBGCCJIT_VERSION_SCRIPT_OPTION = \
	$(if $(LD_VERSION_SCRIPT_OPTION),\
	  -Wl$(COMMA)$(LD_VERSION_SCRIPT_OPTION)$(COMMA)$(srcdir)/jit/libgccjit.map)

LIBGCCJIT_SONAME_OPTION = \
	$(if $(LD_SONAME_OPTION), \
	     -Wl$(COMMA)$(LD_SONAME_OPTION)$(COMMA)$(LIBGCCJIT_SONAME))

jit: $(LIBGCCJIT_FILENAME) \
	$(LIBGCCJIT_SYMLINK) \
	$(LIBGCCJIT_LINKER_NAME_SYMLINK) \
	$(FULL_DRIVER_NAME)

endif
endif

jit.serial = $(LIBGCCJIT_FILENAME)

# Tell GNU make to ignore these if they exist.
.PHONY: jit

jit_OBJS = attribs.o \
	jit/dummy-frontend.o \
	jit/libgccjit.o \
	jit/jit-logging.o \
	jit/jit-recording.o \
	jit/jit-playback.o \
	jit/jit-result.o \
	jit/jit-tempdir.o \
	jit/jit-builtins.o \
	jit/jit-spec.o \
	gcc.o

ifneq (,$(findstring mingw,$(target)))
jit_OBJS += jit/jit-w32.o
endif

# Use strict warnings for this front end.
jit-warn = $(STRICT_WARN)

ifneq (,$(findstring mingw,$(target)))
# Create import library
LIBGCCJIT_EXTRA_OPTS = -Wl,--out-implib,$(LIBGCCJIT_IMPORT_LIB)
else
ifneq (,$(findstring darwin,$(host)))
LIBGCCJIT_VERS = $(LIBGCCJIT_CURRENT).$(LIBGCCJIT_REVISION).$(LIBGCCJIT_AGE)
LIBGCCJIT_EXTRA_OPTS = -Wl,-current_version,$(LIBGCCJIT_VERS) \
 -Wl,-compatibility_version,$(LIBGCCJIT_COMPAT) \
  $(LIBGCCJIT_VERSION_SCRIPT_OPTION) $(LIBGCCJIT_SONAME_OPTION)
# Use the default (shared) libgcc.
JIT_LDFLAGS = $(filter-out -static-libgcc, $(LDFLAGS))
ifeq (,$(findstring darwin8,$(host)))
JIT_LDFLAGS += -Wl,-rpath,@loader_path
endif
else
JIT_LDFLAGS = $(LDFLAGS)
LIBGCCJIT_EXTRA_OPTS = $(LIBGCCJIT_VERSION_SCRIPT_OPTION) \
	$(LIBGCCJIT_SONAME_OPTION)
endif
endif

# Only link objects from $(EXTRA_GCC_OBJS) that's not already
# included in libbackend.a ($(EXTRA_OBJS)).
EXTRA_GCC_OBJS_EXCLUSIVE = $(foreach _obj1, $(EXTRA_GCC_OBJS), \
	$(if $(filter $(_obj1), $(EXTRA_OBJS)),, $(_obj1)))

# We avoid using $(BACKEND) from Makefile.in in order to avoid pulling
# in main.o
$(LIBGCCJIT_FILENAME): $(jit_OBJS) \
	libbackend.a libcommon-target.a libcommon.a \
	$(CPPLIB) $(LIBDECNUMBER) \
	$(LIBDEPS) $(srcdir)/jit/libgccjit.map \
	$(EXTRA_GCC_OBJS_EXCLUSIVE) $(jit.prev)
	@$(call LINK_PROGRESS,$(INDEX.jit),start)
	+$(LLINKER) $(ALL_LINKERFLAGS) $(JIT_LDFLAGS) -o $@ -shared \
	     $(jit_OBJS) libbackend.a libcommon-target.a libcommon.a \
	     $(CPPLIB) $(LIBDECNUMBER) $(EXTRA_GCC_LIBS) $(LIBS) $(BACKENDLIBS) \
	     $(EXTRA_GCC_OBJS_EXCLUSIVE) \
	     $(LIBGCCJIT_EXTRA_OPTS)
	@$(call LINK_PROGRESS,$(INDEX.jit),end)

# Create symlinks when not building for Windows
ifeq (,$(findstring mingw,$(target)))

ifeq (,$(findstring darwin,$(host)))
# but only one level for Darwin, version info is embedded.
$(LIBGCCJIT_SONAME_SYMLINK): $(LIBGCCJIT_FILENAME)
	ln -sf $(LIBGCCJIT_FILENAME) $(LIBGCCJIT_SONAME_SYMLINK)
endif

$(LIBGCCJIT_LINKER_NAME_SYMLINK): $(LIBGCCJIT_SONAME_SYMLINK)
	ln -sf $(LIBGCCJIT_SONAME_SYMLINK) $(LIBGCCJIT_LINKER_NAME_SYMLINK)
endif

#
# Build hooks:

jit.all.cross:
jit.start.encap:
jit.rest.encap:

# Documentation build hooks.
#
# The documentation can be built using the texinfo toolchain, or
# the sphinx toolchain
#
# The jit documentation is authored using Sphinx, which has numerous
# advantages over Texinfo, including:
#
#   * much faster
#
#   * use of CSS and JS to provide less of a 1990s feel in the generated
#     HTML.
#
#   * sane, stable HTML page and anchor names
#
#   * sane HTML navigation: ability to move forward and back in the HTML
#     at every node to read the HTML like a book
#
#   * syntax-coloring of examples
#
#   * the ability to "include" fragments of code inline.  This is used
#     heavily by the jit docs, so that the example code is shared by both
#     the test suite and the documentation to ensure that the examples
#     appearing in the docs actually compile and work
#
# Sphinx is not a "blessed" dependency, and so a prebuilt libgccjit.texinfo
# file built by Sphinx is checked into the source tree to avoid requiring
# everyone to have Sphinx installed.
#
# This prebuilt libgccjit.texinfo has the "include" fragments "baked in",
# and so contains the content from the sphinx toolchain, but lacks the
# syntax-coloring, and the generated HTML is (IMHO) greatly inferior to
# that generated by Sphinx.

# These targets redirect HTML creation and installation to either
# jit.sphinx.(install-)html or jit.texinfo.(install-)html.
jit.html: jit.$(doc_build_sys).html
jit.install-html: jit.$(doc_build_sys).install-html

# For now, use texinfo for pdf, since the sphinx latex toolchain currently
# fails for me deep inside pdflatex (see notes below)
jit.pdf: jit.texinfo.pdf
jit.install-pdf: jit.texinfo.install-pdf

# Hooks for building docs using texinfo
JIT_TEXI_FILES = $(srcdir)/jit/docs/_build/texinfo/libgccjit.texi

jit.info: doc/libgccjit.info
doc/libgccjit.info: $(JIT_TEXI_FILES)
	if test "x$(BUILD_INFO)" = xinfo; then \
	  rm -f doc/libgccjit.info*; \
	  $(MAKEINFO) $(MAKEINFOFLAGS) -I $(gcc_docdir) \
		-I $(gcc_docdir)/include -o $@ $<; \
	else true; fi

jit.install-info: $(DESTDIR)$(infodir)/libgccjit.info

jit.dvi: doc/libgccjit.dvi
doc/libgccjit.dvi: $(JIT_TEXI_FILES)
	$(TEXI2DVI) -I $(abs_docdir) -I $(abs_docdir)/include -o $@ $<

jit.texinfo.html: $(build_htmldir)/jit/index.html

$(build_htmldir)/jit/index.html: $(srcdir)/jit/docs/_build/texinfo/libgccjit.texi
	$(mkinstalldirs) $(@D)
	rm -f $(@D)/*
	$(TEXI2HTML) -I $(gcc_docdir)/include -I $(srcdir)/jit -o $(@D) $<

jit.texinfo.install-html: jit.texinfo.html
	@$(NORMAL_INSTALL)
	test -z "$(htmldir)" || $(mkinstalldirs) "$(DESTDIR)$(htmldir)"
	@for p in $(build_htmldir)/jit; do \
	  if test -f "$$p" || test -d "$$p"; then d=""; else d="$(srcdir)/"; fi; \
	  f=$(html__strip_dir) \
	  if test -d "$$d$$p"; then \
	    echo " $(mkinstalldirs) '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(mkinstalldirs) "$(DESTDIR)$(htmldir)/$$f" || exit 1; \
	    echo " $(INSTALL_DATA) '$$d$$p'/* '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(INSTALL_DATA) "$$d$$p"/* "$(DESTDIR)$(htmldir)/$$f"; \
	  else \
	    echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(htmldir)/$$f'"; \
	    $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(htmldir)/$$f"; \
	  fi; \
	done

jit.texinfo.pdf: doc/libgccjit.pdf

doc/libgccjit.pdf: $(JIT_TEXI_FILES)
	$(TEXI2PDF) -I $(abs_docdir) -I $(abs_docdir)/include -o $@ $<

jit.texinfo.install-pdf: doc/libgccjit.pdf
	@$(NORMAL_INSTALL)
	test -z "$(pdfdir)" || $(mkinstalldirs) "$(DESTDIR)$(pdfdir)/gcc"
	@for p in doc/libgccjit.pdf; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(pdf__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(pdfdir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(pdfdir)/gcc/$$f"; \
	done

# Hooks for building docs using the Sphinx toolchain:

SPHINX_BUILD_DIR=jit/sphinx-build

jit.sphinx.html:
	$(mkinstalldirs) $(SPHINX_BUILD_DIR)
	(cd $(srcdir)/jit/docs && \
	  make html BUILDDIR=$(PWD)/$(SPHINX_BUILD_DIR) )

jit_htmldir=$(htmldir)/jit

jit.sphinx.install-html: jit.sphinx.html
	@$(NORMAL_INSTALL)
	test -z "$(jit_htmldir)" || $(mkinstalldirs) "$(DESTDIR)$(jit_htmldir)"
	@for f in $(shell cd $(SPHINX_BUILD_DIR)/html && find) ; do \
	  if test -f $(SPHINX_BUILD_DIR)/html/"$$f"; then \
	     $(INSTALL_DATA) $(SPHINX_BUILD_DIR)/html/"$$f" $(DESTDIR)$(jit_htmldir)/"$$f"; \
	  else \
	     mkdir $(DESTDIR)$(jit_htmldir)/"$$f"; \
	  fi; \
	done

# (This one is currently failing deep inside pdflatex for me;
# see https://bugzilla.redhat.com/show_bug.cgi?id=1148845 )
jit.sphinx.pdf: $(SPHINX_BUILD_DIR)/latex/libgccjit.pdf
$(SPHINX_BUILD_DIR)/latex/libgccjit.pdf:
	$(mkinstalldirs) $(SPHINX_BUILD_DIR)
	(cd $(srcdir)/jit/docs && \
	  make latexpdf BUILDDIR=$(PWD)/$(SPHINX_BUILD_DIR) )

jit.sphinx.install-pdf: $(SPHINX_BUILD_DIR)/latex/libgccjit.pdf
	@$(NORMAL_INSTALL)
	test -z "$(pdfdir)" || $(mkinstalldirs) "$(DESTDIR)$(pdfdir)/gcc"
	@for p in $(SPHINX_BUILD_DIR)/latex/libgccjit.pdf; do \
	  if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
	  f=$(pdf__strip_dir) \
	  echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(pdfdir)/gcc/$$f'"; \
	  $(INSTALL_DATA) "$$d$$p" "$(DESTDIR)$(pdfdir)/gcc/$$f"; \
	done

jit.srcinfo:
jit.srcextra:

jit.tags:

jit.man:

jit.srcman:

lang_checks += check-jit
lang_checks_parallelized += check-jit
# This number is somewhat arbitrary.  Two tests are much slower
# than all the others (test-combination.c and test-threads.c) so
# we want them to be placed in different "buckets".
check_jit_parallelize = 10

# No jit-specific selftests
selftest-jit:

#
# Install hooks:
jit.install-headers: installdirs
	$(INSTALL_DATA) $(srcdir)/jit/libgccjit.h \
	  $(DESTDIR)$(includedir)/libgccjit.h
	$(INSTALL_DATA) $(srcdir)/jit/libgccjit++.h \
	  $(DESTDIR)$(includedir)/libgccjit++.h

ifneq (,$(findstring mingw,$(target)))
jit.install-common: installdirs jit.install-headers
# Install import library
	$(INSTALL_PROGRAM) $(LIBGCCJIT_IMPORT_LIB) \
	  $(DESTDIR)$(libdir)/$(LIBGCCJIT_IMPORT_LIB)
# Install DLL file
	$(INSTALL_PROGRAM) $(LIBGCCJIT_FILENAME) \
	  $(DESTDIR)$(bindir)/$(LIBGCCJIT_FILENAME)

else
ifneq (,$(findstring darwin,$(host)))
# but only one level for Darwin

jit.install-common: installdirs jit.install-headers
	$(INSTALL_PROGRAM) $(LIBGCCJIT_FILENAME) \
	  $(DESTDIR)$(libdir)/$(LIBGCCJIT_FILENAME)
	ln -sf \
	  $(LIBGCCJIT_SONAME_SYMLINK)\
	  $(DESTDIR)$(libdir)/$(LIBGCCJIT_LINKER_NAME_SYMLINK)

else
jit.install-common: installdirs jit.install-headers
	$(INSTALL_PROGRAM) $(LIBGCCJIT_FILENAME) \
	  $(DESTDIR)$(libdir)/$(LIBGCCJIT_FILENAME)
	ln -sf \
	  $(LIBGCCJIT_FILENAME) \
	  $(DESTDIR)$(libdir)/$(LIBGCCJIT_SONAME_SYMLINK)
	ln -sf \
	  $(LIBGCCJIT_SONAME_SYMLINK)\
	  $(DESTDIR)$(libdir)/$(LIBGCCJIT_LINKER_NAME_SYMLINK)
endif
endif

jit.install-man:

jit.install-plugin:

jit.uninstall:

#
# Clean hooks:
# A lot of the ancillary files are deleted by the main makefile.
# We just have to delete files specific to us.

jit.mostlyclean:
	-rm -f $(LIBGCCJIT_FILENAME) $(LIBGCCJIT_SYMLINK)
	-rm -f $(LIBGCCJIT_LINKER_NAME_SYMLINK) $(FULL_DRIVER_NAME)
	-rm -f $(LIBGCCJIT_SONAME)
	-rm -f $(jit_OBJS)

jit.clean:

jit.distclean:

jit.maintainer-clean:

#
# Stage hooks:
# The main makefile has already created stage?/jit.

jit.stage1: stage1-start
	-mv jit/*$(objext) stage1/jit
jit.stage2: stage2-start
	-mv jit/*$(objext) stage2/jit
jit.stage3: stage3-start
	-mv jit/*$(objext) stage3/jit
jit.stage4: stage4-start
	-mv jit/*$(objext) stage4/jit
jit.stageprofile: stageprofile-start
	-mv jit/*$(objext) stageprofile/jit
jit.stagefeedback: stagefeedback-start
	-mv jit/*$(objext) stagefeedback/jit
