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.

468 lines
19KB

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