cmake_minimum_required(VERSION 3.10)

# Project name
project({{meta.optimizer_name}})

# Build the generated example as C99.
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Resolve the generated static library name for the current platform.
if(WIN32)
  set(OPEN_STATIC_LIB ${CMAKE_CURRENT_SOURCE_DIR}/target/{{ build_config.build_mode }}/{{meta.optimizer_name}}.lib)
else()
  set(OPEN_STATIC_LIB ${CMAKE_CURRENT_SOURCE_DIR}/target/{{ build_config.build_mode }}/lib{{meta.optimizer_name}}.a)
endif()

find_package(Threads REQUIRED)

# Add the executable
add_executable(optimizer example_optimizer.c)

# Add libraries to the executable
target_link_libraries(
  optimizer
  PRIVATE
    ${OPEN_STATIC_LIB}
    Threads::Threads
)

if(UNIX)
  target_link_libraries(optimizer PRIVATE m)
endif()

if(CMAKE_DL_LIBS)
  target_link_libraries(optimizer PRIVATE ${CMAKE_DL_LIBS})
endif()

if(WIN32)
  # Rust static libraries built with the MSVC toolchain depend on a small set
  # of Windows system libraries that must be linked by the final C executable.
  target_link_libraries(
    optimizer
    PRIVATE
      advapi32
      bcrypt
      kernel32
      ntdll
      userenv
      ws2_32
  )
endif()

add_custom_target(run
    COMMAND $<TARGET_FILE:optimizer>
    DEPENDS optimizer
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
