/*	Copyright  (c)	Günter Woigk 2018 - 2018
					mailto:kio@little-bat.de

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	Permission to use, copy, modify, distribute, and sell this software and
	its documentation for any purpose is hereby granted without fee, provided
	that the above copyright notice appear in all copies and that both that
	copyright notice and this permission notice appear in supporting
	documentation, and that the name of the copyright holder not be used
	in advertising or publicity pertaining to distribution of the software
	without specific, written prior permission.  The copyright holder makes no
	representations about the suitability of this software for any purpose.
	It is provided "as is" without express or implied warranty.

	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
	PERFORMANCE OF THIS SOFTWARE.
*/

#include "Libraries/kio/kio.h"
#include "Make.h"
#include "Compiler/Compiler.h"
#include "Preprocessor/Preprocessor.h"
#include "Targets/Target.h"
#include "globals.h"
#include "Preprocessor/Word.h"
#include "FileInfo.h"


Make::Make()
:
	compiler(nullptr)
{}

Make::~Make()
{
	delete compiler;
}

void Make::preprocess (cstr sourcefile, cstr listdir)
{
	// preprocess only
	// uses everything from globals.h

	assert(target != nullptr);

	::preprocess(sourcefile, listdir);
}

void Make::compile (cstr sourcefile, cstr listdir)
{
	// preprocess and compile only
	// uses everything from globals.h

	preprocess(sourcefile, listdir);
	if (errors.count() != 0) return;

	if (!compiler) compiler = new Compiler;
	compiler->compile(sourcefile, listdir);
}

void Make::assemble (cstr sourcefile, cstr outputdir, cstr listdir)
{
	// preprocess, compile and assemble this file
	// uses everything from globals.h
	// the passed 'sourcefile' may be different from the global 'sourcefile'. (it may be a required file)

	compile(sourcefile, listdir);
	if (errors.count() != 0) return;

	target->assemble(sourcefile, outputdir, listdir);
}

void Make::make (cstr sourcefile, cstr outputdir, cstr listdir)
{
	// preprocess, compile and assemble this file and all required files
	// create final output file
	// uses everything from globals.h

	assert(target != nullptr);
	assert(errors.count() == 0);

	// requirements:
	// all #required sources shall be linked in order of appearance before the file which #required them.
	// this rule is applied recursively if required files in return require files.
	//
	// problem: recursively required sources are only detected when the required source is compiled
	//	=> all_files[] is reverted: files will be linked last-to-first
	//	=> required_files[] (from preprocess) must be appended in reverse order to all_files[]
	//	=> files already in all_files[] are moved to the end of all_files[] so that they are linked earlier
	//
	// problem: two files require each other
	//	=> files_done[] tracks already compiled files.
	//	   the link order of these (and possible dependent) files is not easily to predict.

	StrArray all_files;
	StrArray files_done;
	all_files.append(sourcefile);

	for (uint i=0; i<all_files.count(); i++)
	{
		cstr file = all_files[i];
		if (files_done.contains(file)) continue;
		files_done.append(file);

		setTarget(target_name);
		assemble(file,nullptr,listdir);
		if (errors.count() != 0) return;

		while (required_files.count())
		{
			cstr file = required_files.pop();

			uint k = all_files.indexof(file);
			if (k != ~0u)
			{
				all_files.remove(k);
				if (i >= k) i--;
			}

			all_files.append(std::move(file));
			assert(file == nullptr);
		}
	}

	assert(all_files.count() == files_done.count());

	// link:
	all_files.revert();

	// TODO: link
}





























