<commit_before>/*
 * Copyright (C) 2007-2013 <NAME>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <fkit/stdio.h>
#include <fkit/Dir.h>
#include <fkit/File.h>
#include "BuildPlan.h"
#include "ConfigureStage.h"

namespace fmake
{

bool ConfigureStage::run()
{
	if (complete_) return success_;
	complete_ = true;

	for (int i = 0; i < plan()->systemPrerequisitesByName()->size(); ++i)
	{
		String name = plan()->systemPrerequisitesByName()->keyAt(i);
		SystemPrerequisiteList *prerequisiteList = plan()->systemPrerequisitesByName()->valueAt(i);

		for (int j = 0; j < prerequisiteList->size(); ++j)
		{
			SystemPrerequisite *prerequisite = prerequisiteList->at(j);
			String includePath;
			if (!findIncludePath(prerequisite, &includePath)) {
				if (!prerequisite->optional()) {
					ferr() << "Missing system prerequisite \"" << prerequisite->name() << "\": Include path not found" << nl;
					return success_ = false;
				}
			}
			String libraryPath;
			if (!findLibraryPath(prerequisite, &libraryPath)) {
				if (!prerequisite->optional()) {
					ferr() << "Missing system prerequisite \"" << prerequisite->name() << "\": Library path not found" << nl;
					return success_ = false;
				}
			}

			if (includePath != "") plan()->includePaths()->append(includePath);
			if (libraryPath != "") plan()->libraryPaths()->append(libraryPath);

			for (int k = 0; k < prerequisite->libraries()->size(); ++k)
				plan()->libraries()->append(prerequisite->libraries()->at(k));

			for (int k = 0; k < prerequisite->compileFlags()->size(); ++k)
				plan()->customCompileFlags()->append(prerequisite->compileFlags()->at(k));

			if (plan()->options() & BuildPlan::Debug) {
				for (int k = 0; k < prerequisite->debugCompileFlags()->size(); ++k)
					plan()->customCompileFlags()->append(prerequisite->debugCompileFlags()->at(k));
			}
			if (plan()->options() & BuildPlan::Release) {
				for (int k = 0; k < prerequisite->releaseCompileFlags()->size(); ++k)
					plan()->customCompileFlags()->append(prerequisite->releaseCompileFlags()->at(k));
			}
		}
	}

	return success_ = true;
}

bool ConfigureStage::findIncludePath(SystemPrerequisite *prerequisite, String *includePath)
{
	for (int i = 0; i < prerequisite->includePaths()->size(); ++i) {
		String path = prerequisite->includePaths()->at(i);
		if (!Dir::exists(path)) continue;
		int j = 0;
		for (; j < prerequisite->testIncludes()->size(); ++j) {
			String checkPath = path + "/" + prerequisite->testIncludes()->at(j);
			if (!File::exists(checkPath)) break;
		}
		if (j == prerequisite->testIncludes()->size()) {
			*includePath = path;
			return true;
		}
	}
	*includePath = "";
	return prerequisite->includePaths()->size() == 0;
}

bool ConfigureStage::findLibraryPath(SystemPrerequisite *prerequisite, String *libraryPath)
{
	for (int i = 0; i < prerequisite->libraryPaths()->size(); ++i) {
		String path = prerequisite->libraryPaths()->at(i);
		if (!Dir::exists(path)) continue;
		if (plan()->toolChain()->linkTest(plan(), path, prerequisite->testLibraries())) {
			*libraryPath = path;
			return true;
		}
	}
	*libraryPath = "";
	return prerequisite->libraryPaths()->size() == 0;
}

} // namespace fmake
<commit_msg>Autoconfiguration: autocomplete relative include paths with system default<commit_after>/*
 * Copyright (C) 2007-2013 <NAME>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <fkit/stdio.h>
#include <fkit/Dir.h>
#include <fkit/File.h>
#include "BuildPlan.h"
#include "ConfigureStage.h"

namespace fmake
{

bool ConfigureStage::run()
{
	if (complete_) return success_;
	complete_ = true;

	for (int i = 0; i < plan()->systemPrerequisitesByName()->size(); ++i)
	{
		String name = plan()->systemPrerequisitesByName()->keyAt(i);
		SystemPrerequisiteList *prerequisiteList = plan()->systemPrerequisitesByName()->valueAt(i);

		for (int j = 0; j < prerequisiteList->size(); ++j)
		{
			SystemPrerequisite *prerequisite = prerequisiteList->at(j);
			String includePath;
			if (!findIncludePath(prerequisite, &includePath)) {
				if (!prerequisite->optional()) {
					ferr() << "Missing system prerequisite \"" << prerequisite->name() << "\": Include path not found" << nl;
					return success_ = false;
				}
			}
			String libraryPath;
			if (!findLibraryPath(prerequisite, &libraryPath)) {
				if (!prerequisite->optional()) {
					ferr() << "Missing system prerequisite \"" << prerequisite->name() << "\": Library path not found" << nl;
					return success_ = false;
				}
			}

			if (includePath != "") plan()->includePaths()->append(includePath);
			if (libraryPath != "") plan()->libraryPaths()->append(libraryPath);

			for (int k = 0; k < prerequisite->libraries()->size(); ++k)
				plan()->libraries()->append(prerequisite->libraries()->at(k));

			for (int k = 0; k < prerequisite->compileFlags()->size(); ++k)
				plan()->customCompileFlags()->append(prerequisite->compileFlags()->at(k));

			if (plan()->options() & BuildPlan::Debug) {
				for (int k = 0; k < prerequisite->debugCompileFlags()->size(); ++k)
					plan()->customCompileFlags()->append(prerequisite->debugCompileFlags()->at(k));
			}
			if (plan()->options() & BuildPlan::Release) {
				for (int k = 0; k < prerequisite->releaseCompileFlags()->size(); ++k)
					plan()->customCompileFlags()->append(prerequisite->releaseCompileFlags()->at(k));
			}
		}
	}

	return success_ = true;
}

bool ConfigureStage::findIncludePath(SystemPrerequisite *prerequisite, String *includePath)
{
	for (int i = 0; i < prerequisite->includePaths()->size(); ++i) {
		String path = prerequisite->includePaths()->at(i);
		if (!path->isAbsolutePath()) path = "/usr/include/" + path;
		if (!Dir::exists(path)) continue;
		int j = 0;
		for (; j < prerequisite->testIncludes()->size(); ++j) {
			String checkPath = path + "/" + prerequisite->testIncludes()->at(j);
			if (!File::exists(checkPath)) break;
		}
		if (j == prerequisite->testIncludes()->size()) {
			*includePath = path;
			return true;
		}
	}
	*includePath = "";
	return prerequisite->includePaths()->size() == 0;
}

bool ConfigureStage::findLibraryPath(SystemPrerequisite *prerequisite, String *libraryPath)
{
	for (int i = 0; i < prerequisite->libraryPaths()->size(); ++i) {
		String path = prerequisite->libraryPaths()->at(i);
		if (!Dir::exists(path)) continue;
		if (plan()->toolChain()->linkTest(plan(), path, prerequisite->testLibraries())) {
			*libraryPath = path;
			return true;
		}
	}
	*libraryPath = "";
	return prerequisite->libraryPaths()->size() == 0;
}

} // namespace fmake

{'fraction_non_alphanumeric': 0.1844730374439786, 'fraction_numerical': 0.005782853838369235, 'mean_word_length': 4.425882352941176, 'pattern_counts': {'":': 4, '<': 61, '<?xml version=': 0, '>': 138, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1052497', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2100, 'n_words': 514}