]> git.tdb.fi Git - builder.git/commitdiff
Track Android API levels with a bitmask
authorMikko Rasa <tdb@tdb.fi>
Wed, 21 Dec 2022 09:52:11 +0000 (11:52 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 21 Dec 2022 11:59:39 +0000 (13:59 +0200)
source/androidtools.cpp
source/androidtools.h

index 5ef2bec5f23b40ec3ff31a58be99d3d1189ece0e..1633b8b6465dd575acd0676536969a7b9d027af1 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/core/environ.h>
 #include <msp/fs/dir.h>
 #include <msp/fs/stat.h>
@@ -53,13 +54,20 @@ AndroidDevKit::AndroidDevKit(Builder &b, const string &type, const FS::Path &def
                return;
 
        builder.get_logger().log("files", format("Traversing %s", platforms_dir.str()));
+       supported_api_levels = 0;
        for(const string &p: list_filtered(platforms_dir, "^android-[1-9][0-9]*$"))
-               supported_api_levels.insert(lexical_cast<unsigned>(p.substr(8)));
+       {
+               unsigned api = lexical_cast<unsigned>(p.substr(8));
+               if(api>63)
+                       builder.get_logger().log("problems", format("API level %d is too high", api));
+               else
+                       supported_api_levels |= 1<<api;
+       }
 }
 
 void AndroidDevKit::select_api_level(unsigned api)
 {
-       if(!supported_api_levels.count(api))
+       if(!(supported_api_levels&(1<<api)))
                throw invalid_argument("AndroidDevKit::select_api_level");
 
        init_api_level(api);
@@ -219,17 +227,14 @@ AndroidTools::AndroidTools(Builder &builder, const Architecture &arch):
        sdk(builder),
        ndk(builder, arch, sdk)
 {
-       const set<unsigned> &sdk_api_levels = sdk.get_supported_api_levels();
-       const set<unsigned> &ndk_api_levels = ndk.get_supported_api_levels();
-       unsigned highest_common = 0;
-       for(auto i=sdk_api_levels.rbegin(); (!highest_common && i!=sdk_api_levels.rend()); ++i)
-               if(ndk_api_levels.count(*i))
-                       highest_common = *i;
-
-       if(highest_common)
+       if(uint64_t common_api_levels = sdk.get_supported_api_levels()&ndk.get_supported_api_levels())
        {
-               sdk.select_api_level(highest_common);
-               ndk.select_api_level(highest_common);
+               unsigned api = 0;
+               for(unsigned i=32; i>0; i>>=1)
+                       api += i*!!(common_api_levels>>(api+i));
+               builder.get_logger().log("tools", format("Using Android API level %d", api));
+               sdk.select_api_level(api);
+               ndk.select_api_level(api);
        }
        else
                builder.get_logger().log("problems", "No usable Android platforms found");
index bef95f8abea4eecae75cd6a531957663217eb51e..b08691c1434ce011e40eee4004d3ca0699e91725 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef ANDROIDTOOLS_H_
 #define ANDROIDTOOLS_H_
 
-#include <set>
+#include <cstdint>
 #include <msp/fs/path.h>
 #include "toolchain.h"
 
@@ -13,14 +13,16 @@ class AndroidDevKit
 protected:
        Builder &builder;
        Msp::FS::Path root;
-       std::set<unsigned> supported_api_levels;
+       /* Needs refactoring if API levels go over 63.  At present rate this will
+       take decades to occur. */
+       uint64_t supported_api_levels;
 
        AndroidDevKit(Builder &, const std::string &, const Msp::FS::Path & = Msp::FS::Path());
        ~AndroidDevKit() { }
 
 public:
        const Msp::FS::Path &get_root_dir() const { return root; }
-       const std::set<unsigned> &get_supported_api_levels() const { return supported_api_levels; }
+       uint64_t get_supported_api_levels() const { return supported_api_levels; }
        void select_api_level(unsigned);
 protected:
        virtual void init_api_level(unsigned) = 0;