#!/usr/bin/env python

from methods import using_gcc, get_compiler_version

Import('env')
Import('env_modules')

env_detour = env_modules.Clone()

# Thirdparty source files
# TODO: clean this up for recastnav+fastlz
detour_sources = []
thirdparty_sources = []
thirdparty_dir = "#thirdparty/recastnavigation/"
detour_dir = "thirdparty/"
if env['builtin_recast'] and not env["tools"]:
    thirdparty_sources = [
		"Recast/Source/Recast.cpp",
		"Recast/Source/RecastAlloc.cpp",
		"Recast/Source/RecastArea.cpp",
		"Recast/Source/RecastAssert.cpp",
		"Recast/Source/RecastContour.cpp",
		"Recast/Source/RecastFilter.cpp",
		"Recast/Source/RecastLayers.cpp",
		"Recast/Source/RecastMesh.cpp",
		"Recast/Source/RecastMeshDetail.cpp",
		"Recast/Source/RecastRasterization.cpp",
		"Recast/Source/RecastRegion.cpp",
    ]
# Detour source files
detour_sources += [
    	"Detour/Source/DetourAlloc.cpp",
    	"Detour/Source/DetourAssert.cpp",
    	"Detour/Source/DetourCommon.cpp",
    	"Detour/Source/DetourNavMeshBuilder.cpp",
    	"Detour/Source/DetourNavMesh.cpp",
    	"Detour/Source/DetourNavMeshQuery.cpp",
    	"Detour/Source/DetourNode.cpp",
]

# DetourCrowd source files
detour_sources += [
        "DetourCrowd/Source/DetourCrowd.cpp",
        "DetourCrowd/Source/DetourLocalBoundary.cpp",
        "DetourCrowd/Source/DetourObstacleAvoidance.cpp",
        "DetourCrowd/Source/DetourPathCorridor.cpp",
        "DetourCrowd/Source/DetourPathQueue.cpp",
        "DetourCrowd/Source/DetourProximityGrid.cpp",
]

# DetiurTileCache source files
detour_sources += [
        "DetourTileCache/Source/DetourTileCacheBuilder.cpp",
        "DetourTileCache/Source/DetourTileCache.cpp",
]

thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
thirdparty_sources += [detour_dir + file for file in detour_sources]

env_detour.add_source_files(env.modules_sources, thirdparty_sources)
env_detour.Append(CPPPATH=[thirdparty_dir + "/Recast/Include",
    detour_dir + "/Detour/Include",
    detour_dir + "/DetourCrowd/Include",
    detour_dir + "/DetourTileCache/Include",
    "#thirdparty/misc"])
env_detour.Append(CPPFLAGS=["-DNDEBUG", "-DTILE_CACHE"])
# See https://github.com/recastnavigation/recastnavigation/issues/380
# for Wno-error=class-memaccess
if using_gcc(env):
    gcc_version = get_compiler_version(env)
    if gcc_version != None and gcc_version[0] >= 8 and not env["platform"] == "windows":
        env_detour.Append(CPPFLAGS=["-Wno-error=class-memaccess", "-Wno-error=shadow=local"])

# Godot source files
env_detour.add_source_files(env.modules_sources, "*.cpp")

Export('env')
