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.

1163 lines
47KB

  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_XCODE_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. namespace
  22. {
  23. const char* const osxVersionDefault = "default";
  24. const char* const osxVersion10_4 = "10.4 SDK";
  25. const char* const osxVersion10_5 = "10.5 SDK";
  26. const char* const osxVersion10_6 = "10.6 SDK";
  27. const char* const osxVersion10_7 = "10.7 SDK";
  28. const char* const osxArch_Default = "default";
  29. const char* const osxArch_Native = "Native";
  30. const char* const osxArch_32BitUniversal = "32BitUniversal";
  31. const char* const osxArch_64BitUniversal = "64BitUniversal";
  32. const char* const osxArch_64Bit = "64BitIntel";
  33. }
  34. //==============================================================================
  35. class XCodeProjectExporter : public ProjectExporter
  36. {
  37. public:
  38. //==============================================================================
  39. static const char* getNameMac() { return "XCode (MacOSX)"; }
  40. static const char* getNameiOS() { return "XCode (iOS)"; }
  41. static const char* getValueTreeTypeName (bool iOS) { return iOS ? "XCODE_IPHONE" : "XCODE_MAC"; }
  42. //==============================================================================
  43. XCodeProjectExporter (Project& project_, const ValueTree& settings_, const bool iOS_)
  44. : ProjectExporter (project_, settings_),
  45. iOS (iOS_)
  46. {
  47. name = iOS ? getNameiOS() : getNameMac();
  48. if (getTargetLocationString().isEmpty())
  49. getTargetLocationValue() = getDefaultBuildsRootFolder() + (iOS ? "iOS" : "MacOSX");
  50. if (settings ["objCExtraSuffix"].isVoid())
  51. getObjCSuffixValue() = createAlphaNumericUID();
  52. }
  53. static XCodeProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  54. {
  55. if (settings.hasType (getValueTreeTypeName (false)))
  56. return new XCodeProjectExporter (project, settings, false);
  57. else if (settings.hasType (getValueTreeTypeName (true)))
  58. return new XCodeProjectExporter (project, settings, true);
  59. return nullptr;
  60. }
  61. //==============================================================================
  62. Value getObjCSuffixValue() { return getSetting ("objCExtraSuffix"); }
  63. String getObjCSuffixString() const { return settings ["objCExtraSuffix"]; }
  64. Value getPListToMergeValue() { return getSetting ("customPList"); }
  65. String getPListToMergeString() const { return settings ["customPList"]; }
  66. Value getExtraFrameworksValue() { return getSetting (Ids::extraFrameworks); }
  67. String getExtraFrameworksString() const { return settings [Ids::extraFrameworks]; }
  68. int getLaunchPreferenceOrderForCurrentOS()
  69. {
  70. #if JUCE_MAC
  71. return iOS ? 1 : 2;
  72. #else
  73. return 0;
  74. #endif
  75. }
  76. bool isAvailableOnCurrentOS()
  77. {
  78. #if JUCE_MAC
  79. return true;
  80. #else
  81. return false;
  82. #endif
  83. }
  84. bool isPossibleForCurrentProject() { return projectType.isGUIApplication() || ! iOS; }
  85. bool usesMMFiles() const { return true; }
  86. bool isXcode() const { return true; }
  87. bool isOSX() const { return ! iOS; }
  88. bool canCopeWithDuplicateFiles() { return true; }
  89. void createPropertyEditors (PropertyListBuilder& props)
  90. {
  91. ProjectExporter::createPropertyEditors (props);
  92. props.add (new TextPropertyComponent (getObjCSuffixValue(), "Objective-C class name suffix", 64, false),
  93. "Because objective-C linkage is done by string-matching, you can get horrible linkage mix-ups when different modules containing the "
  94. "same class-names are loaded simultaneously. This setting lets you provide a unique string that will be used in naming "
  95. "the obj-C classes in your executable to avoid this.");
  96. if (projectType.isGUIApplication() && ! iOS)
  97. {
  98. props.add (new TextPropertyComponent (getSetting ("documentExtensions"), "Document file extensions", 128, false),
  99. "A comma-separated list of file extensions for documents that your app can open.");
  100. }
  101. else if (iOS)
  102. {
  103. props.add (new BooleanPropertyComponent (getSetting ("UIFileSharingEnabled"), "File Sharing Enabled", "Enabled"),
  104. "Enable this to expose your app's files to iTunes.");
  105. props.add (new BooleanPropertyComponent (getSetting ("UIStatusBarHidden"), "Status Bar Hidden", "Enabled"),
  106. "Enable this to disable the status bar in your app.");
  107. }
  108. props.add (new TextPropertyComponent (getPListToMergeValue(), "Custom PList", 8192, true),
  109. "You can paste the contents of an XML PList file in here, and the settings that it contains will override any "
  110. "settings that the Introjucer creates. BEWARE! When doing this, be careful to remove from the XML any "
  111. "values that you DO want the introjucer to change!");
  112. props.add (new TextPropertyComponent (getExtraFrameworksValue(), "Extra Frameworks", 2048, false),
  113. "A comma-separated list of extra frameworks that should be added to the build. "
  114. "(Don't include the .framework extension in the name)");
  115. if (projectType.isLibrary())
  116. {
  117. const char* const libTypes[] = { "Static Library (.a)", "Dynamic Library (.dylib)", 0 };
  118. const int libTypeValues[] = { 1, 2, 0 };
  119. props.add (new ChoicePropertyComponent (getLibraryType(), "Library Type",
  120. StringArray (libTypes), Array<var> (libTypeValues)));
  121. }
  122. }
  123. void launchProject()
  124. {
  125. getProjectBundle().startAsProcess();
  126. }
  127. //==============================================================================
  128. void create (const OwnedArray<LibraryModule>&) const
  129. {
  130. infoPlistFile = getTargetFolder().getChildFile ("Info.plist");
  131. createIconFile();
  132. File projectBundle (getProjectBundle());
  133. createDirectoryOrThrow (projectBundle);
  134. createObjects();
  135. File projectFile (projectBundle.getChildFile ("project.pbxproj"));
  136. {
  137. MemoryOutputStream mo;
  138. writeProjectFile (mo);
  139. overwriteFileIfDifferentOrThrow (projectFile, mo);
  140. }
  141. writeInfoPlistFile();
  142. }
  143. protected:
  144. Value getLibraryType() { return getSetting (Ids::libraryType); }
  145. bool isStaticLibrary() const { return projectType.isLibrary() && (int) settings [Ids::libraryType] == 1; }
  146. //==============================================================================
  147. class XcodeBuildConfiguration : public BuildConfiguration
  148. {
  149. public:
  150. XcodeBuildConfiguration (Project& project, const ValueTree& settings)
  151. : BuildConfiguration (project, settings)
  152. {
  153. }
  154. Value getMacSDKVersionValue() { return getValue (Ids::osxSDK); }
  155. String getMacSDKVersion() const { return config [Ids::osxSDK]; }
  156. Value getMacCompatibilityVersionValue() { return getValue (Ids::osxCompatibility); }
  157. String getMacCompatibilityVersion() const { return config [Ids::osxCompatibility]; }
  158. Value getMacArchitectureValue() { return getValue (Ids::osxArchitecture); }
  159. String getMacArchitecture() const { return config [Ids::osxArchitecture]; }
  160. Value getCustomXcodeFlagsValue() { return getValue (Ids::customXcodeFlags); }
  161. String getCustomXcodeFlags() const { return config [Ids::customXcodeFlags]; }
  162. void createPropertyEditors (PropertyListBuilder& props)
  163. {
  164. createBasicPropertyEditors (props);
  165. if (getMacSDKVersion().isEmpty())
  166. getMacSDKVersionValue() = osxVersionDefault;
  167. const char* osxVersions[] = { "Use Default", osxVersion10_4, osxVersion10_5, osxVersion10_6, osxVersion10_7, 0 };
  168. const char* osxVersionValues[] = { osxVersionDefault, osxVersion10_4, osxVersion10_5, osxVersion10_6, osxVersion10_7, 0 };
  169. props.add (new ChoicePropertyComponent (getMacSDKVersionValue(), "OSX Base SDK Version", StringArray (osxVersions), Array<var> (osxVersionValues)),
  170. "The version of OSX to link against in the XCode build.");
  171. if (getMacCompatibilityVersion().isEmpty())
  172. getMacCompatibilityVersionValue() = osxVersionDefault;
  173. props.add (new ChoicePropertyComponent (getMacCompatibilityVersionValue(), "OSX Compatibility Version", StringArray (osxVersions), Array<var> (osxVersionValues)),
  174. "The minimum version of OSX that the target binary will be compatible with.");
  175. const char* osxArch[] = { "Use Default", "Native architecture of build machine", "Universal Binary (32-bit)", "Universal Binary (64-bit)", "64-bit Intel", 0 };
  176. const char* osxArchValues[] = { osxArch_Default, osxArch_Native, osxArch_32BitUniversal, osxArch_64BitUniversal, osxArch_64Bit, 0 };
  177. if (getMacArchitecture().isEmpty())
  178. getMacArchitectureValue() = osxArch_Default;
  179. props.add (new ChoicePropertyComponent (getMacArchitectureValue(), "OSX Architecture", StringArray (osxArch), Array<var> (osxArchValues)),
  180. "The type of OSX binary that will be produced.");
  181. props.add (new TextPropertyComponent (getCustomXcodeFlagsValue(), "Custom Xcode flags", 8192, false),
  182. "A comma-separated list of custom Xcode setting flags which will be appended to the list of generated flags, "
  183. "e.g. MACOSX_DEPLOYMENT_TARGET_i386 = 10.5, VALID_ARCHS = \"ppc i386 x86_64\"");
  184. }
  185. };
  186. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  187. {
  188. return new XcodeBuildConfiguration (project, settings);
  189. }
  190. private:
  191. mutable OwnedArray<ValueTree> pbxBuildFiles, pbxFileReferences, pbxGroups, misc, projectConfigs, targetConfigs;
  192. mutable StringArray buildPhaseIDs, resourceIDs, sourceIDs, frameworkIDs;
  193. mutable StringArray frameworkFileIDs, rezFileIDs, resourceFileRefs;
  194. mutable File infoPlistFile, iconFile;
  195. const bool iOS;
  196. static String sanitisePath (const String& path)
  197. {
  198. if (path.startsWithChar ('~'))
  199. return "$(HOME)" + path.substring (1);
  200. return path;
  201. }
  202. File getProjectBundle() const { return getTargetFolder().getChildFile (project.getProjectFilenameRoot()).withFileExtension (".xcodeproj"); }
  203. //==============================================================================
  204. void createObjects() const
  205. {
  206. addFrameworks();
  207. addMainBuildProduct();
  208. if (xcodeCreatePList)
  209. {
  210. RelativePath plistPath (infoPlistFile, getTargetFolder(), RelativePath::buildTargetFolder);
  211. addFileReference (plistPath.toUnixStyle());
  212. resourceFileRefs.add (createFileRefID (plistPath));
  213. }
  214. if (iconFile.exists())
  215. {
  216. RelativePath iconPath (iconFile, getTargetFolder(), RelativePath::buildTargetFolder);
  217. addFileReference (iconPath.toUnixStyle());
  218. resourceIDs.add (addBuildFile (iconPath, false, false));
  219. resourceFileRefs.add (createFileRefID (iconPath));
  220. }
  221. {
  222. StringArray topLevelGroupIDs;
  223. for (int i = 0; i < groups.size(); ++i)
  224. if (groups.getReference(i).getNumChildren() > 0)
  225. topLevelGroupIDs.add (addProjectItem (groups.getReference(i)));
  226. { // Add 'resources' group
  227. String resourcesGroupID (createID ("__resources"));
  228. addGroup (resourcesGroupID, "Resources", resourceFileRefs);
  229. topLevelGroupIDs.add (resourcesGroupID);
  230. }
  231. { // Add 'frameworks' group
  232. String frameworksGroupID (createID ("__frameworks"));
  233. addGroup (frameworksGroupID, "Frameworks", frameworkFileIDs);
  234. topLevelGroupIDs.add (frameworksGroupID);
  235. }
  236. { // Add 'products' group
  237. String productsGroupID (createID ("__products"));
  238. StringArray products;
  239. products.add (createID ("__productFileID"));
  240. addGroup (productsGroupID, "Products", products);
  241. topLevelGroupIDs.add (productsGroupID);
  242. }
  243. addGroup (createID ("__mainsourcegroup"), "Source", topLevelGroupIDs);
  244. }
  245. for (ConstConfigIterator config (*this); config.next();)
  246. {
  247. addProjectConfig (config->getName(), getProjectSettings (*config));
  248. addTargetConfig (config->getName(), getTargetSettings (dynamic_cast <const XcodeBuildConfiguration&> (*config)));
  249. }
  250. addConfigList (projectConfigs, createID ("__projList"));
  251. addConfigList (targetConfigs, createID ("__configList"));
  252. if (! isStaticLibrary())
  253. addBuildPhase ("PBXResourcesBuildPhase", resourceIDs);
  254. if (rezFileIDs.size() > 0)
  255. addBuildPhase ("PBXRezBuildPhase", rezFileIDs);
  256. addBuildPhase ("PBXSourcesBuildPhase", sourceIDs);
  257. if (! isStaticLibrary())
  258. addBuildPhase ("PBXFrameworksBuildPhase", frameworkIDs);
  259. addShellScriptPhase();
  260. addTargetObject();
  261. addProjectObject();
  262. }
  263. static Image fixMacIconImageSize (Image& image)
  264. {
  265. const int w = image.getWidth();
  266. const int h = image.getHeight();
  267. if (w != h || (w != 16 && w != 32 && w != 48 && w != 64))
  268. {
  269. const int newSize = w >= 128 ? 128 : (w >= 64 ? 64 : (w >= 32 ? 32 : 16));
  270. Image newIm (Image::ARGB, newSize, newSize, true, SoftwareImageType());
  271. Graphics g (newIm);
  272. g.drawImageWithin (image, 0, 0, newSize, newSize,
  273. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, false);
  274. return newIm;
  275. }
  276. return image;
  277. }
  278. void writeIcnsFile (const Array<Image>& images, OutputStream& out) const
  279. {
  280. MemoryOutputStream data;
  281. for (int i = 0; i < images.size(); ++i)
  282. {
  283. Image image (fixMacIconImageSize (images.getReference (i)));
  284. const int w = image.getWidth();
  285. const int h = image.getHeight();
  286. const char* type = nullptr;
  287. const char* maskType = nullptr;
  288. if (w == h)
  289. {
  290. if (w == 16) { type = "is32"; maskType = "s8mk"; }
  291. if (w == 32) { type = "il32"; maskType = "l8mk"; }
  292. if (w == 48) { type = "ih32"; maskType = "h8mk"; }
  293. if (w == 128) { type = "it32"; maskType = "t8mk"; }
  294. }
  295. if (type != nullptr)
  296. {
  297. data.write (type, 4);
  298. data.writeIntBigEndian (8 + 4 * w * h);
  299. const Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
  300. int y;
  301. for (y = 0; y < h; ++y)
  302. {
  303. for (int x = 0; x < w; ++x)
  304. {
  305. const Colour pixel (bitmap.getPixelColour (x, y));
  306. data.writeByte ((char) pixel.getAlpha());
  307. data.writeByte ((char) pixel.getRed());
  308. data.writeByte ((char) pixel.getGreen());
  309. data.writeByte ((char) pixel.getBlue());
  310. }
  311. }
  312. data.write (maskType, 4);
  313. data.writeIntBigEndian (8 + w * h);
  314. for (y = 0; y < h; ++y)
  315. {
  316. for (int x = 0; x < w; ++x)
  317. {
  318. const Colour pixel (bitmap.getPixelColour (x, y));
  319. data.writeByte ((char) pixel.getAlpha());
  320. }
  321. }
  322. }
  323. }
  324. jassert (data.getDataSize() > 0); // no suitable sized images?
  325. out.write ("icns", 4);
  326. out.writeIntBigEndian (data.getDataSize() + 8);
  327. out << data;
  328. }
  329. void createIconFile() const
  330. {
  331. Array<Image> images;
  332. Image bigIcon (getBigIcon());
  333. if (bigIcon.isValid())
  334. images.add (bigIcon);
  335. Image smallIcon (getSmallIcon());
  336. if (smallIcon.isValid())
  337. images.add (smallIcon);
  338. if (images.size() > 0)
  339. {
  340. MemoryOutputStream mo;
  341. writeIcnsFile (images, mo);
  342. iconFile = getTargetFolder().getChildFile ("Icon.icns");
  343. overwriteFileIfDifferentOrThrow (iconFile, mo);
  344. }
  345. }
  346. void writeInfoPlistFile() const
  347. {
  348. if (! xcodeCreatePList)
  349. return;
  350. ScopedPointer<XmlElement> plist (XmlDocument::parse (getPListToMergeString()));
  351. if (plist == nullptr || ! plist->hasTagName ("plist"))
  352. plist = new XmlElement ("plist");
  353. XmlElement* dict = plist->getChildByName ("dict");
  354. if (dict == nullptr)
  355. dict = plist->createNewChildElement ("dict");
  356. if (iOS)
  357. addPlistDictionaryKeyBool (dict, "LSRequiresIPhoneOS", true);
  358. addPlistDictionaryKey (dict, "CFBundleExecutable", "${EXECUTABLE_NAME}");
  359. addPlistDictionaryKey (dict, "CFBundleIconFile", iconFile.exists() ? iconFile.getFileName() : String::empty);
  360. addPlistDictionaryKey (dict, "CFBundleIdentifier", project.getBundleIdentifier().toString());
  361. addPlistDictionaryKey (dict, "CFBundleName", projectName);
  362. addPlistDictionaryKey (dict, "CFBundlePackageType", xcodePackageType);
  363. addPlistDictionaryKey (dict, "CFBundleSignature", xcodeBundleSignature);
  364. addPlistDictionaryKey (dict, "CFBundleShortVersionString", project.getVersionString());
  365. addPlistDictionaryKey (dict, "CFBundleVersion", project.getVersionString());
  366. addPlistDictionaryKey (dict, "NSHumanReadableCopyright", project.getCompanyName().toString());
  367. StringArray documentExtensions;
  368. documentExtensions.addTokens (replacePreprocessorDefs (getAllPreprocessorDefs(), settings ["documentExtensions"]),
  369. ",", String::empty);
  370. documentExtensions.trim();
  371. documentExtensions.removeEmptyStrings (true);
  372. if (documentExtensions.size() > 0)
  373. {
  374. dict->createNewChildElement ("key")->addTextElement ("CFBundleDocumentTypes");
  375. XmlElement* dict2 = dict->createNewChildElement ("array")->createNewChildElement ("dict");
  376. for (int i = 0; i < documentExtensions.size(); ++i)
  377. {
  378. String ex (documentExtensions[i]);
  379. if (ex.startsWithChar ('.'))
  380. ex = ex.substring (1);
  381. dict2->createNewChildElement ("key")->addTextElement ("CFBundleTypeExtensions");
  382. dict2->createNewChildElement ("array")->createNewChildElement ("string")->addTextElement (ex);
  383. addPlistDictionaryKey (dict2, "CFBundleTypeName", ex);
  384. addPlistDictionaryKey (dict2, "CFBundleTypeRole", "Editor");
  385. addPlistDictionaryKey (dict2, "NSPersistentStoreTypeKey", "XML");
  386. }
  387. }
  388. if (settings ["UIFileSharingEnabled"])
  389. addPlistDictionaryKeyBool (dict, "UIFileSharingEnabled", true);
  390. if (settings ["UIStatusBarHidden"])
  391. addPlistDictionaryKeyBool (dict, "UIStatusBarHidden", true);
  392. for (int i = 0; i < xcodeExtraPListEntries.size(); ++i)
  393. dict->addChildElement (new XmlElement (xcodeExtraPListEntries.getReference(i)));
  394. MemoryOutputStream mo;
  395. plist->writeToStream (mo, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  396. overwriteFileIfDifferentOrThrow (infoPlistFile, mo);
  397. }
  398. StringArray getHeaderSearchPaths (const BuildConfiguration& config) const
  399. {
  400. StringArray searchPaths (extraSearchPaths);
  401. searchPaths.addArray (config.getHeaderSearchPaths());
  402. searchPaths.removeDuplicates (false);
  403. return searchPaths;
  404. }
  405. static void getLinkerFlagsForStaticLibrary (const RelativePath& library, StringArray& flags, StringArray& librarySearchPaths)
  406. {
  407. jassert (library.getFileNameWithoutExtension().substring (0, 3) == "lib");
  408. flags.add ("-l" + library.getFileNameWithoutExtension().substring (3));
  409. String searchPath (library.toUnixStyle().upToLastOccurrenceOf ("/", false, false));
  410. if (! library.isAbsolute())
  411. searchPath = "$(SRCROOT)/" + searchPath;
  412. librarySearchPaths.add (sanitisePath (searchPath));
  413. }
  414. void getLinkerFlags (const BuildConfiguration& config, StringArray& flags, StringArray& librarySearchPaths) const
  415. {
  416. if (xcodeIsBundle)
  417. flags.add ("-bundle");
  418. const Array<RelativePath>& extraLibs = config.isDebug() ? xcodeExtraLibrariesDebug
  419. : xcodeExtraLibrariesRelease;
  420. for (int i = 0; i < extraLibs.size(); ++i)
  421. getLinkerFlagsForStaticLibrary (extraLibs.getReference(i), flags, librarySearchPaths);
  422. flags.add (replacePreprocessorTokens (config, getExtraLinkerFlagsString()));
  423. flags.removeEmptyStrings (true);
  424. }
  425. StringArray getProjectSettings (const BuildConfiguration& config) const
  426. {
  427. StringArray s;
  428. s.add ("ALWAYS_SEARCH_USER_PATHS = NO");
  429. s.add ("GCC_C_LANGUAGE_STANDARD = c99");
  430. s.add ("GCC_WARN_ABOUT_RETURN_TYPE = YES");
  431. s.add ("GCC_WARN_CHECK_SWITCH_STATEMENTS = YES");
  432. s.add ("GCC_WARN_UNUSED_VARIABLE = YES");
  433. s.add ("GCC_WARN_MISSING_PARENTHESES = YES");
  434. s.add ("GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES");
  435. s.add ("GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES");
  436. s.add ("WARNING_CFLAGS = -Wreorder");
  437. s.add ("GCC_MODEL_TUNING = G5");
  438. if (projectType.isLibrary())
  439. {
  440. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = NO");
  441. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = NO");
  442. }
  443. else
  444. {
  445. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = YES");
  446. }
  447. if (iOS)
  448. {
  449. s.add ("\"CODE_SIGN_IDENTITY[sdk=iphoneos*]\" = \"iPhone Developer\"");
  450. s.add ("SDKROOT = iphoneos");
  451. s.add ("TARGETED_DEVICE_FAMILY = \"1,2\"");
  452. }
  453. s.add ("ZERO_LINK = NO");
  454. if (xcodeCanUseDwarf)
  455. s.add ("DEBUG_INFORMATION_FORMAT = \"dwarf\"");
  456. s.add ("PRODUCT_NAME = \"" + config.getTargetBinaryNameString() + "\"");
  457. return s;
  458. }
  459. StringArray getTargetSettings (const XcodeBuildConfiguration& config) const
  460. {
  461. StringArray s;
  462. {
  463. String srcRoot = rebaseFromProjectFolderToBuildTarget (RelativePath (".", RelativePath::projectFolder)).toUnixStyle();
  464. if (srcRoot.endsWith ("/."))
  465. srcRoot = srcRoot.dropLastCharacters (2);
  466. s.add ("SRCROOT = " + srcRoot.quoted());
  467. }
  468. const String arch (config.getMacArchitecture());
  469. if (arch == osxArch_Native) s.add ("ARCHS = \"$(ARCHS_NATIVE)\"");
  470. else if (arch == osxArch_32BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_BIT)\"");
  471. else if (arch == osxArch_64BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_64_BIT)\"");
  472. else if (arch == osxArch_64Bit) s.add ("ARCHS = \"$(ARCHS_STANDARD_64_BIT)\"");
  473. s.add ("HEADER_SEARCH_PATHS = \"" + replacePreprocessorTokens (config, getHeaderSearchPaths (config).joinIntoString (" ")) + " $(inherited)\"");
  474. s.add ("GCC_OPTIMIZATION_LEVEL = " + config.getGCCOptimisationFlag());
  475. s.add ("INFOPLIST_FILE = " + infoPlistFile.getFileName());
  476. const String extraFlags (replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim());
  477. if (extraFlags.isNotEmpty())
  478. s.add ("OTHER_CPLUSPLUSFLAGS = \"" + extraFlags + "\"");
  479. if (xcodeProductInstallPath.isNotEmpty())
  480. s.add ("INSTALL_PATH = \"" + xcodeProductInstallPath + "\"");
  481. if (xcodeIsBundle)
  482. {
  483. s.add ("LIBRARY_STYLE = Bundle");
  484. s.add ("WRAPPER_EXTENSION = " + xcodeBundleExtension.substring (1));
  485. s.add ("GENERATE_PKGINFO_FILE = YES");
  486. }
  487. if (xcodeOtherRezFlags.isNotEmpty())
  488. s.add ("OTHER_REZFLAGS = \"" + xcodeOtherRezFlags + "\"");
  489. if (projectType.isLibrary())
  490. {
  491. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  492. {
  493. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  494. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  495. s.add ("DSTROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  496. s.add ("SYMROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  497. }
  498. s.add ("CONFIGURATION_BUILD_DIR = \"$(BUILD_DIR)\"");
  499. s.add ("DEPLOYMENT_LOCATION = YES");
  500. }
  501. String gccVersion ("com.apple.compilers.llvm.clang.1_0");
  502. if (! iOS)
  503. {
  504. const String sdk (config.getMacSDKVersion());
  505. const String sdkCompat (config.getMacCompatibilityVersion());
  506. if (sdk == osxVersion10_5) s.add ("SDKROOT = macosx10.5");
  507. else if (sdk == osxVersion10_6) s.add ("SDKROOT = macosx10.6");
  508. else if (sdk == osxVersion10_7) s.add ("SDKROOT = macosx10.7");
  509. if (sdkCompat == osxVersion10_4) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.4");
  510. else if (sdkCompat == osxVersion10_5) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.5");
  511. else if (sdkCompat == osxVersion10_6) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.6");
  512. else if (sdkCompat == osxVersion10_7) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.7");
  513. s.add ("MACOSX_DEPLOYMENT_TARGET_ppc = 10.4");
  514. s.add ("SDKROOT_ppc = macosx10.5");
  515. if (xcodeExcludedFiles64Bit.isNotEmpty())
  516. {
  517. s.add ("EXCLUDED_SOURCE_FILE_NAMES = \"$(EXCLUDED_SOURCE_FILE_NAMES_$(CURRENT_ARCH))\"");
  518. s.add ("EXCLUDED_SOURCE_FILE_NAMES_x86_64 = " + xcodeExcludedFiles64Bit);
  519. }
  520. }
  521. s.add ("GCC_VERSION = " + gccVersion);
  522. s.add ("CLANG_CXX_LANGUAGE_STANDARD = \"c++0x\"");
  523. {
  524. StringArray linkerFlags, librarySearchPaths;
  525. getLinkerFlags (config, linkerFlags, librarySearchPaths);
  526. if (linkerFlags.size() > 0)
  527. s.add ("OTHER_LDFLAGS = \"" + linkerFlags.joinIntoString (" ") + "\"");
  528. librarySearchPaths.addArray (config.getLibrarySearchPaths());
  529. librarySearchPaths.removeDuplicates (false);
  530. if (librarySearchPaths.size() > 0)
  531. {
  532. String libPaths ("LIBRARY_SEARCH_PATHS = (\"$(inherited)\"");
  533. for (int i = 0; i < librarySearchPaths.size(); ++i)
  534. libPaths += ", \"\\\"" + librarySearchPaths[i] + "\\\"\"";
  535. s.add (libPaths + ")");
  536. }
  537. }
  538. StringPairArray defines;
  539. if (config.isDebug())
  540. {
  541. defines.set ("_DEBUG", "1");
  542. defines.set ("DEBUG", "1");
  543. s.add ("ONLY_ACTIVE_ARCH = YES");
  544. s.add ("COPY_PHASE_STRIP = NO");
  545. s.add ("GCC_DYNAMIC_NO_PIC = NO");
  546. }
  547. else
  548. {
  549. defines.set ("_NDEBUG", "1");
  550. defines.set ("NDEBUG", "1");
  551. s.add ("GCC_GENERATE_DEBUGGING_SYMBOLS = NO");
  552. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = YES");
  553. }
  554. {
  555. const String objCSuffix (getObjCSuffixString().trim());
  556. if (objCSuffix.isNotEmpty())
  557. defines.set ("JUCE_ObjCExtraSuffix", replacePreprocessorTokens (config, objCSuffix));
  558. }
  559. {
  560. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  561. StringArray defsList;
  562. for (int i = 0; i < defines.size(); ++i)
  563. {
  564. String def (defines.getAllKeys()[i]);
  565. const String value (defines.getAllValues()[i]);
  566. if (value.isNotEmpty())
  567. def << "=" << value;
  568. defsList.add (def.quoted());
  569. }
  570. s.add ("GCC_PREPROCESSOR_DEFINITIONS = (" + indentList (defsList, ",") + ")");
  571. }
  572. s.addTokens (config.getCustomXcodeFlags(), ",", "\"'");
  573. s.trim();
  574. s.removeEmptyStrings();
  575. s.removeDuplicates (false);
  576. return s;
  577. }
  578. void addFrameworks() const
  579. {
  580. if (! isStaticLibrary())
  581. {
  582. StringArray s (xcodeFrameworks);
  583. s.addTokens (getExtraFrameworksString(), ",;", "\"'");
  584. s.trim();
  585. s.removeDuplicates (true);
  586. s.sort (true);
  587. for (int i = 0; i < s.size(); ++i)
  588. addFramework (s[i]);
  589. }
  590. }
  591. //==============================================================================
  592. void writeProjectFile (OutputStream& output) const
  593. {
  594. output << "// !$*UTF8*$!\n{\n"
  595. "\tarchiveVersion = 1;\n"
  596. "\tclasses = {\n\t};\n"
  597. "\tobjectVersion = 46;\n"
  598. "\tobjects = {\n\n";
  599. Array <ValueTree*> objects;
  600. objects.addArray (pbxBuildFiles);
  601. objects.addArray (pbxFileReferences);
  602. objects.addArray (pbxGroups);
  603. objects.addArray (targetConfigs);
  604. objects.addArray (projectConfigs);
  605. objects.addArray (misc);
  606. for (int i = 0; i < objects.size(); ++i)
  607. {
  608. ValueTree& o = *objects.getUnchecked(i);
  609. output << "\t\t" << o.getType().toString() << " = { ";
  610. for (int j = 0; j < o.getNumProperties(); ++j)
  611. {
  612. const Identifier propertyName (o.getPropertyName(j));
  613. String val (o.getProperty (propertyName).toString());
  614. if (val.isEmpty() || (val.containsAnyOf (" \t;<>()=,&+-_\r\n")
  615. && ! (val.trimStart().startsWithChar ('(')
  616. || val.trimStart().startsWithChar ('{'))))
  617. val = val.quoted();
  618. output << propertyName.toString() << " = " << val << "; ";
  619. }
  620. output << "};\n";
  621. }
  622. output << "\t};\n\trootObject = " << createID ("__root") << ";\n}\n";
  623. }
  624. static void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  625. {
  626. forEachXmlChildElementWithTagName (*xml, e, "key")
  627. {
  628. if (e->getAllSubText().trim().equalsIgnoreCase (key))
  629. {
  630. if (e->getNextElement() != nullptr && e->getNextElement()->hasTagName ("key"))
  631. {
  632. // try to fix broken plist format..
  633. xml->removeChildElement (e, true);
  634. break;
  635. }
  636. else
  637. {
  638. return; // (value already exists)
  639. }
  640. }
  641. }
  642. xml->createNewChildElement ("key") ->addTextElement (key);
  643. xml->createNewChildElement ("string")->addTextElement (value);
  644. }
  645. static void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  646. {
  647. xml->createNewChildElement ("key")->addTextElement (key);
  648. xml->createNewChildElement (value ? "true" : "false");
  649. }
  650. String addBuildFile (const String& path, const String& fileRefID, bool addToSourceBuildPhase, bool inhibitWarnings) const
  651. {
  652. String fileID (createID (path + "buildref"));
  653. if (addToSourceBuildPhase)
  654. sourceIDs.add (fileID);
  655. ValueTree* v = new ValueTree (fileID);
  656. v->setProperty ("isa", "PBXBuildFile", 0);
  657. v->setProperty ("fileRef", fileRefID, 0);
  658. if (inhibitWarnings)
  659. v->setProperty ("settings", "{COMPILER_FLAGS = \"-w\"; }", 0);
  660. pbxBuildFiles.add (v);
  661. return fileID;
  662. }
  663. String addBuildFile (const RelativePath& path, bool addToSourceBuildPhase, bool inhibitWarnings) const
  664. {
  665. return addBuildFile (path.toUnixStyle(), createFileRefID (path), addToSourceBuildPhase, inhibitWarnings);
  666. }
  667. String addFileReference (String pathString) const
  668. {
  669. String sourceTree ("SOURCE_ROOT");
  670. RelativePath path (pathString, RelativePath::unknown);
  671. if (pathString.startsWith ("${"))
  672. {
  673. sourceTree = pathString.substring (2).upToFirstOccurrenceOf ("}", false, false);
  674. pathString = pathString.fromFirstOccurrenceOf ("}/", false, false);
  675. }
  676. else if (path.isAbsolute())
  677. {
  678. sourceTree = "<absolute>";
  679. }
  680. const String fileRefID (createFileRefID (pathString));
  681. ScopedPointer<ValueTree> v (new ValueTree (fileRefID));
  682. v->setProperty ("isa", "PBXFileReference", 0);
  683. v->setProperty ("lastKnownFileType", getFileType (path), 0);
  684. v->setProperty (Ids::name, pathString.fromLastOccurrenceOf ("/", false, false), 0);
  685. v->setProperty ("path", sanitisePath (pathString), 0);
  686. v->setProperty ("sourceTree", sourceTree, 0);
  687. const int existing = pbxFileReferences.indexOfSorted (*this, v);
  688. if (existing >= 0)
  689. {
  690. // If this fails, there's either a string hash collision, or the same file is being added twice (incorrectly)
  691. jassert (pbxFileReferences.getUnchecked (existing)->isEquivalentTo (*v));
  692. }
  693. else
  694. {
  695. pbxFileReferences.addSorted (*this, v.release());
  696. }
  697. return fileRefID;
  698. }
  699. public:
  700. static int compareElements (const ValueTree* first, const ValueTree* second)
  701. {
  702. return first->getType().getCharPointer().compare (second->getType().getCharPointer());
  703. }
  704. private:
  705. static String getFileType (const RelativePath& file)
  706. {
  707. if (file.hasFileExtension ("cpp;cc;cxx")) return "sourcecode.cpp.cpp";
  708. else if (file.hasFileExtension (".mm")) return "sourcecode.cpp.objcpp";
  709. else if (file.hasFileExtension (".m")) return "sourcecode.c.objc";
  710. else if (file.hasFileExtension (headerFileExtensions)) return "sourcecode.c.h";
  711. else if (file.hasFileExtension (".framework")) return "wrapper.framework";
  712. else if (file.hasFileExtension (".jpeg;.jpg")) return "image.jpeg";
  713. else if (file.hasFileExtension ("png;gif")) return "image" + file.getFileExtension();
  714. else if (file.hasFileExtension ("html;htm")) return "text.html";
  715. else if (file.hasFileExtension ("xml;zip;wav")) return "file" + file.getFileExtension();
  716. else if (file.hasFileExtension ("txt;rtf")) return "text" + file.getFileExtension();
  717. else if (file.hasFileExtension ("plist")) return "text.plist.xml";
  718. else if (file.hasFileExtension ("app")) return "wrapper.application";
  719. else if (file.hasFileExtension ("component;vst;plugin")) return "wrapper.cfbundle";
  720. else if (file.hasFileExtension ("xcodeproj")) return "wrapper.pb-project";
  721. else if (file.hasFileExtension ("a")) return "archive.ar";
  722. return "file" + file.getFileExtension();
  723. }
  724. String addFile (const RelativePath& path, bool shouldBeCompiled, bool inhibitWarnings) const
  725. {
  726. const String pathAsString (path.toUnixStyle());
  727. const String refID (addFileReference (path.toUnixStyle()));
  728. if (shouldBeCompiled)
  729. {
  730. if (path.hasFileExtension (".r"))
  731. rezFileIDs.add (addBuildFile (pathAsString, refID, false, inhibitWarnings));
  732. else
  733. addBuildFile (pathAsString, refID, true, inhibitWarnings);
  734. }
  735. return refID;
  736. }
  737. String addProjectItem (const Project::Item& projectItem) const
  738. {
  739. if (projectItem.isGroup())
  740. {
  741. StringArray childIDs;
  742. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  743. {
  744. const String childID (addProjectItem (projectItem.getChild(i)));
  745. if (childID.isNotEmpty())
  746. childIDs.add (childID);
  747. }
  748. return addGroup (projectItem, childIDs);
  749. }
  750. else
  751. {
  752. if (projectItem.shouldBeAddedToTargetProject())
  753. {
  754. String itemPath (projectItem.getFilePath());
  755. bool inhibitWarnings = projectItem.shouldInhibitWarnings();
  756. if (itemPath.startsWith ("${"))
  757. {
  758. const RelativePath path (itemPath, RelativePath::unknown);
  759. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  760. }
  761. else
  762. {
  763. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  764. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  765. }
  766. }
  767. }
  768. return String::empty;
  769. }
  770. void addFramework (const String& frameworkName) const
  771. {
  772. const String path ("System/Library/Frameworks/" + frameworkName + ".framework");
  773. const String fileRefID (createFileRefID (path));
  774. addFileReference ("${SDKROOT}/" + path);
  775. frameworkIDs.add (addBuildFile (path, fileRefID, false, false));
  776. frameworkFileIDs.add (fileRefID);
  777. }
  778. void addGroup (const String& groupID, const String& groupName, const StringArray& childIDs) const
  779. {
  780. ValueTree* v = new ValueTree (groupID);
  781. v->setProperty ("isa", "PBXGroup", 0);
  782. v->setProperty ("children", "(" + indentList (childIDs, ",") + " )", 0);
  783. v->setProperty (Ids::name, groupName, 0);
  784. v->setProperty ("sourceTree", "<group>", 0);
  785. pbxGroups.add (v);
  786. }
  787. String addGroup (const Project::Item& item, StringArray& childIDs) const
  788. {
  789. const String groupName (item.getName());
  790. const String groupID (getIDForGroup (item));
  791. addGroup (groupID, groupName, childIDs);
  792. return groupID;
  793. }
  794. void addMainBuildProduct() const
  795. {
  796. jassert (xcodeFileType.isNotEmpty());
  797. jassert (xcodeBundleExtension.isEmpty() || xcodeBundleExtension.startsWithChar('.'));
  798. String productName (getConfiguration(0)->getTargetBinaryName().toString());
  799. if (xcodeFileType == "archive.ar")
  800. productName = getLibbedFilename (productName);
  801. else
  802. productName += xcodeBundleExtension;
  803. addBuildProduct (xcodeFileType, productName);
  804. }
  805. void addBuildProduct (const String& fileType, const String& binaryName) const
  806. {
  807. ValueTree* v = new ValueTree (createID ("__productFileID"));
  808. v->setProperty ("isa", "PBXFileReference", 0);
  809. v->setProperty ("explicitFileType", fileType, 0);
  810. v->setProperty ("includeInIndex", (int) 0, 0);
  811. v->setProperty ("path", sanitisePath (binaryName), 0);
  812. v->setProperty ("sourceTree", "BUILT_PRODUCTS_DIR", 0);
  813. pbxFileReferences.add (v);
  814. }
  815. void addTargetConfig (const String& configName, const StringArray& buildSettings) const
  816. {
  817. ValueTree* v = new ValueTree (createID ("targetconfigid_" + configName));
  818. v->setProperty ("isa", "XCBuildConfiguration", 0);
  819. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  820. v->setProperty (Ids::name, configName, 0);
  821. targetConfigs.add (v);
  822. }
  823. void addProjectConfig (const String& configName, const StringArray& buildSettings) const
  824. {
  825. ValueTree* v = new ValueTree (createID ("projectconfigid_" + configName));
  826. v->setProperty ("isa", "XCBuildConfiguration", 0);
  827. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  828. v->setProperty (Ids::name, configName, 0);
  829. projectConfigs.add (v);
  830. }
  831. void addConfigList (const OwnedArray <ValueTree>& configsToUse, const String& listID) const
  832. {
  833. StringArray configIDs;
  834. for (int i = 0; i < configsToUse.size(); ++i)
  835. configIDs.add (configsToUse[i]->getType().toString());
  836. ValueTree* v = new ValueTree (listID);
  837. v->setProperty ("isa", "XCConfigurationList", 0);
  838. v->setProperty ("buildConfigurations", "(" + indentList (configIDs, ",") + " )", 0);
  839. v->setProperty ("defaultConfigurationIsVisible", (int) 0, 0);
  840. if (configsToUse[0] != nullptr)
  841. v->setProperty ("defaultConfigurationName", configsToUse[0]->getProperty (Ids::name), 0);
  842. misc.add (v);
  843. }
  844. ValueTree* addBuildPhase (const String& phaseType, const StringArray& fileIds) const
  845. {
  846. String phaseId (createID (phaseType + "resbuildphase"));
  847. buildPhaseIDs.add (phaseId);
  848. ValueTree* v = new ValueTree (phaseId);
  849. v->setProperty ("isa", phaseType, 0);
  850. v->setProperty ("buildActionMask", "2147483647", 0);
  851. v->setProperty ("files", "(" + indentList (fileIds, ",") + " )", 0);
  852. v->setProperty ("runOnlyForDeploymentPostprocessing", (int) 0, 0);
  853. misc.add (v);
  854. return v;
  855. }
  856. void addTargetObject() const
  857. {
  858. ValueTree* const v = new ValueTree (createID ("__target"));
  859. v->setProperty ("isa", "PBXNativeTarget", 0);
  860. v->setProperty ("buildConfigurationList", createID ("__configList"), 0);
  861. v->setProperty ("buildPhases", "(" + indentList (buildPhaseIDs, ",") + " )", 0);
  862. v->setProperty ("buildRules", "( )", 0);
  863. v->setProperty ("dependencies", "( )", 0);
  864. v->setProperty (Ids::name, projectName, 0);
  865. v->setProperty ("productName", projectName, 0);
  866. v->setProperty ("productReference", createID ("__productFileID"), 0);
  867. if (xcodeProductInstallPath.isNotEmpty())
  868. v->setProperty ("productInstallPath", xcodeProductInstallPath, 0);
  869. jassert (xcodeProductType.isNotEmpty());
  870. v->setProperty ("productType", xcodeProductType, 0);
  871. misc.add (v);
  872. }
  873. void addProjectObject() const
  874. {
  875. ValueTree* const v = new ValueTree (createID ("__root"));
  876. v->setProperty ("isa", "PBXProject", 0);
  877. v->setProperty ("buildConfigurationList", createID ("__projList"), 0);
  878. v->setProperty ("compatibilityVersion", "Xcode 3.2", 0);
  879. v->setProperty ("hasScannedForEncodings", (int) 0, 0);
  880. v->setProperty ("mainGroup", createID ("__mainsourcegroup"), 0);
  881. v->setProperty ("projectDirPath", "\"\"", 0);
  882. v->setProperty ("projectRoot", "\"\"", 0);
  883. v->setProperty ("targets", "( " + createID ("__target") + " )", 0);
  884. misc.add (v);
  885. }
  886. void addShellScriptPhase() const
  887. {
  888. if (xcodeShellScript.isNotEmpty())
  889. {
  890. ValueTree* const v = addBuildPhase ("PBXShellScriptBuildPhase", StringArray());
  891. v->setProperty (Ids::name, xcodeShellScriptTitle, 0);
  892. v->setProperty ("shellPath", "/bin/sh", 0);
  893. v->setProperty ("shellScript", xcodeShellScript.replace ("\\", "\\\\")
  894. .replace ("\"", "\\\"")
  895. .replace ("\r\n", "\\n")
  896. .replace ("\n", "\\n"), 0);
  897. }
  898. }
  899. //==============================================================================
  900. static String indentList (const StringArray& list, const String& separator)
  901. {
  902. if (list.size() == 0)
  903. return " ";
  904. return "\n\t\t\t\t" + list.joinIntoString (separator + "\n\t\t\t\t")
  905. + (separator == ";" ? separator : String::empty);
  906. }
  907. String createID (String rootString) const
  908. {
  909. if (rootString.startsWith ("${"))
  910. rootString = rootString.fromFirstOccurrenceOf ("}/", false, false);
  911. rootString += project.getProjectUID();
  912. return MD5 (rootString.toUTF8()).toHexString().substring (0, 24).toUpperCase();
  913. }
  914. String createFileRefID (const RelativePath& path) const
  915. {
  916. return createFileRefID (path.toUnixStyle());
  917. }
  918. String createFileRefID (const String& path) const
  919. {
  920. return createID ("__fileref_" + path);
  921. }
  922. String getIDForGroup (const Project::Item& item) const
  923. {
  924. return createID (item.getID());
  925. }
  926. bool shouldFileBeCompiledByDefault (const RelativePath& file) const
  927. {
  928. return file.hasFileExtension (sourceFileExtensions);
  929. }
  930. };
  931. #endif // __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__