From 20615c2161fb491e4c87b0cc8483c2e600ae6f79 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 21 Aug 2019 18:55:27 +0300 Subject: Add extra_dir parameter for load_default_options() --- libbutl/default-options.mxx | 11 ++-- libbutl/default-options.txx | 122 +++++++++++++++++++++++++++++--------------- 2 files changed, 89 insertions(+), 44 deletions(-) (limited to 'libbutl') diff --git a/libbutl/default-options.mxx b/libbutl/default-options.mxx index 403df47..0f4b194 100644 --- a/libbutl/default-options.mxx +++ b/libbutl/default-options.mxx @@ -79,11 +79,13 @@ LIBBUTL_MODEXPORT namespace butl // // - sys_dir // - home_dir + // - extra_dir (can also be handled during the start/outer traversal) // - start_dir and outer until home_dir or root (both excluding) // - // Except for sys_dir, the options files are looked for in the .build2/ and - // .build2/local/ subdirectories of each directory. For sys_dir they are - // looked for in the directory itself (e.g., /etc/build2/). + // Except for sys_dir and extra_dir, the options files are looked for in the + // .build2/ and .build2/local/ subdirectories of each directory. For + // sys_dir and extra_dir they are looked for in the directory itself (e.g., + // /etc/build2/). // // Note that the search is stopped at the directory containing a file with // --no-default-options. @@ -97,10 +99,13 @@ LIBBUTL_MODEXPORT namespace butl // sufficient ground to definitively conclude that the file is not remote; // to be sure we would need to query the VCS or some such). // + // Note that the extra directory options files are never considered remote. + // template default_options load_default_options (const optional& sys_dir, const optional& home_dir, + const optional& extra_dir, const default_options_files&, F&&); diff --git a/libbutl/default-options.txx b/libbutl/default-options.txx index 276fa63..5a1cc0d 100644 --- a/libbutl/default-options.txx +++ b/libbutl/default-options.txx @@ -20,10 +20,10 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. // options to the resulting list. Return false if --no-default-options is // encountered. // - // Note that we check for the local/ subdirectory even if we don't think it - // belongs to the remote directory; the user may move things around or it - // could be a VCS we don't yet recognize and there doesn't seem to be any - // harm in doing so. + // Note that by default we check for the local/ subdirectory even if we + // don't think it belongs to the remote directory; the user may move things + // around or it could be a VCS we don't yet recognize and there doesn't seem + // to be any harm in doing so. // // Also note that if the directory is remote, then for now the options files // in both the directory itself and its local/ subdirectory are considered @@ -35,8 +35,12 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. bool remote, const small_vector& fs, F&& fn, - default_options& def_ops) + default_options& def_ops, + bool load_sub = true, + bool load_dir = true) { + assert (load_sub || load_dir); + bool r (true); auto load = [&fs, &fn, &def_ops, &r] (const dir_path& d, bool remote) @@ -81,13 +85,13 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. dir_path ld (d / dir_path ("local")); - if (options_dir_exists (ld)) + if (load_sub && options_dir_exists (ld)) load (ld, remote); // Don't load options from .build2/ if --no-default-options is encountered // in .build2/local/. // - if (r) + if (load_dir && r) load (d, remote); return r; @@ -97,17 +101,32 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. default_options load_default_options (const optional& sys_dir, const optional& home_dir, + const optional& extra_dir, const default_options_files& ofs, F&& fn) { + if (sys_dir) + assert (sys_dir->absolute () && sys_dir->normalized ()); + + if (home_dir) + assert (home_dir->absolute () && home_dir->normalized ()); + + if (extra_dir) + assert (extra_dir->absolute () && extra_dir->normalized ()); + default_options r; - // Search for and parse the options files in the specified and outer - // directories until root/home directory and in the system directory, - // stopping if --no-default-options is encountered and reversing the - // resulting options entry list in the end. + // Search for and parse the options files in the start and outer + // directories until root/home directory and in the extra and system + // directories, stopping if --no-default-options is encountered and + // reversing the resulting options entry list in the end. // - bool load (true); + // Note that the extra directory is handled either during the start/outer + // directory traversal, if it turns out to be a subdirectory of any of the + // traversed directories, or right after. + // + bool load (true); + bool load_extra (extra_dir && options_dir_exists (*extra_dir)); if (ofs.start) { @@ -129,7 +148,8 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. } // If the directory is remote, then mark all the previously collected - // local entries (that belong to its subdirectories) as remote too. + // local entries (that belong to its subdirectories but not to the + // extra directory) as remote too. // // @@ Note that currently the local/ subdirectory of a remote // directory is considered remote (see above for details). When @@ -144,7 +164,8 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. // for (default_options_entry& e: r) { - if (!e.remote) + if (!e.remote && + (!extra_dir || e.file.directory () != *extra_dir)) { e.remote = true; @@ -162,12 +183,34 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. { dir_path od (d / dir_path (".build2")); - if (options_dir_exists (od)) + // If the extra directory is a subdirectory of the current + // directory, then load it first, but don't load .build2/ or + // .build2/local/ if it matches any of them. + // + bool load_build2 (true); + bool load_build2_local (true); + + if (load_extra && extra_dir->sub (d)) + { + load = load_default_options_files (*extra_dir, + false /* remote */, + ofs.files, + std::forward (fn), + r); + + load_extra = false; + load_build2 = *extra_dir != od; + load_build2_local = *extra_dir != od / dir_path ("local"); + } + + if (load && options_dir_exists (od)) load = load_default_options_files (od, remote, ofs.files, std::forward (fn), - r); + r, + load_build2_local, + load_build2); } if (!load && remote) @@ -175,35 +218,32 @@ LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason. } } - if (home_dir) - { - assert (home_dir->absolute () && home_dir->normalized ()); + if (load && load_extra) + load = load_default_options_files (*extra_dir, + false /* remote */, + ofs.files, + std::forward (fn), + r); - if (load) - { - dir_path d (*home_dir / dir_path (".build2")); - - if (options_dir_exists (d)) - load = load_default_options_files (d, - false /* remote */, - ofs.files, - std::forward (fn), - r); - } - } - - if (sys_dir) + if (load && home_dir) { - assert (sys_dir->absolute () && sys_dir->normalized ()); - - if (load && options_dir_exists (*sys_dir)) - load_default_options_files (*sys_dir, - false /* remote */, - ofs.files, - std::forward (fn), - r); + dir_path d (*home_dir / dir_path (".build2")); + + if (options_dir_exists (d)) + load = load_default_options_files (d, + false /* remote */, + ofs.files, + std::forward (fn), + r); } + if (load && sys_dir && options_dir_exists (*sys_dir)) + load_default_options_files (*sys_dir, + false /* remote */, + ofs.files, + std::forward (fn), + r); + std::reverse (r.begin (), r.end ()); return r; -- cgit v1.1