-- Generate an Android project makefile.
--
-- originally from:
-- Copyright (c) 2012 Richard Swift and the Premake project

local jni = premake.extensions.jni
local project = premake.project

function jni.cpp(prj)
	_p('# %s project makefile autogenerated by Premake', premake.action.current().shortname)
	_p('LOCAL_PATH := $(call my-dir)')
	
	-- -- build a list of supported target platforms that also includes a generic build
	-- local platforms = premake.filterplatforms(prj.solution, cc.platforms, "Native")

	for cfg in project.eachconfig(prj) do
		local toolset = premake.tools.gcc

		jni.cpp_config(prj, cfg, toolset)
	end
end

-- function jni.getincludedirs(prj,includedirs)
-- 	local result = { }
-- 	for _,dir in ipairs(includedirs) do
-- 		str=prj.basedir .. "/" .. _MAKE.esc(dir)
-- 		table.insert(result, "-I" .. str)
-- 	end
-- 	return result
-- end

function jni.cpp_config(prj, cfg, toolset)

	_p(' include $(CLEAR_VARS)')
	
	name=cfg.targetname
	if name == nil then
		name=prj.name .. cfg.name
	end
	
	_p('  LOCAL_MODULE := %s', name)
	if cfg.kind ~= premake.STATICLIB then
		_p('  LOCAL_LDLIBS := %s', table.concat(toolset.getldflags(cfg), " "))
	end
	_p('  LOCAL_CPPFLAGS  := %s', table.concat(toolset.getcppflags(cfg), " "))
	_p('  LOCAL_CFLAGS    := %s %s %s',
		table.concat(table.join(toolset.getcflags(cfg), cfg.buildoptions), " "),
		table.concat(toolset.getdefines(cfg.defines), " "),
		table.concat(toolset.getincludedirs(prj, cfg.includedirs), " ")
	)

	srcfiles = ''
	
	for _, file in ipairs(cfg.files) do
		if path.iscppfile(file) then
			srcfiles = srcfiles .. ' ' .. jni.esc(file)
		end
	end
	_p('  LOCAL_SRC_FILES := %s', srcfiles)
	
	if cfg.kind == "SharedLib" then
		_p(' include $(BUILD_SHARED_LIBRARY)')
	elseif cfg.kind == "StaticLib" then
		_p(' include $(BUILD_STATIC_LIBRARY)')
	else
		_p(' LOCAL_STATIC_LIBRARIES := android_native_app_glue')
		_p(' include $(BUILD_SHARED_LIBRARY)')
		_p(' $(call import-module,android/native_app_glue)')
	end
	_p('')
end
