summaryrefslogtreecommitdiffhomepage
path: root/src/CommandLine/CLI.cpp
blob: e3b68240f4af0d097f7dfe1af46fc19209c0d049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: BSD-3-Clause
// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/ne-foss-org/build

#include <NeBuildKit/JSONManifestBuilder.h>
#include <NeBuildKit/TOMLManifestBuilder.h>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>

constexpr auto kNeBuildFileJson = "Jbuild.json";
constexpr auto kNeBuildFileToml = "Tbuild.toml";

int main(int argc, char** argv) {
  if (argc < 1) return EXIT_FAILURE;

  NeBuild::BuildConfig config;

  std::vector<std::thread> jobs;

  for (size_t index{1}; index < argc; ++index) {
    std::string index_path = argv[index];

    if (index_path == "-v" || index_path == "-version") {
      NeBuild::Logger::info() << "NeBuild (" << NEBUILD_VERSION << ")\n";
      return EXIT_SUCCESS;
    } else if (index_path == "-dry-run" || index_path == "-n") {
      config.dry_run(true);
      continue;
    } else if (index_path == "-h" || index_path == "-help") {
      NeBuild::Logger::info() << "nebuild <options> <{Jbuild, Tbuild}/file.{json, toml}>\n";
      return EXIT_SUCCESS;
    }

    auto       index_cpy = index;
    std::mutex mutex;

    jobs.push_back(std::thread{
        [&mutex, &index, &index_cpy, &argc, &argv, &config](std::string index_path) -> void {
          std::unique_lock<decltype(mutex)>          lk{mutex};
          std::unique_ptr<NeBuild::IManifestBuilder> builder;

          constexpr auto kJsonExtension = ".json";

          if (index_path.ends_with(kJsonExtension) || index_path == kNeBuildFileJson) {
            builder = std::make_unique<NeBuild::JSONManifestBuilder>();

            /// report failed build to config.
            if (!builder) {
              config.has_failed(true);
              return;
            }
          } else {
            constexpr auto kTomlExtension = ".toml";
            builder                       = std::make_unique<NeBuild::TOMLManifestBuilder>();

            if (!index_path.ends_with(kTomlExtension) && index_path != kNeBuildFileToml) {
              NeBuild::Logger::info()
                  << "error: file '" << index_path << "' is not a manifest file!" << std::endl;
              config.has_failed(true);
              return;
            }
          }

          std::string next_path;

          if ((index_cpy + 1) < argc && argv[index_cpy + 1]) next_path = argv[index_cpy + 1];

          if (next_path == "-build-system") {
            NeBuild::Logger::info() << builder->BuildSystem() << std::endl;
            std::exit(EXIT_SUCCESS);
          }

          NeBuild::Logger::info() << "building manifest: " << index_path << std::endl;

          config.path(index_path);

          if (builder && !builder->BuildTarget(config)) {
            config.has_failed(true);
          }
        },
        index_path});
  }

  for (auto& job : jobs) job.join();

  // check for whether config is valid. if so return failure, or success.
  return !config ? EXIT_FAILURE : EXIT_SUCCESS;
}