Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
TOMLManifestBuilder.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org)
3// Licensed under the Apache License, Version 2.0 (see LICENSE file)
4// Official repository: https://github.com/ne-app-eu/bld
5
6#ifndef NEBUILD_WINDOWS
7
9#include <optional>
10#include <toml++/toml.hpp>
11#include <filesystem>
12#include <optional>
13
14namespace BldKit {
15
16namespace FS = std::filesystem;
17
24bool TOMLManifestBuilder::BuildTarget(BuildConfig& config) {
25 std::string path;
26
27 if (config.path_.empty()) {
28 BldKit::Logger::info() << "error: file path is empty." << std::endl;
29 return false;
30 } else {
31 path = config.path_;
32
33 if (!FS::exists(path)) {
34 BldKit::Logger::info() << "error: file '" << path << "' does not exist." << std::endl;
35 return false;
36 }
37 }
38
39 try {
40 auto toml_file = toml::parse_file(path);
41
42 try {
43 auto* description = toml_file["description"].as_string();
44
45 if (!description) throw std::runtime_error({});
46
47 BldKit::Logger::info() << "package path: " << path << std::endl;
48
49 if (description)
50 BldKit::Logger::info() << "description: " << description->get() << std::endl;
51 } catch (...) {
53 }
54
55 auto* compiler_path_ptr = toml_file["compiler_path"].as_string();
56
57 if (!compiler_path_ptr) return false;
58
59 std::string compiler = compiler_path_ptr->get();
60 std::string command = compiler + " ";
61
62 auto* header_search_path_ptr = toml_file["compiler_headers_path"].as_array();
63
64 if (!header_search_path_ptr) return false;
65
66 auto header_search_path = header_search_path_ptr;
67
68 if (header_search_path) {
69 for (auto& headers : *header_search_path) {
70 command += "-I" + headers.as_string()->get() + " ";
71 }
72 }
73
74 auto headers_path = toml_file["headers_path"].as_array();
75
76 if (headers_path) {
77 for (auto& headers : *headers_path) {
78 command += "-I" + headers.as_string()->get() + " ";
79 }
80 }
81
82 auto sources_files = toml_file["sources_path"].as_array();
83
84 if (sources_files) {
85 for (auto& sources : *sources_files) {
86 command += sources.as_string()->get() + " ";
87 }
88 }
89
90 auto macros_list = toml_file["cpp_macros"].as_array();
91
92 if (macros_list) {
93 for (auto& macro : *macros_list) {
94 command += "-D" + macro.as_string()->get() + " ";
95 }
96 }
97
98 auto compiler_flags = toml_file["compiler_flags"].as_array();
99
100 if (compiler_flags) {
101 for (auto& flag : *compiler_flags) {
102 command += flag.as_string()->get() + " ";
103 }
104 }
105
106 if (!toml_file["compiler_std"].is_string()) return false;
107
108 command += "-std=" + toml_file["compiler_std"].as_string()->get() + " ";
109
110 if (toml_file["output_name"].as_string() == nullptr) return false;
111
112 command += "-o " + toml_file["output_name"].as_string()->get();
113
114 auto target = toml_file["output_name"].as_string()->get();
115
116 BldKit::Logger::info() << "output: " << target << "\n";
117
118 auto ret_exec = std::system(command.c_str());
119
120 if (ret_exec > 0) {
121 BldKit::Logger::info() << "error: exit with message: " << std::strerror(ret_exec) << "."
122 << std::endl;
123 config.has_failed_ = true;
124 return false;
125 }
126 } catch (const std::exception& err) {
127 BldKit::Logger::info() << "error: exit with message: " << err.what() << "." << std::endl;
128 config.has_failed_ = true;
129 return false;
130 }
131
132 return true;
133}
134
138const std::string_view TOMLManifestBuilder::BuildSystem() {
139 return "NeBuild (toml++::toml)";
140}
141
142} // namespace BldKit
143
144#endif
TOML manifest builder header file.
A TOML path.
Definition path.hpp:209
constexpr bool is_string
Metafunction for determining if a type is, or is a reference to, a std::string or toml::value<std::st...
std::ostream & info() noexcept
replacement for std::cout for NeBuild logging.
Definition Config.h:69