BuildCache
program_wrapper.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2018 Marcus Geelnard
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_PROGRAM_WRAPPER_HPP_
21 #define BUILDCACHE_PROGRAM_WRAPPER_HPP_
22 
23 #include <base/file_utils.hpp>
24 #include <base/string_list.hpp>
25 #include <cache/cache.hpp>
26 #include <cache/expected_file.hpp>
27 #include <sys/sys_utils.hpp>
28 
29 #include <string>
30 
31 namespace bcache {
39 public:
40  virtual ~program_wrapper_t();
41 
45  bool handle_command(int& return_code);
46 
49  virtual bool can_handle_command() = 0;
50 
51 protected:
54  public:
56  capabilities_t(const string_list_t& cap_strings);
57 
58  bool create_target_dirs() const {
59  return m_create_target_dirs;
60  }
61 
62  bool direct_mode() const {
63  return m_direct_mode;
64  }
65 
66  bool hard_links() const {
67  return m_hard_links;
68  }
69 
70  private:
71  bool m_create_target_dirs = false;
72  bool m_direct_mode = false;
73  bool m_hard_links = false;
74  };
75 
76  // This constructor is called by derived classes.
77  program_wrapper_t(const file::exe_path_t& exe_path, const string_list_t& args);
78 
90  virtual void resolve_args();
91 
110 
114  virtual std::map<std::string, expected_file_t> get_build_files();
115 
119  virtual std::string get_program_id();
120 
130 
137  virtual std::map<std::string, std::string> get_relevant_env_vars();
138 
143  virtual string_list_t get_input_files();
144 
149  virtual std::string preprocess_source();
150 
162 
166 
167  const file::exe_path_t& m_exe_path;
168  const string_list_t& m_args;
169 
175 
176 private:
177  std::string get_program_id_cached();
178 
179  cache_t m_cache;
180 };
181 } // namespace bcache
182 
183 #endif // BUILDCACHE_PROGRAM_WRAPPER_HPP_
bcache::file::exe_path_t
Path to an executable file.
Definition: file_utils.hpp:127
bcache::program_wrapper_t::get_relevant_arguments
virtual string_list_t get_relevant_arguments()
Get relevant command line arguments for hashing.
Definition: program_wrapper.cpp:272
bcache::sys::run_result_t
Run results from an external command.
Definition: sys_utils.hpp:30
bcache::program_wrapper_t::handle_command
bool handle_command(int &return_code)
Try to wrap a program command.
Definition: program_wrapper.cpp:67
bcache::program_wrapper_t::get_implicit_input_files
virtual string_list_t get_implicit_input_files()
Get a list of paths to implicit input files.
Definition: program_wrapper.cpp:294
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::string_list_t
A convenient vector of strings container with support functions for program argument handling.
Definition: string_list.hpp:30
bcache::program_wrapper_t::get_input_files
virtual string_list_t get_input_files()
Get the paths to the input files for the command.
Definition: program_wrapper.cpp:283
bcache::program_wrapper_t::get_capabilities
virtual string_list_t get_capabilities()
Generate a list of supported capabilites.
Definition: program_wrapper.cpp:253
bcache::program_wrapper_t::preprocess_source
virtual std::string preprocess_source()
Generate the preprocessed source text.
Definition: program_wrapper.cpp:289
bcache::program_wrapper_t
The base class for all program wrappers.
Definition: program_wrapper.hpp:38
bcache::cache_t
An interface to the different caches.
Definition: cache.hpp:34
bcache::program_wrapper_t::get_build_files
virtual std::map< std::string, expected_file_t > get_build_files()
Get the paths to the files that are to be generated by the command.
Definition: program_wrapper.cpp:259
bcache::program_wrapper_t::can_handle_command
virtual bool can_handle_command()=0
Check if this class implements a wrapper for the given command.
bcache::program_wrapper_t::get_program_id
virtual std::string get_program_id()
Get a string that uniquely identifies the program.
Definition: program_wrapper.cpp:265
bcache::program_wrapper_t::get_relevant_env_vars
virtual std::map< std::string, std::string > get_relevant_env_vars()
Get relevant environment variables for hashing.
Definition: program_wrapper.cpp:277
bcache::program_wrapper_t::resolve_args
virtual void resolve_args()
Resolve arguments on the command line.
Definition: program_wrapper.cpp:249
bcache::program_wrapper_t::capabilities_t
A helper class for managing wrapper capabilities.
Definition: program_wrapper.hpp:53
bcache::program_wrapper_t::run_for_miss
virtual sys::run_result_t run_for_miss()
Run the actual command (when there is a cache miss).
Definition: program_wrapper.cpp:300
bcache::program_wrapper_t::m_active_capabilities
capabilities_t m_active_capabilities
Active capabilities.
Definition: program_wrapper.hpp:174