BuildCache
lua_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_LUA_WRAPPER_HPP_
21 #define BUILDCACHE_LUA_WRAPPER_HPP_
22 
23 #include <base/env_utils.hpp>
24 #include <wrappers/program_wrapper.hpp>
25 
26 extern "C" {
27 typedef struct lua_State lua_State;
28 }
29 
30 namespace bcache {
36 public:
37  lua_wrapper_t(const file::exe_path_t& exe_path,
38  const string_list_t& args,
39  const std::string& lua_script_path);
40 
41  bool can_handle_command() override;
42 
43 private:
44  // A helper class for managing the Lua state.
45  class runner_t {
46  public:
47  runner_t(const std::string& script_path, const string_list_t& args);
48  ~runner_t();
49 
50  bool call(const std::string& func);
51 
52  const std::string& script() const {
53  return m_script;
54  }
55 
56  lua_State* state() {
57  return m_state;
58  }
59 
60  private:
61  void init_lua_state();
62  void setup_lua_libs_and_globals();
63  [[noreturn]] void bail(const std::string& message);
64 
65  lua_State* m_state = nullptr;
66  const std::string m_script_path;
67  const string_list_t m_args;
68  scoped_set_env_t m_lua_path_env;
69  std::string m_script;
70  };
71 
72  void resolve_args() override;
74  std::map<std::string, expected_file_t> get_build_files() override;
75  std::string get_program_id() override;
77  std::map<std::string, std::string> get_relevant_env_vars() override;
78  string_list_t get_input_files() override;
79  std::string preprocess_source() override;
82 
83  runner_t m_runner;
84 };
85 } // namespace bcache
86 #endif // BUILDCACHE_LUA_WRAPPER_HPP_
bcache::lua_wrapper_t::get_relevant_arguments
string_list_t get_relevant_arguments() override
Get relevant command line arguments for hashing.
Definition: lua_wrapper.cpp:565
bcache::lua_wrapper_t::resolve_args
void resolve_args() override
Resolve arguments on the command line.
Definition: lua_wrapper.cpp:537
bcache::file::exe_path_t
Path to an executable file.
Definition: file_utils.hpp:127
bcache::lua_wrapper_t::get_program_id
std::string get_program_id() override
Get a string that uniquely identifies the program.
Definition: lua_wrapper.cpp:558
bcache::sys::run_result_t
Run results from an external command.
Definition: sys_utils.hpp:30
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::lua_wrapper_t::runner_t
Definition: lua_wrapper.hpp:45
bcache::lua_wrapper_t::get_input_files
string_list_t get_input_files() override
Get the paths to the input files for the command.
Definition: lua_wrapper.cpp:579
bcache::program_wrapper_t
The base class for all program wrappers.
Definition: program_wrapper.hpp:38
bcache::lua_wrapper_t::get_implicit_input_files
string_list_t get_implicit_input_files() override
Get a list of paths to implicit input files.
Definition: lua_wrapper.cpp:593
bcache::lua_wrapper_t::get_build_files
std::map< std::string, expected_file_t > get_build_files() override
Get the paths to the files that are to be generated by the command.
Definition: lua_wrapper.cpp:550
bcache::lua_wrapper_t::can_handle_command
bool can_handle_command() override
Check if this class implements a wrapper for the given command.
Definition: lua_wrapper.cpp:517
bcache::scoped_set_env_t
A class for temporarily modifying an environment variable.
Definition: env_utils.hpp:55
bcache::lua_wrapper_t::get_relevant_env_vars
std::map< std::string, std::string > get_relevant_env_vars() override
Get relevant environment variables for hashing.
Definition: lua_wrapper.cpp:572
bcache::lua_wrapper_t::get_capabilities
string_list_t get_capabilities() override
Generate a list of supported capabilites.
Definition: lua_wrapper.cpp:543
bcache::lua_wrapper_t::preprocess_source
std::string preprocess_source() override
Generate the preprocessed source text.
Definition: lua_wrapper.cpp:586
bcache::lua_wrapper_t::run_for_miss
sys::run_result_t run_for_miss() override
Run the actual command (when there is a cache miss).
Definition: lua_wrapper.cpp:600
bcache::lua_wrapper_t
A program wrapper that wraps Lua scripts.
Definition: lua_wrapper.hpp:35