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.

459 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. }
  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. bool canCopeWithDuplicateFiles() { return false; }
  60. void launchProject()
  61. {
  62. }
  63. void createPropertyEditors (PropertyListBuilder& props)
  64. {
  65. ProjectExporter::createPropertyEditors (props);
  66. props.add (new TextPropertyComponent (getSDKPath(), "Android SDK Path", 1024, false),
  67. "The path to the Android SDK folder on the target build machine");
  68. props.add (new TextPropertyComponent (getNDKPath(), "Android NDK Path", 1024, false),
  69. "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. "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, true);
  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. writeProjectPropertiesFile (target.getChildFile ("project.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. protected:
  107. //==============================================================================
  108. class AndroidBuildConfiguration : public BuildConfiguration
  109. {
  110. public:
  111. AndroidBuildConfiguration (Project& project, const ValueTree& settings)
  112. : BuildConfiguration (project, settings)
  113. {
  114. androidDynamicLibs.add ("GLESv1_CM");
  115. androidDynamicLibs.add ("GLESv2");
  116. }
  117. void createPropertyEditors (PropertyListBuilder& props)
  118. {
  119. createBasicPropertyEditors (props);
  120. }
  121. StringArray androidDynamicLibs;
  122. };
  123. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  124. {
  125. return new AndroidBuildConfiguration (project, settings);
  126. }
  127. private:
  128. //==============================================================================
  129. XmlElement* createManifestXML()
  130. {
  131. XmlElement* manifest = new XmlElement ("manifest");
  132. manifest->setAttribute ("xmlns:android", "http://schemas.android.com/apk/res/android");
  133. manifest->setAttribute ("android:versionCode", "1");
  134. manifest->setAttribute ("android:versionName", "1.0");
  135. manifest->setAttribute ("package", "com.juce");
  136. XmlElement* screens = manifest->createNewChildElement ("supports-screens");
  137. screens->setAttribute ("android:smallScreens", "true");
  138. screens->setAttribute ("android:normalScreens", "true");
  139. screens->setAttribute ("android:largeScreens", "true");
  140. //screens->setAttribute ("android:xlargeScreens", "true");
  141. screens->setAttribute ("android:anyDensity", "true");
  142. if (getInternetNeeded().getValue())
  143. {
  144. XmlElement* permission = manifest->createNewChildElement ("uses-permission");
  145. permission->setAttribute ("android:name", "android.permission.INTERNET");
  146. }
  147. XmlElement* app = manifest->createNewChildElement ("application");
  148. app->setAttribute ("android:label", "@string/app_name");
  149. app->setAttribute ("android:icon", "@drawable/icon");
  150. XmlElement* act = app->createNewChildElement ("activity");
  151. act->setAttribute ("android:name", "JuceAppActivity");
  152. act->setAttribute ("android:label", "@string/app_name");
  153. XmlElement* intent = act->createNewChildElement ("intent-filter");
  154. intent->createNewChildElement ("action")->setAttribute ("android:name", "android.intent.action.MAIN");
  155. intent->createNewChildElement ("category")->setAttribute ("android:name", "android.intent.category.LAUNCHER");
  156. return manifest;
  157. }
  158. //==============================================================================
  159. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  160. {
  161. if (projectItem.isGroup())
  162. {
  163. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  164. findAllFilesToCompile (projectItem.getChild(i), results);
  165. }
  166. else
  167. {
  168. if (projectItem.shouldBeCompiled())
  169. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  170. }
  171. }
  172. void writeApplicationMk (const File& file)
  173. {
  174. MemoryOutputStream mo;
  175. mo << "# 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. << "APP_STL := gnustl_static" << newLine
  179. << "APP_CPPFLAGS += -fsigned-char -fexceptions -frtti" << newLine
  180. << "APP_PLATFORM := android-7" << newLine;
  181. overwriteFileIfDifferentOrThrow (file, mo);
  182. }
  183. void writeAndroidMk (const File& file)
  184. {
  185. Array<RelativePath> files;
  186. for (int i = 0; i < groups.size(); ++i)
  187. findAllFilesToCompile (groups.getReference(i), files);
  188. MemoryOutputStream mo;
  189. writeAndroidMk (mo, files);
  190. overwriteFileIfDifferentOrThrow (file, mo);
  191. }
  192. void writeAndroidMk (OutputStream& out, const Array<RelativePath>& files)
  193. {
  194. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  195. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  196. << newLine
  197. << "LOCAL_PATH := $(call my-dir)" << newLine
  198. << newLine
  199. << "include $(CLEAR_VARS)" << newLine
  200. << newLine
  201. << "LOCAL_MODULE := juce_jni" << newLine
  202. << "LOCAL_SRC_FILES := \\" << newLine;
  203. for (int i = 0; i < files.size(); ++i)
  204. out << " ../" << escapeSpaces (files.getReference(i).toUnixStyle()) << "\\" << newLine;
  205. String debugSettings, releaseSettings;
  206. out << newLine
  207. << "ifeq ($(CONFIG),Debug)" << newLine;
  208. writeConfigSettings (out, true);
  209. out << "else" << newLine;
  210. writeConfigSettings (out, false);
  211. out << "endif" << newLine
  212. << newLine
  213. << "include $(BUILD_SHARED_LIBRARY)" << newLine;
  214. }
  215. void writeConfigSettings (OutputStream& out, bool forDebug)
  216. {
  217. for (ConfigIterator config (*this); config.next();)
  218. {
  219. if (config->isDebug() == forDebug)
  220. {
  221. out << " LOCAL_CPPFLAGS += " << createCPPFlags (*config) << newLine
  222. << getDynamicLibs (dynamic_cast <const AndroidBuildConfiguration&> (*config));
  223. break;
  224. }
  225. }
  226. }
  227. String getDynamicLibs (const AndroidBuildConfiguration& config)
  228. {
  229. if (config.androidDynamicLibs.size() == 0)
  230. return String::empty;
  231. String flags (" LOCAL_LDLIBS :=");
  232. flags << config.getGCCLibraryPathFlags();
  233. for (int i = 0; i < config.androidDynamicLibs.size(); ++i)
  234. flags << " -l" << config.androidDynamicLibs[i];
  235. return flags + newLine;
  236. }
  237. String createIncludePathFlags (const BuildConfiguration& config)
  238. {
  239. String flags;
  240. StringArray searchPaths (extraSearchPaths);
  241. searchPaths.addArray (config.getHeaderSearchPaths());
  242. searchPaths.removeDuplicates (false);
  243. for (int i = 0; i < searchPaths.size(); ++i)
  244. flags << " -I " << FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])).quoted();
  245. return flags;
  246. }
  247. String createCPPFlags (const BuildConfiguration& config)
  248. {
  249. StringPairArray defines;
  250. defines.set ("JUCE_ANDROID", "1");
  251. String flags ("-fsigned-char -fexceptions -frtti");
  252. if (config.isDebug().getValue())
  253. {
  254. flags << " -g";
  255. defines.set ("DEBUG", "1");
  256. defines.set ("_DEBUG", "1");
  257. }
  258. else
  259. {
  260. defines.set ("NDEBUG", "1");
  261. }
  262. flags << createIncludePathFlags (config)
  263. << " -O" << config.getGCCOptimisationFlag();
  264. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  265. return flags + createGCCPreprocessorFlags (defines);
  266. }
  267. //==============================================================================
  268. XmlElement* createAntBuildXML()
  269. {
  270. XmlElement* proj = new XmlElement ("project");
  271. proj->setAttribute ("name", projectName);
  272. proj->setAttribute ("default", "debug");
  273. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "local.properties");
  274. proj->createNewChildElement ("loadproperties")->setAttribute ("srcFile", "project.properties");
  275. XmlElement* path = proj->createNewChildElement ("path");
  276. path->setAttribute ("id", "android.antlibs");
  277. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/anttasks.jar");
  278. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/sdklib.jar");
  279. path->createNewChildElement ("pathelement")->setAttribute ("path", "${sdk.dir}/tools/lib/androidprefs.jar");
  280. XmlElement* taskdef = proj->createNewChildElement ("taskdef");
  281. taskdef->setAttribute ("name", "setup");
  282. taskdef->setAttribute ("classname", "com.android.ant.SetupTask");
  283. taskdef->setAttribute ("classpathref", "android.antlibs");
  284. addNDKBuildStep (proj, "clean", "clean");
  285. //addLinkStep (proj, "${basedir}/" + rebaseFromProjectFolderToBuildTarget (RelativePath()).toUnixStyle() + "/", "jni/app");
  286. addLinkStep (proj, "${basedir}/" + getJucePathFromTargetFolder().toUnixStyle() + "/modules/juce_core/native/java/", "src/com/juce");
  287. addNDKBuildStep (proj, "debug", "CONFIG=Debug");
  288. addNDKBuildStep (proj, "release", "CONFIG=Release");
  289. proj->createNewChildElement ("import")->setAttribute ("file", "${sdk.dir}/tools/ant/build.xml");
  290. return proj;
  291. }
  292. static void addNDKBuildStep (XmlElement* project, const String& type, const String& arg)
  293. {
  294. XmlElement* target = project->createNewChildElement ("target");
  295. target->setAttribute ("name", type);
  296. XmlElement* executable = target->createNewChildElement ("exec");
  297. executable->setAttribute ("executable", "${ndk.dir}/ndk-build");
  298. executable->setAttribute ("dir", "${basedir}");
  299. executable->setAttribute ("failonerror", "true");
  300. executable->createNewChildElement ("arg")->setAttribute ("value", "--jobs=2");
  301. executable->createNewChildElement ("arg")->setAttribute ("value", arg);
  302. }
  303. static void addLinkStep (XmlElement* project, const String& from, const String& to)
  304. {
  305. XmlElement* executable = project->createNewChildElement ("exec");
  306. executable->setAttribute ("executable", "ln");
  307. executable->setAttribute ("dir", "${basedir}");
  308. executable->setAttribute ("failonerror", "false");
  309. executable->createNewChildElement ("arg")->setAttribute ("value", "-s");
  310. executable->createNewChildElement ("arg")->setAttribute ("value", from);
  311. executable->createNewChildElement ("arg")->setAttribute ("value", to);
  312. }
  313. void writeProjectPropertiesFile (const File& file)
  314. {
  315. MemoryOutputStream mo;
  316. mo << "# This file is used to override default values used by the Ant build system." << newLine
  317. << "# It is automatically generated - DO NOT EDIT IT or your changes will be lost!." << newLine
  318. << newLine
  319. << "target=Google Inc.:Google APIs:7" << newLine
  320. << newLine;
  321. overwriteFileIfDifferentOrThrow (file, mo);
  322. }
  323. void writeLocalPropertiesFile (const File& file)
  324. {
  325. MemoryOutputStream mo;
  326. mo << "# This file is used to override default values used by the Ant build system." << newLine
  327. << "# It is automatically generated by the Introjucer - DO NOT EDIT IT or your changes will be lost!." << newLine
  328. << newLine
  329. << "sdk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getSDKPath().toString())) << newLine
  330. << "ndk.dir=" << escapeSpaces (replacePreprocessorDefs (getAllPreprocessorDefs(), getNDKPath().toString())) << newLine
  331. << newLine;
  332. overwriteFileIfDifferentOrThrow (file, mo);
  333. }
  334. void writeIcon (const File& file, int size)
  335. {
  336. Image im (getBestIconForSize (size, false));
  337. if (im.isValid())
  338. {
  339. PNGImageFormat png;
  340. MemoryOutputStream mo;
  341. if (! png.writeImageToStream (im, mo))
  342. throw SaveError ("Can't generate Android icon file");
  343. overwriteFileIfDifferentOrThrow (file, mo);
  344. }
  345. }
  346. void writeStringsFile (const File& file)
  347. {
  348. XmlElement strings ("resources");
  349. XmlElement* name = strings.createNewChildElement ("string");
  350. name->setAttribute ("name", "app_name");
  351. name->addTextElement (projectName);
  352. writeXmlOrThrow (strings, file, "utf-8", 100);
  353. }
  354. //==============================================================================
  355. JUCE_DECLARE_NON_COPYABLE (AndroidProjectExporter);
  356. };
  357. #endif // __JUCER_PROJECTEXPORT_ANDROID_JUCEHEADER__