project(HttpsDnsProxy C)
cmake_minimum_required(VERSION 2.8)

# FUNCTIONS

# source: https://stackoverflow.com/a/27990434
function(define_file_basename_for_sources targetname)
  get_target_property(source_files "${targetname}" SOURCES)
  foreach(sourcefile ${source_files})
    get_filename_component(basename "${sourcefile}" NAME)
    set_property(
      SOURCE "${sourcefile}" APPEND
      PROPERTY COMPILE_DEFINITIONS "__FILENAME__=\"${basename}\"")
  endforeach()
endfunction()

# CONFIG

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
  message(STATUS "Setting build type to '${CMAKE_BUILD_TYPE}' as none was specified.")
endif()

#set(CMAKE_C_FLAGS "-Wall -Wextra --pedantic -Wno-strict-aliasing -Wno-variadic-macros")
#set(CMAKE_C_FLAGS_DEBUG "-g -DDEBUG")
#set(CMAKE_C_FLAGS_RELEASE "-O2")

# VERSION
set(GIT_VERSION "UNKNOWN")
message(WARNING "Could not find git command! Version is set to: ${GIT_VERSION}")

# LIBRARY DEPENDENCIES

include_directories(
  src)

# BUILD

# The main binary
set(TARGET_NAME "https_dns_proxy")
aux_source_directory(src SRC_LIST)
set(SRC_LIST ${SRC_LIST})
add_executable(${TARGET_NAME} ${SRC_LIST})
set(LIBS ${LIBS} cares curl ev resolv)
target_link_libraries(${TARGET_NAME} ${LIBS})

define_file_basename_for_sources("https_dns_proxy")

set_source_files_properties(
  src/main.c src/options.c
  PROPERTIES COMPILE_FLAGS "-DGIT_VERSION='\"${GIT_VERSION}\"'")


# INSTALL

install(TARGETS ${TARGET_NAME} DESTINATION bin)