#!/usr/bin/env python
import os
import sys

if not os.path.isdir("modules/mavlink"):
    raise UserError("MAVLink sources missing, run git submodule update --init --recursive")
if not os.path.isdir("include/mavlink"):
    raise UserError("MAVLink generated headers missing, run python update_mavlink.py")

env = SConscript("modules/godot-cpp/SConstruct")

# For reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags

# tweak this if you want to use different folders, or more folders, to store your source code in.
env.Append(CPPPATH=["src/"])
env.Append(CPPPATH=["include/"])
sources = Glob("src/*.cpp")

# Generated with local Godot installation
env["custom_api_file"] = "extension_api.json"

# Information for Clang tooling, including clangd language server
# To create the compile_database
env.Tool('compilation_db')
cdb = env.CompilationDatabase('compile_commands.json')
Alias('compile_commands', cdb)

if env["platform"] == "macos":
    library = env.SharedLibrary(
        "project/bin/libmarshconnector.{}.{}.framework/libmarshconnector.{}.{}".format(
            env["platform"], env["target"], env["platform"], env["target"]
        ),
        source=sources,
    )
elif env["platform"] == "ios":
    if env["ios_simulator"]:
        library = env.StaticLibrary(
            "project/bin/libmarshconnector.{}.{}.simulator.a".format(env["platform"], env["target"]),
            source=sources,
        )
    else:
        library = env.StaticLibrary(
            "project/bin/libmarshconnector.{}.{}.a".format(env["platform"], env["target"]),
            source=sources,
        )
else:
    library = env.SharedLibrary(
        "project/bin/libmarshconnector{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
        source=sources,
    )
    # Triggered a lot in MAVLink headers
    env.Append(CXXFLAGS=["-Wno-address-of-packed-member"])

Default(library)
