# Portable make only: no GNU-make conditionals and no $(wildcard), either of
# which would force SystemRequirements: GNU make, and no -W* or optimisation
# flags, which CRAN sets and rejects overrides of. design.md section 3.
#
# -DTF_PSA_CRYPTO_CONFIG_FILE replaces upstream's psa/crypto_config.h with
# src/zuc_crypto_config.h, so the enabled feature set is exactly what that
# file lists. It is the only -D here: everything else is a #define there,
# where it can be commented, and tools/vendor/verify checks that file against
# the manifest's `defines` column.
VENDOR = vendor/tf-psa-crypto

# Two -I roots, not upstream's six. The vendored tree is flattened into inc/
# and lib/ because R CMD check rejects a tarball path over 100 bytes and
# upstream's drivers/builtin/include/mbedtls/private/ hierarchy is over it
# before src/vendor/ is prefixed -- the same limit that breaks deep vendored
# trees on Windows. tools/vendor/keep/tf-psa-crypto.txt maps each file, and
# tools/vendor/verify measures the result.
PKG_CPPFLAGS = -I. \
  -I../inst/include \
  -I$(VENDOR)/inc \
  -I$(VENDOR)/lib \
  -DTF_PSA_CRYPTO_CONFIG_FILE='"zuc_crypto_config.h"' \
  $(ZUCRYPT_TEST_CPPFLAGS)

# ZUCRYPT_TEST_CPPFLAGS is empty in every real build: make reads it from the
# environment, and nothing sets it. aes-paths.yaml sets it to
# -DZUC_AES_SOFTWARE_ONLY to build and test the software AES fallback, which
# no machine with AES instructions would otherwise run (zuc_crypto_config.h).
# An environment variable rather than a make conditional, because a portable
# Makevars has none.

# $(C_VISIBILITY) is R's own substitution for -fvisibility=hidden, empty on a
# compiler that has no such flag. It is what keeps mbedtls_* and psa_* out of
# the dynamic symbol table, so zucrypt's vendored copy and any other package's
# independently vendored one cannot bind to each other, and so no OpenSSL-ABI
# name is exported beside the libcrypto zuhttp links (design.md sections 3
# and 4). tests/testthat/test-abi.R asserts it on the installed object.
PKG_CFLAGS = $(C_VISIBILITY)

# The vendored object list.
#
# Upstream's own build compiles every file in core/, drivers/builtin/src/,
# platform/, utilities/, dispatch/ and extras/ and lets the configuration
# guard the unused ones down to empty translation units. Under
# src/zuc_crypto_config.h that is 77 files of which 42 define a single symbol;
# the other 59 are compiled, produce nothing, and are not vendored at all.
#
# The list is derived, not chosen: compile all 77, keep the ones whose object
# has a symbol, then take the header closure with `cc -MM`. That closure is
# tools/vendor/keep/tf-psa-crypto.txt, and the two have to move together --
# dropping a source here without dropping it there leaves a file in the
# tarball that nothing builds. .agents/stage-1-spike.md records the method.
VENDOR_OBJECTS = \
  $(VENDOR)/lib/psa_crypto.o \
  $(VENDOR)/lib/psa_crypto_client.o \
  $(VENDOR)/lib/psa_crypto_driver_wrappers_no_static.o \
  $(VENDOR)/lib/psa_crypto_slot_management.o \
  $(VENDOR)/lib/psa_util.o \
  $(VENDOR)/lib/aes.o \
  $(VENDOR)/lib/aesni.o \
  $(VENDOR)/lib/aesce.o \
  $(VENDOR)/lib/cipher.o \
  $(VENDOR)/lib/cipher_wrap.o \
  $(VENDOR)/lib/psa_crypto_cipher.o \
  $(VENDOR)/lib/gcm.o \
  $(VENDOR)/lib/psa_crypto_aead.o \
  $(VENDOR)/lib/psa_crypto_hash.o \
  $(VENDOR)/lib/psa_crypto_mac.o \
  $(VENDOR)/lib/psa_crypto_rsa.o \
  $(VENDOR)/lib/psa_util_internal.o \
  $(VENDOR)/lib/sha1.o \
  $(VENDOR)/lib/sha256.o \
  $(VENDOR)/lib/sha512.o \
  $(VENDOR)/lib/platform_util.o \
  $(VENDOR)/lib/constant_time.o \
  $(VENDOR)/lib/ecdsa.o \
  $(VENDOR)/lib/ecp.o \
  $(VENDOR)/lib/ecp_curves.o \
  $(VENDOR)/lib/hmac_drbg.o \
  $(VENDOR)/lib/pk_ecc.o \
  $(VENDOR)/lib/psa_crypto_ecp.o \
  $(VENDOR)/lib/asn1parse.o \
  $(VENDOR)/lib/asn1write.o \
  $(VENDOR)/lib/base64.o \
  $(VENDOR)/lib/bignum.o \
  $(VENDOR)/lib/bignum_core.o \
  $(VENDOR)/lib/md.o \
  $(VENDOR)/lib/oid.o \
  $(VENDOR)/lib/pem.o \
  $(VENDOR)/lib/pk.o \
  $(VENDOR)/lib/pk_rsa.o \
  $(VENDOR)/lib/pk_wrap.o \
  $(VENDOR)/lib/pkparse.o \
  $(VENDOR)/lib/pkwrite.o \
  $(VENDOR)/lib/rsa.o \
  $(VENDOR)/lib/rsa_alt_helpers.o


# ADAPTER_OBJECTS is the R-free core: the adapter and the vendored backend,
# and nothing that has ever seen an R header. It is what goes into
# libzucrypt.a below, so keep it that way -- a consumer links that archive
# into its own shared object, where R glue would be a duplicate symbol and
# could not work anyway. tests/testthat/test-linking.R audits the built
# archive for R symbols rather than trusting this comment.
ADAPTER_OBJECTS = \
  zuc_status.o \
  zuc_backend.o \
  zuc_hash.o \
  zuc_aes.o \
  zuc_util.o \
  zuc_random.o \
  zuc_aead.o \
  zuc_kdf.o \
  zuc_pkey.o \
  $(VENDOR_OBJECTS)

# The four files that do include R headers. zucrypt_test.c is compiled into
# every build, never only into a debug one: a harness that exists only where
# nobody runs it tests nothing.
OBJECTS = \
  zucrypt_r.o \
  zucrypt_crypt.o \
  zucrypt_api.o \
  zucrypt_test.o \
  $(ADAPTER_OBJECTS)

# ---------------------------------------------------------------------------
# The LinkingTo surface: libzucrypt.a, installed to lib${R_ARCH}/
#
# For a consumer that cannot carry an Imports: -- zuxlsx links its siblings as
# static archives with no runtime dependency at all. It gets the adapter and
# the vendored backend, and sees only inst/include/zucrypt.h: no upstream
# header is installed, because PSA's headers describe whatever configuration
# they were generated for and a consumer has no way to reproduce ours.
#
# The archive inherits $(ALL_CFLAGS) by being built from the same objects the
# shared object was linked from. That is what makes it position independent
# ($(CPICFLAGS) is in there), and it means the archive is exactly the code
# that was tested here rather than a second compilation of it.
#
# "all" has to be the FIRST target in this file: make takes the first target
# it reads as the default goal, so a rule above this one becomes what an
# unqualified `make` builds, which shows up as the shared object quietly not
# being relinked. (Writing R Extensions 1.2.1.)
.PHONY: all

all: $(SHLIB) libzucrypt.a

# Written under a name of its own and renamed into place, in one shell, so
# two builds in this directory at once -- testthat's parallel workers each
# load_all() when the DLL is missing -- cannot leave each other a
# half-written archive ("ar: libzucrypt.a: Inappropriate file type").
libzucrypt.a: $(SHLIB)
	tmp=libzucrypt.a.$$$$ && rm -f $$tmp && \
	  $(AR) rcs $$tmp $(ADAPTER_OBJECTS) && mv -f $$tmp libzucrypt.a

# ---------------------------------------------------------------------------
# Header prerequisites (#109)
#
# R CMD INSTALL on a checkout keeps the objects in src/ between runs, and make
# rebuilds only an object older than one of its prerequisites. With none but
# its .c file, a change to zuc_crypto_config.h, an installed header or a
# re-vendored header reused objects compiled before it: the AES path stayed
# hardware after ZUC_AES_SOFTWARE_ONLY was defined, and a stale zucrypt_api.o
# filled the function table in an old layout. So every object depends on
# every header, a coarse rule that rebuilds everything on any header change
# and never too little. tools/check-rebuild.sh checks it.
#
# The vendored list is written out, because portable make has no $(wildcard):
# tools/vendor/record generates the block between the markers, and
# tools/vendor/verify fails when it and the vendored tree disagree.
# BEGIN VENDOR_HEADERS (generated by tools/vendor/record)
VENDOR_HEADERS = \
  $(VENDOR)/inc/mbedtls/asn1.h \
  $(VENDOR)/inc/mbedtls/asn1write.h \
  $(VENDOR)/inc/mbedtls/base64.h \
  $(VENDOR)/inc/mbedtls/compat-3-crypto.h \
  $(VENDOR)/inc/mbedtls/constant_time.h \
  $(VENDOR)/inc/mbedtls/md.h \
  $(VENDOR)/inc/mbedtls/pem.h \
  $(VENDOR)/inc/mbedtls/pk.h \
  $(VENDOR)/inc/mbedtls/platform.h \
  $(VENDOR)/inc/mbedtls/platform_util.h \
  $(VENDOR)/inc/mbedtls/private/aes.h \
  $(VENDOR)/inc/mbedtls/private/bignum.h \
  $(VENDOR)/inc/mbedtls/private/camellia.h \
  $(VENDOR)/inc/mbedtls/private/ccm.h \
  $(VENDOR)/inc/mbedtls/private/chacha20.h \
  $(VENDOR)/inc/mbedtls/private/chachapoly.h \
  $(VENDOR)/inc/mbedtls/private/cipher.h \
  $(VENDOR)/inc/mbedtls/private/cmac.h \
  $(VENDOR)/inc/mbedtls/private/crypto_adjust_config_enable_builtins.h \
  $(VENDOR)/inc/mbedtls/private/crypto_adjust_config_tweak_builtins.h \
  $(VENDOR)/inc/mbedtls/private/crypto_builtin_composites.h \
  $(VENDOR)/inc/mbedtls/private/crypto_builtin_key_derivation.h \
  $(VENDOR)/inc/mbedtls/private/crypto_builtin_primitives.h \
  $(VENDOR)/inc/mbedtls/private/ecdsa.h \
  $(VENDOR)/inc/mbedtls/private/ecjpake.h \
  $(VENDOR)/inc/mbedtls/private/ecp.h \
  $(VENDOR)/inc/mbedtls/private/entropy.h \
  $(VENDOR)/inc/mbedtls/private/error_common.h \
  $(VENDOR)/inc/mbedtls/private/gcm.h \
  $(VENDOR)/inc/mbedtls/private/hmac_drbg.h \
  $(VENDOR)/inc/mbedtls/private/md5.h \
  $(VENDOR)/inc/mbedtls/private/pk_private.h \
  $(VENDOR)/inc/mbedtls/private/poly1305.h \
  $(VENDOR)/inc/mbedtls/private/ripemd160.h \
  $(VENDOR)/inc/mbedtls/private/rsa.h \
  $(VENDOR)/inc/mbedtls/private/sha1.h \
  $(VENDOR)/inc/mbedtls/private/sha256.h \
  $(VENDOR)/inc/mbedtls/private/sha3.h \
  $(VENDOR)/inc/mbedtls/private/sha512.h \
  $(VENDOR)/inc/mbedtls/private_access.h \
  $(VENDOR)/inc/mbedtls/psa_util.h \
  $(VENDOR)/inc/mbedtls/threading.h \
  $(VENDOR)/inc/psa/crypto.h \
  $(VENDOR)/inc/psa/crypto_compat.h \
  $(VENDOR)/inc/psa/crypto_driver_common.h \
  $(VENDOR)/inc/psa/crypto_driver_contexts_composites.h \
  $(VENDOR)/inc/psa/crypto_driver_contexts_key_derivation.h \
  $(VENDOR)/inc/psa/crypto_driver_contexts_primitives.h \
  $(VENDOR)/inc/psa/crypto_driver_random.h \
  $(VENDOR)/inc/psa/crypto_extra.h \
  $(VENDOR)/inc/psa/crypto_platform.h \
  $(VENDOR)/inc/psa/crypto_sizes.h \
  $(VENDOR)/inc/psa/crypto_struct.h \
  $(VENDOR)/inc/psa/crypto_types.h \
  $(VENDOR)/inc/psa/crypto_values.h \
  $(VENDOR)/inc/tf-psa-crypto/build_info.h \
  $(VENDOR)/inc/tf-psa-crypto/private/crypto_adjust_config_auto_enabled.h \
  $(VENDOR)/inc/tf-psa-crypto/private/crypto_adjust_config_dependencies.h \
  $(VENDOR)/inc/tf-psa-crypto/private/crypto_adjust_config_derived.h \
  $(VENDOR)/inc/tf-psa-crypto/private/crypto_adjust_config_key_pair_types.h \
  $(VENDOR)/inc/tf-psa-crypto/private/crypto_adjust_config_support.h \
  $(VENDOR)/inc/tf-psa-crypto/private/crypto_adjust_config_synonyms.h \
  $(VENDOR)/lib/aesce.h \
  $(VENDOR)/lib/aesni.h \
  $(VENDOR)/lib/alignment.h \
  $(VENDOR)/lib/base64_internal.h \
  $(VENDOR)/lib/bignum_core.h \
  $(VENDOR)/lib/bignum_core_invasive.h \
  $(VENDOR)/lib/bignum_internal.h \
  $(VENDOR)/lib/bignum_mod.h \
  $(VENDOR)/lib/bn_mul.h \
  $(VENDOR)/lib/check_crypto_config.h \
  $(VENDOR)/lib/cipher_invasive.h \
  $(VENDOR)/lib/cipher_wrap.h \
  $(VENDOR)/lib/constant_time_impl.h \
  $(VENDOR)/lib/constant_time_internal.h \
  $(VENDOR)/lib/crypto_oid.h \
  $(VENDOR)/lib/ctr.h \
  $(VENDOR)/lib/ecp_invasive.h \
  $(VENDOR)/lib/md_psa.h \
  $(VENDOR)/lib/md_wrap.h \
  $(VENDOR)/lib/pk_internal.h \
  $(VENDOR)/lib/pk_wrap.h \
  $(VENDOR)/lib/pkwrite.h \
  $(VENDOR)/lib/psa_crypto_aead.h \
  $(VENDOR)/lib/psa_crypto_cipher.h \
  $(VENDOR)/lib/psa_crypto_core.h \
  $(VENDOR)/lib/psa_crypto_core_common.h \
  $(VENDOR)/lib/psa_crypto_driver_wrappers.h \
  $(VENDOR)/lib/psa_crypto_driver_wrappers_no_static.h \
  $(VENDOR)/lib/psa_crypto_ecp.h \
  $(VENDOR)/lib/psa_crypto_ffdh.h \
  $(VENDOR)/lib/psa_crypto_hash.h \
  $(VENDOR)/lib/psa_crypto_invasive.h \
  $(VENDOR)/lib/psa_crypto_mac.h \
  $(VENDOR)/lib/psa_crypto_pake.h \
  $(VENDOR)/lib/psa_crypto_random.h \
  $(VENDOR)/lib/psa_crypto_random_impl.h \
  $(VENDOR)/lib/psa_crypto_rsa.h \
  $(VENDOR)/lib/psa_crypto_slot_management.h \
  $(VENDOR)/lib/psa_crypto_storage.h \
  $(VENDOR)/lib/psa_crypto_xof.h \
  $(VENDOR)/lib/psa_util_internal.h \
  $(VENDOR)/lib/rsa_alt_helpers.h \
  $(VENDOR)/lib/rsa_internal.h \
  $(VENDOR)/lib/rsa_invasive.h \
  $(VENDOR)/lib/tf_psa_crypto_common.h \
  $(VENDOR)/lib/tf_psa_crypto_platform_requirements.h \
  $(VENDOR)/lib/threading_internal.h
# END VENDOR_HEADERS

ZUCRYPT_HEADERS = Makevars \
  zuc_crypto_config.h \
  zuc_internal.h \
  zuc_live.h \
  ../inst/include/zucrypt.h \
  ../inst/include/zucrypt-r.h \
  $(VENDOR_HEADERS)

$(OBJECTS): $(ZUCRYPT_HEADERS)
