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.

446 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-mac_x86";
  43. if (getNDKPath().toString().isEmpty())
  44. getNDKPath() = "${user.home}/SDKs/android-ndk-r6b";
  45. if (getInternetNeeded().toString().isEmpty())
  46. getInternetNeeded() = true;
  47. androidDynamicLibs.add ("GLESv1_CM");
  48. }
  49. //==============================================================================
  50. int getLaunchPreferenceOrderForCurrentOS()
  51. {
  52. #if JUCE_ANDROID
  53. return 1;
  54. #else
  55. return 0;
  56. #endif
  57. }
  58. bool isPossibleForCurrentProject() { return projectType.isGUIApplication(); }
  59. bool usesMMFiles() const { return false; }
  60. void launchProject()
  61. {
  62. }
  63. void createPropertyEditors (Array <PropertyComponent*>& props)
  64. {
  65. ProjectExporter::createPropertyEditors (props);
  66. props.add (new TextPropertyComponent (getSDKPath(), "Android SDK Path", 1024, false));
  67. props.getLast()->setTooltip ("The path to the Android SDK folder on the target build machine");
  68. props.add (new TextPropertyComponent (getNDKPath(), "Android NDK Path", 1024, false));
  69. props.getLast()->setTooltip ("The path to the Android NDK folder on the target build machine");
  70. props.add (new BooleanPropertyComponent (getInternetNeeded(), "Internet Access", "Specify internet access permission in the manifest"));
  71. props.getLast()->setTooltip ("If enabled, this will set the android.permission.INTERNET flag in the manifest.");
  72. }
  73. Value getSDKPath() const { return getSetting (Ids::androidSDKPath); }
  74. Value getNDKPath() const { return getSetting (Ids::androidNDKPath); }
  75. Value getInternetNeeded() const { return getSetting (Ids::androidInternetNeeded); }
  76. //==============================================================================
  77. void create()
  78. {
  79. const File target (getTargetFolder());
  80. const File jniFolder (target.getChildFile ("jni"));
  81. createDirectoryOrThrow (target.getChildFile ("src/com"));
  82. createDirectoryOrThrow (jniFolder);
  83. createDirectoryOrThrow (target.getChildFile ("res/drawable-hdpi"));
  84. createDirectoryOrThrow (target.getChildFile ("res/drawable-mdpi"));
  85. createDirectoryOrThrow (target.getChildFile ("res/drawable-ldpi"));
  86. createDirectoryOrThrow (target.getChildFile ("res/values"));
  87. createDirectoryOrThrow (target.getChildFile ("libs"));
  88. createDirectoryOrThrow (target.getChildFile ("bin"));
  89. {
  90. ScopedPointer<XmlElement> manifest (createManifestXML());
  91. writeXmlOrThrow (*manifest, target.getChildFile ("AndroidManifest.xml"), "utf-8", 100);
  92. }
  93. writeApplicationMk (jniFolder.getChildFile ("Application.mk"));
  94. writeAndroidMk (jniFolder.getChildFile ("Android.mk"));
  95. {
  96. ScopedPointer<XmlElement> antBuildXml (createAntBuildXML());
  97. writeXmlOrThrow (*antBuildXml, target.getChildFile ("build.xml"), "UTF-8", 100);
  98. }
  99. writeBuildPropertiesFile (target.getChildFile ("build.properties"));
  100. writeDefaultPropertiesFile (target.getChildFile ("default.properties"));
  101. writeLocalPropertiesFile (target.getChildFile ("local.properties"));
  102. writeIcon (target.getChildFile ("res/drawable-hdpi/icon.png"), 72);
  103. writeIcon (target.getChildFile ("res/drawable-mdpi/icon.png"), 48);
  104. writeIcon (target.getChildFile ("res/drawable-ldpi/icon.png"), 36);
  105. writeStringsFile (target.getChildFile ("res/values/strings.xml"));
  106. }
  107. private:
  108. //==============================================================================
  109. XmlElement* createManifestXML()
  110. {
  111. XmlElement* manifest = new XmlElement ("manifest");
  112. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  113. manifest->setAttribute ("android:versionCode", "1");
  114. manifest->setAttribute ("android:versionName", "1.0");
  115. manifest->setAttribute ("package", "com.juce");
  116. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  117. screens->setAttribute ("android:smallScreens", "true");
  118. screens->setAttribute ("android:normalScreens", "true");
  119. screens->setAttribute ("android:largeScreens", "true");
  120. screens->setAttribute ("android:xlargeScreens", "true");
  121. screens->setAttribute ("android:anyDensity", "true");
  122. if (getInternetNeeded().getValue())
  123. {
  124. XmlElement* permission = manifest->createNewChildElement ("uses-permission");
  125. permission->setAttribute ("android:name", "android.permission.INTERNET");
  126. }
  127. XmlElement* app = manifest->createNewChildElement ("application");
  128. app->setAttribute ("android:label", "@string/app_name");
  129. app->setAttribute ("android:icon", "@drawable/icon");
  130. XmlElement* act = app->createNewChildElement ("activity");
  131. act->setAttribute ("android:name", "JuceAppActivity");
  132. act->setAttribute ("android:label", "@string/app_name");
  133. XmlElement* intent = act->createNewChildElement ("intent-filter");
  134. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  135. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  136. return manifest;
  137. }
  138. //==============================================================================
  139. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  140. {
  141. if (projectItem.isGroup())
  142. {
  143. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  144. findAllFilesToCompile (projectItem.getChild(i), results);
  145. }
  146. else
  147. {
  148. if (projectItem.shouldBeCompiled())
  149. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  150. }
  151. }
  152. void writeApplicationMk (const File& file)
  153. {
  154. MemoryOutputStream mo;
  155. mo << "# Automatically generated makefile, created by the Introjucer" << newLine
  156. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  157. << newLine
  158. << "APP_STL := gnustl_static" << newLine
  159. << "APP_CPPFLAGS += -fsigned-char -fexceptions -frtti" << newLine;
  160. overwriteFileIfDifferentOrThrow (file, mo);
  161. }
  162. void writeAndroidMk (const File& file)
  163. {
  164. Array<RelativePath> files;
  165. for (int i = 0; i < groups.size(); ++i)
  166. findAllFilesToCompile (groups.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 Introjucer" << newLine
  174. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer 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. << getDynamicLibs()
  193. << newLine
  194. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  195. }
  196. String getDynamicLibs()
  197. {
  198. if (androidDynamicLibs.size() == 0)
  199. return String::empty;
  200. String flags ("LOCAL_LDLIBS :=");
  201. for (int i = 0; i < androidDynamicLibs.size(); ++i)
  202. flags << " -l" << androidDynamicLibs[i];
  203. return flags + newLine;
  204. }
  205. String createIncludePathFlags (const Project::BuildConfiguration& config)
  206. {
  207. String flags;
  208. StringArray searchPaths (extraSearchPaths);
  209. searchPaths.addArray (config.getHeaderSearchPaths());
  210. searchPaths.removeDuplicates (false);
  211. for (int i = 0; i < searchPaths.size(); ++i)
  212. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  213. return flags;
  214. }
  215. String createCPPFlags (bool forDebug)
  216. {
  217. String flags ("-fsigned-char -fexceptions -frtti");
  218. if (forDebug)
  219. flags << " -g";
  220. for (int i = 0; i < configs.size(); ++i)
  221. {
  222. if (configs.getReference(i).isDebug() == forDebug)
  223. {
  224. flags << createIncludePathFlags (configs.getReference(i));
  225. break;
  226. }
  227. }
  228. StringPairArray defines;
  229. defines.set ("JUCE_ANDROID", "1");
  230. if (forDebug)
  231. {
  232. defines.set ("DEBUG", "1");
  233. defines.set ("_DEBUG", "1");
  234. }
  235. else
  236. {
  237. defines.set ("NDEBUG", "1");
  238. }
  239. for (int i = 0; i < configs.size(); ++i)
  240. {
  241. const Project::BuildConfiguration& config = configs.getReference(i);
  242. if (config.isDebug() == forDebug)
  243. {
  244. flags << " -O" << config.getGCCOptimisationFlag();
  245. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  246. break;
  247. }
  248. }
  249. return flags + createGCCPreprocessorFlags (defines);
  250. }
  251. //==============================================================================
  252. XmlElement* createAntBuildXML()
  253. {
  254. XmlElement* proj = new XmlElement ("project");
  255. proj->setAttribute ("name", projectName);
  256. proj->setAttribute ("default", "debug");
  257. proj->createNewChildElement ("property")->setAttribute ("file", "local.properties");
  258. proj->createNewChildElement ("property")->setAttribute ("file", "build.properties");
  259. proj->createNewChildElement ("property")->setAttribute ("file", "default.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() + "/src/native/android/java/", "src/com/juce");
  272. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  273. addNDKBuildStep (proj, "release", "CONFIG=Release");
  274. proj->createNewChildElement ("setup");
  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 writeBuildPropertiesFile (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. overwriteFileIfDifferentOrThrow (file, mo);
  303. }
  304. void writeDefaultPropertiesFile (const File& file)
  305. {
  306. MemoryOutputStream mo;
  307. mo << "# This file is used to override default values used by the Ant build system." << newLine
  308. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  309. << newLine
  310. << "target=android-9"
  311. << newLine;
  312. overwriteFileIfDifferentOrThrow (file, mo);
  313. }
  314. void writeLocalPropertiesFile (const File& file)
  315. {
  316. MemoryOutputStream mo;
  317. mo << "# This file is used to override default values used by the Ant build system." << newLine
  318. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  319. << newLine
  320. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  321. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  322. << newLine;
  323. overwriteFileIfDifferentOrThrow (file, mo);
  324. }
  325. void writeIcon (const File& file, int size)
  326. {
  327. Image im (getBestIconForSize (size, false));
  328. if (im.isValid())
  329. {
  330. PNGImageFormat png;
  331. MemoryOutputStream mo;
  332. if (! png.writeImageToStream (im, mo))
  333. throw SaveError ("Can't generate Android icon file");
  334. overwriteFileIfDifferentOrThrow (file, mo);
  335. }
  336. }
  337. void writeStringsFile (const File& file)
  338. {
  339. XmlElement strings ("resources");
  340. XmlElement* name = strings.createNewChildElement ("string");
  341. name->setAttribute ("name", "app_name");
  342. name->addTextElement (projectName);
  343. writeXmlOrThrow (strings, file, "utf-8", 100);
  344. }
  345. //==============================================================================
  346. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  347. };
  348. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__