cmake_minimum_required(VERSION 3.16)
project(rhash-cpu C CXX ASM)

option(CF_NATIVE "Optimize for the build machine (-march=native)" ON)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 11)

if(CF_NATIVE)
  set(CF_ARCH_FLAGS "-march=native")
  set(ARCH "native" CACHE STRING "" FORCE)  # RandomX library
else()
  # Portable baseline that still has AES-NI / PCLMUL / AVX2 (Haswell+, Zen+)
  set(CF_ARCH_FLAGS "-march=x86-64-v3 -maes -mpclmul")
endif()

set(CMAKE_C_FLAGS_RELEASE   "-O3 -fno-plt ${CF_ARCH_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fno-plt ${CF_ARCH_FLAGS}")

# ---- RandomX (tevador, BSD-3) — allocator patched to use rhash-cpu huge-page hooks
add_subdirectory(third_party/randomx EXCLUDE_FROM_ALL)

find_package(OpenSSL REQUIRED)
find_package(Threads REQUIRED)

set(CF_SOURCES
  src/main.cpp
  src/util/log.cpp
  src/util/status.cpp
  src/util/shares.cpp
  src/core/poolbook.cpp
  src/core/license.cpp
  src/ui/dash.cpp
  src/ui/sysmon.cpp
  src/core/cpu.cpp
  src/core/memory.cpp
  src/core/msr.cpp
  src/miner/job.cpp
  src/miner/miner.cpp
  src/net/conn.cpp
  src/net/pool.cpp
  src/net/stratum_cn.cpp
  src/net/fee.cpp
  src/algo/factory.cpp
  src/algo/randomx/rx.cpp
  src/miner/btc.cpp
  src/net/stratum_btc.cpp
  src/algo/yespower/yp.cpp
  src/algo/verus/verus.cpp
  src/net/stratum_verus.cpp
  src/algo/ghostrider/cn.cpp
  src/algo/ghostrider/gr.cpp
  src/algo/ghostrider/gr_test.cpp
)

# ---- GhostRider deps: sphlib 512-bit hashes + CryptoNight finalizers (MIT / public domain)
file(GLOB CF_SPH third_party/sph/*.c)
list(FILTER CF_SPH EXCLUDE REGEX "(md_helper|aes_helper|sph_hamsi_helper)\\.c$")
add_library(cf_sph STATIC ${CF_SPH})
target_include_directories(cf_sph PUBLIC third_party/sph)
target_compile_options(cf_sph PRIVATE -w -fno-strict-aliasing)
add_library(cf_cnextra STATIC third_party/cnextra/c_blake256.c third_party/cnextra/c_groestl.c
  third_party/cnextra/c_jh.c third_party/cnextra/c_skein.c third_party/cnextra/c_keccak.c)
target_include_directories(cf_cnextra PUBLIC third_party/cnextra)
target_compile_options(cf_cnextra PRIVATE -w -fno-strict-aliasing)

# ---- VerusHash 2.2 core (Verus, MIT / Apache-2.0 clhash)
add_library(cf_verus STATIC third_party/verus/haraka.c third_party/verus/verus_clhash_sv22.c)
target_include_directories(cf_verus PUBLIC third_party/verus)
target_compile_options(cf_verus PRIVATE -w)

# ---- yespower (Openwall) — SIMD-optimized implementation + SHA-256
add_library(cf_yespower STATIC third_party/yespower/yespower-opt.c third_party/yespower/sha256.c)
target_include_directories(cf_yespower PUBLIC third_party/yespower)
target_compile_options(cf_yespower PRIVATE -w -fomit-frame-pointer)

add_executable(rhash-cpu ${CF_SOURCES})
target_include_directories(rhash-cpu PRIVATE src third_party/json third_party/randomx/src third_party/sph third_party/cnextra)
target_compile_options(rhash-cpu PRIVATE -Wall -Wextra -Wno-unused-parameter)
target_compile_definitions(rhash-cpu PRIVATE CF_HAVE_BITCOIN_STRATUM=1 CF_HAVE_VERUS_STRATUM=1)
target_link_libraries(rhash-cpu PRIVATE cf_yespower cf_verus cf_sph cf_cnextra randomx OpenSSL::SSL OpenSSL::Crypto Threads::Threads)

install(TARGETS rhash-cpu RUNTIME DESTINATION bin)

# ---- rhash-keygen: makes license keys (author only; needs rhash-private.key, which is not in the source)
add_executable(rhash-keygen tools/keygen.cpp src/core/license.cpp)
target_include_directories(rhash-keygen PRIVATE src)
target_compile_definitions(rhash-keygen PRIVATE CF_KEYGEN)
target_link_libraries(rhash-keygen PRIVATE OpenSSL::Crypto)

# ---- unit tests
add_executable(rhash-cpu-tests tests/unit_tests.cpp src/miner/btc.cpp)
target_include_directories(rhash-cpu-tests PRIVATE src)
target_link_libraries(rhash-cpu-tests PRIVATE cf_yespower)

# haraka.h uses benign type-punning macros; silence that one warning for the file that includes it
set_source_files_properties(src/algo/verus/verus.cpp PROPERTIES COMPILE_OPTIONS "-Wno-strict-aliasing")
set_source_files_properties(src/algo/ghostrider/cn.cpp src/algo/ghostrider/gr.cpp PROPERTIES COMPILE_OPTIONS "-fno-strict-aliasing")
