Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
JSONManifestBuilder.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
7#include <json/json.h>
8#include <fstream>
9
10namespace BldKit {
11
12namespace FS = std::filesystem;
13
20bool JSONManifestBuilder::BuildTarget(BuildConfig& config) {
21 std::string path;
22
23 if (config.path_.empty()) {
24 BldKit::Logger::info() << "error: file path is empty." << std::endl;
25 return false;
26 } else {
27 path = config.path_;
28
29 if (!FS::exists(path)) {
30 BldKit::Logger::info() << "error: file '" << path << "' does not exist." << std::endl;
31 return false;
32 }
33 }
34
35 try {
36 std::ifstream json(path);
37
38 if (!json.good()) {
39 BldKit::Logger::info() << "error: file '" << path << "' is not a valid json."
40 << std::endl;
41 return false;
42 }
43
44 nlohmann::json json_obj = nlohmann::json::parse(json);
45
46 try {
47 nlohmann::json description = json_obj["description"];
48
49 BldKit::Logger::info() << "package path: " << path << std::endl;
50
51 if (auto res = description.get<std::string>(); !res.empty())
52 BldKit::Logger::info() << "description: " << res << std::endl;
53 } catch (...) {
54 }
55
56 std::string compiler = json_obj["compiler_path"].get<std::string>();
57
58 std::string command = compiler + " ";
59
60 nlohmann::json header_search_path = json_obj["compiler_headers_path"];
61
62 for (auto& headers : header_search_path) {
63 command += "-I" + headers.get<std::string>() + " ";
64 }
65
66 nlohmann::json headers_path = json_obj["headers_path"];
67
68 for (auto& headers : headers_path) {
69 command += "-I" + headers.get<std::string>() + " ";
70 }
71
72 nlohmann::json sources_files = json_obj["sources_path"];
73
74 for (auto& sources : sources_files) {
75 command += sources.get<std::string>() + " ";
76 }
77
78 nlohmann::json macros_list = json_obj["cpp_macros"];
79
80 for (auto& macro : macros_list) {
81 command += "-D" + macro.get<std::string>() + " ";
82 }
83
84 nlohmann::json compiler_flags = json_obj["compiler_flags"];
85
86 for (auto& flag : compiler_flags) {
87 command += flag.get<std::string>() + " ";
88 }
89
90 if (json_obj["compiler_std"].is_string())
91 command += "-std=" + json_obj["compiler_std"].get<std::string>() + " ";
92
93 command += "-o " + json_obj["output_name"].get<std::string>();
94
95 auto target = json_obj["output_name"].get<std::string>();
96
97 BldKit::Logger::info() << "output path: " << target << "\n";
98
99 auto ret_exec = std::system(command.c_str());
100
101 if (ret_exec > 0) {
102 BldKit::Logger::info() << "error: exit with message: " << std::strerror(ret_exec) << "."
103 << std::endl;
104 config.has_failed_ = true;
105 return false;
106 }
107 } catch (const std::exception& err) {
108 BldKit::Logger::info() << "error: exit with message: " << err.what() << "." << std::endl;
109 config.has_failed_ = true;
110
111 return false;
112 }
113
114 return true;
115}
116
120const std::string_view JSONManifestBuilder::BuildSystem() {
121 return "NeBuild (nlohmann::json)";
122}
123
124} // namespace BldKit
#define res
Definition 17.names.cpp:531
JSON manifest builder header file.
namespace for Niels Lohmann
Definition json.h:19402
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...
basic_json<> json
default specialization
Definition json.h:3422
std::ostream & info() noexcept
replacement for std::cout for NeBuild logging.
Definition Config.h:69