# Copyright (c) 2014-present, The osquery authors
#
# This source code is licensed as defined by the LICENSE file found in the
# root directory of this source tree.
#
# SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)

function(specsMain)
  add_osquery_library(specs_tables INTERFACE)

  generateNativeTables()
  list(APPEND table_target_list "${generateNativeTables_output}")

  generateForeignTables(${foreign_spec_file_list_output})
  list(APPEND table_target_list "${generateForeignTables_output}")

  foreach(table_target ${table_target_list})
    target_link_libraries("${table_target}" PUBLIC osquery_tables_tableimplementations)
    enableLinkWholeArchive("${table_target}")
  endforeach()

  target_link_libraries(specs_tables INTERFACE ${table_target_list})

  generateCopyFileTarget("specs_table_files" "" "REGEX" "*.table" "${TEST_CONFIGS_DIR}/specs")
endfunction()

function(generateForeignTables spec_file_list)
  generateTableCategoryAmalgamation("foreign" ${spec_file_list})
  set(generateForeignTables_output "${generateTableCategoryAmalgamation_output}" PARENT_SCOPE)
endfunction()

function(generateNativeTables)
  set(platform_independent_spec_files
    azure_instance_metadata.table
    azure_instance_tags.table
    carbon_black_info.table
    carves.table
    certificates.table
    chrome_extensions.table
    chrome_extension_content_scripts.table
    curl.table
    curl_certificate.table
    etc_hosts.table
    etc_protocols.table
    etc_services.table
    groups.table
    firefox_addons.table
    hash.table
    interface_addresses.table
    interface_details.table
    listening_ports.table
    logged_in_users.table
    memory_devices.table
    npm_packages.table
    os_version.table
    platform_info.table
    process_memory_map.table
    process_open_sockets.table
    processes.table
    python_packages.table
    routes.table
    ssh_configs.table
    startup_items.table
    system_info.table
    uptime.table
    user_ssh_keys.table
    users.table
    utility/file.table
    utility/osquery_events.table
    utility/osquery_extensions.table
    utility/osquery_flags.table
    utility/osquery_info.table
    utility/osquery_packs.table
    utility/osquery_registry.table
    utility/osquery_schedule.table
    utility/time.table
    yara.table
    ycloud_instance_metadata.table
  )

  if(TARGET_PROCESSOR STREQUAL "x86_64")
    list(APPEND platform_independent_spec_files cpuid.table)
  endif()

  set(platform_dependent_spec_files
    "arp_cache.table:linux,macos,windows"
    "vscode_extensions.table:linux,macos,windows"
    "cpu_info.table:linux,macos,windows"
    "darwin/account_policy_data.table:macos"
    "darwin/ad_config.table:macos"
    "darwin/alf.table:macos"
    "darwin/alf_exceptions.table:macos"
    "darwin/alf_explicit_auths.table:macos"
    "darwin/app_schemes.table:macos"
    "darwin/apps.table:macos"
    "darwin/asl.table:macos"
    "darwin/authorization_mechanisms.table:macos"
    "darwin/authorizations.table:macos"
    "darwin/browser_plugins.table:macos"
    "darwin/connected_displays.table:macos"
    "darwin/crashes.table:macos"
    "darwin/cups_destinations.table:macos"
    "darwin/cups_jobs.table:macos"
    "darwin/device_firmware.table:macos"
    "darwin/disk_events.table:macos"
    "darwin/es_process_events.table:macos"
    "darwin/es_process_file_events.table:macos"
    "darwin/event_taps.table:macos"
    "darwin/fan_speed_sensors.table:macos"
    "darwin/gatekeeper.table:macos"
    "darwin/gatekeeper_approved_apps.table:macos"
    "darwin/homebrew_packages.table:macos"
    "darwin/ibridge_info.table:macos"
    "darwin/iokit_devicetree.table:macos"
    "darwin/iokit_registry.table:macos"
    "darwin/kernel_extensions.table:macos"
    "darwin/kernel_panics.table:macos"
    "darwin/keychain_acls.table:macos"
    "darwin/keychain_items.table:macos"
    "darwin/launchd.table:macos"
    "darwin/launchd_overrides.table:macos"
    "darwin/location_services.table:macos"
    "darwin/managed_policies.table:macos"
    "darwin/mdfind.table:macos"
    "darwin/mdls.table:macos"
    "darwin/nfs_shares.table:macos"
    "darwin/nvram.table:macos"
    "darwin/package_bom.table:macos"
    "darwin/package_install_history.table:macos"
    "darwin/package_receipts.table:macos"
    "darwin/password_policy.table:macos"
    "darwin/plist.table:macos"
    "darwin/power_sensors.table:macos"
    "darwin/preferences.table:macos"
    "darwin/quicklook_cache.table:macos"
    "darwin/running_apps.table:macos"
    "darwin/safari_extensions.table:macos"
    "darwin/sandboxes.table:macos"
    "darwin/screenlock.table:macos"
    "darwin/shared_folders.table:macos"
    "darwin/sharing_preferences.table:macos"
    "darwin/signature.table:macos"
    "darwin/sip_config.table:macos"
    "darwin/smc_keys.table:macos"
    "darwin/system_extensions.table:macos"
    "darwin/temperature_sensors.table:macos"
    "darwin/time_machine_backups.table:macos"
    "darwin/time_machine_destinations.table:macos"
    "darwin/unified_log.table:macos"
    "darwin/user_interaction_events.table:macos"
    "darwin/virtual_memory_info.table:macos"
    "darwin/wifi_networks.table:macos"
    "darwin/wifi_scan.table:macos"
    "darwin/wifi_status.table:macos"
    "darwin/xprotect_entries.table:macos"
    "darwin/xprotect_meta.table:macos"
    "darwin/xprotect_reports.table:macos"
    "linux/apparmor_events.table:linux"
    "linux/apparmor_profiles.table:linux"
    "linux/apt_sources.table:linux"
    "linux/iptables.table:linux"
    "linux/kernel_keys.table:linux"
    "linux/kernel_modules.table:linux"
    "linux/lxd_instances.table:linux"
    "linux/lxd_instance_config.table:linux"
    "linux/lxd_instance_devices.table:linux"
    "linux/lxd_cluster.table:linux"
    "linux/lxd_cluster_members.table:linux"
    "linux/lxd_images.table:linux"
    "linux/lxd_certificates.table:linux"
    "linux/lxd_networks.table:linux"
    "linux/lxd_storage_pools.table:linux"
    "linux/memory_info.table:linux"
    "linux/memory_map.table:linux"
    "linux/md_devices.table:linux"
    "linux/md_drives.table:linux"
    "linux/md_personalities.table:linux"
    "linux/msr.table:linux"
    "linux/portage_keywords.table:linux"
    "linux/portage_packages.table:linux"
    "linux/portage_use.table:linux"
    "linux/process_file_events.table:linux"
    "linux/process_namespaces.table:linux"
    "linux/process_open_pipes.table:linux"
    "linux/rpm_package_files.table:linux"
    "linux/rpm_packages.table:linux"
    "linux/seccomp_events.table:linux"
    "linux/selinux_events.table:linux"
    "linux/selinux_settings.table:linux"
    "linux/shadow.table:linux"
    "linux/shared_memory.table:linux"
    "linux/syslog_events.table:linux"
    "linux/systemd_units.table:linux"
    "linux/yum_sources.table:linux"
    "linwin/intel_me_info.table:linux,windows"
    "macwin/battery.table:macos,windows"
    "kernel_info.table:linux,macos,windows"
    "posix/extended_attributes.table:linux,macos"
    "posix/acpi_tables.table:linux,macos"
    "posix/augeas.table:linux,macos"
    "posix/authorized_keys.table:linux,macos"
    "posix/block_devices.table:linux,macos"
    "posix/cpu_time.table:linux,macos"
    "posix/crontab.table:linux,macos"
    "posix/disk_encryption.table:linux,macos"
    "posix/dns_resolvers.table:linux,macos"
    "posix/docker_container_labels.table:linux,macos"
    "posix/docker_container_envs.table:linux,macos"
    "posix/docker_container_mounts.table:linux,macos"
    "posix/docker_container_networks.table:linux,macos"
    "posix/docker_container_ports.table:linux,macos"
    "posix/docker_container_processes.table:linux,macos"
    "posix/docker_container_fs_changes.table:linux,macos"
    "posix/docker_container_stats.table:linux,macos"
    "posix/docker_containers.table:linux,macos"
    "posix/docker_image_history.table:linux,macos"
    "posix/docker_image_labels.table:linux,macos"
    "posix/docker_image_layers.table:linux,macos"
    "posix/docker_images.table:linux,macos"
    "posix/docker_info.table:linux,macos"
    "posix/docker_network_labels.table:linux,macos"
    "posix/docker_networks.table:linux,macos"
    "posix/docker_version.table:linux,macos"
    "posix/docker_volume_labels.table:linux,macos"
    "posix/docker_volumes.table:linux,macos"
    "posix/file_events.table:linux,macos"
    "posix/hardware_events.table:linux,macos"
    "posix/interface_ipv6.table:linux,macos"
    "posix/known_hosts.table:linux,macos"
    "posix/last.table:linux,macos"
    "posix/load_average.table:linux,macos"
    "posix/magic.table:linux,macos"
    "posix/memory_array_mapped_addresses.table:linux,macos"
    "posix/memory_arrays.table:linux,macos"
    "posix/memory_device_mapped_addresses.table:linux,macos"
    "posix/memory_error_info.table:linux,macos"
    "posix/mounts.table:linux,macos"
    "posix/oem_strings.table:linux,macos"
    "posix/pci_devices.table:linux,macos"
    "posix/process_envs.table:linux,macos"
    "posix/process_events.table:linux,macos"
    "posix/process_open_files.table:linux,macos"
    "posix/prometheus_metrics.table:linux,macos"
    "posix/shell_history.table:linux,macos"
    "posix/smbios_tables.table:linux,macos"
    "posix/socket_events.table:linux,macos"
    "posix/sudoers.table:linux,macos"
    "posix/suid_bin.table:linux,macos"
    "posix/system_controls.table:linux,macos"
    "posix/ulimit_info.table:linux,macos"
    "posix/usb_devices.table:linux,macos"
    "posix/user_events.table:linux,macos"
    "posix/yara_events.table:linux,macos"
    "secureboot.table:linux,macos,windows"
    "sleuthkit/device_file.table:linux,macos,windows"
    "sleuthkit/device_hash.table:linux,macos,windows"
    "sleuthkit/device_partitions.table:linux,macos,windows"
    "user_groups.table:linux,macos,windows"
    "windows/background_activities_moderator.table:windows"
    "windows/bitlocker_info.table:windows"
    "windows/chassis_info.table:windows"
    "windows/logon_sessions.table:windows"
    "windows/ntfs_acl_permissions.table:windows"
    "windows/office_mru.table:windows"
    "windows/scheduled_tasks.table:windows"
    "windows/windows_crashes.table:windows"
    "windows/services.table:windows"
    "windows/kva_speculative_info.table:windows"
    "windows/patches.table:windows"
    "windows/windows_update_history.table:windows"
    "windows/ie_extensions.table:windows"
    "windows/shared_resources.table:windows"
    "windows/wmi_cli_event_consumers.table:windows"
    "windows/video_info.table:windows"
    "windows/registry.table:windows"
    "windows/drivers.table:windows"
    "windows/disk_info.table:windows"
    "windows/chocolatey_packages.table:windows"
    "windows/logical_drives.table:windows"
    "windows/ntdomains.table:windows"
    "windows/authenticode.table:windows"
    "windows/pipes.table:windows"
    "windows/wmi_filter_consumer_binding.table:windows"
    "windows/wmi_event_filters.table:windows"
    "windows/windows_events.table:windows"
    "windows/windows_eventlog.table:windows"
    "windows/appcompat_shims.table:windows"
    "windows/ntfs_journal_events.table:windows"
    "windows/powershell_events.table:windows"
    "windows/winbaseobj.table:windows"
    "windows/wmi_bios_info.table:windows"
    "windows/programs.table:windows"
    "windows/wmi_script_event_consumers.table:windows"
    "windows/physical_disk_performance.table:windows"
    "windows/autoexec.table:windows"
    "windows/default_environment.table:windows"
    "windows/windows_security_products.table:windows"
    "windows/windows_security_center.table:windows"
    "windows/windows_optional_features.table:windows"
    "windows/connectivity.table:windows"
    "windows/windows_firewall_rules.table:windows"
    "windows/userassist.table:windows"
    "windows/dns_cache.table:windows"
    "windows/deviceguard_status.table:windows"
    "windows/shimcache.table:windows"
    "windows/shellbags.table:windows"
    "windows/prefetch.table:windows"
    "windows/tpm_info.table:windows"
    "windows/security_profile_info.table:windows"
    "windows/windows_search.table:windows"
  )

  if(OSQUERY_BUILD_BPF)
    list(APPEND platform_dependent_spec_files
      "linux/bpf_process_events.table:linux"
      "linux/bpf_socket_events.table:linux"
    )
  endif()

  if(OSQUERY_BUILD_ETW)
    list(APPEND platform_dependent_spec_files
      "windows/process_etw_events.table:windows"
    )
  endif()

  if(OSQUERY_BUILD_AWS)
    list(APPEND platform_independent_spec_files
      "ec2_instance_metadata.table"
      "ec2_instance_tags.table"
    )
  endif()

  if(OSQUERY_BUILD_DPKG)
    list(APPEND platform_dependent_spec_files
      "linux/deb_packages.table:linux"
    )
  endif()

  foreach(spec_descriptor ${platform_dependent_spec_files})
    string(REPLACE ":" ";" spec_descriptor "${spec_descriptor}")
    list(GET spec_descriptor 0 spec_file)

    list(GET spec_descriptor 1 spec_descriptor)
    string(REPLACE "," ";" spec_file_platforms "${spec_descriptor}")

    list(FIND spec_file_platforms "linux" is_linux_compatible)
    list(FIND spec_file_platforms "macos" is_macos_compatible)
    list(FIND spec_file_platforms "freebsd" is_freebsd_compatible)
    list(FIND spec_file_platforms "windows" is_windows_compatible)

    if(DEFINED PLATFORM_LINUX AND NOT ${is_linux_compatible} EQUAL -1)
      set(enable_spec_file true)
    elseif(DEFINED PLATFORM_MACOS AND NOT ${is_macos_compatible} EQUAL -1)
      set(enable_spec_file true)
    elseif(DEFINED PLATFORM_FREEBSD AND NOT ${is_freebsd_compatible} EQUAL -1)
      set(enable_spec_file true)
    elseif(DEFINED PLATFORM_WINDOWS AND NOT ${is_windows_compatible} EQUAL -1)
      set(enable_spec_file true)
    else()
      set(enable_spec_file false)
    endif()

    if(${enable_spec_file})
      list(APPEND spec_file_list
        "${spec_file}"
      )
    else()
      list(APPEND foreign_spec_file_list
        "${spec_file}"
      )
    endif()
  endforeach()

  list(APPEND spec_file_list ${platform_independent_spec_files})

  generateTableCategoryAmalgamation("native" ${spec_file_list})
  set(generateNativeTables_output "${generateTableCategoryAmalgamation_output}" PARENT_SCOPE)
  set(foreign_spec_file_list_output "${foreign_spec_file_list}" PARENT_SCOPE)
endfunction()

specsMain()
