aboutsummaryrefslogtreecommitdiff
path: root/build2/dist
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2016-01-05 11:55:15 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2016-01-05 11:55:15 +0200
commit9fb791e9fad6c63fc1dac49f4d05ae63b8a3db9b (patch)
treed60322d4382ca5f97b676c5abe2e39524f35eab4 /build2/dist
parentf159b1dac68c8714f7ba71ca168e3b695891aad9 (diff)
Rename build directory/namespace to build2
Diffstat (limited to 'build2/dist')
-rw-r--r--build2/dist/module26
-rw-r--r--build2/dist/module.cxx142
-rw-r--r--build2/dist/operation18
-rw-r--r--build2/dist/operation.cxx459
-rw-r--r--build2/dist/rule29
-rw-r--r--build2/dist/rule.cxx55
6 files changed, 729 insertions, 0 deletions
diff --git a/build2/dist/module b/build2/dist/module
new file mode 100644
index 0000000..29dca71
--- /dev/null
+++ b/build2/dist/module
@@ -0,0 +1,26 @@
+// file : build2/dist/module -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifndef BUILD2_DIST_MODULE
+#define BUILD2_DIST_MODULE
+
+#include <build2/types>
+#include <build2/utility>
+
+#include <build2/module>
+
+namespace build2
+{
+ namespace dist
+ {
+ extern "C" void
+ dist_boot (scope&, const location&, unique_ptr<module>&);
+
+ extern "C" bool
+ dist_init (
+ scope&, scope&, const location&, unique_ptr<module>&, bool, bool);
+ }
+}
+
+#endif // BUILD2_DIST_MODULE
diff --git a/build2/dist/module.cxx b/build2/dist/module.cxx
new file mode 100644
index 0000000..00b709e
--- /dev/null
+++ b/build2/dist/module.cxx
@@ -0,0 +1,142 @@
+// file : build2/dist/module.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <build2/dist/module>
+
+#include <build2/scope>
+#include <build2/file>
+#include <build2/diagnostics>
+
+#include <build2/config/utility>
+
+#include <build2/dist/rule>
+#include <build2/dist/operation>
+
+using namespace std;
+using namespace butl;
+
+namespace build2
+{
+ namespace dist
+ {
+ static rule rule_;
+
+ extern "C" void
+ dist_boot (scope& r, const location&, unique_ptr<module>&)
+ {
+ tracer trace ("dist::boot");
+
+ level5 ([&]{trace << "for " << r.out_path ();});
+
+ // Register meta-operation.
+ //
+ r.meta_operations.insert (dist_id, dist);
+ }
+
+ extern "C" bool
+ dist_init (scope& r,
+ scope&,
+ const location& l,
+ unique_ptr<module>&,
+ bool first,
+ bool)
+ {
+ tracer trace ("dist::init");
+
+ if (!first)
+ {
+ warn (l) << "multiple dist module initializations";
+ return true;
+ }
+
+ const dir_path& out_root (r.out_path ());
+ level5 ([&]{trace << "for " << out_root;});
+
+ // Enter module variables.
+ //
+ if (first)
+ {
+ auto& v (var_pool);
+
+ v.find ("dist", bool_type);
+
+ v.find ("dist.package", string_type);
+
+ v.find ("dist.root", dir_path_type);
+ v.find ("config.dist.root", dir_path_type);
+
+ //@@ VAR type
+ //
+ v.find ("dist.cmd", string_type);
+ v.find ("config.dist.cmd", string_type);
+
+ v.find ("dist.archives", strings_type);
+ v.find ("config.dist.archives", strings_type);
+ }
+
+ // Register our wildcard rule. Do it explicitly for the alias
+ // to prevent something like insert<target>(dist_id, test_id)
+ // taking precedence.
+ //
+ r.rules.insert<target> (dist_id, 0, "dist", rule_);
+ r.rules.insert<alias> (dist_id, 0, "dist.alias", rule_);
+
+ // Configuration.
+ //
+ // Note that we don't use any defaults for root -- the location
+ // must be explicitly specified or we will complain if and when
+ // we try to dist.
+ //
+ using namespace config;
+
+ bool s (specified (r, "config.dist"));
+
+ // dist.root
+ //
+ {
+ value& v (r.assign ("dist.root"));
+
+ if (s)
+ {
+ const value& cv (optional_absolute (r, "config.dist.root"));
+
+ if (cv && !cv.empty ())
+ v = cv;
+ }
+ }
+
+ // dist.cmd
+ //
+ {
+ value& v (r.assign ("dist.cmd"));
+
+ if (s)
+ {
+ const value& cv (required (r, "config.dist.cmd", "install").first);
+
+ if (cv && !cv.empty ())
+ v = cv;
+ }
+ else
+ v = "install";
+ }
+
+ // dist.archives
+ //
+ {
+ value& v (r.assign ("dist.archives"));
+
+ if (s)
+ {
+ const value& cv (optional (r, "config.dist.archives"));
+
+ if (cv && !cv.empty ())
+ v = cv;
+ }
+ }
+
+ return true;
+ }
+ }
+}
diff --git a/build2/dist/operation b/build2/dist/operation
new file mode 100644
index 0000000..eb5da76
--- /dev/null
+++ b/build2/dist/operation
@@ -0,0 +1,18 @@
+// file : build2/dist/operation -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifndef BUILD2_DIST_OPERATION
+#define BUILD2_DIST_OPERATION
+
+#include <build2/operation>
+
+namespace build2
+{
+ namespace dist
+ {
+ extern meta_operation_info dist;
+ }
+}
+
+#endif // BUILD2_DIST_OPERATION
diff --git a/build2/dist/operation.cxx b/build2/dist/operation.cxx
new file mode 100644
index 0000000..24bdd31
--- /dev/null
+++ b/build2/dist/operation.cxx
@@ -0,0 +1,459 @@
+// file : build2/dist/operation.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <build2/dist/operation>
+
+#include <cassert>
+
+#include <butl/process>
+#include <butl/filesystem>
+
+#include <build2/file>
+#include <build2/dump>
+#include <build2/scope>
+#include <build2/target>
+#include <build2/context>
+#include <build2/algorithm>
+#include <build2/diagnostics>
+
+using namespace std;
+using namespace butl;
+
+namespace build2
+{
+ namespace dist
+ {
+ static void
+ dist_meta_operation_pre ()
+ {
+ // Reset the dependency state so that we don't end up with stray
+ // files from previous batches.
+ //
+ // @@ This is called too late, after we have bootstrapped the
+ // project.
+ //
+ //reset ();
+ }
+
+ static operation_id
+ dist_operation_pre (operation_id o)
+ {
+ if (o != default_id)
+ fail << "explicit operation specified for dist meta-operation";
+
+ return o;
+ }
+
+ static void
+ dist_match (action, action_targets&)
+ {
+ // Don't match anything -- see execute ().
+ }
+
+ // install -d <dir>
+ //
+ static void
+ install (const string& cmd, const dir_path&);
+
+ // install <file> <dir>
+ //
+ static void
+ install (const string& cmd, file&, const dir_path&);
+
+ // cd <root> && tar|zip ... <pkg>.<ext> <pkg>
+ //
+ static void
+ archive (const dir_path& root, const string& pkg, const string& ext);
+
+ static void
+ dist_execute (action, const action_targets& ts, bool)
+ {
+ tracer trace ("dist_execute");
+
+ // For now we assume all the targets are from the same project.
+ //
+ target& t (*static_cast<target*> (ts[0]));
+ scope* rs (t.base_scope ().root_scope ());
+
+ if (rs == nullptr)
+ fail << "out of project target " << t;
+
+ const dir_path& out_root (rs->out_path ());
+ const dir_path& src_root (rs->src_path ());
+
+ if (out_root == src_root)
+ fail << "in-tree distribution of target " << t <<
+ info << "distribution requires out-of-tree build";
+
+ // Make sure we have the necessary configuration before
+ // we get down to business.
+ //
+ auto l (rs->vars["dist.root"]);
+
+ if (!l || l->empty ())
+ fail << "unknown root distribution directory" <<
+ info << "did you forget to specify config.dist.root?";
+
+ const dir_path& dist_root (as<dir_path> (*l));
+
+ if (!dir_exists (dist_root))
+ fail << "root distribution directory " << dist_root
+ << " does not exist";
+
+ l = rs->vars["dist.package"];
+
+ if (!l || l->empty ())
+ fail << "unknown distribution package name" <<
+ info << "did you forget to set dist.package?";
+
+ const string& dist_package (as<string> (*l));
+ const string& dist_cmd (as<string> (*rs->vars["dist.cmd"]));
+
+ // Get the list of operations supported by this project. Skip
+ // default_id.
+ //
+ for (operations::size_type id (default_id + 1);
+ id < rs->operations.size ();
+ ++id)
+ {
+ const operation_info* oi (rs->operations[id]);
+ if (oi == nullptr)
+ continue;
+
+ // Note that we are not calling operation_pre/post() callbacks
+ // here since the meta operation is dist and we know what we
+ // are doing.
+ //
+ current_inner_oif = oi;
+ current_outer_oif = nullptr;
+ current_mode = oi->mode;
+ dependency_count = 0;
+
+ action a (dist_id, id);
+
+ if (verb >= 6)
+ dump (a);
+
+ for (void* v: ts)
+ {
+ target& t (*static_cast<target*> (v));
+
+ if (rs != t.base_scope ().root_scope ())
+ fail << "out of project target " << t;
+
+ level5 ([&]{trace << diag_doing (a, t);});
+
+ match (a, t);
+ }
+
+ if (verb >= 6)
+ dump (a);
+ }
+
+ // Add buildfiles that are not normally loaded as part of the
+ // project, for example, the export stub. They will still be
+ // ignored on the next step if the user explicitly marked them
+ // nodist.
+ //
+ auto add_adhoc = [&src_root, &trace] (const dir_path& d, const char* f)
+ {
+ path p (d / path (f));
+ if (file_exists (p))
+ {
+ const char* e (p.extension ());
+ targets.insert<buildfile> (
+ p.directory (),
+ p.leaf ().base ().string (),
+ &extension_pool.find (e == nullptr ? "" : e), // Specified.
+ trace);
+ }
+ };
+
+ add_adhoc (src_root, "build/export.build");
+
+ // The same for subprojects that have been loaded.
+ //
+ if (auto l = rs->vars["subprojects"])
+ {
+ for (auto p: as<subprojects> (*l))
+ {
+ const dir_path& pd (p.second);
+ dir_path out_nroot (out_root / pd);
+ scope& nrs (scopes.find (out_nroot));
+
+ if (nrs.out_path () != out_nroot) // This subproject not loaded.
+ continue;
+
+ const dir_path& src_nroot (nrs.src_path ());
+
+ if (!src_nroot.sub (src_root)) // Not a strong amalgamation.
+ continue;
+
+ add_adhoc (src_nroot, "build/export.build");
+ }
+ }
+
+ // Collect the files. We want to take the snapshot of targets
+ // since updating some of them may result in more targets being
+ // entered.
+ //
+ action_targets files;
+ const variable& dist_var (var_pool.find ("dist"));
+
+ for (const auto& pt: targets)
+ {
+ file* ft (pt->is_a<file> ());
+
+ if (ft == nullptr) // Not a file.
+ continue;
+
+ if (ft->dir.sub (src_root))
+ {
+ // Include unless explicitly excluded.
+ //
+ auto l ((*ft)[dist_var]);
+
+ if (l && !as<bool> (*l))
+ level5 ([&]{trace << "excluding " << *ft;});
+ else
+ files.push_back (ft);
+
+ continue;
+ }
+
+ if (ft->dir.sub (out_root))
+ {
+ // Exclude unless explicitly included.
+ //
+ auto l ((*ft)[dist_var]);
+
+ if (l && as<bool> (*l))
+ {
+ level5 ([&]{trace << "including " << *ft;});
+ files.push_back (ft);
+ }
+
+ continue;
+ }
+ }
+
+ // Make sure what we need to distribute is up to date.
+ //
+ {
+ if (perform.meta_operation_pre != nullptr)
+ perform.meta_operation_pre ();
+
+ current_mif = &perform;
+
+ if (perform.operation_pre != nullptr)
+ perform.operation_pre (update_id);
+
+ current_inner_oif = &update;
+ current_outer_oif = nullptr;
+ current_mode = update.mode;
+ dependency_count = 0;
+
+ action a (perform_id, update_id);
+
+ perform.match (a, files);
+ perform.execute (a, files, true); // Run quiet.
+
+ if (perform.operation_post != nullptr)
+ perform.operation_post (update_id);
+
+ if (perform.meta_operation_post != nullptr)
+ perform.meta_operation_post ();
+ }
+
+ dir_path td (dist_root / dir_path (dist_package));
+
+ // Clean up the target directory.
+ //
+ // @@ Not for incremental dist?
+ //
+ if (build2::rmdir_r (td) == rmdir_status::not_empty)
+ fail << "unable to clean target directory " << td;
+
+ install (dist_cmd, td);
+
+ // Copy over all the files.
+ //
+ for (void* v: files)
+ {
+ file& t (*static_cast<file*> (v));
+
+ // Figure out where this file is inside the target directory.
+ //
+ dir_path d (td);
+ d /= t.dir.sub (src_root)
+ ? t.dir.leaf (src_root)
+ : t.dir.leaf (out_root);
+
+ if (!dir_exists (d))
+ install (dist_cmd, d);
+
+ install (dist_cmd, t, d);
+ }
+
+ // Archive if requested.
+ //
+ if (auto l = rs->vars["dist.archives"])
+ {
+ for (const string& e: as<strings> (*l))
+ archive (dist_root, dist_package, e);
+ }
+ }
+
+ // install -d <dir>
+ //
+ static void
+ install (const string& cmd, const dir_path& d)
+ {
+ path reld (relative (d));
+
+ cstrings args {cmd.c_str (), "-d"};
+
+ args.push_back ("-m");
+ args.push_back ("755");
+ args.push_back (reld.string ().c_str ());
+ args.push_back (nullptr);
+
+ if (verb >= 2)
+ print_process (args);
+ else if (verb)
+ text << "dist -d " << d;
+
+ try
+ {
+ process pr (args.data ());
+
+ if (!pr.wait ())
+ throw failed ();
+ }
+ catch (const process_error& e)
+ {
+ error << "unable to execute " << args[0] << ": " << e.what ();
+
+ if (e.child ())
+ exit (1);
+
+ throw failed ();
+ }
+ }
+
+ // install <file> <dir>
+ //
+ static void
+ install (const string& cmd, file& t, const dir_path& d)
+ {
+ path reld (relative (d));
+ path relf (relative (t.path ()));
+
+ cstrings args {cmd.c_str ()};
+
+ // Preserve timestamps. This could becomes important if, for
+ // example, we have pre-generated sources. Note that the
+ // install-sh script doesn't support this option, while both
+ // Linux and BSD install's do.
+ //
+ args.push_back ("-p");
+
+ // Assume the file is executable if the owner has execute
+ // permission, in which case we make it executable for
+ // everyone.
+ //
+ args.push_back ("-m");
+ args.push_back (
+ (path_permissions (t.path ()) & permissions::xu) == permissions::xu
+ ? "755"
+ : "644");
+
+ args.push_back (relf.string ().c_str ());
+ args.push_back (reld.string ().c_str ());
+ args.push_back (nullptr);
+
+ if (verb >= 2)
+ print_process (args);
+ else if (verb)
+ text << "dist " << t;
+
+ try
+ {
+ process pr (args.data ());
+
+ if (!pr.wait ())
+ throw failed ();
+ }
+ catch (const process_error& e)
+ {
+ error << "unable to execute " << args[0] << ": " << e.what ();
+
+ if (e.child ())
+ exit (1);
+
+ throw failed ();
+ }
+ }
+
+ static void
+ archive (const dir_path& root, const string& pkg, const string& e)
+ {
+ string a (pkg + '.' + e);
+
+ // Delete old archive for good measure.
+ //
+ path ap (root / path (a));
+ if (file_exists (ap))
+ rmfile (ap);
+
+ // Use zip for .zip archives. Everything else goes to tar in the
+ // auto-compress mode (-a).
+ //
+ cstrings args;
+ if (e == "zip")
+ args = {"zip", "-rq", a.c_str (), pkg.c_str (), nullptr};
+ else
+ args = {"tar", "-a", "-cf", a.c_str (), pkg.c_str (), nullptr};
+
+ if (verb >= 2)
+ print_process (args);
+ else if (verb)
+ text << args[0] << " " << ap;
+
+ try
+ {
+ // Change child's working directory to dist_root.
+ //
+ process pr (root.string ().c_str (), args.data ());
+
+ if (!pr.wait ())
+ throw failed ();
+ }
+ catch (const process_error& e)
+ {
+ error << "unable to execute " << args[0] << ": " << e.what ();
+
+ if (e.child ())
+ exit (1);
+
+ throw failed ();
+ }
+ }
+
+ meta_operation_info dist {
+ "dist",
+ "distribute",
+ "distributing",
+ "has nothing to distribute", // We cannot "be distributed".
+ &dist_meta_operation_pre,
+ &dist_operation_pre,
+ &load, // normal load
+ &search, // normal search
+ &dist_match,
+ &dist_execute,
+ nullptr, // operation post
+ nullptr // meta-operation post
+ };
+ }
+}
diff --git a/build2/dist/rule b/build2/dist/rule
new file mode 100644
index 0000000..2fbf33b
--- /dev/null
+++ b/build2/dist/rule
@@ -0,0 +1,29 @@
+// file : build2/dist/rule -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#ifndef BUILD2_DIST_RULE
+#define BUILD2_DIST_RULE
+
+#include <build2/rule>
+#include <build2/types>
+#include <build2/target>
+#include <build2/operation>
+
+namespace build2
+{
+ namespace dist
+ {
+ class rule: public build2::rule
+ {
+ public:
+ virtual match_result
+ match (action, target&, const std::string&) const;
+
+ virtual recipe
+ apply (action, target&, const match_result&) const;
+ };
+ }
+}
+
+#endif // BUILD2_DIST_RULE
diff --git a/build2/dist/rule.cxx b/build2/dist/rule.cxx
new file mode 100644
index 0000000..10f35f5
--- /dev/null
+++ b/build2/dist/rule.cxx
@@ -0,0 +1,55 @@
+// file : build2/dist/rule.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <build2/dist/rule>
+
+#include <build2/scope>
+#include <build2/target>
+#include <build2/algorithm>
+#include <build2/diagnostics>
+
+using namespace std;
+
+namespace build2
+{
+ namespace dist
+ {
+ match_result rule::
+ match (action, target& t, const std::string&) const
+ {
+ return t; // We always match.
+ }
+
+ recipe rule::
+ apply (action a, target& t, const match_result&) const
+ {
+ const dir_path& out_root (t.root_scope ().out_path ());
+
+ auto r (group_prerequisite_members (a, t, false));
+ for (auto i (r.begin ()); i != r.end (); ++i)
+ {
+ prerequisite_member p (*i);
+
+ // Skip prerequisites imported from other projects.
+ //
+ if (p.proj () != nullptr)
+ continue;
+
+ // If we can, go inside see-through groups.
+ //
+ if (p.type ().see_through && i.enter_group ())
+ continue;
+
+ target& pt (p.search ());
+
+ // Don't match targets that are outside of our project.
+ //
+ if (pt.dir.sub (out_root))
+ build2::match (a, pt);
+ }
+
+ return noop_recipe; // We will never be executed.
+ }
+ }
+}