diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-23 07:37:08 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2021-06-23 07:37:08 +0200 |
commit | e4c0168e4e8834aea0cfe06b8b2fc8bd63f591bb (patch) | |
tree | aa8060de3abd73dfbf89ed35d054fc470cf445ea /configure.cc | |
parent | 68bf2848332fd15a2b6dfa49f3a58fccd899dbd7 (diff) |
Split libcppbuild.cc into multiple files/modules.
Diffstat (limited to 'configure.cc')
-rw-r--r-- | configure.cc | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/configure.cc b/configure.cc new file mode 100644 index 0000000..e84f808 --- /dev/null +++ b/configure.cc @@ -0,0 +1,153 @@ +#include "configure.h" + +#include <iostream> +#include <filesystem> +#include <fstream> + +#include <getoptpp/getoptpp.hpp> + +#include "settings.h" +#include "execute.h" +#include "libcppbuild.h" +#include "tasks.h" + +std::filesystem::path configurationFile("configuration.cc"); +std::filesystem::path configHeaderFile("config.h"); + +const std::map<std::string, std::string> default_configuration{}; +const std::map<std::string, std::string>& __attribute__((weak)) configuration() +{ + return default_configuration; +} + +bool hasConfiguration(const std::string& key) +{ + const auto& c = configuration(); + return c.find(key) != c.end(); +} + +const std::string& getConfiguration(const std::string& key, + const std::string& defaultValue) +{ + const auto& c = configuration(); + if(hasConfiguration(key)) + { + return c.at(key); + } + + return defaultValue; +} + +int configure(int argc, char* argv[]) +{ + Settings settings; + + settings.builddir = "build"; + + std::string cmd_str; + for(int i = 0; i < argc; ++i) + { + if(i > 0) + { + cmd_str += " "; + } + cmd_str += argv[i]; + } + + dg::Options opt; + int key{256}; + + std::string build_arch; + std::string host_arch; + + opt.add("build-dir", required_argument, 'b', + "Set output directory for build files (default: '" + + settings.builddir + "').", + [&]() { + settings.builddir = optarg; + return 0; + }); + + opt.add("verbose", no_argument, 'v', + "Be verbose. Add multiple times for more verbosity.", + [&]() { + settings.verbose++; + return 0; + }); + + opt.add("build", required_argument, key++, + "Configure for building on specified architecture.", + [&]() { + build_arch = optarg; + return 0; + }); + + opt.add("host", required_argument, key++, + "Cross-compile to build programs to run on specified architecture.", + [&]() { + host_arch = optarg; + return 0; + }); + + opt.add("help", no_argument, 'h', + "Print this help text.", + [&]() { + std::cout << "configure usage stuff\n"; + opt.help(); + exit(0); + return 0; + }); + + opt.process(argc, argv); + + if(host_arch.empty()) + { + host_arch = build_arch; + } + + auto tasks = getTasks(settings); + + bool needs_cpp{false}; + bool needs_c{false}; + bool needs_ar{false}; + for(const auto& task :tasks) + { + switch(task->sourceLanguage()) + { + case Language::Auto: + std::cerr << "TargetLanguage not deduced!\n"; + exit(1); + break; + case Language::C: + needs_cpp = false; + break; + case Language::Cpp: + needs_c = true; + break; + } + } + + { + std::ofstream istr(configurationFile); + istr << "#include \"libcppbuild.h\"\n\n"; + istr << "const std::map<std::string, std::string>& configuration()\n"; + istr << "{\n"; + istr << " static std::map<std::string, std::string> c =\n"; + istr << " {\n"; + istr << " { \"cmd\", \"" << cmd_str << "\" },\n"; + istr << " { \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n"; + istr << " { \"" << cfg::target_cc << "\", \"/usr/bin/gcc\" },\n"; + istr << " { \"" << cfg::target_cpp << "\", \"/usr/bin/g++\" },\n"; + istr << " { \"" << cfg::target_ar << "\", \"/usr/bin/ar\" },\n"; + istr << " { \"" << cfg::target_ld << "\", \"/usr/bin/ld\" },\n"; + istr << " { \"" << cfg::host_cc << "\", \"/usr/bin/gcc\" },\n"; + istr << " { \"" << cfg::host_cpp << "\", \"/usr/bin/g++\" },\n"; + istr << " { \"" << cfg::host_ar << "\", \"/usr/bin/ar\" },\n"; + istr << " { \"" << cfg::host_ld << "\", \"/usr/bin/ld\" },\n"; + istr << " };\n"; + istr << " return c;\n"; + istr << "}\n"; + } + + return 0; +} |