qt.version = $config.libbuild2_qt_tests.qt

using qt.moc

switch $qt.version
{
  case 0
    libs =
  case 5
    import libs = libQt5Core%lib{Qt5Core}
  case 6
    import libs = libQt6Core%lib{Qt6Core}
}

# Headers, source files.
#
exe{driver}: hxx{source sink} cxx{driver source sink}

# C++ source files generated by moc.
#
# cxx{*} are generated from hxx{} and are compiled (by convention).
#
# moc{*} are generated from cxx{} and are included.
#
exe{driver}: cxx{moc_source}: include = adhoc # Included (breaking convention).
exe{driver}: cxx{sink_moc}                    # Compiled.
exe{driver}: moc{sink}                        # Included.

# Test that generated headers/source files are supported.
#
hxx{source}: in{source}

# Utility library through which lib{Qt*Core}'s library metadata (poptions) is
# made available to moc (see below for details). Note: the rule hint is for
# when libs is empty (see above).
#
[rule_hint=cxx] libue{QtCoreMeta}: $libs

exe{driver}: libue{QtCoreMeta}

# Header containing the compiler's predefined macros.
#
[rule_hint=cxx.predefs] hxx{moc_predefs}:
{
  # Cause this macro to be defined in the predefs header. Used to trigger
  # build errors unless a valid predefs header is being passed to moc.
  #
  cxx.coptions += -DMOC_TEST_PREDEFS_INCLUDED
}

# Set the include path prefix.
#
# Also test handling of multiple earlier instances using different forms with
# invalid prefixes.
#
qt.moc.options = --p=xxx --p yyy -pzzz -p moc

# Generate:
#
# moc_source.cxx from source.hxx
# sink_moc.cxx   from sink.hxx (note: unconventional output filename)
# sinc.moc       from sink.cxx
#
# Use a rule hint for cxx{sink_moc} because it breaks the moc naming
# conventions (which recommend a `moc_` prefix for outputs generated from
# headers).
#
# Add libue{QtCoreMeta} as a prerequisite to cause lib{Qt*Core}'s library
# metadata (exported options such as macro definitions, header search
# directories, etc.) to be passed to moc. Note that we cannot pass
# lib{Qt*Core} directly because the moc rule would be unable to choose between
# the shared or static version.
#
cxx{moc_source}:                    hxx{source} libue{QtCoreMeta}
[rule_hint=qt.moc] cxx{sink_moc}:   hxx{sink}   libue{QtCoreMeta}
moc{sink}:                          cxx{sink}   libue{QtCoreMeta}

# Pass the compiler predefs header to hxx{moc_source} and hxx{sink_moc} in the
# two different supported ways. Skip moc{sink} to test the no-predefs case.
#

# Disable the automatic predefs header for everything by default.
#
qt.moc.auto_predefs = false

# Arrange for hxx{moc_predefs} to be parsed as a #include before the source
# file (using moc's --include option) using the "manual" method.
#
cxx{moc_source}: hxx{moc_predefs}: include = adhoc
cxx{moc_source}: qt.moc.options += --include $out_base/moc_predefs.hxx

# For cxx{sink_moc}, have the module automatically generate a compiler predefs
# header (a different one than hxx{moc_predefs} above) and pass it to moc.
#
cxx{sink_moc}: qt.moc.auto_predefs = true

cxx.poptions += "-I$out_root" "-I$src_root"
