aboutsummaryrefslogtreecommitdiff
path: root/include/Conf.hpp
blob: 12164ea4901a9bac092a2037aed6eb78ab7a5aae (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#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 "Platform.hpp"
#include "Version.hpp"
#include <vector>
#include <unordered_map>
#include <cassert>
#include <stdexcept>

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

        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)
        {

        }
        
        ConfigValue(const std::unordered_map<std::string, StringView> &_map) :
            type(Type::OBJECT),
            map(_map)
        {

        }

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

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

        const std::vector<StringView> asList() const
        {
            assert(isList());
            return values;
        }
        
        const std::unordered_map<std::string, StringView>& asObject() const
        {
            assert(isObject());
            return map;
        }
    private:
        Type type;
        std::vector<StringView> values;
        std::unordered_map<std::string, StringView> map;
    };

    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;
    };

    enum OptimizationLevel
    {
        OPT_LEV_NONE,
        OPT_LEV_DEBUG,
        OPT_LEV_RELEASE
    };

    enum class Compiler
    {
        GCC,
        MSVC,
        MINGW_W64
    };
    
    enum class CVersion
    {
        C89, // aka ansi
        C99,
        C11
    };
    
    enum class CPPVersion
    {
        CPP11,
        CPP14,
        CPP17
    };

    enum class Language
    {
        NONE,
        C,
        CPP,
        ZIG
    };

    struct SourceFile
    {
        Language language;
        std::string filepath;
    };

    #if OS_TYPE == OS_TYPE_WINDOWS
        #ifdef SIBS_ENV_32BIT
            const Platform SYSTEM_PLATFORM = PLATFORM_WIN32;
        #else
            const Platform SYSTEM_PLATFORM = PLATFORM_WIN64;
        #endif
            #define CONFIG_STATIC_LIB_FILE_EXTENSION L"lib"
            #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION L"dll"
    #elif OS_TYPE == OS_TYPE_LINUX
        #ifdef SIBS_ARCH_ARM
            #ifdef SIBS_ENV_32BIT
                const Platform SYSTEM_PLATFORM = PLATFORM_LINUX_ARM32;
            #else
                const Platform SYSTEM_PLATFORM = PLATFORM_LINUX_ARM64;
            #endif
        #elif defined(SIBS_ARCH_X86)
            #ifdef SIBS_ENV_32BIT
                const Platform SYSTEM_PLATFORM = PLATFORM_LINUX_X86_32;
            #else
                const Platform SYSTEM_PLATFORM = PLATFORM_LINUX_X86_64;
            #endif
        #else
            #error "non arm/x86 linux platform"
        #endif
            #define CONFIG_STATIC_LIB_FILE_EXTENSION "a"
            #ifdef __CYGWIN__
                #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION "dll"
            #else
                #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION "so"
            #endif
    #elif OS_TYPE == OS_TYPE_APPLE
        #ifdef SIBS_ENV_32BIT
            const Platform SYSTEM_PLATFORM = PLATFORM_MACOS32;
        #else
            const Platform SYSTEM_PLATFORM = PLATFORM_MACOS64;
        #endif
            #define CONFIG_STATIC_LIB_FILE_EXTENSION "a"
            #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION "dylib"
    #elif OS_TYPE == OS_TYPE_OPENBSD
        #ifdef SIBS_ENV_32BIT
            const Platform SYSTEM_PLATFORM = PLATFORM_OPENBSD32;
        #else
            const Platform SYSTEM_PLATFORM = PLATFORM_OPENBSD64;
        #endif
            #define CONFIG_STATIC_LIB_FILE_EXTENSION "a"
            #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION "so"
    #elif OS_TYPE == OS_TYPE_HAIKU
        #ifdef SIBS_ENV_32BIT
            const Platform SYSTEM_PLATFORM = PLATFORM_HAIKU32;
        #else
            const Platform SYSTEM_PLATFORM = PLATFORM_HAIKU64;
        #endif
            #define CONFIG_STATIC_LIB_FILE_EXTENSION "a"
            #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION "so"
    #endif

    const char* asString(OptimizationLevel optLevel);
    bool directoryToIgnore(const FileString &dir, const std::vector<std::string> &ignoreDirList);
    bool isProjectNameValid(const std::string &projectName);

    enum class Sanitize {
        NONE,
        ADDRESS,
        UNDEFINED,
        LEAK,
        THREAD
    };

    const Sanitize SANITIZE_INVALID = (Sanitize)-1;

    class SibsConfig : public ConfigCallback
    {
    public:
        SibsConfig(Compiler _compiler, const FileString &_projectPath, OptimizationLevel _optimizationLevel, bool _buildTests) : 
            compiler(_compiler), 
            projectPath(_projectPath), 
            packageType((PackageType)-1), 
            optimizationLevel(_optimizationLevel), 
            finishedProcessing(false), 
            useCmake(false),
            buildTests(_buildTests),
            cVersion(CVersion::C11),
            cppVersion(CPPVersion::CPP14),
            mainProject(false),
            sanitize(Sanitize::NONE),
            showWarnings(false),
            errorOnWarning(false),
            zigTestAllFiles(false),
            packaging(false),
            bundling(false),
            platform(SYSTEM_PLATFORM)
        {
            cmakeDirGlobal = projectPath;
            switch(optimizationLevel)
            {
                case OPT_LEV_DEBUG:
                    cmakeArgsGlobal = TINYDIR_STRING("-G Ninja \"-DCMAKE_BUILD_TYPE=Debug\"");
                    break;
                case OPT_LEV_RELEASE:
                    cmakeArgsGlobal = TINYDIR_STRING("-G Ninja \"-DCMAKE_BUILD_TYPE=Release\"");
                    break;
            }
        }
        
        SibsConfig operator=(SibsConfig &other) = delete;
        
        virtual ~SibsConfig();

        Compiler getCompiler() const
        {
            return compiler;
        }

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

        virtual PackageType getPackageType() const
        {
            return packageType;
        }

        virtual const FileString& getTestPath() const
        {
            return testPath;
        }
        
        void setTestPath(const FileString &testPath)
        {
            this->testPath = testPath;
        }

        virtual const std::vector<PackageListDependency*>& getPackageListDependencies() const
        {
            return packageListDependencies;
        }

        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<std::string>& getLibs() const
        {
            return libs;
        }

        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;
        }
        
        const FileString& getCmakeDir() const
        {
            return cmakeDirGlobal;
        }
        
        const FileString& getCmakeDirStatic() const
        {
            return !cmakeDirStatic.empty() ? cmakeDirStatic : cmakeDirGlobal;
        }
        
        const FileString& getCmakeDirDynamic() const
        {
            return !cmakeDirDynamic.empty() ? cmakeDirDynamic : cmakeDirGlobal;
        }
        
        // Get cmake args for all builds. This is args under [cmake] only
        const FileString& getCmakeArgs() const
        {
            return cmakeArgsGlobal;
        }
        
        // Get cmake args for static build. This is a combination of args under [cmake] and under [cmake.static]
        FileString getCmakeArgsStatic() const
        {
			FileString result;
            result.reserve(cmakeArgsGlobal.size() + 1 + cmakeArgsStatic.size());
            result += cmakeArgsGlobal;
            result += TINYDIR_STRING(" ");
            result += cmakeArgsStatic;
            return result;
        }
        
        // Get cmake args for dynamic build. This is a combination of args under [cmake] and under [cmake.dynamic]
        FileString getCmakeArgsDynamic() const
        {
			FileString result;
            result.reserve(cmakeArgsGlobal.size() + 1 + cmakeArgsDynamic.size());
            result += cmakeArgsGlobal;
            result += TINYDIR_STRING(" ");
            result += cmakeArgsDynamic;
            return result;
        }
        
        CVersion getCversion() const
        {
            return cVersion;
        }
        
        CPPVersion getCppVersion() const
        {
            return cppVersion;
        }
        
        bool shouldUseCmake() const
        {
            return useCmake;
        }
        
        bool shouldBuildTests() const
        {
            return buildTests;
        }
        
        void setPackageType(PackageType packageType)
        {
            this->packageType = packageType;
        }
        
        bool isMainProject() const
        {
            return mainProject;
        }
        
        void setMainProject(bool mainProject)
        {
            this->mainProject = mainProject;
        }
        
        Sanitize getSanitize() const
        {
            return sanitize;
        }
        
        void setSanitize(Sanitize sanitize)
        {
            this->sanitize = sanitize;
        }
        
        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;

        virtual bool isTest() const { return false; }
        
        // Get define value by name. 
        // Return empty string if the value is empty or if the defined value doesn't exist
        const std::string& getDefinedValue(const std::string &name) const;
        
        std::vector<FileString> zigTestFiles;
        bool showWarnings;
        bool errorOnWarning;
        bool zigTestAllFiles;
        bool packaging;
        bool bundling;
        std::string versionStr;
        PackageVersion version;
        Platform platform;
        std::vector<std::string> includeDirs;
        std::vector<std::string> exposeIncludeDirs;
        std::vector<std::string> ignoreDirs;
        std::vector<std::string> libs;
    protected:
        virtual void processObject(StringView name) override;
        virtual void processField(StringView name, const ConfigValue &value) override;
        void parseConfig(const StringView &name, const ConfigValue &value);
        void parseDependencies(const StringView &name, const ConfigValue &value);
        virtual void finished() override;
        void failInvalidFieldUnderObject(const StringView &fieldName) const;
        void validatePackageTypeDefined() const;
    private:
        void parseCLang(const StringView &fieldName, const ConfigValue &fieldValue);
        void parseCppLang(const StringView &fieldName, const ConfigValue &fieldValue);
        void parsePlatformConfig(const StringView &fieldName, const ConfigValue &fieldValue);
        std::string parsePlatformConfigStatic(const StringView &fieldName, const ConfigValue &fieldValue);
        void parsePlatformConfigStaticDebug(const StringView &fieldName, const ConfigValue &fieldValue);
        void parsePlatformConfigStaticRelease(const StringView &fieldName, const ConfigValue &fieldValue);
        void parseCmake(const StringView &fieldName, const ConfigValue &fieldValue, FileString &cmakeDir, FileString &cmakeArgs);
        void validatePackageName() const;
    protected:
        StringView currentObject;
        Compiler compiler;
        FileString projectPath;
        std::string packageName;
        FileString testPath;
        PackageType packageType;
        std::vector<PackageListDependency*> packageListDependencies;
        std::vector<Platform> platforms;
        std::unordered_map<std::string, std::string> defines;
        OptimizationLevel optimizationLevel;
        std::vector<std::string> debugStaticLibs;
        std::vector<std::string> releaseStaticLibs;
        FileString cmakeDirGlobal;
        FileString cmakeDirStatic;
        FileString cmakeDirDynamic;
        FileString cmakeArgsGlobal;
		FileString cmakeArgsStatic;
		FileString cmakeArgsDynamic;
        CVersion cVersion;
        CPPVersion cppVersion;
        bool useCmake;
        bool buildTests;
        bool finishedProcessing;
        bool mainProject;
        Sanitize sanitize;
    };

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

        virtual ~SibsTestConfig(){}

        bool isTest() const override { return true; }

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

    class Config
    {
    public:
        static Result<bool> readFromFile(const _tinydir_char_t *filepath, SibsConfig &config);
    };
    
    void readSibsConfig(const FileString &projectPath, const FileString &projectConfFilePath, SibsConfig &sibsConfig, FileString &buildPath);
}

#endif //SIBS_CONF_HPP