131 lines
No EOL
3.3 KiB
C++
131 lines
No EOL
3.3 KiB
C++
#include "BuildSession.h"
|
|
|
|
#include "Logger.h"
|
|
#include "Process.h"
|
|
#include "Directory.h"
|
|
|
|
BuildSession::BuildSession(const String& source_dir,
|
|
const String& build_dir)
|
|
: m_source_dir(source_dir)
|
|
{
|
|
if (build_dir.empty())
|
|
{
|
|
m_build_dir = FileSystemPath::current_dir().value();
|
|
}
|
|
else
|
|
{
|
|
m_build_dir = build_dir;
|
|
}
|
|
const auto build_str = m_build_dir.str() + "/build";
|
|
m_build_dir = FileSystemPath(build_str);
|
|
m_compiler_flags.push_back("-g");
|
|
m_compiler_flags.push_back("-fno-exceptions");
|
|
m_compiler_flags.push_back("-fno-rtti");
|
|
}
|
|
|
|
Status BuildSession::scan()
|
|
{
|
|
LOG_INFO("Scanning sources at:" << m_source_dir);
|
|
Vector<FileSystemPath> yaml_files;
|
|
STATUS_CHECK(Directory::getFilesWithExtension(
|
|
m_source_dir,
|
|
".yaml",
|
|
yaml_files,
|
|
true), "Error looking for build files");
|
|
|
|
for(const auto& yaml_file : yaml_files)
|
|
{
|
|
if (yaml_file.file_name() == "build")
|
|
{
|
|
STATUS_CHECK(add_library(yaml_file),
|
|
"Error adding library");
|
|
}
|
|
}
|
|
return {};
|
|
}
|
|
|
|
Status BuildSession::add_library(const FileSystemPath& config_path)
|
|
{
|
|
LOG_INFO("Adding library at: " << config_path);
|
|
BuildLibrary lib(config_path);
|
|
STATUS_CHECK(lib.scan(), "Error scanning library");
|
|
m_libraries.push_back(lib);
|
|
return {};
|
|
}
|
|
|
|
Status BuildSession::build()
|
|
{
|
|
LOG_INFO("Creating dir: " << m_build_dir);
|
|
Directory::create(m_build_dir, true);
|
|
for(auto& library : m_libraries)
|
|
{
|
|
auto run_status = compile_library(library);
|
|
if (!run_status.ok())
|
|
{
|
|
return run_status;
|
|
}
|
|
run_status = create_archive(library);
|
|
if (!run_status.ok())
|
|
{
|
|
return run_status;
|
|
}
|
|
//break;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
Status BuildSession::compile_library(BuildLibrary& lib)
|
|
{
|
|
for(const auto& source : lib.get_sources())
|
|
{
|
|
const auto run_status = compile_source_file(source, lib);
|
|
if (!run_status.ok())
|
|
{
|
|
return run_status;
|
|
}
|
|
//break;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
Status BuildSession::create_archive(BuildLibrary& lib)
|
|
{
|
|
Vector<String> args;
|
|
args.push_back("rcs");
|
|
|
|
auto name = "lib" + lib.get_name() + ".a";
|
|
args.push_back(name);
|
|
|
|
for(const auto& source : lib.get_sources())
|
|
{
|
|
const auto output_path = m_build_dir / source.file_name();
|
|
const auto output_file = output_path.str() + ".o";
|
|
args.push_back(output_file);
|
|
}
|
|
|
|
LOG_INFO("Archiving " << name);
|
|
return Process::launch(m_archive_command, args);
|
|
}
|
|
|
|
Status BuildSession::compile_source_file(const FileSystemPath& source,
|
|
const BuildLibrary& lib)
|
|
{
|
|
Vector<String> args = m_compiler_flags;
|
|
args.push_back("-c");
|
|
add_include_dirs(args, lib);
|
|
args.push_back(source.str());
|
|
|
|
const auto output_path = m_build_dir / source.file_name();
|
|
args.push_back("-o");
|
|
args.push_back(output_path.str() + ".o");
|
|
LOG_INFO("Compiling " << source.file_name());
|
|
return Process::launch(m_compiler_command, args);
|
|
}
|
|
|
|
void BuildSession::add_include_dirs(Vector<String>& args, const BuildLibrary& lib)
|
|
{
|
|
for(const auto& include_dir : lib.get_include_dirs())
|
|
{
|
|
args.push_back(_s("-I") + include_dir.str());
|
|
}
|
|
} |