#  Copyright (C) 2026 ChiPass Team <contact@chipass.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 or (at your option)
#  version 3 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Alloc must be defined in a static library to prevent clashing with Clang ASAN definitions.
add_library(ChiPass_alloc STATIC
  Alloc.cpp
)
target_link_libraries(ChiPass_alloc PRIVATE
  PkgConfig::botan
  Qt6::Core
)

chipass_add_component(core)

target_sources(ChiPass_core PRIVATE
  AutoTypeAssociations.cpp
  Base32.cpp
  Bootstrap.cpp
  Clock.cpp
  Config.cpp
  CustomData.cpp
  Database.cpp
  DatabaseStats.cpp
  Entry.cpp
  EntryAttachments.cpp
  EntryAttributes.cpp
  EntrySearcher.cpp
  FileWatcher.cpp
  Group.cpp
  HibpOffline.cpp
  InactivityTimer.cpp
  Merger.cpp
  Metadata.cpp
  ModifiableObject.cpp
  PasswordGenerator.cpp
  PasswordHealth.cpp
  PassphraseGenerator.cpp
  Resources.cpp
  SignalMultiplexer.cpp
  TimeDelta.cpp
  TimeInfo.cpp
  Tools.cpp
  Totp.cpp
  Translator.cpp
  UrlTools.cpp
  Utils.cpp
  TextStream.cpp

  ${CMAKE_SOURCE_DIR}/share/wordlists/wordlists.qrc
)
target_link_libraries(ChiPass_core PUBLIC
  ChiPass_alloc
)

if (APPLE)
  target_sources(ChiPass_core PRIVATE
    MacPasteboard.cpp
  )
endif()

if (CHIPASS_ENABLE_NETWORKING)
  target_sources(ChiPass_core PRIVATE
    HibpDownloader.cpp
    NetworkManager.cpp
  )
endif()


# Allocator feature tests
#
if (UNIX)
  check_cxx_source_compiles(
    [=[
      #include <malloc.h>
      int main() {
        malloc_usable_size(NULL);
        return 0;
      }
    ]=]
    HAVE_MALLOC_USABLE_SIZE
  )

  set(definitions
    $<$<BOOL:${HAVE_MALLOC_USABLE_SIZE}>:HAVE_MALLOC_USABLE_SIZE>
  )
  set_source_files_properties(Alloc.cpp PROPERTIES COMPILE_DEFINITIONS "${definitions}")
endif()


# Anti-debug feature tests
#
if (UNIX)
  check_cxx_source_compiles(
    [=[
      #include <sys/resource.h>
      int main() {
        struct rlimit limit;
        limit.rlim_cur = 0;
        limit.rlim_max = 0;
        setrlimit(RLIMIT_CORE, &limit);
        return 0;
      }
    ]=]
    HAVE_RLIMIT_CORE
  )

  check_cxx_source_compiles(
    [=[
      #include <sys/prctl.h>
      int main() {
        prctl(PR_SET_DUMPABLE, 0);
        return 0;
      }
    ]=]
    HAVE_PR_SET_DUMPABLE
  )

  check_cxx_source_compiles(
    [=[
      #include <sys/types.h>
      #include <sys/ptrace.h>
      int main() {
        ptrace(PT_DENY_ATTACH, 0, 0, 0);
        return 0;
      }
    ]=]
    HAVE_PT_DENY_ATTACH
  )

  set(definitions
    $<$<BOOL:${HAVE_RLIMIT_CORE}>:HAVE_RLIMIT_CORE>
    $<$<BOOL:${HAVE_PR_SET_DUMPABLE}>:HAVE_PR_SET_DUMPABLE>
    $<$<BOOL:${HAVE_PT_DENY_ATTACH}>:HAVE_PT_DENY_ATTACH>
  )
  set_source_files_properties(Bootstrap.cpp PROPERTIES COMPILE_DEFINITIONS "${definitions}")
endif()
