Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
CLI.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
8#ifndef NEBUILD_WINDOWS
10#endif
11
12#include <memory>
13#include <mutex>
14#include <thread>
15#include <vector>
16
18 std::vector<std::string> manifest_paths;
19 std::size_t manifest_count{0};
20
21 int operator()(const std::string& index_path, BldKit::BuildConfig& config) {
22 if (index_path == "-v" || index_path == "--version") {
23 BldKit::Logger::info() << "NeBuild (" << NEBUILD_VERSION << ")\n";
24 return EXIT_SUCCESS;
25 } else if (index_path == "--dry-run" || index_path == "-n") {
26 config.dry_run(true);
27 return EXIT_SUCCESS;
28 } else if (index_path == "-h" || index_path == "--help") {
29 BldKit::Logger::info() << "nebld <options> <{Jbuild, Tbuild}/file.{json, toml}>\n";
30 return EXIT_SUCCESS;
31 } else if (index_path == "/h" || index_path == "/help") {
32 BldKit::Logger::info() << "nebld <options> <{Jbuild, Tbuild}/file.{json, toml}>\n";
33 return EXIT_SUCCESS;
34 } else if (index_path == "/v" || index_path == "/version") {
35 BldKit::Logger::info() << "NeBuild (" << NEBUILD_VERSION << ")\n";
36 return EXIT_SUCCESS;
37 } else if (index_path == "/dry-run" || index_path == "/n") {
38 config.dry_run(true);
39 return EXIT_SUCCESS;
40 }
41
42 return EXIT_FAILURE;
43 }
44};
45
46int main(int argc, char** argv) {
47 if (argc < 1) return EXIT_FAILURE;
48
49 try {
51 BuildCommandLine cmdline;
52
53 std::vector<std::thread> jobs;
54
55 for (size_t index{1}; index < argc; ++index) {
56 std::string index_path = argv[index];
57
59 if (cmdline(index_path, config) == EXIT_SUCCESS) continue;
60
61 cmdline.manifest_paths.push_back(index_path);
62 cmdline.manifest_count++;
63 }
64
65 if (cmdline.manifest_count == 0) {
66 BldKit::Logger::info() << "error: no manifest file provided!" << std::endl;
67 return EXIT_FAILURE;
68 }
69
70 size_t index{0};
71
72 for (const auto& index_path : cmdline.manifest_paths) {
73 size_t index_cpy{index};
74 std::mutex mutex;
75
76 jobs.push_back(std::thread{
77 [&mutex, &index, &index_cpy, &argc, &argv, &config](std::string index_path) -> void {
78 std::unique_lock<decltype(mutex)> lk{mutex};
79 std::unique_ptr<BldKit::IManifestBuilder> builder;
80
81 constexpr auto kJsonExtension = ".json";
82
83 if (index_path.ends_with(kJsonExtension)) {
84 builder = std::make_unique<BldKit::JSONManifestBuilder>();
85 } else {
86#ifndef NEBUILD_WINDOWS
87 constexpr auto kTomlExtension = ".toml";
88
89 builder = std::make_unique<BldKit::TOMLManifestBuilder>();
90
91 if (!index_path.ends_with(kTomlExtension)) {
93 << "error: file '" << index_path << "' is not a manifest file!" << std::endl;
94 config.has_failed(true);
95 return;
96 }
97#else
98 BldKit::Logger::info() << "error: file '" << index_path
99 << "' is not a manifest file! (TOML support is not "
100 "available on Windows)"
101 << std::endl;
102 config.has_failed(true);
103 return;
104#endif
105 }
106
107 std::string path;
108
109 if ((index_cpy + 1) < argc && argv[index_cpy + 1]) path = argv[index_cpy + 1];
110
111 if (path == "--build-system" || path == "-B") {
112 BldKit::Logger::info() << builder->BuildSystem() << std::endl;
113 std::exit(EXIT_SUCCESS);
114 }
115
116 BldKit::Logger::info() << "building manifest: " << index_path << std::endl;
117
118 config.path(index_path);
119
120 if (builder && !builder->BuildTarget(config)) {
121 config.has_failed(true);
122 }
123 },
124 index_path});
125 }
126
127 for (auto& job : jobs) job.join();
128
129 // check for whether config is valid. if so return failure, or success.
130 return !config ? EXIT_FAILURE : EXIT_SUCCESS;
131 } catch (const std::runtime_error& err) {
132 BldKit::Logger::info() << "error: " << err.what() << std::endl;
133 }
134}
#define index
Definition 17.names.cpp:482
#define main
#define NEBUILD_VERSION
Definition Config.h:16
JSON manifest builder header file.
TOML manifest builder header file.
#define EXIT_SUCCESS
#define EXIT_FAILURE
A TOML path.
Definition path.hpp:209
std::ostream & info() noexcept
replacement for std::cout for NeBuild logging.
Definition Config.h:69
const std::string & path()
Definition Config.h:51
bool has_failed()
Definition Config.h:48
std::size_t manifest_count
Definition CLI.cpp:19
int operator()(const std::string &index_path, BldKit::BuildConfig &config)
Definition CLI.cpp:21
std::vector< std::string > manifest_paths
Definition CLI.cpp:18