import("$tizen_core/build/native/native.gni")
import("$tizen_core/build/native/lib.gni")
import("$tizen_core/build/native/edc.gni")
import("$tizen_core/build/native/po.gni")
import("$tizen_core/build/native/res.gni")
import("$tizen_core/build/hash.gni")

template("native_project") {
    forward_variables_from(invoker, "*", [])

    if (!defined(lib_files)) { lib_files = [] }
    if (!defined(edc_files)) { edc_files = [] }
    if (!defined(po_files)) { po_files = [] }
    if (!defined(resources)) { resources = [] }
    if (!defined(libs)) { libs = [] }

    out_files = []
    proj_deps = []

    if (defined(deps)) {
        foreach(proj, deps) {
            proj_deps += [ "//${proj}:out"]
        }
        deps = []
    }

    # copy precompiled libs
    static_libs = filter_include(lib_files, [ "lib/$profile/$arch/*.a", "lib/${arch}/*.a" ])
    if (static_libs == []) {
        static_libs = filter_include(lib_files, [ "lib/*.a" ])
    }
    libs += static_libs

    shared_libs = filter_include(lib_files, [ "lib/$profile/$arch/*.so", "lib/${arch}/*.so" ])
    if (shared_libs == []) {
        shared_libs = filter_include(lib_files, [ "lib/*.so" ])
    }
    libs += shared_libs

    pre_libs_targets = []
    foreach(file_to_copy, shared_libs) {
        copy_lib(file_to_copy) {}
        out_files += get_target_outputs(":$file_to_copy")
        pre_libs_targets += [ ":$file_to_copy" ]
    }
    proj_deps += pre_libs_targets

    # compile native code to binary or library
    build_native(output_name) {
        sources = src_files
        deps = proj_deps
    }
    proj_deps += [ ":$output_name" ]

    if (!test_build) {
        if (project_type == "native_app") {
            out_files += [ "$root_out_dir/$native_out_dir/bin/$output_name" ]
        } else if (project_type == "shared_lib") {
            out_files += [ "$root_out_dir/$native_out_dir/lib/lib$output_name.so" ]
        }
    } else {
        if (project_type == "test_runner") {
            out_files += [ "$root_out_dir/$native_out_dir/bin/$output_name" ]
        }
    }

    # compile edc files
    foreach(edc_file, edc_files) {
        build_edc(edc_file) {}
        proj_deps += [ ":$edc_file" ]
        out_files += get_target_outputs(":$edc_file")
    }

    # compile po files
    foreach(po_file, po_files) {
        build_po(po_file) {}
        proj_deps += [ ":$po_file" ]
        out_files += get_target_outputs(":$po_file")
    }

    # copy resources
    foreach(file_to_copy, resources) {
        copy_res(file_to_copy) {}
        proj_deps += [ ":$file_to_copy" ]
        out_files += get_target_outputs(":$file_to_copy")
    }

    # create hashes
    hash_deps = []
    foreach(file, out_files) {
        target = rebase_path(file, root_out_dir)
        calc_hash("$target.hash") {
            file_to_hash = file
            deps = proj_deps
        }
        hash_deps += [":$target.hash"]
    }

    group("out") { deps = [ ":$output_name" ] }
    group("all") { deps = proj_deps }
    group("hash") { deps = hash_deps }
}
