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.

409 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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-mac_x86";
  43. if (getNDKPath().toString().isEmpty())
  44. getNDKPath() = "${user.home}/SDKs/android-ndk-r5";
  45. if (getInternetNeeded().toString().isEmpty())
  46. getInternetNeeded() = true;
  47. }
  48. //==============================================================================
  49. int getLaunchPreferenceOrderForCurrentOS()
  50. {
  51. #if JUCE_ANDROID
  52. return 1;
  53. #else
  54. return 0;
  55. #endif
  56. }
  57. bool isPossibleForCurrentProject() { return project.getProjectType().isGUIApplication(); }
  58. bool usesMMFiles() const { return false; }
  59. void launchProject()
  60. {
  61. }
  62. void createPropertyEditors (Array <PropertyComponent*>& props)
  63. {
  64. ProjectExporter::createPropertyEditors (props);
  65. props.add (new TextPropertyComponent (getSDKPath(), "Android SDK Path", 1024, false));
  66. props.getLast()->setTooltip ("The path to the Android SDK folder on the target build machine");
  67. props.add (new TextPropertyComponent (getNDKPath(), "Android NDK Path", 1024, false));
  68. props.getLast()->setTooltip ("The path to the Android NDK folder on the target build machine");
  69. props.add (new BooleanPropertyComponent (getInternetNeeded(), "Internet Access", "Specify internet access permission in the manifest"));
  70. props.getLast()->setTooltip ("If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  71. }
  72. Value getSDKPath() const { return getSetting (Ids::androidSDKPath); }
  73. Value getNDKPath() const { return getSetting (Ids::androidNDKPath); }
  74. Value getInternetNeeded() const { return getSetting (Ids::androidInternetNeeded); }
  75. //==============================================================================
  76. void create()
  77. {
  78. const File target (getTargetFolder());
  79. const File jniFolder (target.getChildFile ("jni"));
  80. createDirectoryOrThrow (target.getChildFile ("src/com"));
  81. createDirectoryOrThrow (jniFolder);
  82. createDirectoryOrThrow (target.getChildFile ("res/drawable-hdpi"));
  83. createDirectoryOrThrow (target.getChildFile ("res/drawable-mdpi"));
  84. createDirectoryOrThrow (target.getChildFile ("res/drawable-ldpi"));
  85. createDirectoryOrThrow (target.getChildFile ("res/values"));
  86. createDirectoryOrThrow (target.getChildFile ("libs"));
  87. createDirectoryOrThrow (target.getChildFile ("bin"));
  88. {
  89. ScopedPointer<XmlElement> manifest (createManifestXML());
  90. writeXmlOrThrow (*manifest, target.getChildFile ("AndroidManifest.xml"), "utf-8", 100);
  91. }
  92. writeApplicationMk (jniFolder.getChildFile ("Application.mk"));
  93. writeAndroidMk (jniFolder.getChildFile ("Android.mk"));
  94. {
  95. ScopedPointer<XmlElement> antBuildXml (createAntBuildXML());
  96. writeXmlOrThrow (*antBuildXml, target.getChildFile ("build.xml"), "UTF-8", 100);
  97. }
  98. writeBuildPropertiesFile (target.getChildFile ("build.properties"));
  99. writeDefaultPropertiesFile (target.getChildFile ("default.properties"));
  100. writeLocalPropertiesFile (target.getChildFile ("local.properties"));
  101. writeIcon (target.getChildFile ("res/drawable-hdpi/icon.png"), 72);
  102. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), 48);
  103. writeIcon (target.getChildFile ("res/drawable-ldpi/icon.png"), 36);
  104. writeStringsFile (target.getChildFile ("res/values/strings.xml"));
  105. }
  106. private:
  107. //==============================================================================
  108. XmlElement* createManifestXML()
  109. {
  110. XmlElement* manifest = new XmlElement ("manifest");
  111. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  112. manifest->setAttribute ("android:versionCode", "1");
  113. manifest->setAttribute ("android:versionName", "1.0");
  114. manifest->setAttribute ("package", "com.juce");
  115. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  116. screens->setAttribute ("android:smallScreens", "true");
  117. screens->setAttribute ("android:normalScreens", "true");
  118. screens->setAttribute ("android:largeScreens", "true");
  119. screens->setAttribute ("android:xlargeScreens", "true");
  120. screens->setAttribute ("android:anyDensity", "true");
  121. if (getInternetNeeded().getValue())
  122. {
  123. XmlElement* permission = manifest->createNewChildElement ("uses-permission");
  124. permission->setAttribute ("android:name", "android.permission.INTERNET");
  125. }
  126. XmlElement* app = manifest->createNewChildElement ("application");
  127. app->setAttribute ("android:label", "@string/app_name");
  128. app->setAttribute ("android:icon", "@drawable/icon");
  129. XmlElement* act = app->createNewChildElement ("activity");
  130. act->setAttribute ("android:name", "JuceAppActivity");
  131. act->setAttribute ("android:label", "@string/app_name");
  132. XmlElement* intent = act->createNewChildElement ("intent-filter");
  133. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  134. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  135. return manifest;
  136. }
  137. //==============================================================================
  138. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  139. {
  140. if (projectItem.isGroup())
  141. {
  142. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  143. findAllFilesToCompile (projectItem.getChild(i), results);
  144. }
  145. else
  146. {
  147. if (projectItem.shouldBeCompiled())
  148. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  149. }
  150. }
  151. void writeApplicationMk (const File& file)
  152. {
  153. MemoryOutputStream mo;
  154. mo << "# Automatically generated makefile, created by the Jucer" << newLine
  155. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  156. << newLine
  157. << "APP_STL := gnustl_static" << newLine
  158. << "APP_CPPFLAGS += -fsigned-char -fexceptions -frtti" << newLine;
  159. overwriteFileIfDifferentOrThrow (file, mo);
  160. }
  161. void writeAndroidMk (const File& file)
  162. {
  163. Array<RelativePath> files;
  164. findAllFilesToCompile (project.getMainGroup(), files);
  165. for (int i = 0; i < generatedGroups.size(); ++i)
  166. findAllFilesToCompile (generatedGroups.getReference(i), files);
  167. MemoryOutputStream mo;
  168. writeAndroidMk (mo, files);
  169. overwriteFileIfDifferentOrThrow (file, mo);
  170. }
  171. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files)
  172. {
  173. out << "# Automatically generated makefile, created by the Jucer" << newLine
  174. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  175. << newLine
  176. << "LOCAL_PATH := $(call my-dir)" << newLine
  177. << newLine
  178. << "include $(CLEAR_VARS)" << newLine
  179. << newLine
  180. << "LOCAL_CPP_EXTENSION := cpp" << newLine
  181. << "LOCAL_MODULE := juce_jni" << newLine
  182. << "LOCAL_SRC_FILES := \\" << newLine;
  183. for (int i = 0; i < files.size(); ++i)
  184. out << " ../" << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  185. out << newLine
  186. << "ifeq ($(CONFIG),Debug)" << newLine
  187. << " LOCAL_CPPFLAGS += " << createCPPFlags (true) << newLine
  188. << "else" << newLine
  189. << " LOCAL_CPPFLAGS += " << createCPPFlags (false) << newLine
  190. << "endif" << newLine
  191. << newLine
  192. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  193. }
  194. String createCPPFlags (bool forDebug)
  195. {
  196. String flags ("-fsigned-char -fexceptions -frtti");
  197. if (forDebug)
  198. flags << " -g";
  199. StringPairArray defines;
  200. defines.set ("JUCE_ANDROID", "1");
  201. if (forDebug)
  202. {
  203. defines.set ("DEBUG", "1");
  204. defines.set ("_DEBUG", "1");
  205. }
  206. else
  207. {
  208. defines.set ("NDEBUG", "1");
  209. }
  210. for (int i = 0; i < project.getNumConfigurations(); ++i)
  211. {
  212. Project::BuildConfiguration config (project.getConfiguration(i));
  213. if (config.isDebug() == forDebug)
  214. {
  215. flags << " -O" << config.getGCCOptimisationFlag();
  216. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  217. break;
  218. }
  219. }
  220. return flags + createGCCPreprocessorFlags (defines);
  221. }
  222. //==============================================================================
  223. XmlElement* createAntBuildXML()
  224. {
  225. XmlElement* proj = new XmlElement ("project");
  226. proj->setAttribute ("name", project.getProjectName().toString());
  227. proj->setAttribute ("default", "debug");
  228. proj->createNewChildElement ("property")->setAttribute ("file", "local.properties");
  229. proj->createNewChildElement ("property")->setAttribute ("file", "build.properties");
  230. proj->createNewChildElement ("property")->setAttribute ("file", "default.properties");
  231. XmlElement* path = proj->createNewChildElement ("path");
  232. path->setAttribute ("id", "android.antlibs");
  233. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/anttasks.jar");
  234. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/sdklib.jar");
  235. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/androidprefs.jar");
  236. XmlElement* taskdef = proj->createNewChildElement ("taskdef");
  237. taskdef->setAttribute ("name", "setup");
  238. taskdef->setAttribute ("classname", "com.android.ant.SetupTask");
  239. taskdef->setAttribute ("classpathref", "android.antlibs");
  240. addNDKBuildStep (proj, "clean", "clean");
  241. //addLinkStep (proj, "${basedir}/" + rebaseFromProjectFolderToBuildTarget (RelativePath()).toUnixStyle() + "/", "jni/app");
  242. addLinkStep (proj, "${basedir}/" + getJucePathFromTargetFolder().toUnixStyle() + "/src/native/android/java/", "src/com/juce");
  243. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  244. addNDKBuildStep (proj, "release", "CONFIG=Release");
  245. proj->createNewChildElement ("setup");
  246. return proj;
  247. }
  248. static void addNDKBuildStep (XmlElement* project, const String& type, const String& arg)
  249. {
  250. XmlElement* target = project->createNewChildElement ("target");
  251. target->setAttribute ("name", type);
  252. XmlElement* executable = target->createNewChildElement ("exec");
  253. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  254. executable->setAttribute ("dir", "${basedir}");
  255. executable->setAttribute ("failonerror", "true");
  256. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  257. executable->createNewChildElement ("arg")->setAttribute ("value", arg);
  258. }
  259. static void addLinkStep (XmlElement* project, const String& from, const String& to)
  260. {
  261. XmlElement* executable = project->createNewChildElement ("exec");
  262. executable->setAttribute ("executable", "ln");
  263. executable->setAttribute ("dir", "${basedir}");
  264. executable->setAttribute ("failonerror", "false");
  265. executable->createNewChildElement ("arg")->setAttribute ("value", "-s");
  266. executable->createNewChildElement ("arg")->setAttribute ("value", from);
  267. executable->createNewChildElement ("arg")->setAttribute ("value", to);
  268. }
  269. void writeBuildPropertiesFile (const File& file)
  270. {
  271. MemoryOutputStream mo;
  272. mo << "# This file is used to override default values used by the Ant build system." << newLine;
  273. overwriteFileIfDifferentOrThrow (file, mo);
  274. }
  275. void writeDefaultPropertiesFile (const File& file)
  276. {
  277. MemoryOutputStream mo;
  278. mo << "# This file is used to override default values used by the Ant build system." << newLine
  279. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  280. << newLine
  281. << "target=android-9"
  282. << newLine;
  283. overwriteFileIfDifferentOrThrow (file, mo);
  284. }
  285. void writeLocalPropertiesFile (const File& file)
  286. {
  287. MemoryOutputStream mo;
  288. mo << "# This file is used to override default values used by the Ant build system." << newLine
  289. << "# It is automatically generated by the Jucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  290. << newLine
  291. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  292. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  293. << newLine;
  294. overwriteFileIfDifferentOrThrow (file, mo);
  295. }
  296. void writeIcon (const File& file, int size)
  297. {
  298. Image im (project.getBestIconForSize (size, false));
  299. if (im.isValid())
  300. {
  301. PNGImageFormat png;
  302. MemoryOutputStream mo;
  303. if (! png.writeImageToStream (im, mo))
  304. throw SaveError ("Can't generate Android icon file");
  305. overwriteFileIfDifferentOrThrow (file, mo);
  306. }
  307. }
  308. void writeStringsFile (const File& file)
  309. {
  310. XmlElement strings ("resources");
  311. XmlElement* name = strings.createNewChildElement ("string");
  312. name->setAttribute ("name", "app_name");
  313. name->addTextElement (project.getProjectName().toString());
  314. writeXmlOrThrow (strings, file, "utf-8", 100);
  315. }
  316. //==============================================================================
  317. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  318. };
  319. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__