CMAKE_MINIMUM_REQUIRED(VERSION 3.0...3.20)
PROJECT(TDengine)

INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc)
INCLUDE_DIRECTORIES(inc)

FIND_PACKAGE(Git)
IF (GIT_FOUND)
    MESSAGE("Git found")
    EXECUTE_PROCESS(
        COMMAND ${GIT_EXECUTABLE} log --pretty=oneline -n 1 ${CMAKE_CURRENT_LIST_DIR}
        WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
        RESULT_VARIABLE RESULT
        OUTPUT_VARIABLE TAOSDEMO_COMMIT_SHA1)
    IF ("${TAOSDEMO_COMMIT_SHA1}" STREQUAL "")
        SET(TAOSDEMO_COMMIT_SHA1 "unknown")
    ELSE ()
        STRING(SUBSTRING "${TAOSDEMO_COMMIT_SHA1}" 0 7 TAOSDEMO_COMMIT_SHA1)
        STRING(STRIP "${TAOSDEMO_COMMIT_SHA1}" TAOSDEMO_COMMIT_SHA1)
    ENDIF ()
    EXECUTE_PROCESS(
        COMMAND ${GIT_EXECUTABLE} status -z -s ${CMAKE_CURRENT_LIST_DIR}
        RESULT_VARIABLE RESULT
        OUTPUT_VARIABLE TAOSDEMO_STATUS)
    IF (TD_LINUX)
      EXECUTE_PROCESS(
        COMMAND bash "-c" "echo '${TAOSDEMO_STATUS}' | awk '{print $1}'"
        RESULT_VARIABLE RESULT
        OUTPUT_VARIABLE TAOSDEMO_STATUS)
    ENDIF (TD_LINUX)
ELSE()
    MESSAGE("Git not found")
    SET(TAOSDEMO_COMMIT_SHA1 "unknown")
    SET(TAOSDEMO_STATUS "unknown")
ENDIF (GIT_FOUND)


MESSAGE("taosdemo's latest commit in short is:" ${TAOSDEMO_COMMIT_SHA1})
STRING(STRIP "${TAOSDEMO_STATUS}" TAOSDEMO_STATUS)

IF (TAOSDEMO_STATUS MATCHES "M")
    SET(TAOSDEMO_STATUS "modified")
ELSE()
    SET(TAOSDEMO_STATUS "")
ENDIF ()
MESSAGE("taosdemo's status is:" ${TAOSDEMO_STATUS})

ADD_DEFINITIONS(-DTAOSDEMO_COMMIT_SHA1="${TAOSDEMO_COMMIT_SHA1}")
ADD_DEFINITIONS(-DTAOSDEMO_STATUS="${TAOSDEMO_STATUS}")

MESSAGE("TD_VER_NUMBER is:" ${TD_VER_NUMBER})
IF ("${TD_VER_NUMBER}" STREQUAL "")
    SET(TD_VERSION_NUMBER "TDengine-version-unknown")
ELSE()
    SET(TD_VERSION_NUMBER ${TD_VER_NUMBER})
ENDIF ()
MESSAGE("TD_VERSION_NUMBER is:" ${TD_VERSION_NUMBER})
ADD_DEFINITIONS(-DTD_VERNUMBER="${TD_VERSION_NUMBER}")

IF (TD_LINUX_64 AND JEMALLOC_ENABLED)
  ADD_DEFINITIONS(-DTD_JEMALLOC_ENABLED -I${CMAKE_BINARY_DIR}/build/include -L${CMAKE_BINARY_DIR}/build/lib -Wl,-rpath,${CMAKE_BINARY_DIR}/build/lib -ljemalloc)
  SET(LINK_JEMALLOC "-L${CMAKE_BINARY_DIR}/build/lib -ljemalloc")
ELSE ()
  SET(LINK_JEMALLOC "")
ENDIF ()

IF (TD_LINUX)
  AUX_SOURCE_DIRECTORY(./src SRC)
  ADD_EXECUTABLE(taosdemo ${SRC})

  IF (TD_SOMODE_STATIC)
      TARGET_LINK_LIBRARIES(taosdemo taos_static cJson lua ${LINK_JEMALLOC})
  ELSE ()
      TARGET_LINK_LIBRARIES(taosdemo taos cJson ${LINK_JEMALLOC})
  ENDIF ()
ELSEIF (TD_WINDOWS)
  AUX_SOURCE_DIRECTORY(./src SRC)
  ADD_EXECUTABLE(taosdemo ${SRC})
  SET_SOURCE_FILES_PROPERTIES(./src/demoUtil.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoData.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoInsert.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoCommandOpt.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoQuery.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoMain.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoSubscribe.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoOutput.c PROPERTIES COMPILE_FLAGS -w)
  SET_SOURCE_FILES_PROPERTIES(./src/demoJsonOpt.c PROPERTIES COMPILE_FLAGS -w)
  IF (TD_SOMODE_STATIC)
    TARGET_LINK_LIBRARIES(taosdemo taos_static cJson lua)
  ELSE ()
    TARGET_LINK_LIBRARIES(taosdemo taos cJson lua)
  ENDIF ()
ELSEIF (TD_DARWIN)
  # missing a few dependencies, such as <argp.h>
  AUX_SOURCE_DIRECTORY(./src SRC)
  ADD_EXECUTABLE(taosdemo ${SRC})
  
  IF (TD_SOMODE_STATIC)
    TARGET_LINK_LIBRARIES(taosdemo taos_static cJson lua)
  ELSE ()
    TARGET_LINK_LIBRARIES(taosdemo taos cJson lua)
  ENDIF ()
ENDIF ()

