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.

430 lines
17KB

  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-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 projectType.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 Introjucer" << newLine
  155. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer 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. for (int i = 0; i < groups.size(); ++i)
  165. findAllFilesToCompile (groups.getReference(i), files);
  166. MemoryOutputStream mo;
  167. writeAndroidMk (mo, files);
  168. overwriteFileIfDifferentOrThrow (file, mo);
  169. }
  170. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files)
  171. {
  172. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  173. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  174. << newLine
  175. << "LOCAL_PATH := $(call my-dir)" << newLine
  176. << newLine
  177. << "include $(CLEAR_VARS)" << newLine
  178. << newLine
  179. << "LOCAL_CPP_EXTENSION := cpp" << newLine
  180. << "LOCAL_MODULE := juce_jni" << newLine
  181. << "LOCAL_SRC_FILES := \\" << newLine;
  182. for (int i = 0; i < files.size(); ++i)
  183. out << " ../" << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  184. out << newLine
  185. << "ifeq ($(CONFIG),Debug)" << newLine
  186. << " LOCAL_CPPFLAGS += " << createCPPFlags (true) << newLine
  187. << "else" << newLine
  188. << " LOCAL_CPPFLAGS += " << createCPPFlags (false) << newLine
  189. << "endif" << newLine
  190. << newLine
  191. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  192. }
  193. String createIncludePathFlags (const Project::BuildConfiguration& config)
  194. {
  195. String flags;
  196. StringArray searchPaths (extraSearchPaths);
  197. searchPaths.addArray (config.getHeaderSearchPaths());
  198. searchPaths.removeDuplicates (false);
  199. for (int i = 0; i < searchPaths.size(); ++i)
  200. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  201. return flags;
  202. }
  203. String createCPPFlags (bool forDebug)
  204. {
  205. String flags ("-fsigned-char -fexceptions -frtti");
  206. if (forDebug)
  207. flags << " -g";
  208. for (int i = 0; i < configs.size(); ++i)
  209. {
  210. if (configs.getReference(i).isDebug() == forDebug)
  211. {
  212. flags << createIncludePathFlags (configs.getReference(i));
  213. break;
  214. }
  215. }
  216. StringPairArray defines;
  217. defines.set ("JUCE_ANDROID", "1");
  218. if (forDebug)
  219. {
  220. defines.set ("DEBUG", "1");
  221. defines.set ("_DEBUG", "1");
  222. }
  223. else
  224. {
  225. defines.set ("NDEBUG", "1");
  226. }
  227. for (int i = 0; i < configs.size(); ++i)
  228. {
  229. const Project::BuildConfiguration& config = configs.getReference(i);
  230. if (config.isDebug() == forDebug)
  231. {
  232. flags << " -O" << config.getGCCOptimisationFlag();
  233. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  234. break;
  235. }
  236. }
  237. return flags + createGCCPreprocessorFlags (defines);
  238. }
  239. //==============================================================================
  240. XmlElement* createAntBuildXML()
  241. {
  242. XmlElement* proj = new XmlElement ("project");
  243. proj->setAttribute ("name", projectName);
  244. proj->setAttribute ("default", "debug");
  245. proj->createNewChildElement ("property")->setAttribute ("file", "local.properties");
  246. proj->createNewChildElement ("property")->setAttribute ("file", "build.properties");
  247. proj->createNewChildElement ("property")->setAttribute ("file", "default.properties");
  248. XmlElement* path = proj->createNewChildElement ("path");
  249. path->setAttribute ("id", "android.antlibs");
  250. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/anttasks.jar");
  251. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/sdklib.jar");
  252. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/androidprefs.jar");
  253. XmlElement* taskdef = proj->createNewChildElement ("taskdef");
  254. taskdef->setAttribute ("name", "setup");
  255. taskdef->setAttribute ("classname", "com.android.ant.SetupTask");
  256. taskdef->setAttribute ("classpathref", "android.antlibs");
  257. addNDKBuildStep (proj, "clean", "clean");
  258. //addLinkStep (proj, "${basedir}/" + rebaseFromProjectFolderToBuildTarget (RelativePath()).toUnixStyle() + "/", "jni/app");
  259. addLinkStep (proj, "${basedir}/" + getJucePathFromTargetFolder().toUnixStyle() + "/src/native/android/java/", "src/com/juce");
  260. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  261. addNDKBuildStep (proj, "release", "CONFIG=Release");
  262. proj->createNewChildElement ("setup");
  263. return proj;
  264. }
  265. static void addNDKBuildStep (XmlElement* project, const String& type, const String& arg)
  266. {
  267. XmlElement* target = project->createNewChildElement ("target");
  268. target->setAttribute ("name", type);
  269. XmlElement* executable = target->createNewChildElement ("exec");
  270. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  271. executable->setAttribute ("dir", "${basedir}");
  272. executable->setAttribute ("failonerror", "true");
  273. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  274. executable->createNewChildElement ("arg")->setAttribute ("value", arg);
  275. }
  276. static void addLinkStep (XmlElement* project, const String& from, const String& to)
  277. {
  278. XmlElement* executable = project->createNewChildElement ("exec");
  279. executable->setAttribute ("executable", "ln");
  280. executable->setAttribute ("dir", "${basedir}");
  281. executable->setAttribute ("failonerror", "false");
  282. executable->createNewChildElement ("arg")->setAttribute ("value", "-s");
  283. executable->createNewChildElement ("arg")->setAttribute ("value", from);
  284. executable->createNewChildElement ("arg")->setAttribute ("value", to);
  285. }
  286. void writeBuildPropertiesFile (const File& file)
  287. {
  288. MemoryOutputStream mo;
  289. mo << "# This file is used to override default values used by the Ant build system." << newLine;
  290. overwriteFileIfDifferentOrThrow (file, mo);
  291. }
  292. void writeDefaultPropertiesFile (const File& file)
  293. {
  294. MemoryOutputStream mo;
  295. mo << "# This file is used to override default values used by the Ant build system." << newLine
  296. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  297. << newLine
  298. << "target=android-9"
  299. << newLine;
  300. overwriteFileIfDifferentOrThrow (file, mo);
  301. }
  302. void writeLocalPropertiesFile (const File& file)
  303. {
  304. MemoryOutputStream mo;
  305. mo << "# This file is used to override default values used by the Ant build system." << newLine
  306. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  307. << newLine
  308. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  309. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  310. << newLine;
  311. overwriteFileIfDifferentOrThrow (file, mo);
  312. }
  313. void writeIcon (const File& file, int size)
  314. {
  315. Image im (getBestIconForSize (size, false));
  316. if (im.isValid())
  317. {
  318. PNGImageFormat png;
  319. MemoryOutputStream mo;
  320. if (! png.writeImageToStream (im, mo))
  321. throw SaveError ("Can't generate Android icon file");
  322. overwriteFileIfDifferentOrThrow (file, mo);
  323. }
  324. }
  325. void writeStringsFile (const File& file)
  326. {
  327. XmlElement strings ("resources");
  328. XmlElement* name = strings.createNewChildElement ("string");
  329. name->setAttribute ("name", "app_name");
  330. name->addTextElement (projectName);
  331. writeXmlOrThrow (strings, file, "utf-8", 100);
  332. }
  333. //==============================================================================
  334. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  335. };
  336. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__