grbc/src/target_lib.cc

92 lines
No EOL
2.5 KiB
C++

#include "grbc/generator.h"
#include "grbc/spec.h"
#include "grbc/state.h"
TargetInfo grbc_library(const LibraryConfig &library_config) {
TargetInfo target_config{};
std::string lib_name = library_config.name;
lib_name += grbc_get_lib_extension(library_config.lib_type);
std::string compiler_args;
std::string linker_args;
for (auto &package : library_config.requirements) {
compiler_args += package.compiler_flags + " ";
linker_args += package.linker_flags + " ";
}
for (auto &linker_arg : library_config.linker_flags) {
linker_args += linker_arg + " ";
}
for (auto &compiler_arg : library_config.compile_flags) {
compiler_args += compiler_arg + " ";
}
if (library_config.lib_type == LibraryType_Shared) {
compiler_args += "-fPIC ";
}
// Include directories
compiler_args += grbc_include_dirs_to_cflags(library_config.include_dirs);
GeneratorTarget target{};
std::vector<std::string> object_files;
for (auto &src_file : library_config.files) {
GeneratorCompileCommand compile_cmd{};
compile_cmd.source_file = src_file;
compile_cmd.object_file = grbc_object_file(src_file);
compile_cmd.compiler_flags = compiler_args;
compile_cmd.language_type = library_config.language_type;
object_files.push_back(compile_cmd.object_file);
target.compile_commands.push_back(compile_cmd);
}
// Libraries which need to be built before us
std::vector<std::string> libraries;
for (auto &lib : library_config.requirements) {
libraries.push_back(lib.file_name);
}
// Generate package config
PackageConfig pkg_cfg{};
pkg_cfg.name = library_config.name;
pkg_cfg.libraries = library_config.package_config.libraries;
pkg_cfg.include_dirs = library_config.include_dirs;
pkg_cfg.linker_flags.push_back(grbc_get_config().build_dir + "/" + lib_name);
if (!pkg_cfg.name.empty()) {
Package pkg = grbc_bake_package_config(pkg_cfg);
pkg.file_name = lib_name;
GState::get().packages.insert({pkg_cfg.name, pkg});
}
GeneratorLinkTargetCommand link_target{};
if (library_config.lib_type == LibraryType_Shared) {
link_target.target_type = GeneratorTargetType_SharedLibrary;
} else {
link_target.target_type = GeneratorTargetType_StaticLibrary;
}
link_target.object_files = object_files;
link_target.linker_flags = linker_args;
link_target.output_name = lib_name;
link_target.libraries = libraries;
target.link_target_commands.push_back(link_target);
GState::get().targets.push_back(target);
return target_config;
}