# Copyright (c) 2025 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("//build/ohos.gni")

# List all the input args
declare_args() {
  CANGJIE_RUNTIME_PATH = "//third_party/cangjie_runtime/runtime"
  BUILD_TYPE = "Release"
  OHOS_PRODUCT_NAME = "rk3568"
  ASAN_SUPPORT_FLAG = 0
  HWASAN_SUPPORT_FLAG = 0
  SANITIZER_SUPPORT_FLAG = 0
  GWPASAN_SUPPORT_FLAG = 1
  CJ_SDK_VERSION = ""
  DISABLE_VERSION_CHECK = 1
  COPYGC_FLAG = "1"
  RUNTIME_FORWARD_PTRAUTH_CFI = 1
  RUNTIME_BACKWARD_PTRAUTH_CFI = 1
  OHOS_INCLUDE_DIR = "//prebuilts/ohos-sdk/linux/20/native/sysroot/usr"
  OHOS_INCLUDE_COMPONENT = ["hilog", "hitrace"]
}

if(current_os == "ohos") {
  if (target_cpu == "arm64") {
    OHOS_FLAG = 1
    TARGET_ARCH = "aarch64"
  } else if (target_cpu == "x86_64") {
    OHOS_FLAG = 2
    TARGET_ARCH = "x86_64"
  }
}

if (is_asan) {
  if (use_hwasan) {
    HWASAN_SUPPORT_FLAG = 1
  } else {
    ASAN_SUPPORT_FLAG = 1
  }
  SANITIZER_SUPPORT_FLAG = 1
  GWPASAN_SUPPORT_FLAG = 0
}

# include dirs
include_dirs = [
  "${CANGJIE_RUNTIME_PATH}/src",
  "${CANGJIE_RUNTIME_PATH}/build/include/",
  "//prebuilts/clang/ohos/linux-x86_64/llvm/include/libcxx-ohos/include/c++/v1",
  "//out/${OHOS_PRODUCT_NAME}/obj/third_party/musl/usr/include/${TARGET_ARCH}-linux-ohos",
]

# C/CXX Build flags
cflags = []
cflags_c = []
cflags_cc = []
asmflags = []
ldflags = []
defines = []
if (BUILD_TYPE == "Release") {
  cflags_cc = [
    "-D_FORTIFY_SOURCE=2",
    "-O2",
  ]
  cflags_c = [
    "-D_FORTIFY_SOURCE=2",
    "-O2",
  ]
  defines += ["_FORTIFY_SOURCE=2"]
} else if (BUILD_TYPE == "Debug") {
  cflags_cc = [
    "-O0",
  ]
  cflags_c = [
    "-O0",
  ]
  defines += ["MRT_DEBUG=1"]
} else if (BUILD_TYPE == "RelWithDebInfo") {
  cflags_cc = [
    "-D_FORTIFY_SOURCE=2",
    "-O2",
  ]
  cflags_c = [
    "-D_FORTIFY_SOURCE=2",
    "-O2",
  ]
  defines += ["_FORTIFY_SOURCE=2"]
}

# Basic compilation options
base_options = [
  "-Wno-unused-command-line-argument",
  "-fno-omit-frame-pointer",
  "-fvisibility=default",
  "-fno-exceptions",
  "-fno-rtti",
  "-ffunction-sections",
  "-Wall",
  "-fPIC",
  "-Werror",
  "-Wunused-variable",
  "-Wfloat-equal",
  "-Wextra",
  "-Wno-unused-parameter",
  "-Wno-sign-compare",
  "-Wframe-larger-than=10240",
  "-gdwarf-4",
  "-Wdeprecated-copy",
  "-fno-strict-aliasing",
  "--param=ssp-buffer-size=4",
  "-Wno-builtin-macro-redefined",
  "-D__DATE__=",
  "-D__TIME__=",
  "-D__TIMESTAMP__=",
  "-funwind-tables",
  "-fcolor-diagnostics",
  "-fmerge-all-constants",
  "-Xclang",
  "-mllvm",
  "-Xclang",
  "-instcombine-lower-dbg-declare=0",
  "-no-canonical-prefixes",
  "-fno-short-enums",
  "-Wthread-safety",
  "-Wno-missing-field-initializers",
  "-Wno-error=deprecated-copy",
  "-Wno-unneeded-internal-declaration",
  "-Wno-error=c99-designator",
  "-Wno-error=anon-enum-enum-conversion",
  "-Wno-error=implicit-fallthrough",
  "-Wno-error=sizeof-array-div",
  "-Wno-error=reorder-init-list",
  "-Wno-error=range-loop-construct",
  "-Wno-error=implicit-int-float-conversion",
  "-Wno-error=inconsistent-dllimport",
  "-Wno-error=unknown-warning-option",
  "-Wno-error=abstract-final-class",
  "-Wno-error=int-in-bool-context",
  "-Wno-error=xor-used-as-pow",
  "-Wno-error=return-stack-address",
  "-Wno-error=dangling-gsl",
  "-Wno-undefined-var-template",
  "-Wno-nonportable-include-path",
  "-Wno-user-defined-warnings",
  "-Wno-unused-lambda-capture",
  "-Wno-null-pointer-arithmetic",
  "-Wno-enum-compare-switch",
  "-fdata-sections",
  "-flto",
  "-fsanitize=cfi",
  "-fno-sanitize=cfi-nvcall,cfi-icall",
  "-mbranch-protection=pac-ret",
  "-g2",
  "-ggnu-pubnames",
  "-fno-common",
  "-Wheader-hygiene",
  "-Wstring-conversion",
  "-Wtautological-overlap-compare",
  "-Wl,--allow-shlib-undefined",
  "-fno-emulated-tls",
]

if (OHOS_FLAG == 1) {
  base_options += [
    "--target=aarch64-linux-ohos",
  ]
} else if (OHOS_FLAG == 2) {
  base_options += [
    "--target=x86_64-linux-ohos",
  ]
}

cflags_c += base_options
cflags_cc += base_options

cflags_c += ["-std=c99"]

# Runtime compilation definitions
defines += [
  "__OHOS__",
  "MRT_USE_CJTHREAD",
  "MRT_USE_CJTHREAD_RENAME",
  "COMPILE_DYNAMIC",
]

if (DISABLE_VERSION_CHECK == 1) {
  defines += [
    "DISABLE_VERSION_CHECK",
  ]
}

if (CJ_SDK_VERSION != "") {
  defines += [
    "CJ_SDK_VERSION=\"${CJ_SDK_VERSION}\"",
  ]
}

if (COPYGC_FLAG == 1) {
  defines += [
    "MRT_USE_COPYGC",
  ]
}

if (ASAN_SUPPORT_FLAG == 1) {
  defines += [
    "CANGJIE_ASAN_SUPPORT",
    "GENERAL_ASAN_SUPPORT_INTERFACE",
  ]
} else if (HWASAN_SUPPORT_FLAG == 1) {
  defines += [
    "CANGJIE_HWASAN_SUPPORT",
    "GENERAL_ASAN_SUPPORT_INTERFACE",
  ]
} else if (GWPASAN_SUPPORT_FLAG == 1) {
  defines += [
    "CANGJIE_GWPASAN_SUPPORT",
  ]
}

if (SANITIZER_SUPPORT_FLAG == 1) {
  defines += [
    "CANGJIE_SANITIZER_SUPPORT",
  ]
}

ptrauth_cfi_defines = []
if (RUNTIME_FORWARD_PTRAUTH_CFI == 1) {
  ptrauth_cfi_defines += [
    "ENABLE_FORWARD_PTRAUTH_CFI",
  ]
}
if (RUNTIME_BACKWARD_PTRAUTH_CFI == 1) {
  ptrauth_cfi_defines += [
    "ENABLE_BACKWARD_PTRAUTH_CFI",
  ]
}
defines += ptrauth_cfi_defines

asmflags = cflags_cc
ldflags = cflags_cc

LINK_PATH = rebase_path("${OHOS_INCLUDE_DIR}/lib/${TARGET_ARCH}-linux-ohos")
ldflags += [
  "-L${LINK_PATH}",
]