#include "BackendUtils.hpp" #include "../include/FileUtil.hpp" #include "ninja/Ninja.hpp" using namespace std; using namespace sibs; namespace backend { static bool isPathSubPathOf(const FileString &path, const FileString &subPathOf) { return _tinydir_strncmp(path.c_str(), subPathOf.c_str(), subPathOf.size()) == 0; } const _tinydir_char_t *cFileExtensions[] = { TINYDIR_STRING("c"), TINYDIR_STRING("C"), TINYDIR_STRING("cc") }; const _tinydir_char_t *cppFileExtensions[] = { TINYDIR_STRING("cp"), TINYDIR_STRING("cpp"), TINYDIR_STRING("cxx"), TINYDIR_STRING("c++") }; sibs::Language BackendUtils::getFileLanguage(tinydir_file *file) { if(!file->is_reg) return sibs::Language::NONE; for(const _tinydir_char_t *sourceFileExtension : cFileExtensions) { if(_tinydir_strcmp(sourceFileExtension, file->extension) == 0) return sibs::Language::C; } for(const _tinydir_char_t *sourceFileExtension : cppFileExtensions) { if(_tinydir_strcmp(sourceFileExtension, file->extension) == 0) return sibs::Language::CPP; } if(_tinydir_strcmp(TINYDIR_STRING("zig"), file->extension) == 0) return sibs::Language::ZIG; return sibs::Language::NONE; } void BackendUtils::collectSourceFiles(const _tinydir_char_t *projectPath, Ninja *ninjaProject, const SibsConfig &sibsConfig, bool recursive) { walkDir(projectPath, [ninjaProject, &sibsConfig, recursive](tinydir_file *file) { FileString pathNative = file->path; #if OS_FAMILY == OS_FAMILY_WINDOWS replaceChar(pathNative, L'/', L'\\'); #endif if(file->is_reg) { sibs::Language fileLanguage = getFileLanguage(file); if (fileLanguage != sibs::Language::NONE) { string filePathUtf8 = toUtf8(pathNative.c_str() + sibsConfig.getProjectPath().size()); ninjaProject->addSourceFile(fileLanguage, filePathUtf8.c_str()); } else { //printf("Ignoring non-source file: %s\n", file->path + projectPath.size()); } } else { // TODO: Verify what happens if test has test sub directory if (!sibsConfig.getTestPath().empty() && isPathSubPathOf(pathNative.c_str(), sibsConfig.getTestPath())) { string filePathUtf8 = toUtf8(pathNative.c_str()); ninjaProject->addTestSourceDir(filePathUtf8.c_str()); } else if(recursive && !directoryToIgnore(pathNative, sibsConfig.getIgnoreDirs()) && !_tinydir_strcmp(pathNative.c_str(), TINYDIR_STRING("sibs-build"))) { FileString projectConfPath = file->path; #if OS_FAMILY == OS_FAMILY_WINDOWS projectConfPath += L'\\'; #else projectConfPath += '/'; #endif projectConfPath += TINYDIR_STRING("project.conf"); auto projectConfFileType = getFileType(projectConfPath.c_str()); if(projectConfFileType == FileType::REGULAR) { backend::Ninja *subProject = new backend::Ninja(); SibsConfig *subProjectConfig = new SibsConfig(sibsConfig.getCompiler(), file->path, sibsConfig.getOptimizationLevel(), false); FileString subProjectBuildPath; readSibsConfig(file->path, projectConfPath, *subProjectConfig, subProjectBuildPath); collectSourceFiles(file->path, subProject, *subProjectConfig, true); ninjaProject->addSubProject(subProject, subProjectConfig, move(subProjectBuildPath)); } else collectSourceFiles(file->path, ninjaProject, sibsConfig, true); } } }); } }