# 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")
import("../runtime_config.gni")

source_Base = [
  "Base/Log.cpp",
  "Base/SysCall.cpp",
  "Base/CString.cpp",
  "Base/FixedCString.cpp",
  "Base/TimeUtils.cpp",
  "Base/LogFile.cpp",
  "Base/MemUtils.cpp",
]
source_Common = [
  "Common/BaseObject.cpp",
  "Common/RunType.cpp",
  "Common/PageCache.cpp",
  "Common/ThreadCache.cpp",
  "Common/CentralCache.cpp",
  "Common/NativeAllocator.cpp",
]
source_Concurrency = [
  "Concurrency/Concurrency.cpp",
  "Concurrency/CJThreadModel/CJThreadModel.cpp",
  "Concurrency/CJThreadModel/Cki.cpp",
]
source_CupProfiler = [
  "CpuProfiler/CpuProfiler.cpp",
  "CpuProfiler/SamplesRecord.cpp",
]
source_Expection = [
  "Exception/Exception.cpp",
  "Exception/EhTable.cpp",
  "Exception/ExceptionCApi.cpp",
]
source_Heap = [
  "Heap/GcThreadPool.cpp",
  "Heap/Heap.cpp",
  "Heap/Allocator/Allocator.cpp",
  "Heap/Allocator/CartesianTree.cpp",
  "Heap/Allocator/MemMap.cpp",
  "Heap/Allocator/RegionManager.cpp",
  "Heap/Allocator/RegionSpace.cpp",
  "Heap/Barrier/Barrier.cpp",
  "Heap/Collector/GcRequest.cpp",
  "Heap/Collector/GcStats.cpp",
  "Heap/Collector/Collector.cpp",
  "Heap/Collector/CollectorProxy.cpp",
  "Heap/Collector/CollectorResources.cpp",
  "Heap/Collector/TracingCollector.cpp",
  "Heap/Collector/TaskQueue.cpp",
  "Heap/Collector/FinalizerProcessor.cpp",
  "Heap/Collector/CopyCollector.cpp",
  "Heap/Collector/LiveInfo.cpp",
  "Heap/Collector/ForwardDataManager.cpp",
  "Heap/WCollector/IdleBarrier.cpp",
  "Heap/WCollector/EnumBarrier.cpp",
  "Heap/WCollector/TraceBarrier.cpp",
  "Heap/WCollector/PostTraceBarrier.cpp",
  "Heap/WCollector/PreforwardBarrier.cpp",
  "Heap/WCollector/ForwardBarrier.cpp",
  "Heap/WCollector/WCollector.cpp"
]
source_Inspector = [
  "Inspector/ProfilerAgentImpl.cpp",
  "Inspector/HeapSnapshotJsonSerializer.cpp",
  "Inspector/FileStream.cpp",
  "Inspector/CjHeapData.cpp",
  "Inspector/CjAllocData.cpp",
]
source_Loader = [
  "Loader/ILoader.cpp",
  "Loader/BinaryFile/BaseFile.cpp",
  "Loader/BinaryFile/CjFile/CjFile.cpp",
  "Loader/CjSemanticVersion/CjSemanticVersion.cpp",
  "Loader/CjFileLoader/CjFileLoader.cpp",
]
source_Mutator = [
  "Mutator/Mutator.cpp",
  "Mutator/SatbBuffer.cpp",
  "Mutator/MutatorManager.cpp",
  "Mutator/ThreadLocal.cpp",
]
source_ObjectModel = [
  "ObjectModel/MObject.cpp",
  "ObjectModel/MClass.cpp",
  "ObjectModel/MArray.cpp",
  "ObjectModel/MFunc.cpp",
  "ObjectModel/PackageInfo.cpp",
  "ObjectModel/FieldInfo.cpp",
  "ObjectModel/MethodInfo.cpp",
]

source_os = [
  "os/Linux/Loader.cpp",
  "os/Linux/Path.cpp",
]

source_Signal = [
  "Signal/SignalStack.cpp",
  "Signal/SignalUtils.cpp",
]
source_Sync = [
  "Sync/Sync.cpp",
]
source_UnwindStack = [
  "UnwindStack/StackInfo.cpp",
  "UnwindStack/EhStackInfo.cpp",
  "UnwindStack/GcStackInfo.cpp",
  "UnwindStack/StackGrowStackInfo.cpp",
  "UnwindStack/PrintStackInfo.cpp",
  "UnwindStack/PrintSignalStackInfo.cpp",
  "UnwindStack/MachineFrame.cpp",
  "UnwindStack/UnwindContext.cpp",
  "UnwindStack/UnwindCApi.cpp",
  "UnwindStack/FrameInfo.cpp",
  "UnwindStack/MangleNameHelper.cpp",
  "UnwindStack/StackMetadataHelper.cpp",
  "Demangler/Demangler.cpp",
  "Demangler/DeCompression.cpp",
]
include_Sanitzer = [
  "Sanitizer",
]
if (ASAN_SUPPORT_FLAG == 1) {
  source_Sanitizer = [
    "Sanitizer/AddressSanitizer/AsanInterface.cpp",
  ]
  include_Sanitzer += [
    "Sanitizer/AddressSanitizer",
  ]
} else if (HWASAN_SUPPORT_FLAG == 1) {
  source_Sanitizer = [
    "Sanitizer/HwAddressSanitizer/HwasanInterface.cpp",
  ]
  include_Sanitzer += [
    "Sanitizer/HwAddressSanitizer",
  ]
} else if (GWPASAN_SUPPORT_FLAG == 1) {
  source_Sanitizer = [
    "Sanitizer/GwpAddressSanitizer/GwpAsanInterface.cpp",
  ]
  include_Sanitzer += [
    "Sanitizer/GwpAddressSanitizer",
  ]
} else {
  source_Sanitizer = []
}

EXPORT_MAP_PATH = rebase_path("${CANGJIE_RUNTIME_PATH}/src/ohos_local_export.map")

ohos_shared_library("libcangjie-runtime") {
  cflags += []
  cflags_c += []
  cflags_cc += []
  asmflags += []
  ldflags += []
  defines += []
  sources = []
  sources += source_Base
  sources += source_Common
  sources += source_Concurrency
  sources += source_CupProfiler
  sources += source_Expection
  sources += source_Heap
  sources += source_Inspector
  sources += source_Loader
  sources += source_Mutator
  sources += source_ObjectModel
  sources += source_os
  sources += source_Sync
  sources += source_UnwindStack
  sources += source_Sanitizer
  sources += source_Signal
  sources +=  [
    "CompilerCalls.cpp",
    "ExceptionManager.cpp",
    "StackManager.cpp",
    "HeapManager.cpp",
    "LoaderManager.cpp",
    "TypeInfoManager.cpp",
    "LogManager.cpp",
    "SignalManager.cpp",
    "CangjieRuntime.cpp",
    "CjScheduler.cpp",
    "CjTimer.cpp",
    "CangjieRuntimeApi.cpp",
    "arch/${TARGET_ARCH}_linux/N2CStub.S",
    "arch/${TARGET_ARCH}_linux/C2NStub.S",
    "arch/${TARGET_ARCH}_linux/CalleeSavedStub.S",
    "arch/${TARGET_ARCH}_linux/StackGrowStub.S",
    "arch/${TARGET_ARCH}_linux/RestoreContextForEH.S",
    "arch/${TARGET_ARCH}_linux/HandleSafepointStub.S",
  ]

  include_dirs += [
    "Demangler",
    "Heap",
    "Loader",
    "Loader/BinaryFile",
    "Loader/BinaryFile/CjFile",
    "Loader/CjFileLoader",
    "CJThread/src/base/external/include",
  ]
  include_dirs += include_Sanitzer

  deps = [
    "./CJThread:libcangjie-thread",
  ]

  external_deps = [
    "bounds_checking_function:libsec_shared"
  ]

  ldflags += [
    "-rdynamic",
    "-Wl,-z,relro",
    "-Wl,-z,now",
    "-Wl,-z,noexecstack",
    "-Wl,--gc-sections",
    "-Wl,--version-script=${EXPORT_MAP_PATH}",
  ]

  libs = [
    "unwind",
    "clang_rt.builtins",
    "hilog_ndk.z",
    "hitrace_ndk.z",
  ]

  output_extension = "so"
  subsystem_name = "thirdparty"
  part_name = "cangjie_runtime"
  relative_install_dir = "platformsdk/cjsdk/runtime"
}

ohos_static_library("libcangjie-runtime-static") {
  cflags += []
  cflags_c += []
  cflags_cc += []
  asmflags += []
  ldflags += []
  defines += []
  sources = []
  sources += source_Base
  sources += source_Common
  sources += source_Concurrency
  sources += source_CupProfiler
  sources += source_Expection
  sources += source_Heap
  sources += source_Inspector
  sources += source_Loader
  sources += source_Mutator
  sources += source_ObjectModel
  sources += source_os
  sources += source_Sync
  sources += source_UnwindStack
  sources += source_Sanitizer
  sources += source_Signal
  sources +=  [
    "CompilerCalls.cpp",
    "ExceptionManager.cpp",
    "StackManager.cpp",
    "HeapManager.cpp",
    "LoaderManager.cpp",
    "TypeInfoManager.cpp",
    "LogManager.cpp",
    "SignalManager.cpp",
    "CangjieRuntime.cpp",
    "CjScheduler.cpp",
    "CjTimer.cpp",
    "CangjieRuntimeApi.cpp",
    "arch/${TARGET_ARCH}_linux/N2CStub.S",
    "arch/${TARGET_ARCH}_linux/C2NStub.S",
    "arch/${TARGET_ARCH}_linux/CalleeSavedStub.S",
    "arch/${TARGET_ARCH}_linux/StackGrowStub.S",
    "arch/${TARGET_ARCH}_linux/RestoreContextForEH.S",
    "arch/${TARGET_ARCH}_linux/HandleSafepointStub.S",
  ]

  include_dirs += [
    "Demangler",
    "Heap",
    "Loader",
    "Loader/BinaryFile",
    "Loader/BinaryFile/CjFile",
    "Loader/CjFileLoader",
    "CJThread/src/base/external/include",
  ]
  include_dirs += include_Sanitzer

  deps = [
    "./CJThread:libcangjie-thread",
  ]

  external_deps = [
    "bounds_checking_function:libsec_static"
  ]

  subsystem_name = "thirdparty"
  part_name = "cangjie_runtime"
}