# GPU Framework version 14.0.0

cmake_minimum_required(VERSION 3.9.0)
option(BUILD_SHARED_LIBS "Build shared libraries (DLLs)." ON)

# check if the externally dependent library exist, report an error if not.
# ShaderFilesGenerator depends on Utils
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../Utils/CMakeLists.txt")
  # message(STATUS "Externally needed header found")
  set(Utils_dir ../Utils)
else()
  message(STATUS FATAL_ERROR "External needed Utils library is not found. ShaderFilesGenerator can not be built.")
endif()

project(ShaderFilesGenerator CXX)

# set the module path so include can find modules
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Scripts/CMake")

# CMAKE_MODULE_PATH is set, so include looks for modules in that directory
include(MacroUtils)

# setup the compiler & linker environment
setupCompilerAndLinker()

if (NOT GLEW_FOUND)
  # setup the build preprocessor environment
  setupGPUFrameworkPreprocessorDefines()

  # setup all external libraries
  setupGPUFrameworkExternalLibraries()
endif()

# if Utils is not built (stand alone case), then build it.
if (NOT TARGET Utils)
  #When specifying an out-of-tree source a binary directory must be explicitly specified,
  add_subdirectory(${Utils_dir} ${CMAKE_BINARY_DIR}/Utils)
endif()

include(include/CMakeLists.txt)
include(src/CMakeLists.txt)

#########################################################################
##
## Defines the ShaderFilesGenerator libraries.

add_executable(ShaderFilesGenerator
  ${SHADER_FILES_GENERATOR_SRC_CPP} ${SHADER_FILES_GENERATOR_HEADER}
  ${OPENGL_RENDERING_ENGINE_HEADER_SHADER_FILES_GENERATOR}
  )

if (WIN32)
  target_link_libraries(ShaderFilesGenerator Utils)
elseif (UNIX)
  # stdc++fs is for #include <experimental/filesystem>
  target_link_libraries(ShaderFilesGenerator Utils stdc++fs)
endif()

# for both internal build and parent build (ie being pulled in via add_subdirectory() rather than being a standalone build)
target_include_directories(
  ShaderFilesGenerator PUBLIC
  ${GLEW_INCLUDE_HEADERS}
  ${ShaderFilesGenerator_SOURCE_DIR}/include
  ${ShaderFilesGenerator_SOURCE_DIR}/../Utils/include
  ${ShaderFilesGenerator_SOURCE_DIR}/../OpenGLRenderingEngine/include
  )

# create the ShaderFilesGenerator folder for ShaderFilesGenerator execution
if (NOT EXISTS ${CMAKE_BINARY_DIR}/ShaderFilesGenerator)
  file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/ShaderFilesGenerator)
endif()
set(SHADER_FILES_GENERATOR_BUILD_DIR ${CMAKE_BINARY_DIR}/ShaderFilesGenerator)

# this moves ShaderFilesGenerator executable into ShaderFilesGenerator folder -> first line is for all the platforms
set_target_properties(ShaderFilesGenerator PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${SHADER_FILES_GENERATOR_BUILD_DIR})
if (MSVC)
  set_target_properties(ShaderFilesGenerator PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG   ${SHADER_FILES_GENERATOR_BUILD_DIR})
  set_target_properties(ShaderFilesGenerator PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${SHADER_FILES_GENERATOR_BUILD_DIR})
  # etc for the other available configuration types (MinSizeRel, RelWithDebInfo)
endif()

# copy Utils.dll into ShaderFilesGenerator folder for ShaderFilesGenerator execution
if (WIN32)
  add_custom_command(
    TARGET ShaderFilesGenerator
    POST_BUILD
    COMMAND ${CMAKE_COMMAND}
    #$<TARGET_FILE:Utils> is the path of Utils target
    ARGS -E copy $<TARGET_FILE:Utils> "${CMAKE_BINARY_DIR}/ShaderFilesGenerator"
    )
endif()

# copy the dummy AllGLSLShaderFiles related .h and .cpp files to relevant ShaderFilesGenerator directories only if they don't exit (first CMake run of the ShaderFilesGenerator project)
if (NOT EXISTS ${ShaderFilesGenerator_SOURCE_DIR}/../ShaderFilesGenerator/include/AllGLSLShaderFiles.h)
  configure_file(${ShaderFilesGenerator_SOURCE_DIR}/../Scripts/AllGLSLShaderFiles.in ${ShaderFilesGenerator_SOURCE_DIR}/../ShaderFilesGenerator/include/AllGLSLShaderFiles.h COPYONLY)
endif()
if (NOT EXISTS ${ShaderFilesGenerator_SOURCE_DIR}/../ShaderFilesGenerator/src/AllGLSLShaderFiles.cpp)
  configure_file(${ShaderFilesGenerator_SOURCE_DIR}/../Scripts/AllGLSLShaderFiles.in ${ShaderFilesGenerator_SOURCE_DIR}/../ShaderFilesGenerator/src/AllGLSLShaderFiles.cpp   COPYONLY)
endif()