summaryrefslogtreecommitdiffhomepage
path: root/dev/src/JSONManifestBuilder.cc
blob: 0b010fe2881c0aa00e8438cbd0ea2b85c628e639 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ============================================================= //
// nebuild
// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
// ============================================================= //

#include <BuildKit/JSONManifestBuilder.h>

#if defined(NEBUILD_POSIX)
#include <dlfcn.h>
#endif

using JSON = nlohmann::json;

namespace FS = std::filesystem;

using namespace NeBuild;

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

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

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

  try {
    std::ifstream json(path);

    if (!json.good()) {
      NeBuild::Logger::info() << "nebuild: error: file '" << path << "' is not a valid JSON"
                              << std::endl;
      return false;
    }

    JSON json_obj = JSON::parse(json);

    std::string compiler = json_obj["compiler_path"].get<std::string>();

    JSON header_search_path = json_obj["headers_path"];
    JSON sources_files      = json_obj["sources_path"];

    std::string command = compiler + " ";

    for (auto& sources : sources_files) {
      command += sources.get<std::string>() + " ";
    }

    for (auto& headers : header_search_path) {
      command += "-I" + headers.get<std::string>() + " ";
    }

    JSON macros_list = json_obj["cpp_macros"];

    for (auto& macro : macros_list) {
      command += "-D" + macro.get<std::string>() + " ";
    }

    JSON compiler_flags = json_obj["compiler_flags"];

    for (auto& flag : compiler_flags) {
      command += flag.get<std::string>() + " ";
    }

    if (json_obj["compiler_std"].is_string())
      command += "-std=" + json_obj["compiler_std"].get<std::string>() + " ";

    command += "-o " + json_obj["output_name"].get<std::string>();

    auto target = json_obj["output_name"].get<std::string>();

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

    try {
      if (json_obj["dry_run"].get<bool>()) return true;
    } catch (...) {
    }

    if (dry_run) {
      return true;
    }

    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;
      return false;
    }

    try {
      if (json_obj["run_after_build"].get<bool>()) {
        if (target.ends_with(".so") || target.ends_with(".dylib")) {
#if defined(NEBUILD_POSIX)
          auto dll = dlopen(target.c_str(), RTLD_LAZY);

          if (dll) {
            int (*entrypoint)(void) = nullptr;
            entrypoint = (decltype(entrypoint))dlsym(dll, "shared_runner");

            if (entrypoint) entrypoint();

            dlclose(dll);

            return true;
          }
#endif

          return false;
        } else {
          std::ifstream     file = std::ifstream(target);
          std::stringstream ss;

          ss << file.rdbuf();

          if (ss.str()[0] == 'O' && ss.str()[1] == 'p' && ss.str()[2] == 'e' && ss.str()[3] == 'n')
            NeBuild::Logger::info()
                << "error: can't open PEF dynamic library, it mayn't contain an entrypoint"
                << std::endl;
          else if (ss.str()[0] == 'n' && ss.str()[1] == 'e' && ss.str()[2] == 'p' &&
                   ss.str()[3] == 'O')
            NeBuild::Logger::info()
                << "error: can't open FEP dynamic library, it mayn't contain an entrypoint"
                << std::endl;

          return false;
        }

#if defined(NEBUILD_WINDOWS)
        std::system((".\\" + target).c_str());
#else
        std::system(("./" + target).c_str());
#endif
      }
    } catch (...) {
      return true;
    }
  } catch (std::runtime_error& err) {
    NeBuild::Logger::info() << "error: " << err.what() << std::endl;

    return false;
  }

  return true;
}

const char* JSONManifestBuilder::BuildSystem() {
  return "NeBuild (JSON)";
}