project('rtengine', ['c', 'cpp'],
  default_options: ['buildtype=debug', 
    'b_sanitize=none', 
    'c_std=c17', 
    'cpp_std=c++20',
    'warning_level=2',
    'werror=true',
    'b_vscrt=static_from_buildtype',
    'default_library=static',
    'cpp_rtti=false',
    'cpp_eh=none'])

compiler = meson.get_compiler('c')
buildtype = get_option('buildtype')

# Build options
if compiler.get_argument_syntax() == 'gcc'
  add_project_arguments(
    ['-Wno-sign-conversion',
      '-Wdouble-promotion', '-Wno-unused-function', '-Wno-unused-parameter',
      '-fms-extensions'],
    language : ['c', 'cpp']
    )
elif compiler.get_argument_syntax() == 'msvc'
  add_project_arguments(
    ['/wd4146', '/wd4245', '/wd4100', '/D_CRT_SECURE_NO_WARNINGS'],
    language:  ['c', 'cpp']
    )
  if buildtype == 'debug'
    add_project_arguments(['/RTCsu'], language : ['c', 'cpp'])
  endif
  # Allow nameless struct/unions (standard in C11)
  add_project_arguments(['/wd4201'], language : 'cpp')
endif

if get_option('default_library') == 'static'
  add_project_arguments(['-DRT_STATIC_LIB'], language : ['c', 'cpp'])
endif

if get_option('error_report_debugbreak')
  add_project_arguments(['-DRT_ERROR_REPORT_DEBUGBREAK'], language : ['c', 'cpp'])
endif

# Debug specific flags
if buildtype == 'debug' or buildtype == 'debugoptimized'
  add_project_arguments([ '-DRT_DEBUG'], language : ['c', 'cpp'])
endif

# OS Specific flags
if host_machine.system() == 'linux'
  add_project_arguments(['-D_GNU_SOURCE'], language : ['c', 'cpp'])
endif


# Handle Linux to Windows cross compilation.
# By default, mingw does not find its own windows headers...
if build_machine.system() == 'linux' and host_machine.system() == 'windows'
  message('Adding /usr/share/mingw-w64/include to the project include path.')
  add_project_arguments(['-isystem/usr/share/mingw-w64/include',
    '-DRT_CROSS_LINUX_WINDOWS',
    '-D_WIN32_WINNT=0x600',
    '-lgcc',
    '-municode'],
    language: ['c', 'cpp'],
    native: false)
endif

fs = import('fs')
cmake = import('cmake')

# Gather dependencies
thread_dep = dependency('threads')
m_dep = compiler.find_library('m', required : false)

# Subprojects installed via wraps
meshoptimizer_opts = cmake.subproject_options()
meshoptimizer_opts.add_cmake_defines({'CMAKE_POSITION_INDEPENDENT_CODE': true})
meshoptimizer_proj = cmake.subproject('meshoptimizer', options: meshoptimizer_opts)
meshoptimizer_dep = meshoptimizer_proj.get_variable('meshoptimizer_dep')

glfw_proj = subproject('glfw', default_options: ['default_library=shared', 'warning_level=0', 'werror=false'])
glfw_dep = glfw_proj.get_variable('glfw_dep')

if host_machine.system() == 'linux' and get_option('use_xlib')
  add_project_arguments(['-DRT_USE_XLIB'], language : ['c', 'cpp'])
elif host_machine.system() == 'linux' and get_option('use_wayland')
  add_project_arguments(['-DRT_USE_WAYLAND'], language: ['c', 'cpp'])
endif

# Copy file utility
copy_util = find_program('scripts/copy_util.py')

engine_incdir = include_directories('src')

contrib_dir = meson.project_source_root() / 'contrib'
contrib_incdir = include_directories('contrib')

# Targets append to this, to enable us to run shared library builds from IDEs
engine_lib_paths = []
engine_libs = []

subdir('src')

# Handle linking against both runtime and renderer if we build static libs
engine_link_libs = []
if get_option('default_library') == 'static'
  if get_option('static_renderer') == 'vk'
    engine_link_libs = [runtime_lib, vk_renderer_lib]
  elif get_option('static_renderer') == 'null'
    engine_link_libs = [runtime_lib, null_renderer_lib]
  elif get_option('static_renderer') == 'dx11'
    engine_link_libs = [runtime_lib, dx11_renderer_lib]
  else
    error('Invalid static_renderer option ', get_option('static_renderer'))
  endif
else
  engine_link_libs = [runtime_lib]
endif

# Unit/Integration test driver
subdir('tests')

# Declare dependencies, to enable using the engine as a subproject
engine_dep = declare_dependency(link_with : engine_link_libs,
  include_directories : engine_incdir)

assetc_dep = declare_dependency(link_with : asset_compiler,
  include_directories : engine_incdir)

# "inline" game
if get_option('game_as_subdir')
  subdir('src/game')

  # If we are building shared libraries, copy them to the games output directory.
  # This ensures that we can debug with visual studio.
  foreach lib_path : engine_lib_paths
    run_target('copy'+fs.name(lib_path),
      command : [copy_util, lib_path, join_paths(game_build_dir, fs.name(lib_path))],
      depends : engine_libs)
  endforeach
endif

