Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce0bd06d authored by Joe Onorato's avatar Joe Onorato
Browse files

[bit] Cache the build variables so we don't have to run make to figure them...

[bit] Cache the build variables so we don't have to run make to figure them out unless the environment has changed.

Test: bit GooglePermissionControllerTest:*
Change-Id: Ie79226026477df22115ed1146875b82c2255bdef
parent 08f1ef2d
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -623,12 +623,13 @@ run_phases(vector<Target*> targets, const Options& options)
    const string buildProduct = get_required_env("TARGET_PRODUCT", false);
    const string buildVariant = get_required_env("TARGET_BUILD_VARIANT", false);
    const string buildType = get_required_env("TARGET_BUILD_TYPE", false);

    const string buildOut = get_out_dir();
    chdir_or_exit(buildTop.c_str());

    const string buildDevice = get_build_var("TARGET_DEVICE", false);
    const string buildId = get_build_var("BUILD_ID", false);
    const string buildOut = get_out_dir();
    BuildVars buildVars(buildOut, buildProduct, buildVariant, buildType);

    const string buildDevice = buildVars.GetBuildVar("TARGET_DEVICE", false);
    const string buildId = buildVars.GetBuildVar("BUILD_ID", false);

    // Get the modules for the targets
    map<string,Module> modules;
@@ -1004,13 +1005,16 @@ run_phases(vector<Target*> targets, const Options& options)
void
run_tab_completion(const string& word)
{
    const string buildTop = get_required_env("ANDROID_BUILD_TOP", true);
    const string buildTop = get_required_env("ANDROID_BUILD_TOP", false);
    const string buildProduct = get_required_env("TARGET_PRODUCT", false);
    const string buildVariant = get_required_env("TARGET_BUILD_VARIANT", false);
    const string buildType = get_required_env("TARGET_BUILD_TYPE", false);
    const string buildOut = get_out_dir();

    chdir_or_exit(buildTop.c_str());

    string buildDevice = sniff_device_name(buildOut, buildProduct);
    BuildVars buildVars(buildOut, buildProduct, buildVariant, buildType);

    string buildDevice = buildVars.GetBuildVar("TARGET_DEVICE", false);

    map<string,Module> modules;
    read_modules(buildOut, buildDevice, &modules, true);
+102 −37
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "util.h"

#include <json/reader.h>
#include <json/writer.h>
#include <json/value.h>

#include <fstream>
@@ -34,62 +35,126 @@

using namespace std;

map<string,string> g_buildVars;
static bool
map_contains(const map<string,string>& m, const string& k, const string& v) {
    map<string,string>::const_iterator it = m.find(k);
    if (it == m.end()) {
        return false;
    }
    return it->second == v;
}

string
get_build_var(const string& name, bool quiet)
static string
make_cache_filename(const string& outDir)
{
    int err;
    string filename(outDir);
    return filename + "/.bit_cache";
}

    map<string,string>::iterator it = g_buildVars.find(name);
    if (it == g_buildVars.end()) {
        Command cmd("build/soong/soong_ui.bash");
        cmd.AddArg("--dumpvar-mode");
        cmd.AddArg(name);
BuildVars::BuildVars(const string& outDir, const string& buildProduct,
        const string& buildVariant, const string& buildType)
    :m_filename(),
     m_cache()
{
    m_cache["TARGET_PRODUCT"] = buildProduct;
    m_cache["TARGET_BUILD_VARIANT"] = buildVariant;
    m_cache["TARGET_BUILD_TYPE"] = buildType;

        string output = trim(get_command_output(cmd, &err, quiet));
        if (err == 0) {
            g_buildVars[name] = output;
            return output;
        } else {
            return string();
        }
    } else {
        return it->second;
    // If we have any problems reading the file, that's ok, just do
    // uncached calls to make / soong.

    if (outDir == "") {
        return;
    }


    m_filename = make_cache_filename(outDir);

    std::ifstream stream(m_filename, std::ifstream::binary);

    if (stream.fail()) {
        return;
    }

string
sniff_device_name(const string& buildOut, const string& product)
{
    string match("ro.build.product=" + product);
    Json::Value json;
    Json::Reader reader;
    if (!reader.parse(stream, json)) {
        return;
    }

    string base(buildOut + "/target/product");
    DIR* dir = opendir(base.c_str());
    if (dir == NULL) {
        return string();
    if (!json.isObject()) {
        return;
    }

    dirent* entry;
    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_name[0] == '.') {
    map<string,string> cache;

    vector<string> names = json.getMemberNames();
    const int N = names.size();
    for (int i=0; i<N; i++) {
        const string& name = names[i];
        const Json::Value& value = json[name];
        if (!value.isString()) {
            continue;
        }
        if (entry->d_type == DT_DIR) {
            string filename(base + "/" + entry->d_name + "/system/build.prop");
            vector<string> lines;
            split_lines(&lines, read_file(filename));
            for (size_t i=0; i<lines.size(); i++) {
                if (lines[i] == match) {
                    return entry->d_name;
        cache[name] = value.asString();
    }

    // If all of the base variables match, then we can use this cache.  Otherwise, use our
    // base one.  The next time someone reads a value, the new one, with our base varaibles
    // will be saved.
    if (map_contains(cache, "TARGET_PRODUCT", buildProduct)
            && map_contains(cache, "TARGET_BUILD_VARIANT", buildVariant)
            && map_contains(cache, "TARGET_BUILD_TYPE", buildType)) {
        m_cache = cache;
    }
}

BuildVars::~BuildVars()
{
}

void
BuildVars::save()
{
    if (m_filename == "") {
        return;
    }

    Json::StyledStreamWriter writer("  ");

    Json::Value json(Json::objectValue);

    for (map<string,string>::const_iterator it = m_cache.begin(); it != m_cache.end(); it++) {
        json[it->first] = it->second;
    }

    closedir(dir);
    std::ofstream stream(m_filename, std::ofstream::binary);
    writer.write(stream, json);
}

string
BuildVars::GetBuildVar(const string& name, bool quiet)
{
    int err;

    map<string,string>::iterator it = m_cache.find(name);
    if (it == m_cache.end()) {
        Command cmd("build/soong/soong_ui.bash");
        cmd.AddArg("--dumpvar-mode");
        cmd.AddArg(name);

        string output = trim(get_command_output(cmd, &err, quiet));
        if (err == 0) {
            m_cache[name] = output;
            save();
            return output;
        } else {
            return string();
        }
    } else {
        return it->second;
    }
}

void
json_error(const string& filename, const char* error, bool quiet)
+18 −8
Original line number Diff line number Diff line
@@ -31,16 +31,26 @@ struct Module
    vector<string> installed;
};

string get_build_var(const string& name, bool quiet);

/**
 * Poke around in the out directory and try to find a device name that matches
 * our product. This is faster than running get_build_var and good enough for
 * tab completion.
 *
 * Returns the empty string if we can't find one.
 * Class to encapsulate getting build variables. Caches the
 * results if possible.
 */
string sniff_device_name(const string& buildOut, const string& product);
class BuildVars
{
public:
    BuildVars(const string& outDir, const string& buildProduct,
            const string& buildVariant, const string& buildType);
    ~BuildVars();

    string GetBuildVar(const string& name, bool quiet);

private:
    void save();

    string m_filename;

    map<string,string> m_cache;
};

void read_modules(const string& buildOut, const string& buildDevice,
        map<string,Module>* modules, bool quiet);