# Copyright (c) 2020 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.

config("cpu_arch") {
  cflags = target_arch_cflags
  cflags_cc = cflags
  ldflags = cflags
  asmflags = cflags
}

config("gtest_feature_cpp") {
  cflags_cc = [ "-std=c++17" ]
}

config("language_c") {
  cflags_c = [ "-std=c99" ]
}

config("language_cpp") {
  cflags_cc = [ "-std=c++11" ]
}

config("kernel_macros") {
  if (ohos_kernel_type == "liteos_a") {
    defines = [
      "__LITEOS__",
      "__LITEOS_A__",
    ]
  } else if (ohos_kernel_type == "liteos_m") {
    defines = [
      "__LITEOS__",
      "__LITEOS_M__",
    ]
  } else if (ohos_kernel_type == "uniproton") {
    defines = [
      "__uniproton__",
      "__UNIPROTON__",
    ]
  } else if (ohos_kernel_type == "linux") {
    defines = [
      "__linux__",
      "__LINUX__",
    ]
  }
}

config("werror") {
  cflags = [ "-Werror" ]
  cflags_cc = cflags
}

config("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("security") {
  # Need to support fortify ability first in musl libc, so disable the option temporarily
  # defines = [ "_FORTIFY_SOURCE=2" ]

  cflags = [ "-fstack-protector-all" ]
  cflags_cc = cflags

  ldflags = [
    "-Wl,-z,now",
    "-Wl,-z,relro",
    "-Wl,-z,noexecstack",
  ]
}

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

config("no_exceptions") {
  cflags_cc = [ "-fno-exceptions" ]
  cflags_objcc = cflags_cc
  ldflags = cflags_cc
}

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

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

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

config("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("release") {
  defines = [ "OHOS_RELEASE" ]
}

config("debug") {
  defines = [ "OHOS_DEBUG" ]
}

config("clang_opt") {
  cflags = [
    "-Oz",
    "-flto",
  ]
  cflags_cc = cflags
}

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

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

config("board_config") {
  cflags = []
  cflags_c = []
  cflags_cc = []
  ldflags = []
  include_dirs = []
  defines = []
  asmflags = []
  cflags += board_cflags
  if (defined(board_c_cflags)) {
    cflags_c += board_c_cflags
  }
  cflags_cc += board_cxx_flags
  ldflags += board_ld_flags
  include_dirs += board_include_dirs
  if (defined(board_macro_defines)) {
    defines += board_macro_defines
  }
  if (defined(board_asmflags)) {
    asmflags += board_asmflags
  }
}

config("board_exe_ld_flags") {
  ldflags = []
  if (defined(board_exe_ld_flags)) {
    ldflags += board_exe_ld_flags
  }
}

sysroot_path = rebase_path(ohos_current_sysroot)
config("sysroot_flags") {
  if (ohos_build_compiler == "clang") {
    cflags = [
      "--target=$target_triple",
      "--sysroot=$sysroot_path",
    ]
  } else {
    cflags = [
      "--sysroot=$sysroot_path",
      "-specs=musl-gcc.specs",
    ]
  }
  cflags_cc = cflags
  ldflags = cflags
  asmflags = cflags
}