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.

1141 lines
46KB

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