summaryrefslogtreecommitdiffhomepage
path: root/src/lib/TOMLManifestBuilder.cc
blob: 257c4a37326e1a76ec2d0811694c3f6baa710143 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// ============================================================= //
// nebuild
// Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under BSD-3 license.
// ============================================================= //

#include <NeBuildKit/TOMLManifestBuilder.h>
#include <filesystem>
#include <toml++/toml.hpp>

namespace NeBuild {
namespace FS = std::filesystem;

/// =========================================================== ///
/// @brief Builds a TOML target from a TOML file.
/// @param arg_sz filename size (must be 1 or greater).
/// @param arg_val filename path (must be a valid TOML file).
/// @retval true building has succeeded.
/// @retval false fail to build, see error message.
/// =========================================================== ///
bool TOMLManifestBuilder::BuildTarget(BuildConfig& config) {
  std::string path;

  if (config.path_.empty()) {
    NeBuild::Logger::info() << "nebuild: error: file path is empty" << std::endl;
    return false;
  } else {
    path = config.path_;

    if (!FS::exists(path)) {
      NeBuild::Logger::info() << "nebuild: error: file '" << path << "' does not exist"
                              << std::endl;
      return false;
    }
  }

  try {
    auto toml_file = toml::parse_file(path);

    std::string compiler = toml_file["compiler_path"].as_string()->get();

    std::string command = compiler + " ";

    auto header_search_path = toml_file["compiler_headers_path"].as_array();

    if (header_search_path) {
      for (auto& headers : *header_search_path) {
        command += "-I" + headers.as_string()->get() + " ";
      }
    }

    auto headers_path = toml_file["headers_path"].as_array();

    if (headers_path) {
      for (auto& headers : *headers_path) {
        command += "-I" + headers.as_string()->get() + " ";
      }
    }

    auto sources_files = toml_file["sources_path"].as_array();

    if (sources_files) {
      for (auto& sources : *sources_files) {
        command += sources.as_string()->get() + " ";
      }
    }

    auto macros_list = toml_file["cpp_macros"].as_array();

    if (macros_list) {
      for (auto& macro : *macros_list) {
        command += "-D" + macro.as_string()->get() + " ";
      }
    }

    auto compiler_flags = toml_file["compiler_flags"].as_array();

    if (compiler_flags) {
      for (auto& flag : *compiler_flags) {
        command += flag.as_string()->get() + " ";
      }
    }

    if (!toml_file["compiler_std"].is_string()) return false;

    command += "-std=" + toml_file["compiler_std"].as_string()->get() + " ";

    if (toml_file["output_name"].as_string() == nullptr) return false;

    command += "-o " + toml_file["output_name"].as_string()->get();

    auto target = toml_file["output_name"].as_string()->get();

    NeBuild::Logger::info() << "output: " << target << "\n";

    auto ret_exec = std::system(command.c_str());

    if (ret_exec > 0) {
      NeBuild::Logger::info() << "error: exit with message: " << std::strerror(ret_exec) << ""
                              << std::endl;
      config.has_failed_ = true;
      return false;
    }

    if (!config.dry_run_) {
      auto run_after_build = toml_file["run_after_build"].as_boolean();
      if (!run_after_build) return true;

      auto val = run_after_build->get();
      if (val) {
        ret_exec = std::system(target.c_str());

        if (ret_exec > 0) {
          NeBuild::Logger::info() << "error: exit with message: " << std::strerror(ret_exec) << ""
                                  << std::endl;
          config.has_failed_ = true;
          return false;
        }
      }
    }
  } catch (std::runtime_error& err) {
    NeBuild::Logger::info() << "error: exit with message: " << err.what() << "" << std::endl;
    config.has_failed_ = true;
    return false;
  }

  return true;
}

/// =========================================================== ///
/// @brief Returns the build system name.
/// =========================================================== ///
const std::string_view TOMLManifestBuilder::BuildSystem() {
  return "NeBuild (TOML)";
}
}  // namespace NeBuild