# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("${device_path}/config.gni")

config("clang_cpu_arch") {
  arch_cflags = board_cflags
  if (board_arch != "") {
    arch_cflags += [ "-march=$board_arch" ]
  }
  if (board_cpu != "") {
    arch_cflags += [ "-mcpu=$board_cpu" ]
  }
  cflags = arch_cflags
  cflags_cc = cflags
  ldflags = cflags
  asmflags = cflags
}

config("clang_common") {
  defines = [ "_XOPEN_SOURCE=700" ]
  cflags = [
    "-fno-common",
    "-fno-builtin",
    "-fno-strict-aliasing",
    "-Wall",
  ]
  if (ohos_kernel_type == "linux") {
    cflags += [
      "-funwind-tables",
      "-fasynchronous-unwind-tables",
    ]
  }
  cflags_cc = cflags
  cflags += [ "-fsigned-char" ]
}

config("clang_security") {
  defines = [ "_FORTIFY_SOURCE=2" ]
  cflags = [ "-fstack-protector-all" ]
  cflags_cc = cflags
  ldflags = [
    "-Wl,-z,now",
    "-Wl,-z,relro",
    "-Wl,-z,noexecstack",
  ]
}

config("clang_exceptions") {
  cflags_cc = [ "-fexceptions" ]
  cflags_objcc = cflags_cc
}

config("clang_stack_protector") {
  cflags = [ "-fstack-protector-all" ]
  cflags_cc = cflags
}

config("clang_static_pie_config") {
  cflags = [ "-fPIE" ]
  cflags_cc = cflags
}

config("clang_shared_library_config") {
  cflags = [ "-fPIC" ]
  cflags_cc = cflags
}

config("clang_pie_executable_config") {
  ldflags = [ "-pie" ]
}

config("ohos_clang") {
  if (ohos_kernel_type == "linux") {
    defines = [
      "_LIBCPP_HAS_MUSL_LIBC",
      "__BUILD_LINUX_WITH_CLANG",
    ]
  }
  ldflags = [
    "-fuse-ld=lld",
    "--rtlib=compiler-rt",
  ]
}

config("clang_opt") {
  cflags = [ "-Os" ]
  cflags_cc = cflags
}

config("clang_default_link_path") {
  ldflags = [
    "-L.",
    "-Wl,-rpath-link=.",
  ]
}

config("clang_kernel_configs") {
  configs = []
  if (ohos_kernel_type == "liteos_a") {
    configs += [
      ":clang_security",
      ":clang_exceptions",
      "//build/lite/config/kernel/liteos/cortex_a:default",
    ]
  }
  if (ohos_kernel_type == "linux") {
    configs += [
      ":clang_security",
      ":clang_exceptions",
    ]
  }
  if (ohos_kernel_type == "liteos_m") {
    configs += [ ":clang_stack_protector" ]
  }
}

config("clang_build_type_configs") {
  configs = []
  if (ohos_build_type == "debug") {
    configs += [ "//build/config/compiler/lite/common:debug" ]
  } else if (ohos_build_type == "release") {
    configs += [ "//build/config/compiler/lite/common:release" ]
  }
}

config("clang_compiler_configs") {
  configs = []
  if (current_os == "ohos") {
    configs += [
      ":clang_cpu_arch",
      ":clang_common",
      ":clang_default_link_path",
      ":clang_kernel_configs",
      "//build/config/compiler/lite/common:board_config",
      "//build/config/compiler/lite/common:kernel_macros",
    ]
  }
  configs += [ ":clang_build_type_configs" ]
}

config("clang_shared_library_configs") {
  configs = [
    ":clang_compiler_configs",
    ":clang_shared_library_config",
  ]
}

config("clang_static_library_configs") {
  configs = [ ":clang_compiler_configs" ]
  if (ohos_kernel_type != "liteos_m") {
    configs += [ ":clang_static_pie_config" ]
  }
}

config("clang_executable_configs") {
  configs = [ ":clang_compiler_configs" ]
  if (ohos_kernel_type != "liteos_m") {
    configs += [
      ":clang_static_pie_config",
      ":clang_pie_executable_config",
      "//build/config/compiler/lite/common:board_exe_ld_flags",
    ]
  }
}