summaryrefslogtreecommitdiff
path: root/tools/minicargo/manifest.h
blob: d0c537e56e2ad3ea18d545584b5c80dd8327fed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * mrustc "minicargo" (minimal cargo clone)
 * - By John Hodge (Mutabah)
 *
 * manifest.h
 * - Cargo.toml package manifests
 */
#pragma once

#include <string>
#include <vector>
#include <map>
#include <memory>
#include <functional>
#include <path.h>

class PackageManifest;
class Repository;
struct TomlKeyValue;

struct PackageVersion
{
    unsigned major;
    unsigned minor;
    unsigned patch;

    static PackageVersion from_string(const ::std::string& s);

    PackageVersion next_minor() const {
        if(major == 0) {
            return PackageVersion { 0, minor, patch+1 };
        }
        else {
            return PackageVersion { major, minor+1, 0 };
        }
    }
    PackageVersion next_breaking() const {
        if(major == 0) {
            return PackageVersion { 0, minor + 1, 0 };
        }
        else {
            return PackageVersion { major + 1, 0, 0 };
        }
    }
    PackageVersion prev_compat() const {
        if(major == 0) {
            // Before 1.0, there's no patch levels
            return *this;
        }
        else {
            // Anything from the same patch series
            return PackageVersion { major, minor, 0 };
        }
    }

    bool operator==(const PackageVersion& x) const {
        if( major != x.major )  return false;
        if( minor != x.minor )  return false;
        return patch == x.patch;
    }
    bool operator!=(const PackageVersion& x) const {
        if( major != x.major )  return true;
        if( minor != x.minor )  return true;
        return patch != x.patch;
    }
    bool operator<(const PackageVersion& x) const {
        if( major != x.major )  return major < x.major;
        if( minor != x.minor )  return minor < x.minor;
        return patch < x.patch;
    }
    bool operator<=(const PackageVersion& x) const {
        if( major != x.major )  return major < x.major;
        if( minor != x.minor )  return minor < x.minor;
        return patch <= x.patch;
    }
    bool operator>(const PackageVersion& x) const {
        if( major != x.major )  return major > x.major;
        if( minor != x.minor )  return minor > x.minor;
        return patch > x.patch;
    }
    bool operator>=(const PackageVersion& x) const {
        if( major != x.major )  return major > x.major;
        if( minor != x.minor )  return minor > x.minor;
        return patch >= x.patch;
    }

    friend ::std::ostream& operator<<(::std::ostream& os, const PackageVersion& v) {
        os << v.major << "." << v.minor << "." << v.patch;
        return os;
    }
};
struct PackageVersionSpec
{
    struct Bound
    {
        enum class Type
        {
            Compatible, // "^" - Allows anything up to the next major version
            MinorCompatible,   // "~X.Y" - Allows anything up to the next minor version
            Greater,
            GreaterEqual,
            Equal,
            LessEqual,
            Less,
        };

        Type    ty;
        PackageVersion  ver;
    };
    // TODO: Just upper and lower?
    ::std::vector<Bound>   m_bounds;

    // Construct from a string
    static PackageVersionSpec from_string(const ::std::string& s);

    /// Check if this spec accepts the passed version
    bool accepts(const PackageVersion& v) const;

    friend ::std::ostream& operator<<(::std::ostream& os, const PackageVersionSpec& v) {
        bool first = true;
        for(const auto& b : v.m_bounds) {
            if(!first)
                os << ",";
            first = false;
            switch(b.ty)
            {
            case Bound::Type::Compatible: os << "^";  break;
            case Bound::Type::MinorCompatible: os << "~"; break;
            case Bound::Type::Greater:    os << ">";  break;
            case Bound::Type::GreaterEqual: os << ">=";  break;
            case Bound::Type::Equal:      os << "=";  break;
            case Bound::Type::LessEqual:  os << "<=";  break;
            case Bound::Type::Less:       os << "<";  break;
            }
            os << b.ver;
        }
        return os;
    }
};

class PackageRef
{
    friend class PackageManifest;
    ::std::string   m_name;
    PackageVersionSpec  m_version;

    bool m_optional = false;
    ::std::string   m_path;

    // Features requested by this reference
    bool    m_use_default_features = true;
    ::std::vector<::std::string>    m_features;
    bool    m_optional_enabled = false;

    ::std::shared_ptr<PackageManifest> m_manifest;

    PackageRef(const ::std::string& n) :
        m_name(n)
    {
    }

    void fill_from_kv(bool was_created, const TomlKeyValue& kv, size_t ofs);

public:
    const ::std::string& name() const { return m_name; }
    //const ::std::string& get_repo_name() const  { return m_repo; }
    const PackageVersionSpec& get_version() const { return m_version; }

    bool is_disabled() const { return m_optional && !m_optional_enabled; }

    const bool has_path() const { return m_path != ""; }
    const ::std::string& path() const { return m_path; }
    const bool has_git() const { return false; }

    const PackageManifest& get_package() const {
        if(!m_manifest) throw ::std::runtime_error("Manifest not loaded for package " + m_name);
        return *m_manifest;
    }

    void load_manifest(Repository& repo, const ::helpers::path& base_path, bool include_build_deps);
};

struct PackageTarget
{
    enum class Type
    {
        Lib,
        Bin,
        Test,
        Bench,
        Example,
    };
    enum class CrateType
    {
        dylib,
        rlib,
        staticlib,
        cdylib,
        proc_macro,
    };

    Type    m_type;
    ::std::string   m_name;
    ::std::string   m_path;
    bool    m_enable_test = true;
    bool    m_enable_doctest = true;
    bool    m_enable_bench = true;
    bool    m_enable_doc = true;
    bool    m_is_plugin = false;
    bool    m_is_proc_macro = false;
    bool    m_is_own_harness = false;

    ::std::vector<CrateType>    m_crate_types;
    ::std::vector<::std::string>    m_required_features;

    PackageTarget(Type ty):
        m_type(ty)
    {
        switch(ty)
        {
        case Type::Lib:
            m_path = "src/lib.rs";
            break;
        case Type::Bin:
            //m_path = "src/main.rs";
            break;
        default:
            break;
        }
    }
};

class BuildScriptOutput
{
public:
    // `cargo:minicargo-pre-build=make -C bar/`
    // MiniCargo hack
    ::std::vector<::std::string>    pre_build_commands;

    // cargo:rustc-link-search=foo/bar/baz
    ::std::vector<::std::pair<const char*,::std::string>>    rustc_link_search;
    // cargo:rustc-link-lib=mysql
    ::std::vector<::std::pair<const char*,::std::string>>    rustc_link_lib;
    // cargo:rustc-cfg=foo
    ::std::vector<::std::string>    rustc_cfg;
    // cargo:rustc-flags=-l foo
    ::std::vector<::std::string>    rustc_flags;
    // cargo:rustc-env=FOO=BAR
    ::std::vector<::std::string>    rustc_env;

    // cargo:foo=bar when [package]links=baz
    ::std::vector<::std::pair<::std::string, ::std::string>>    downstream_env;
};

class PackageManifest
{
    ::std::string   m_manifest_path;

    ::std::string   m_name;
    PackageVersion  m_version;
    ::std::string   m_links;

    ::std::string   m_build_script;

    ::std::vector<PackageRef>   m_dependencies;
    ::std::vector<PackageRef>   m_build_dependencies;
    ::std::vector<PackageRef>   m_dev_dependencies;

    ::std::vector<PackageTarget>    m_targets;

    BuildScriptOutput   m_build_script_output;

    ::std::map<::std::string, ::std::vector<::std::string>>    m_features;
    ::std::vector<::std::string>    m_default_features;
    ::std::vector<::std::string>    m_active_features;

    PackageManifest();

public:
    static PackageManifest load_from_toml(const ::std::string& path);

    const PackageVersion& version() const { return m_version; }
    bool has_library() const;
    const PackageTarget& get_library() const;

    bool foreach_ty(PackageTarget::Type ty, ::std::function<bool(const PackageTarget&)> cb) const {
        for(const auto& t : m_targets ) {
            if( t.m_type == ty ) {
                if( !cb(t) )
                    return false;
            }
        }
        return true;
    }
    bool foreach_binaries(::std::function<bool(const PackageTarget&)> cb) const {
        return foreach_ty(PackageTarget::Type::Bin, cb);
    }

    const ::helpers::path directory() const {
        return ::helpers::path(m_manifest_path).parent();
    }
    const ::std::string& manifest_path() const {
        return m_manifest_path;
    }
    const ::std::string& name() const {
        return m_name;
    }
    const ::std::string& build_script() const {
        return m_build_script;
    }
    const BuildScriptOutput& build_script_output() const {
        return m_build_script_output;
    }
    const ::std::vector<PackageRef>& dependencies() const {
        return m_dependencies;
    }
    const ::std::vector<PackageRef>& build_dependencies() const {
        return m_build_dependencies;
    }
    const ::std::vector<PackageRef>& dev_dependencies() const {
        return m_dev_dependencies;
    }
    const ::std::vector<::std::string>& active_features() const {
        return m_active_features;
    }

    void set_features(const ::std::vector<::std::string>& features, bool enable_default);
    void load_dependencies(Repository& repo, bool include_build, bool include_dev=false);

    void load_build_script(const ::std::string& path);
};