The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

439 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. //==============================================================================
  22. class AndroidProjectExporter : public ProjectExporter
  23. {
  24. public:
  25. //==============================================================================
  26. static const char* getNameAndroid() { return "Android Project"; }
  27. static const char* getValueTreeTypeName() { return "ANDROID"; }
  28. static AndroidProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  29. {
  30. if (settings.hasType (getValueTreeTypeName()))
  31. return new AndroidProjectExporter (project, settings);
  32. return 0;
  33. }
  34. //==============================================================================
  35. AndroidProjectExporter (Project& project_, const ValueTree& settings_)
  36. : ProjectExporter (project_, settings_)
  37. {
  38. name = getNameAndroid();
  39. if (getTargetLocation().toString().isEmpty())
  40. getTargetLocation() = getDefaultBuildsRootFolder() + "Android";
  41. if (getSDKPath().toString().isEmpty())
  42. getSDKPath() = "${user.home}/SDKs/android-sdk-macosx";
  43. if (getNDKPath().toString().isEmpty())
  44. getNDKPath() = "${user.home}/SDKs/android-ndk-r7";
  45. if (getInternetNeeded().toString().isEmpty())
  46. getInternetNeeded() = true;
  47. androidDynamicLibs.add ("GLESv1_CM");
  48. androidDynamicLibs.add ("GLESv2");
  49. }
  50. //==============================================================================
  51. int getLaunchPreferenceOrderForCurrentOS()
  52. {
  53. #if JUCE_ANDROID
  54. return 1;
  55. #else
  56. return 0;
  57. #endif
  58. }
  59. bool isPossibleForCurrentProject() { return projectType.isGUIApplication(); }
  60. bool usesMMFiles() const { return false; }
  61. bool canCopeWithDuplicateFiles() { return false; }
  62. void launchProject()
  63. {
  64. }
  65. void createPropertyEditors (PropertyListBuilder& props)
  66. {
  67. ProjectExporter::createPropertyEditors (props);
  68. props.add (new TextPropertyComponent (getSDKPath(), "Android SDK Path", 1024, false),
  69. "The path to the Android SDK folder on the target build machine");
  70. props.add (new TextPropertyComponent (getNDKPath(), "Android NDK Path", 1024, false),
  71. "The path to the Android NDK folder on the target build machine");
  72. props.add (new BooleanPropertyComponent (getInternetNeeded(), "Internet Access", "Specify internet access permission in the manifest"),
  73. "If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  74. }
  75. Value getSDKPath() const { return getSetting (Ids::androidSDKPath); }
  76. Value getNDKPath() const { return getSetting (Ids::androidNDKPath); }
  77. Value getInternetNeeded() const { return getSetting (Ids::androidInternetNeeded); }
  78. //==============================================================================
  79. void create()
  80. {
  81. const File target (getTargetFolder());
  82. const File jniFolder (target.getChildFile ("jni"));
  83. createDirectoryOrThrow (target.getChildFile ("src/com"));
  84. createDirectoryOrThrow (jniFolder);
  85. createDirectoryOrThrow (target.getChildFile ("res/drawable-hdpi"));
  86. createDirectoryOrThrow (target.getChildFile ("res/drawable-mdpi"));
  87. createDirectoryOrThrow (target.getChildFile ("res/drawable-ldpi"));
  88. createDirectoryOrThrow (target.getChildFile ("res/values"));
  89. createDirectoryOrThrow (target.getChildFile ("libs"));
  90. createDirectoryOrThrow (target.getChildFile ("bin"));
  91. {
  92. ScopedPointer<XmlElement> manifest (createManifestXML());
  93. writeXmlOrThrow (*manifest, target.getChildFile ("AndroidManifest.xml"), "utf-8", 100, true);
  94. }
  95. writeApplicationMk (jniFolder.getChildFile ("Application.mk"));
  96. writeAndroidMk (jniFolder.getChildFile ("Android.mk"));
  97. {
  98. ScopedPointer<XmlElement> antBuildXml (createAntBuildXML());
  99. writeXmlOrThrow (*antBuildXml, target.getChildFile ("build.xml"), "UTF-8", 100);
  100. }
  101. writeProjectPropertiesFile (target.getChildFile ("project.properties"));
  102. writeLocalPropertiesFile (target.getChildFile ("local.properties"));
  103. writeIcon (target.getChildFile ("res/drawable-hdpi/icon.png"), 72);
  104. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), 48);
  105. writeIcon (target.getChildFile ("res/drawable-ldpi/icon.png"), 36);
  106. writeStringsFile (target.getChildFile ("res/values/strings.xml"));
  107. }
  108. private:
  109. //==============================================================================
  110. XmlElement* createManifestXML()
  111. {
  112. XmlElement* manifest = new XmlElement ("manifest");
  113. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  114. manifest->setAttribute ("android:versionCode", "1");
  115. manifest->setAttribute ("android:versionName", "1.0");
  116. manifest->setAttribute ("package", "com.juce");
  117. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  118. screens->setAttribute ("android:smallScreens", "true");
  119. screens->setAttribute ("android:normalScreens", "true");
  120. screens->setAttribute ("android:largeScreens", "true");
  121. //screens->setAttribute ("android:xlargeScreens", "true");
  122. screens->setAttribute ("android:anyDensity", "true");
  123. if (getInternetNeeded().getValue())
  124. {
  125. XmlElement* permission = manifest->createNewChildElement ("uses-permission");
  126. permission->setAttribute ("android:name", "android.permission.INTERNET");
  127. }
  128. XmlElement* app = manifest->createNewChildElement ("application");
  129. app->setAttribute ("android:label", "@string/app_name");
  130. app->setAttribute ("android:icon", "@drawable/icon");
  131. XmlElement* act = app->createNewChildElement ("activity");
  132. act->setAttribute ("android:name", "JuceAppActivity");
  133. act->setAttribute ("android:label", "@string/app_name");
  134. XmlElement* intent = act->createNewChildElement ("intent-filter");
  135. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  136. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  137. return manifest;
  138. }
  139. //==============================================================================
  140. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  141. {
  142. if (projectItem.isGroup())
  143. {
  144. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  145. findAllFilesToCompile (projectItem.getChild(i), results);
  146. }
  147. else
  148. {
  149. if (projectItem.shouldBeCompiled())
  150. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  151. }
  152. }
  153. void writeApplicationMk (const File& file)
  154. {
  155. MemoryOutputStream mo;
  156. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  157. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  158. << newLine
  159. << "APP_STL := gnustl_static" << newLine
  160. << "APP_CPPFLAGS += -fsigned-char -fexceptions -frtti" << newLine
  161. << "APP_PLATFORM := android-7" << newLine;
  162. overwriteFileIfDifferentOrThrow (file, mo);
  163. }
  164. void writeAndroidMk (const File& file)
  165. {
  166. Array<RelativePath> files;
  167. for (int i = 0; i < groups.size(); ++i)
  168. findAllFilesToCompile (groups.getReference(i), files);
  169. MemoryOutputStream mo;
  170. writeAndroidMk (mo, files);
  171. overwriteFileIfDifferentOrThrow (file, mo);
  172. }
  173. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files)
  174. {
  175. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  176. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  177. << newLine
  178. << "LOCAL_PATH := $(call my-dir)" << newLine
  179. << newLine
  180. << "include $(CLEAR_VARS)" << newLine
  181. << newLine
  182. << "LOCAL_MODULE := juce_jni" << newLine
  183. << "LOCAL_SRC_FILES := \\" << newLine;
  184. for (int i = 0; i < files.size(); ++i)
  185. out << " ../" << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  186. out << newLine
  187. << "ifeq ($(CONFIG),Debug)" << newLine
  188. << " LOCAL_CPPFLAGS += " << createCPPFlags (true) << newLine
  189. << "else" << newLine
  190. << " LOCAL_CPPFLAGS += " << createCPPFlags (false) << newLine
  191. << "endif" << newLine
  192. << newLine
  193. << getDynamicLibs()
  194. << newLine
  195. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  196. }
  197. String getDynamicLibs()
  198. {
  199. if (androidDynamicLibs.size() == 0)
  200. return String::empty;
  201. String flags ("LOCAL_LDLIBS :=");
  202. for (int i = 0; i < androidDynamicLibs.size(); ++i)
  203. flags << " -l" << androidDynamicLibs[i];
  204. return flags + newLine;
  205. }
  206. String createIncludePathFlags (const Project::BuildConfiguration& config)
  207. {
  208. String flags;
  209. StringArray searchPaths (extraSearchPaths);
  210. searchPaths.addArray (config.getHeaderSearchPaths());
  211. searchPaths.removeDuplicates (false);
  212. for (int i = 0; i < searchPaths.size(); ++i)
  213. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  214. return flags;
  215. }
  216. String createCPPFlags (bool forDebug)
  217. {
  218. String flags ("-fsigned-char -fexceptions -frtti");
  219. if (forDebug)
  220. flags << " -g";
  221. for (int i = 0; i < configs.size(); ++i)
  222. {
  223. if (configs.getReference(i).isDebug() == forDebug)
  224. {
  225. flags << createIncludePathFlags (configs.getReference(i));
  226. break;
  227. }
  228. }
  229. StringPairArray defines;
  230. defines.set ("JUCE_ANDROID", "1");
  231. if (forDebug)
  232. {
  233. defines.set ("DEBUG", "1");
  234. defines.set ("_DEBUG", "1");
  235. }
  236. else
  237. {
  238. defines.set ("NDEBUG", "1");
  239. }
  240. for (int i = 0; i < configs.size(); ++i)
  241. {
  242. const Project::BuildConfiguration& config = configs.getReference(i);
  243. if (config.isDebug() == forDebug)
  244. {
  245. flags << " -O" << config.getGCCOptimisationFlag();
  246. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  247. break;
  248. }
  249. }
  250. return flags + createGCCPreprocessorFlags (defines);
  251. }
  252. //==============================================================================
  253. XmlElement* createAntBuildXML()
  254. {
  255. XmlElement* proj = new XmlElement ("project");
  256. proj->setAttribute ("name", projectName);
  257. proj->setAttribute ("default", "debug");
  258. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "local.properties");
  259. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "project.properties");
  260. XmlElement* path = proj->createNewChildElement ("path");
  261. path->setAttribute ("id", "android.antlibs");
  262. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/anttasks.jar");
  263. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/sdklib.jar");
  264. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/androidprefs.jar");
  265. XmlElement* taskdef = proj->createNewChildElement ("taskdef");
  266. taskdef->setAttribute ("name", "setup");
  267. taskdef->setAttribute ("classname", "com.android.ant.SetupTask");
  268. taskdef->setAttribute ("classpathref", "android.antlibs");
  269. addNDKBuildStep (proj, "clean", "clean");
  270. //addLinkStep (proj, "${basedir}/" + rebaseFromProjectFolderToBuildTarget (RelativePath()).toUnixStyle() + "/", "jni/app");
  271. addLinkStep (proj, "${basedir}/" + getJucePathFromTargetFolder().toUnixStyle() + "/modules/juce_core/native/java/", "src/com/juce");
  272. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  273. addNDKBuildStep (proj, "release", "CONFIG=Release");
  274. proj->createNewChildElement ("import")->setAttribute ("file", "${sdk.dir}/tools/ant/build.xml");
  275. return proj;
  276. }
  277. static void addNDKBuildStep (XmlElement* project, const String& type, const String& arg)
  278. {
  279. XmlElement* target = project->createNewChildElement ("target");
  280. target->setAttribute ("name", type);
  281. XmlElement* executable = target->createNewChildElement ("exec");
  282. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  283. executable->setAttribute ("dir", "${basedir}");
  284. executable->setAttribute ("failonerror", "true");
  285. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  286. executable->createNewChildElement ("arg")->setAttribute ("value", arg);
  287. }
  288. static void addLinkStep (XmlElement* project, const String& from, const String& to)
  289. {
  290. XmlElement* executable = project->createNewChildElement ("exec");
  291. executable->setAttribute ("executable", "ln");
  292. executable->setAttribute ("dir", "${basedir}");
  293. executable->setAttribute ("failonerror", "false");
  294. executable->createNewChildElement ("arg")->setAttribute ("value", "-s");
  295. executable->createNewChildElement ("arg")->setAttribute ("value", from);
  296. executable->createNewChildElement ("arg")->setAttribute ("value", to);
  297. }
  298. void writeProjectPropertiesFile (const File& file)
  299. {
  300. MemoryOutputStream mo;
  301. mo << "# This file is used to override default values used by the Ant build system." << newLine
  302. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  303. << newLine
  304. << "target=Google Inc.:Google APIs:7" << newLine
  305. << newLine;
  306. overwriteFileIfDifferentOrThrow (file, mo);
  307. }
  308. void writeLocalPropertiesFile (const File& file)
  309. {
  310. MemoryOutputStream mo;
  311. mo << "# This file is used to override default values used by the Ant build system." << newLine
  312. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  313. << newLine
  314. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  315. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  316. << newLine;
  317. overwriteFileIfDifferentOrThrow (file, mo);
  318. }
  319. void writeIcon (const File& file, int size)
  320. {
  321. Image im (getBestIconForSize (size, false));
  322. if (im.isValid())
  323. {
  324. PNGImageFormat png;
  325. MemoryOutputStream mo;
  326. if (! png.writeImageToStream (im, mo))
  327. throw SaveError ("Can't generate Android icon file");
  328. overwriteFileIfDifferentOrThrow (file, mo);
  329. }
  330. }
  331. void writeStringsFile (const File& file)
  332. {
  333. XmlElement strings ("resources");
  334. XmlElement* name = strings.createNewChildElement ("string");
  335. name->setAttribute ("name", "app_name");
  336. name->addTextElement (projectName);
  337. writeXmlOrThrow (strings, file, "utf-8", 100);
  338. }
  339. //==============================================================================
  340. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  341. };
  342. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__