aboutsummaryrefslogtreecommitdiff
path: root/include/Conf.hpp
blob: d8ce3a8be0b570176d6aceb4c0486b88666e0527 (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
#ifndef SIBS_CONF_HPP
#define SIBS_CONF_HPP

#include "FileUtil.hpp"
#include "Result.hpp"
#include "StringView.hpp"
#include "utils.hpp"
#include "Dependency.hpp"
#include "Package.hpp"
#include <vector>
#include <unordered_map>
#include <cassert>
#include <stdexcept>

namespace sibs
{
    class ConfigValue
    {
    public:
        enum class Type
        {
            NONE,
            SINGLE,
            LIST
        };

        ConfigValue() : type(Type::NONE) {}

        ConfigValue(StringView value) :
            type(Type::SINGLE)
        {
            values.push_back(value);
        }

        ConfigValue(const std::vector<StringView> &_values) :
            type(Type::LIST),
            values(_values)
        {

        }

        bool isSingle() const { return type == Type::SINGLE; }
        bool isList() const { return type == Type::LIST; }

        StringView asSingle() const
        {
            assert(isSingle());
            return values[0];
        }

        const std::vector<StringView> asList() const
        {
            assert(isList());
            return values;
        }
    private:
        Type type;
        std::vector<StringView> values;
    };

    class Parser;

    class ParserException : public std::runtime_error
    {
    public:
        ParserException(const std::string &errMsg) : runtime_error(errMsg)
        {

        }
    };

    class ConfigCallback
    {
        friend class Parser;
    public:
        virtual ~ConfigCallback(){}
    protected:
        virtual void processObject(StringView name) = 0;
        virtual void processField(StringView name, const ConfigValue &value) = 0;
        virtual void finished() = 0;
    };

    class Config
    {
    public:
        static Result<bool> readFromFile(const _tinydir_char_t *filepath, const ConfigCallback &callback);
    };

    enum OptimizationLevel
    {
        OPT_LEV_NONE,
        OPT_LEV_DEBUG,
        OPT_LEV_RELEASE
    };

    enum class Compiler
    {
        GCC,
        MSVC
    };

    enum Platform
    {
        PLATFORM_ANY,

        PLATFORM_LINUX32,
        PLATFORM_LINUX64,

        PLATFORM_WIN32,
        PLATFORM_WIN64
    };

    // TODO: Detect this at runtime?
    #if OS_TYPE == OS_TYPE_WINDOWS
        #ifdef SIBS_ENV_32BIT
            const Platform SYSTEM_PLATFORM = Platform::PLATFORM_WIN32;
            const StringView CONFIG_SYSTEM_PLATFORM = "config.win32";
            const StringView CONFIG_STATIC_DEBUG_PLATFORM = "config.win32.static.debug";
            const StringView CONFIG_STATIC_RELEASE_PLATFORM = "config.win32.static.release";
        #else
            const Platform SYSTEM_PLATFORM = Platform::PLATFORM_WIN64;
            const StringView CONFIG_SYSTEM_PLATFORM = "config.win64";
            const StringView CONFIG_STATIC_DEBUG_PLATFORM = "config.win64.static.debug";
            const StringView CONFIG_STATIC_RELEASE_PLATFORM = "config.win64.static.release";
        #endif
    #elif OS_TYPE == OS_TYPE_LINUX
        #ifdef SIBS_ENV_32BIT
            const Platform SYSTEM_PLATFORM = Platform::PLATFORM_LINUX32;
            const StringView CONFIG_SYSTEM_PLATFORM = "config.linux32";
            const StringView CONFIG_STATIC_DEBUG_PLATFORM = "config.linux32.static.debug";
            const StringView CONFIG_STATIC_RELEASE_PLATFORM = "config.linux32.static.release";
        #else
            const Platform SYSTEM_PLATFORM = Platform::PLATFORM_LINUX64;
            const StringView CONFIG_SYSTEM_PLATFORM = "config.linux64";
            const StringView CONFIG_STATIC_DEBUG_PLATFORM = "config.linux64.static.debug";
            const StringView CONFIG_STATIC_RELEASE_PLATFORM = "config.linux64.static.release";
        #endif
    #endif

    bool containsPlatform(const std::vector<Platform> &platforms, Platform platform);
    const char* asString(Platform platform);
    const char* asString(OptimizationLevel optLevel);
    bool directoryToIgnore(const FileString &dir, const std::vector<std::string> &ignoreDirList);

    class SibsConfig : public ConfigCallback
    {
    public:
        SibsConfig(Compiler _compiler, const FileString &_projectPath, OptimizationLevel _optimizationLevel = OPT_LEV_DEBUG) : compiler(_compiler), projectPath(_projectPath), packageType((PackageType)-1), optimizationLevel(_optimizationLevel), finishedProcessing(false) {}
        virtual ~SibsConfig(){}

        Compiler getCompiler() const
        {
            return compiler;
        }

        virtual const std::string& getPackageName() const
        {
            assert(finishedProcessing);
            return packageName;
        }

        virtual PackageType getPackageType() const
        {
            assert(finishedProcessing);
            return packageType;
        }

        virtual const FileString& getTestPath() const
        {
            return testPath;
        }

        virtual const std::vector<Dependency>& getDependencies() const
        {
            return dependencies;
        }

        virtual const FileString& getProjectPath() const
        {
            return projectPath;
        }

        virtual const std::vector<std::string>& getIncludeDirs() const
        {
            return includeDirs;
        }

        virtual const std::vector<std::string>& getGlobalIncludeDirs() const
        {
            return exposeIncludeDirs;
        }

        virtual const std::vector<Platform>& getPlatforms() const
        {
            return platforms;
        }

        virtual OptimizationLevel getOptimizationLevel() const
        {
            return optimizationLevel;
        }

        const std::vector<std::string>& getDebugStaticLibs() const
        {
            return debugStaticLibs;
        }

        const std::vector<std::string>& getReleaseStaticLibs() const
        {
            return releaseStaticLibs;
        }
        
        const std::vector<std::string>& getIgnoreDirs() const
        {
            return ignoreDirs;
        }
        
        void setPackageType(PackageType packageType)
        {
            this->packageType = packageType;
        }
        
        virtual bool isDefined(const std::string &name) const;
        virtual bool define(const std::string &name, const std::string &value);
        virtual const std::unordered_map<std::string, std::string>& getDefines() const;
    protected:
        virtual void processObject(StringView name) override;
        virtual void processField(StringView name, const ConfigValue &value) override;
        virtual void finished() override;
    protected:
        StringView currentObject;
        Compiler compiler;
        FileString projectPath;
        std::string packageName;
        FileString testPath;
        PackageType packageType;
        std::vector<Dependency> dependencies;
        std::vector<std::string> includeDirs;
        std::vector<std::string> exposeIncludeDirs;
        std::vector<Platform> platforms;
        std::vector<std::string> ignoreDirs;
        std::unordered_map<std::string, std::string> defines;
        OptimizationLevel optimizationLevel;
        std::vector<std::string> debugStaticLibs;
        std::vector<std::string> releaseStaticLibs;
        bool finishedProcessing;
    };

    class SibsTestConfig : public SibsConfig
    {
    public:
        SibsTestConfig(Compiler _compiler, const FileString &_projectPath, OptimizationLevel _optimizationLevel) : SibsConfig(_compiler, _projectPath, _optimizationLevel)
        {
            packageName = "test";
        }

        virtual ~SibsTestConfig(){}

        PackageType getPackageType() const override
        {
            return PackageType::EXECUTABLE;
        }
    protected:
        void processObject(StringView name) override;
        void processField(StringView name, const ConfigValue &value) override;
        void finished() override;
    };
}

#endif //SIBS_CONF_HPP