project.cxx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "project.h"
  2. namespace build {
  3. // TODO(samjaffe): Are there actually any arguments that belong here?
  4. project::project(int argc, char const * const * const argv) {}
  5. project & project::set_version(version vers) {
  6. vers_ = vers;
  7. return *this;
  8. }
  9. project & project::set_name(std::string const & name) {
  10. name_ = name;
  11. return *this;
  12. }
  13. project & project::add_source_file(fs::path const & file) {
  14. if (!fs::is_regular_file(file)) {
  15. throw std::logic_error(file.string() + " is not a regular file");
  16. }
  17. source_files_.emplace_back(file);
  18. return *this;
  19. }
  20. std::pair<std::vector<object_file>, int> project::generate_objects() const {
  21. fs::path obj_dir(".obj");
  22. fs::create_directory(obj_dir);
  23. std::vector<object_file> objects;
  24. for (auto const & file : source_files_) {
  25. output_file const outfile =
  26. obj_dir / file.get().filename().replace_extension(".o");
  27. auto cmd = command_builder(CXX, 1) << vers_ << file << outfile;
  28. if (int rc = cmd.execute()) { return {{}, rc}; }
  29. objects.emplace_back(outfile);
  30. }
  31. return {objects, 0};
  32. }
  33. int project::generate() const {
  34. std::cout << "Generating Project: " << name_ << std::endl;
  35. // Step 1: Compile Object files
  36. auto [objects, rc] = generate_objects();
  37. if (rc) { return rc; }
  38. // Step 2: Generate Executable
  39. fs::path bin_dir(".bin");
  40. fs::create_directory(bin_dir);
  41. output_file const outfile = bin_dir / name_;
  42. auto cmd = command_builder(CXX, 1) << vers_ << outfile << objects;
  43. return cmd.execute();
  44. }
  45. }