main.cpp 6.46 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2019 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#include "header_rewrite.hpp"
#include "uncomment.hpp"
#include "util.hpp"

using namespace std;

class ResourceInfo
{
public:
    ResourceInfo(const string& source, const vector<string>& _subdirs, bool recursive = false)
        : search_path(source)
        , subdirs(_subdirs)
        , is_recursive(recursive)

    {
    }

    const string search_path;
    const vector<string> subdirs;
    const bool is_recursive;

    vector<string> files;
};

string find_path(const string& path)
{
    string rc;
    iterate_files(path,
                  [&](const string& file, bool is_dir) {
                      if (is_dir)
                      {
                          string dir_name = get_file_name(file);
                          if (is_version_number(dir_name))
                          {
                              rc = file;
                          }
                      }
                  },
                  true);
    return rc;
}

int main(int argc, char** argv)
{
    time_t main_timestamp = get_timestamp(argv[0]);
    static vector<string> valid_ext = {".h", ".hpp", ".tcc", ""};
    string output_path;
    string base_name;

    for (size_t i = 1; i < argc; i++)
    {
76 77
        string arg = argv[i];
        if (arg == "--output")
78 79 80
        {
            output_path = argv[++i];
        }
81
        else if (arg == "--base_name")
82 83 84 85 86
        {
            base_name = argv[++i];
        }
    }

87 88 89
    vector<ResourceInfo> include_paths;

#ifdef __APPLE__
90
#ifdef EIGEN_HEADERS_PATH
91
    include_paths.push_back({EIGEN_HEADERS_PATH, {}, true});
92 93
#endif
#ifdef MKLDNN_HEADERS_PATH
94
    include_paths.push_back({MKLDNN_HEADERS_PATH, {}, true});
95 96
#endif
#ifdef TBB_HEADERS_PATH
97 98 99 100 101
    include_paths.push_back({TBB_HEADERS_PATH, {}, true});
#endif
    include_paths.push_back({NGRAPH_HEADERS_PATH, {}, true});
    include_paths.push_back({CLANG_BUILTIN_HEADERS_PATH, {}, true});
#else // __APPLE__
102
    include_paths.push_back({CLANG_BUILTIN_HEADERS_PATH, {}, true});
103
#ifdef EIGEN_HEADERS_PATH
104
    include_paths.push_back({EIGEN_HEADERS_PATH, {}, true});
105 106
#endif
#ifdef MKLDNN_HEADERS_PATH
107
    include_paths.push_back({MKLDNN_HEADERS_PATH, {}, true});
108
#endif
109
    include_paths.push_back({NGRAPH_HEADERS_PATH, {}, true});
110
#ifdef TBB_HEADERS_PATH
111
    include_paths.push_back({TBB_HEADERS_PATH, {}, true});
112
#endif
113
#endif
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

    if (output_path.empty())
    {
        cout << "must specify output path with --output option" << endl;
        return -1;
    }

    time_t output_timestamp = get_timestamp(output_path);

    for (ResourceInfo& path : include_paths)
    {
        // cout << "path " << path.source_path << " -> " << path.target_path << endl;
        vector<string> path_list;
        path_list.push_back(path.search_path);
        for (const string& p : path.subdirs)
        {
            path_list.push_back(path_join(path.search_path, p));
        }
        for (const string& p : path_list)
        {
            iterate_files(p,
                          [&](const string& file, bool is_dir) {
                              if (!is_dir)
                              {
                                  string ext = get_file_ext(file);
                                  if (contains(valid_ext, ext))
                                  {
                                      //   cout << "add " << path.search_path << ", " << file << endl;
                                      path.files.push_back(file);
                                  }
                              }
                          },
                          path.is_recursive);
        }
    }

    // test for changes to any headers
    bool update_needed = main_timestamp > output_timestamp;
    if (!update_needed)
    {
        for (ResourceInfo& path : include_paths)
        {
            for (const string& header_file : path.files)
            {
                time_t file_timestamp = get_timestamp(header_file);
                if (file_timestamp > output_timestamp)
                {
                    update_needed = true;
                    break;
                }
            }
        }
    }

    if (update_needed)
    {
170 171 172
        size_t total_size = 0;
        size_t total_count = 0;
        const string prefix = "pReFiX";
173 174 175 176 177
        ofstream out(output_path);
        out << "#pragma clang diagnostic ignored \"-Weverything\"\n";
        out << "#include <vector>\n";
        out << "namespace ngraph\n";
        out << "{\n";
178
        out << "    const std::vector<std::string> builtin_search_paths =\n";
179 180 181
        out << "    {\n";
        for (const ResourceInfo& path : include_paths)
        {
182 183 184 185 186 187 188 189 190
            out << "        \"" << path.search_path << "\",\n";
        }
        out << "    };\n";

        out << "    const std::vector<std::pair<std::string, std::string>> builtin_headers =\n";
        out << "    {\n";
        for (const ResourceInfo& path : include_paths)
        {
            for (const string& header_path : path.files)
191
            {
192 193 194
                string header_data = read_file_to_string(header_path);
                string relative_path = header_path.substr(path.search_path.size() + 1);
                header_data = rewrite_header(header_data, relative_path);
195 196 197 198
                // header_data = uncomment(header_data);
                total_size += header_data.size();
                total_count++;

199 200 201
                out << "        {";
                out << "\"" << header_path << "\",\nR\"" << prefix << "(" << header_data << ")"
                    << prefix << "\"},\n";
202 203 204 205 206 207 208
            }
        }
        out << "    };\n";
        out << "}\n";
        cout.imbue(locale(""));
        cout << "Total size " << total_size << " in " << total_count << " files\n";
    }
209
    return 0;
210
}