import("//compiler-rt/lib/builtins/sources.gni")
import("//compiler-rt/target.gni")
import("//llvm/utils/gn/build/buildflags.gni")

lse_targets = []

if (current_cpu == "arm64") {
  foreach(pat,
          [
            "cas",
            "swp",
            "ldadd",
            "ldclr",
            "ldeor",
            "ldset",
          ]) {
    foreach(size,
            [
              "1",
              "2",
              "4",
              "8",
              "16",
            ]) {
      foreach(model,
              [
                "1",
                "2",
                "3",
                "4",
              ]) {
        if (pat == "cas" || size != "16") {
          source_set("lse_${pat}_${size}_${model}") {
            # Assign to sources like this to hide from
            # sync_source_lists_from_cmake.py which won't find the source file
            # on the CMake side.
            lse_file = "aarch64/lse.S"
            sources = [ lse_file ]
            include_dirs = [ "." ]
            defines = [
              "L_$pat",
              "SIZE=$size",
              "MODEL=$model",
            ]
          }
          lse_targets += [ ":lse_${pat}_${size}_${model}" ]
        }
      }
    }
  }
}

static_library("builtins") {
  output_dir = crt_current_out_dir
  if (current_os == "mac") {
    output_name = "clang_rt.osx"
  } else if (current_os == "ios" && current_cpu == "arm64") {
    output_name = "clang_rt.ios"
  } else if (current_os == "ios" && current_cpu == "x64") {
    output_name = "clang_rt.iossim"
  } else {
    output_name = "clang_rt.builtins$crt_current_target_suffix"
  }

  complete_static_lib = true
  configs -= [ "//llvm/utils/gn/build:thin_archive" ]

  cflags = []
  if (current_os != "win") {
    cflags = [
      "-fPIC",
      "-fno-builtin",
    ]
    if (current_os != "android") {
      cflags += [ "-fvisibility=hidden" ]
    }
    if (!is_debug) {
      cflags += [ "-fomit-frame-pointer" ]
    }
    cflags_c = [ "-std=c11" ]
    cflags_cc = [ "-nostdinc++" ]
  }

  defines = builtins_defines
  sources = builtins_sources

  deps = lse_targets
  include_dirs = [ "//third-party/siphash/include" ]
}

# Currently unused but necessary to make sync_source_lists_from_cmake.py happy.
source_set("_unused") {
  sources = [
    # Thumb1
    "arm/addsf3.S",
    "arm/comparesf2.S",
    "arm/divsi3.S",
    "arm/udivsi3.S",
    "arm/fnan2.c",
    "arm/fnorm2.c",
    "arm/funder.c",
    "arm/mulsf3.S",
    "arm/divsf3.S",
    "arm/thumb1/mulsf3.S",
  ]
}
