cmake_minimum_required(VERSION 3.5)
project(nanitevm)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 11)

set(CMAKE_C_FLAGS "-g3 -Wall -Wextra -Wno-unused-parameter")

add_definitions(-DSB_HAVE_DWORD_MUL -DPLATFORM_HAS_INSTRUCTION_HOOK)

option(platform "Platform" nanite)

if(${platform} STREQUAL "nanite")
add_definitions(-DUSE_BRIDGE)
add_definitions(-DUSE_OPAL)
add_definitions(-DUSE_SIDE_CHANNEL)
endif()

set(SOURCE_FILES
        ../sweet-b/src/sb_fe.c
        ../sweet-b/src/sb_fe.h
        ../sweet-b/src/sb_hmac_drbg.c
        ../sweet-b/include/sb_hmac_drbg.h
        ../sweet-b/src/sb_hmac_sha256.c
        ../sweet-b/include/sb_hmac_sha256.h
        ../sweet-b/src/sb_sha256.c
        ../sweet-b/include/sb_sha256.h
        ../sweet-b/include/sb_sw_context.h
        ../sweet-b/src/sb_sw_curves.h
        ../sweet-b/src/sb_sw_lib.c
        ../sweet-b/include/sb_sw_lib.h
        ../sweet-b/include/sb_types.h
        ../sweet-b/include/sb_hkdf.h
        ../sweet-b/src/sb_hkdf.c
        ../crypto/aes_ecb_128.h
        ../crypto/aes_ecb_128.c
        ../crypto/ctc.h
        ../crypto/ctc.c
        classes.h
        configure.h
        constants.h
        exceptions.c
        exceptions.h
        exit_trace.h
        interpreter.c
        interpreter.h
        language.c
        language.h
        load.c
        load.h
        math.c
        memory.c
        memory.h
        nativeemul.c
        object.c
        opcodes.h
        op_arithmetic.h
        op_arrays.h
        op_control.h
        op_conversions.h
        op_locals.h
        op_logical.h
        op_methods.h
        op_objects.h
        op_other.h
        op_skip.h
        op_stack.h
        op_unused.h
        platform_posix_time.c
        platform_config.h
        platform_crypt.c
        platform_hooks.h
        poll.c
        poll.h
        run.c
        run.h
        stack.h
        state.c
        state.h
        threads.c
        threads.h
        trace.h
        platform_posix.c
        types.h)

include_directories(.)
include_directories(../crypto/)
include_directories(../sweet-b/include)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/specialclasses.h
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/special/dbtoh.sh class ${CMAKE_CURRENT_SOURCE_DIR}/special/${platform}/specialclasses.db ${CMAKE_CURRENT_BINARY_DIR}/specialclasses.h
        MAIN_DEPENDENCY special/${platform}/specialclasses.db
)

add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/specialsignatures.h
        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/special/dbtoh.sh signature ${CMAKE_CURRENT_SOURCE_DIR}/special/${platform}/specialsignatures.db ${CMAKE_CURRENT_BINARY_DIR}/specialsignatures.h
        MAIN_DEPENDENCY special/${platform}/specialsignatures.db
)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
    add_link_options("LINKER:-pagezero_size,0x1000")
endif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")

add_executable(nanitevm ${SOURCE_FILES} specialclasses.h specialsignatures.h)

if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    target_link_libraries(${PROJECT_NAME} m rt)
endif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")

