#!/bin/bash

# Project Calico BPF dataplane build scripts.
# Copyright (c) 2020-2024 Tigera, Inc. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

# Generate the cross-product of all the compile options, excluding some cases that don't make sense.
# Emit the filename for each option to stdout.
#
# WARNING: naming and set of cases must be kept in sync with tc.ProgFilename() in Felix's compiler.go.

emit_filename() {
  if [ "${core_support}" = "legacy" ]; then
    echo "bin/${from_or_to}_${ep_type}_${host_drop}${fib}${extra}${log_level}.o"
    echo "bin/${from_or_to}_${ep_type}_${host_drop}${fib}${extra}${log_level}_v6.o"
  else
    echo "bin/${from_or_to}_${ep_type}_${host_drop}${fib}${extra}${log_level}_${core_support}.o"
    echo "bin/${from_or_to}_${ep_type}_${host_drop}${fib}${extra}${log_level}_${core_support}_v6.o"
  fi
}

for log_level in debug no_log; do
  echo "bin/conntrack_cleanup_${log_level}_co-re_v4.o"
  echo "bin/conntrack_cleanup_${log_level}_co-re_v6.o"
  echo "bin/connect_balancer_${log_level}_v4.o"
  echo "bin/connect_balancer_${log_level}_v6.o"
  echo "bin/connect_balancer_${log_level}_v46.o"
  echo "bin/connect_balancer_${log_level}_co-re_v4.o"
  echo "bin/connect_balancer_${log_level}_co-re_v46.o"
  echo "bin/connect_balancer_${log_level}_co-re_v6.o"
  echo "bin/xdp_${log_level}.o"
  echo "bin/xdp_${log_level}_co-re_v6.o"

  for core_support in co-re legacy; do
    for host_drop in "" "host_drop_"; do
      if [ "${host_drop}" = "host_drop_" ]; then
        # The workload-to-host drop setting only applies to the from-workload hook.
        ep_types="wep"
      else
        ep_types="wep hep ipip l3 nat lo vxlan"
      fi
      for fib in "" "fib_"; do
        for ep_type in $ep_types; do
          if [ "${fib}" = "fib_" ] && [ "${ep_type}" = "lo" ]; then
            directions="from to"
          elif [ "${fib}" = "fib_" ] && [ ${ep_type} = "hep" ]; then
            directions="from to"
          elif [ "${host_drop}" = "host_drop_" ] || [ "${fib}" = "fib_" ]; then
            # The workload-to-host drop setting only applies to the from-workload hook.
            # The FIB only applies in the from-endpoint hooks.
            directions="from"
          else
            directions="from to"
          fi
          for from_or_to in $directions; do
            extra="dsr_"
            if [ "${ep_type}" = "hep" ]; then
              emit_filename
            fi
            if [ "${from_or_to}" = "from" ] && [ "${ep_type}" = "wep" ]; then
              emit_filename
            fi
            extra=""
            emit_filename
          done
        done
      done
    done
  done
done
