]> git.tdb.fi Git - builder.git/blob - source/androidtools.cpp
c0fe6d1557e5f3b9beb92f510d60e7e9b86eaf28
[builder.git] / source / androidtools.cpp
1 #include <cstdlib>
2 #include <msp/fs/dir.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "androidccompiler.h"
6 #include "androidcxxcompiler.h"
7 #include "androidlinker.h"
8 #include "androidtools.h"
9 #include "architecture.h"
10 #include "builder.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 AndroidTools::AndroidTools(Builder &builder, const Architecture &arch):
16         ndk(builder, arch)
17 {
18         add_tool(new AndroidCCompiler(builder, arch, ndk));
19         add_tool(new AndroidCxxCompiler(builder, arch, ndk));
20         add_tool(new AndroidLinker(builder, arch, ndk));
21 }
22
23
24 AndroidNdk::AndroidNdk(Builder &b, const Architecture &a):
25         builder(b),
26         architecture(a)
27 {
28         if(const char *env_ndk_root = getenv("ANDROID_NDK_ROOT"))
29                 ndk_root = env_ndk_root;
30         else
31                 throw runtime_error("ANDROID_NDK_ROOT must be set");
32
33         find_toolchain_dir();
34         find_platform_dir();
35 }
36
37 void AndroidNdk::find_toolchain_dir()
38 {
39         FS::Path toolchains_dir = ndk_root/"toolchains";
40         list<string> toolchains = list_files(toolchains_dir);
41
42         string prefix = architecture.get_cross_prefix()+"-";
43         string use_toolchain;
44         unsigned latest_version = 0;
45         for(list<string>::const_iterator i=toolchains.begin(); i!=toolchains.end(); ++i)
46                 if(!i->compare(0, prefix.size(), prefix))
47                 {
48                         string version_str = i->substr(prefix.size());
49                         string compiler = "gcc";
50                         if(!isdigit(version_str[0]))
51                         {
52                                 unsigned j;
53                                 for(j=1; (j<version_str.size() && !isdigit(version_str[j])); ++j) ;
54                                 compiler = version_str.substr(0, j);
55                                 version_str = version_str.substr(j);
56                         }
57
58                         if(compiler!="gcc")
59                                 continue;
60
61                         vector<string> version_parts = split(version_str, '.');
62                         unsigned version = (lexical_cast<unsigned>(version_parts[0])<<8);
63                         if(version_parts.size()>1)
64                                 version += lexical_cast<unsigned>(version_parts[1]);
65
66                         if(version>latest_version)
67                         {
68                                 use_toolchain = *i;
69                                 latest_version = version;
70                         }
71                 }
72
73         if(use_toolchain.empty())
74                 throw runtime_error("No applicable toolchains found");
75
76         const Architecture &native_arch = builder.get_native_arch();
77
78         list<string> tc_archs = list_files(toolchains_dir/use_toolchain/"prebuilt");
79         string use_arch;
80         for(list<string>::const_iterator i=tc_archs.begin(); i!=tc_archs.end(); ++i)
81                 if(native_arch.match_name(*i))
82                 {
83                         use_arch = *i;
84                         break;
85                 }
86
87         if(use_arch.empty())
88                 throw runtime_error("No matching toolchain found");
89
90         bin_dir = toolchains_dir/use_toolchain/"prebuilt"/use_arch/"bin";
91 }
92
93 void AndroidNdk::find_platform_dir()
94 {
95         FS::Path platforms_dir = ndk_root/"platforms";
96         list<string> platforms = list_files(platforms_dir);
97
98         string use_platform;
99         unsigned highest_level = 0;
100         for(list<string>::const_iterator i=platforms.begin(); i!=platforms.end(); ++i)
101                 if(!i->compare(0, 8, "android-"))
102                 {
103                         unsigned level = lexical_cast<unsigned>(i->substr(8));
104                         if(level>highest_level)
105                         {
106                                 use_platform = *i;
107                                 highest_level = level;
108                         }
109                 }
110
111         if(use_platform.empty())
112                 throw runtime_error("No applicable platforms found");
113
114         list<string> platform_archs = list_files(platforms_dir/use_platform);
115         string use_arch;
116         for(list<string>::const_iterator i=platform_archs.begin(); i!=platform_archs.end(); ++i)
117                 if(!i->compare(0, 5, "arch-") && architecture.match_name(i->substr(5)))
118                 {
119                         use_arch = *i;
120                         break;
121                 }
122
123         if(use_arch.empty())
124                 throw runtime_error("No matching platform found");
125
126         platform_sysroot = platforms_dir/use_platform/use_arch;
127 }