import impl_libs = libz%lib{z}

tcpu = $c.target.cpu

arm     = ($tcpu == 'arm' || $tcpu == 'aarch64')
intel   = ($tcpu == 'i686'|| $tcpu == 'x86_64')
mips    = ($regex.match($tcpu, 'mipsel.*')   || $regex.match($tcpu, 'mips64el.*'))
powerpc = ($regex.match($tcpu, '^powerpc.*') || $regex.match($tcpu, '^ppc64.*'))

windows = ($c.target.class == 'windows')
linux   = ($c.target.class == 'linux')
bsd     = ($c.target.class == 'bsd')

# Version script.
#
define vsc: file
vsc{*}: extension = vers

lib{png}: {h c}{*} $impl_libs

# Note that ARM support requires compilation of an ASM source file which we do
# not yet support.
#
lib{png}: arm/c{*}:     include = false # $arm
lib{png}: intel/c{*}:   include = $intel
lib{png}: mips/c{*}:    include = $mips
lib{png}: powerpc/c{*}: include = $powerpc
lib{png}: vsc{libpng}:  include = ($linux || $bsd ? adhoc : false)

# Build options.
#
c.poptions =+ "-I$src_base"

# Enable hardware optimizations.
#
# Note that the hardware-specific source files only enable optimized code if
# the corresponding optimization is enabled in the compiler (whether
# explicitly via compiler options or implicitly as is often the case for
# SSE2).
#
# Note also that, although the upstream INSTALL file says to use the macros
# without the _OPT suffix, the upstream code does not appear to support that
# for any architectures but Intel (see pngpriv.h and png.h). So here we
# emulate the upstream `configure` and CMakeLists.txt as closely as we can: if
# we're on the corresponding architecture, define the macro to `on` as if the
# user enabled hardware optimizations; otherwise do not define it at all, as
# if the user did not pass any hardware optimization options.
#
# It's not clear why they use `1` in some cases and `2` in others when both
# mean "on" (all checks in source code are of the form `PNG_*_OPT > 0`).
#
if $arm
  c.poptions += -DPNG_ARM_NEON_OPT=0 # Disable NEON (see above).
elif $intel
  c.poptions += -DPNG_INTEL_SSE_OPT=1
elif $mips
  c.poptions += -DPNG_MIPS_MSA_OPT=2
elif $powerpc
  c.poptions += -DPNG_POWERPC_VSX_OPT=2

if $windows
{
  c.poptions += -D_CRT_SECURE_NO_DEPRECATE
  objs{*}: c.poptions += -DPNG_BUILD_DLL
}
elif ($linux || $bsd)
{
  c.loptions += "-Wl,--version-script=$src_base/libpng.vers"
  c.libs += -lm
}

# Export options.
#
lib{png}: c.export.poptions = "-I$src_base"

if $windows
  libs{png}: c.export.poptions += -DPNG_USE_DLL

# For pre-releases use the complete version to make sure they cannot be used
# in place of another pre-release or the final version. See the version module
# for details on the version.* variable values.
#
if $version.pre_release
  lib{png}: bin.lib.version = "-$version.project_id"
else
  lib{png}: bin.lib.version = "-$version.major.$version.minor"

# Install directly into, say, /usr/include/.
#
# Note that upstream goes through some extra motions and installs the headers
# both into include/ and include/libpngXY/. We are not going to do that for
# now since the library name doesn't include the version (and so in our model
# it's impossible to install multiple versions into the same location) and all
# the installed header names are prefixed with the library name.
#
h{*}: install = false
h{png}@./ h{pngconf}@./ h{pnglibconf}@./: install = include/
