From 95fa2a46519d0820548ab7855b32182ff81f0435 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 20 Feb 2019 22:35:24 +0100 Subject: Change linux platform from 64 to x86_64, add sibs platforms command --- README.md | 4 ++-- include/Conf.hpp | 16 ++++++++++--- include/Platform.hpp | 61 +++++++++++++++++++++++++++++--------------------- include/StringView.hpp | 41 +++++++++++++++++++++++++++++++-- include/env.hpp | 27 +++++++++++++--------- src/Platform.cpp | 29 ++++++++++++++++++++++++ src/main.cpp | 13 ++++++++--- 7 files changed, 145 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index d22c209..c3e0956 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Users are required to manually install some libraries as they can't be included This requirement might be removed later, if the gpu driver libraries required can somehow be detected and downloaded cross platform. Libraries that are downloaded are available at: https://github.com/DEC05EBA/libraries # Cross compilation -Automatic cross compilation (`sibs build --platform `)currently only works from linux64 to win64 by using mingw-w64. You need to install `mingw-w64-gcc` and optionally `mingw-w64-pkg-config` if you want to use mingw-w64 system installed packages. +Automatic cross compilation (`sibs build --platform `)currently only works from linux_x86_64 to win64 by using mingw-w64. You need to install `mingw-w64-gcc` and optionally `mingw-w64-pkg-config` if you want to use mingw-w64 system installed packages. Cross compilation does currently not work if you have zig files as zig doesn't support libc when cross compiling at the moment. You can run `scripts/mingw_package.py` to automatically copy dynamic library dependencies of your executable to the same directory as the executable, so the library can be found when running the executable on windows; this also allows you to bundle your application and distribute it without external dependencies. To run `scripts/mingw_package.py` you need to install pefile python library `sudo pip install pefile`. @@ -124,7 +124,7 @@ Required. Should be one of: "executable", "static", "dynamic", "library" ### version Required. Version string has to be in the format of "xxx.yyy.zzz" where xxx is major, yyy is minor and zzz is patch. Version format is based on [semver 2.0.0](https://semver.org/spec/v2.0.0.html) ### platforms -Required. A list of platforms the package supports. Can contain the following values: "any", "posix", "posix32", "posix64", linux", "linux32", "linux64", "win", "win32", "win64", "macos32", "macos64", "bsd", "openbsd", "openbsd32", "openbsd64", "haiku", "haiku32", "haiku64". +Required. A list of platforms the package supports. Run `sibs platforms` to view a list of supported platforms. If platforms contains "any" then there is no need to specify other platforms ### authors Optional. A list of authors diff --git a/include/Conf.hpp b/include/Conf.hpp index 2645caa..eae79d4 100644 --- a/include/Conf.hpp +++ b/include/Conf.hpp @@ -149,10 +149,20 @@ namespace sibs #define CONFIG_STATIC_LIB_FILE_EXTENSION L"lib" #define CONFIG_DYNAMIC_LIB_FILE_EXTENSION L"dll" #elif OS_TYPE == OS_TYPE_LINUX - #ifdef SIBS_ENV_32BIT - const Platform SYSTEM_PLATFORM = PLATFORM_LINUX32; + #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 - const Platform SYSTEM_PLATFORM = PLATFORM_LINUX64; + #error "non arm/x86 linux platform" #endif #define CONFIG_STATIC_LIB_FILE_EXTENSION "a" #ifdef __CYGWIN__ diff --git a/include/Platform.hpp b/include/Platform.hpp index 6075383..2c9feb5 100644 --- a/include/Platform.hpp +++ b/include/Platform.hpp @@ -3,43 +3,48 @@ #include "types.hpp" #include "StringView.hpp" #include +#include namespace sibs { enum Platform : u64 { - PLATFORM_INVALID = 0x00000000, + PLATFORM_INVALID = 0x0000000000000000, - PLATFORM_ANY = 0xFFFFFFFF, + PLATFORM_ANY = 0xFFFFFFFFFFFFFFFF, - PLATFORM_POSIX = 1 << 1, - PLATFORM_POSIX32 = 1 << 2 | PLATFORM_POSIX, - PLATFORM_POSIX64 = 1 << 3 | PLATFORM_POSIX, + PLATFORM_POSIX = 1ULL << 1ULL, + PLATFORM_POSIX32 = 1ULL << 2ULL | PLATFORM_POSIX, + PLATFORM_POSIX64 = 1ULL << 3ULL | PLATFORM_POSIX, - PLATFORM_LINUX = 1 << 4 | PLATFORM_POSIX, - PLATFORM_LINUX32 = 1 << 5 | PLATFORM_LINUX | PLATFORM_POSIX32, - PLATFORM_LINUX64 = 1 << 6 | PLATFORM_LINUX | PLATFORM_POSIX64, + PLATFORM_LINUX = 1ULL << 4ULL | PLATFORM_POSIX, + PLATFORM_LINUX32 = 1ULL << 5ULL | PLATFORM_LINUX | PLATFORM_POSIX32, + PLATFORM_LINUX64 = 1ULL << 6ULL | PLATFORM_LINUX | PLATFORM_POSIX64, - PLATFORM_WIN = 1 << 7, - PLATFORM_WIN32 = 1 << 8 | PLATFORM_WIN, - PLATFORM_WIN64 = 1 << 9 | PLATFORM_WIN, + PLATFORM_WIN = 1ULL << 7ULL, + PLATFORM_WIN32 = 1ULL << 8ULL | PLATFORM_WIN, + PLATFORM_WIN64 = 1ULL << 9ULL | PLATFORM_WIN, - PLATFORM_MACOS = 1 << 10 | PLATFORM_POSIX, - PLATFORM_MACOS32 = 1 << 11 | PLATFORM_MACOS | PLATFORM_POSIX32, - PLATFORM_MACOS64 = 1 << 12 | PLATFORM_MACOS | PLATFORM_POSIX64, + PLATFORM_MACOS = 1ULL << 10ULL | PLATFORM_POSIX, + PLATFORM_MACOS32 = 1ULL << 11ULL | PLATFORM_MACOS | PLATFORM_POSIX32, + PLATFORM_MACOS64 = 1ULL << 12ULL | PLATFORM_MACOS | PLATFORM_POSIX64, - PLATFORM_BSD = 1 << 13 | PLATFORM_POSIX, - PLATFORM_OPENBSD = 1 << 14 | PLATFORM_BSD, - PLATFORM_OPENBSD32 = 1 << 15 | PLATFORM_OPENBSD | PLATFORM_POSIX32, - PLATFORM_OPENBSD64 = 1 << 16 | PLATFORM_OPENBSD | PLATFORM_POSIX64, + PLATFORM_BSD = 1ULL << 13ULL | PLATFORM_POSIX, + PLATFORM_OPENBSD = 1ULL << 14ULL | PLATFORM_BSD, + PLATFORM_OPENBSD32 = 1ULL << 15ULL | PLATFORM_OPENBSD | PLATFORM_POSIX32, + PLATFORM_OPENBSD64 = 1ULL << 16ULL | PLATFORM_OPENBSD | PLATFORM_POSIX64, - PLATFORM_HAIKU = 1 << 24 | PLATFORM_POSIX, - PLATFORM_HAIKU32 = 1 << 25 | PLATFORM_HAIKU | PLATFORM_POSIX32, - PLATFORM_HAIKU64 = 1 << 26 | PLATFORM_HAIKU | PLATFORM_POSIX64, + PLATFORM_HAIKU = 1ULL << 24ULL | PLATFORM_POSIX, + PLATFORM_HAIKU32 = 1ULL << 25ULL | PLATFORM_HAIKU | PLATFORM_POSIX32, + PLATFORM_HAIKU64 = 1ULL << 26ULL | PLATFORM_HAIKU | PLATFORM_POSIX64, - PLATFORM_LINUX_ARM = 1 << 27 | PLATFORM_LINUX, - PLATFORM_LINUX_ARM32 = 1 << 28 | PLATFORM_LINUX32, - PLATFORM_LINUX_ARM64 = 1 << 29 | PLATFORM_LINUX64 + PLATFORM_LINUX_ARM = 1ULL << 27ULL | PLATFORM_LINUX, + PLATFORM_LINUX_ARM32 = 1ULL << 28ULL | PLATFORM_LINUX32, + PLATFORM_LINUX_ARM64 = 1ULL << 29ULL | PLATFORM_LINUX64, + + PLATFORM_LINUX_X86 = 1ULL << 30ULL | PLATFORM_LINUX, + PLATFORM_LINUX_X86_32 = 1ULL << 31ULL | PLATFORM_LINUX32, + PLATFORM_LINUX_X86_64 = 1ULL << 32ULL | PLATFORM_LINUX64 }; const StringViewMap PLATFORM_BY_NAME = { @@ -65,12 +70,18 @@ namespace sibs { "haiku64", PLATFORM_HAIKU64 }, { "linux_arm", PLATFORM_LINUX_ARM }, { "linux_arm32", PLATFORM_LINUX_ARM32 }, - { "linux_arm64", PLATFORM_LINUX_ARM64 } + { "linux_arm64", PLATFORM_LINUX_ARM64 }, + { "linux_x86", PLATFORM_LINUX_X86 }, + { "linux_x86_32", PLATFORM_LINUX_X86_32 }, + { "linux_x86_64", PLATFORM_LINUX_X86_64 } }; bool containsPlatform(const std::vector &platforms, Platform platform); const char* asString(Platform platform); Platform getPlatformByName(StringView name); + + std::string getPlatformListFormatted(); + // Return true if both platforms are of same type, for example if platform @a is either win, win32, win64 and @b is either win, win32, win64 bool isSamePlatformFamily(Platform a, Platform b); // Returns true if platform @base is a base platform for platform @platform, for example if @platform is win64 and @base is either win64 or win, then this function return true. diff --git a/include/StringView.hpp b/include/StringView.hpp index 4d87d8a..8ca685d 100644 --- a/include/StringView.hpp +++ b/include/StringView.hpp @@ -7,6 +7,7 @@ #include "env.hpp" #include #include +#include #include namespace sibs @@ -36,8 +37,7 @@ namespace sibs bool equals(const StringView &other) const { - if(size != other.size) return false; - return memcmp(data, other.data, size) == 0; + return size == other.size && memcmp(data, other.data, size) == 0; } size_t hash() const @@ -54,6 +54,31 @@ namespace sibs assert(index < size); return data[index]; } + + bool operator < (const StringView &other) const + { + return ((ssize)size - (ssize)other.size < 0) || strncmp(data, other.data, size) < 0; + } + + bool operator > (const StringView &other) const + { + return !operator<(other); + } + + size_t operator()() const + { + return hash(); + } + + bool operator()(const StringView &other) const + { + return equals(other); + } + + bool operator == (const StringView &other) const + { + return equals(other); + } const char *data; usize size; @@ -75,9 +100,21 @@ namespace sibs return lhs.equals(rhs); } }; + + struct StringViewCompareSorted + { + public: + bool operator()(const StringView &lhs, const StringView &rhs) const + { + return lhs < rhs; + } + }; template using StringViewMap = std::unordered_map; + + // template + // using StringViewMapSorted = std::map; } #endif // SIBS_STRING_VIEW_HPP diff --git a/include/env.hpp b/include/env.hpp index b25cb69..8037f26 100644 --- a/include/env.hpp +++ b/include/env.hpp @@ -11,11 +11,6 @@ #define OS_TYPE_HAIKU 10 #if defined(_WIN32) || defined(_WIN64) - #if defined(_WIN64) - #define SIBS_ENV_64BIT - #else - #define SIBS_ENV_32BIT - #endif #define OS_FAMILY OS_FAMILY_WINDOWS #define OS_TYPE OS_TYPE_WINDOWS @@ -64,12 +59,18 @@ #define OS_TYPE OS_TYPE_LINUX #endif -#if defined(__GNUC__) - #if defined(__x86_64__) || defined(__pc64__) - #define SIBS_ENV_64BIT - #else - #define SIBS_ENV_32BIT - #endif +#if defined(__aarch64__) + #define SIBS_ARCH_ARM + #define SIBS_ENV_64BIT +#elif defined(__arm__) || defined(_ARM) || defined(_M_ARM) || defined(__arm) + #define SIBS_ARCH_ARM + #define SIBS_ENV_32BIT +#elif defined(__x86_64__) || defined(__amd64__) || defined(__amd64) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) + #define SIBS_ARCH_X86 + #define SIBS_ENV_64BIT +#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(__IA32__) || defined(_M_I86) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) + #define SIBS_ARCH_X86 + #define SIBS_ENV_32BIT #endif #if !defined(SIBS_ENV_32BIT) && !defined(SIBS_ENV_64BIT) @@ -84,6 +85,10 @@ #error "System not supported. Only Windows, linux, macos and openbsd systems supported right now" #endif +#if !defined(SIBS_ARCH_ARM) && !defined(SIBS_ARCH_X86) + #error "System is not detected as either arm or x86" +#endif + #if !defined(DEBUG) && !defined(NDEBUG) #define DEBUG #endif diff --git a/src/Platform.cpp b/src/Platform.cpp index b011d93..f92e1ea 100644 --- a/src/Platform.cpp +++ b/src/Platform.cpp @@ -1,5 +1,6 @@ #include "../include/Platform.hpp" #include +#include namespace sibs { @@ -63,6 +64,34 @@ namespace sibs return PLATFORM_INVALID; } + static std::string join(const std::vector &strings) + { + std::string result; + ssize size = strings.size(); + ssize i = 0; + for(const std::string &str: strings) + { + result += str; + if(i == size - 2) + result += " and "; + else if(i < size - 2) + result += ", "; + ++i; + } + return result; + } + + std::string getPlatformListFormatted() + { + std::vector platformsSorted; + for(auto &it: PLATFORM_BY_NAME) + { + platformsSorted.push_back({ it.first.data, it.first.size }); + } + std::sort(platformsSorted.begin(), platformsSorted.end()); + return join(platformsSorted); + } + bool isSamePlatformFamily(Platform a, Platform b) { return a & b; diff --git a/src/main.cpp b/src/main.cpp index c954634..bf97450 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -127,6 +127,7 @@ static void usage() printf(" test\t\tBuild and run tests for a sibs project\n"); printf(" package\t\tCreate a redistributable package from a sibs project. Note: Redistributable packages can't use system packages to build\n"); printf(" platform\t\tPrint name of platform (to stdout) and exit\n"); + printf(" platforms\t\tPrint list of supported platforms (to stdout) and exit\n"); exit(1); } @@ -138,7 +139,7 @@ static void usageBuild() printf(" project_path\t\tThe directory containing a project.conf file - Optional (default: current directory)\n"); printf(" --debug|--release\t\tOptimization level to build project and dependencies with (if not a system package) - Optional (default: --debug)\n"); printf(" --sanitize\t\tAdd runtime address/undefined behavior sanitization. Program can be up to 3 times slower and use 10 times as much RAM. Ignored if compiler doesn't support sanitization - Optional (default: disabled)\n"); - printf(" --platform\t\tThe platform to build for - Optional (default: the running platform). Platform can be either linux32, linux64, win32 or win64\n"); + printf(" --platform\t\tThe platform to build for - Optional (default: the running platform)\n"); printf("Examples:\n"); printf(" sibs build\n"); printf(" sibs build dirA/dirB\n"); @@ -522,13 +523,14 @@ static int buildProject(int argc, const _tinydir_char_t **argv) if(platform == PLATFORM_INVALID) { ferr << "Invalid platform " << platformName << endl; + ferr << "Expected one of: " << getPlatformListFormatted() << std::endl; usageBuild(); } - bool crossCompileLinux64ToWin64 = (SYSTEM_PLATFORM == PLATFORM_LINUX64 && platform == PLATFORM_WIN64); + bool crossCompileLinux64ToWin64 = (SYSTEM_PLATFORM == PLATFORM_LINUX_X86_64 && platform == PLATFORM_WIN64); if(platform != SYSTEM_PLATFORM && !crossCompileLinux64ToWin64) { - ferr << "Cross compilation is currently only supported from linux64 to win64" << endl; + ferr << "Cross compilation is currently only supported from linux_X86_64 to win64" << endl; exit(33); } @@ -1434,6 +1436,11 @@ int wmain(int argc, const _tinydir_char_t **argv) printf("%s\n", asString(SYSTEM_PLATFORM)); return 0; } + else if(_tinydir_strcmp(arg, TINYDIR_STRING("platforms")) == 0) + { + printf("%s\n", getPlatformListFormatted().c_str()); + return 0; + } else { ferr << "Expected command to be either 'build', 'new' or 'test', was: " << arg << endl << endl; -- cgit v1.2.3