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.

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