]> git.tdb.fi Git - builder.git/blob - plugins/android/androidcompiler.cpp
Replace the stdc++ library with gnustl_static on Android
[builder.git] / plugins / android / androidcompiler.cpp
1 #include <msp/builder/builder.h>
2 #include <msp/builder/externaltask.h>
3 #include <msp/builder/filetarget.h>
4 #include <msp/core/algorithm.h>
5 #include <msp/fs/dir.h>
6 #include <msp/fs/stat.h>
7 #include <msp/fs/utils.h>
8 #include <msp/strings/format.h>
9 #include <msp/strings/utils.h>
10 #include "androidcompiler.h"
11 #include "androidtools.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 AndroidCompiler::AndroidCompiler(Builder &b, const Architecture &a, const string &t, const AndroidNdk &n):
17         CustomizedTool(b, t, a),
18         ndk(n)
19 {
20         set_command((tag=="CXX" ? "g++" : "gcc"), true);
21         if(ndk.get_root_dir().empty())
22                 problems.push_back("Android NDK not found");
23         else if(ndk.get_bin_dir().empty())
24                 problems.push_back("Android NDK toolchain not found");
25         else
26                 set_command((ndk.get_bin_dir()/command).str());
27
28         if(ndk.get_platform_sysroot().empty())
29                 problems.push_back("Android platform not found");
30         else if(!ndk.get_common_sysroot().empty())
31         {
32                 build_info.sysroot = ndk.get_common_sysroot();
33                 /* The common sysroot has asm headers in arch directories and the
34                 compiler doesn't pick them up automatically */
35                 build_info.incpath.push_back(ndk.get_common_sysroot()/"usr/include"/architecture->get_cross_prefix());
36         }
37         else
38                 build_info.sysroot = ndk.get_platform_sysroot();
39 }
40
41 void AndroidCompiler::do_prepare(ToolData &tool) const
42 {
43         const Architecture &arch = *static_cast<const Tool &>(tool).get_architecture();
44
45         CustomizedTool::do_prepare(tool);
46         if(tag=="CXX")
47         {
48                 auto i = find(tool.build_info.libs, "stdc++");
49                 if(i!=tool.build_info.libs.end())
50                         *i = "gnustl_static";
51                 else
52                         tool.build_info.libs.push_back("gnustl_static");
53
54                 unsigned version = tool.extra_data;
55                 string version_str = format("%d.%d.%d", version>>16, (version>>8)&0xFF, version&0xFF);
56                 FS::Path libstdcxx_dir = ndk.get_root_dir()/"sources"/"cxx-stl"/"gnu-libstdc++";
57                 FS::Path libstdcxx_path;
58                 while(1)
59                 {
60                         libstdcxx_path = libstdcxx_dir/version_str;
61                         if(FS::exists(libstdcxx_path))
62                                 break;
63
64                         string::size_type dot = version_str.rfind('.');
65                         if(dot==string::npos)
66                         {
67                                 tool.problems.push_back("C++ standard library not found");
68                                 return;
69                         }
70
71                         version_str = version_str.substr(0, dot);
72                 }
73
74                 builder.get_logger().log("tools", "Found GNU libstdc++ in %s", libstdcxx_path);
75
76                 FS::Path public_dir = libstdcxx_path/"include";
77                 tool.system_path.push_back(public_dir);
78                 tool.build_info.incpath.push_back(public_dir);
79
80                 FS::Path arch_path = libstdcxx_path/"libs";
81                 builder.get_logger().log("files", "Traversing %s", arch_path.str());
82                 string arch_dir = arch.best_match(list_files(arch_path));
83                 if(!arch_dir.empty())
84                 {
85                         tool.build_info.incpath.push_back(libstdcxx_path/"libs"/arch_dir/"include");
86                         tool.build_info.libpath.push_back(libstdcxx_path/"libs"/arch_dir);
87                 }
88         }
89 }