CMAKE_MINIMUM_REQUIRED(VERSION 3.16.3)
PROJECT(TrackpointApp VERSION 0.0.1 LANGUAGES CXX)

# Include modules
INCLUDE(FetchContent)

# Build TrackpointApp
# Set C++ mode
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED True)

SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -Wunreachable-code -Wno-register")
SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wno-register")

SET(CMAKE_AUTOUIC ON)
SET(CMAKE_AUTOMOC ON)
SET(CMAKE_AUTORCC ON)

SET(CMAKE_INCLUDE_CURRENT_DIR ON)

ADD_COMPILE_DEFINITIONS(GL_SILENCE_DEPRECATION)

# Qt
LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}/thirdparty/qt/install")
FIND_PACKAGE(Qt6 COMPONENTS Core Gui Widgets OpenGLWidgets REQUIRED NO_CMAKE_SYSTEM_PATH)

# Json
INCLUDE(thirdparty/json.cmake)

# Development build: dynamic link
if(NOT BUILD_STATIC_RELEASE)
  # OpenSceneGraph
  INCLUDE(thirdparty/openscenegraph.cmake)

  # lib3mf
  INCLUDE(thirdparty/lib3mf.cmake)
ENDIF()

# Release build: static link
if(BUILD_STATIC_RELEASE)
  ADD_DEFINITIONS(-DINCLUDE_PLUGINS)

  # OpenSceneGraph
  LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}/thirdparty/openscenegraph/install")
  FIND_PACKAGE(OpenSceneGraph REQUIRED COMPONENTS osgViewer osgDB osgGA osgText osgUtil)

  # lib3mf
  LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}/thirdparty/lib3mf/install")
  FIND_PACKAGE(Lib3MF REQUIRED)
ENDIF()

# The executable we want to build
QT_ADD_EXECUTABLE(TrackpointApp
  src/main.cpp
  src/MainWindow.cpp
  include/MainWindow.hpp
  gui/MainWindow.ui
  src/NoMeshWidget.cpp
  include/NoMeshWidget.hpp
  gui/NoMeshWidget.ui
  src/EditWidget.cpp
  include/EditWidget.hpp
  gui/EditWidget.ui
  include/OSGWidget.hpp
  src/OSGWidget.cpp
  src/ProjectStore.cpp
  src/PickHandler.cpp
  src/TrackPoint.cpp
  src/OptiTrackPoint.cpp
  src/ActionPoint.cpp
  src/SteamVRTrackPoint.cpp
  src/OpenScadRenderer.cpp
  src/StringBasics.cpp
  src/TrackPointRenderer.cpp
  src/PointShape.cpp
  src/HudCallback.cpp
  src/STLImport.cpp
  src/MeshTools.cpp
)

if(NOT BUILD_STATIC_RELEASE)
  INCLUDE_DIRECTORIES(
    ${${OPENSCENEGRAPH_PREFIX}_SOURCE_DIR}/include
    ${${OPENSCENEGRAPH_PREFIX}_BINARY_DIR}/include
    ${${LIB3MF_PREFIX}_BINARY_DIR}/Autogenerated/Bindings/Cpp
  )

  TARGET_LINK_LIBRARIES(TrackpointApp PRIVATE
    osg osgViewer osgDB osgGA osgText osgUtil
    lib3mf
  )
ENDIF()

if(BUILD_STATIC_RELEASE)
  INCLUDE_DIRECTORIES(
    ${OPENSCENEGRAPH_INCLUDE_DIRS}
    ${LIB3MF_INCLUDE_DIRS}
  )

  TARGET_LINK_LIBRARIES(TrackpointApp PRIVATE
    ${OPENSCENEGRAPH_LIBRARIES}
    ${LIB3MF_LIBRARIES}
  )
ENDIF()

INCLUDE_DIRECTORIES(
  ${${JSON_PREFIX}_SOURCE_DIR}/include
  ${CMAKE_CURRENT_LIST_DIR}/thirdparty/stl_reader/install
  include
  gui
  resources
)

TARGET_LINK_LIBRARIES(TrackpointApp PRIVATE
  nlohmann_json::nlohmann_json
  Qt6::Core
  Qt6::Gui
  Qt6::Widgets
  Qt6::OpenGLWidgets
)

# Header to have version number available in the code
CONFIGURE_FILE(include/trackpointapp.hpp.in include/trackpointapp.hpp)
