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.

1142 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. addPlistDictionaryKey (dict, "NSHumanReadableCopyright", project.getCompanyName().toString());
  351. StringArray documentExtensions;
  352. documentExtensions.addTokens (replacePreprocessorDefs (getAllPreprocessorDefs(), getSetting ("documentExtensions").toString()),
  353. ",", String::empty);
  354. documentExtensions.trim();
  355. documentExtensions.removeEmptyStrings (true);
  356. if (documentExtensions.size() > 0)
  357. {
  358. dict->createNewChildElement ("key")->addTextElement ("CFBundleDocumentTypes");
  359. XmlElement* dict2 = dict->createNewChildElement ("array")->createNewChildElement ("dict");
  360. for (int i = 0; i < documentExtensions.size(); ++i)
  361. {
  362. String ex (documentExtensions[i]);
  363. if (ex.startsWithChar ('.'))
  364. ex = ex.substring (1);
  365. dict2->createNewChildElement ("key")->addTextElement ("CFBundleTypeExtensions");
  366. dict2->createNewChildElement ("array")->createNewChildElement ("string")->addTextElement (ex);
  367. addPlistDictionaryKey (dict2, "CFBundleTypeName", ex);
  368. addPlistDictionaryKey (dict2, "CFBundleTypeRole", "Editor");
  369. addPlistDictionaryKey (dict2, "NSPersistentStoreTypeKey", "XML");
  370. }
  371. }
  372. if (getSetting ("UIFileSharingEnabled").getValue())
  373. addPlistDictionaryKeyBool (dict, "UIFileSharingEnabled", true);
  374. if (getSetting ("UIStatusBarHidden").getValue())
  375. addPlistDictionaryKeyBool (dict, "UIStatusBarHidden", true);
  376. for (int i = 0; i < xcodeExtraPListEntries.size(); ++i)
  377. dict->addChildElement (new XmlElement (xcodeExtraPListEntries.getReference(i)));
  378. MemoryOutputStream mo;
  379. plist->writeToStream (mo, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
  380. overwriteFileIfDifferentOrThrow (infoPlistFile, mo);
  381. }
  382. StringArray getHeaderSearchPaths (const BuildConfiguration& config)
  383. {
  384. StringArray searchPaths (extraSearchPaths);
  385. searchPaths.addArray (config.getHeaderSearchPaths());
  386. searchPaths.removeDuplicates (false);
  387. return searchPaths;
  388. }
  389. static void getLinkerFlagsForStaticLibrary (const RelativePath& library, StringArray& flags, StringArray& librarySearchPaths)
  390. {
  391. jassert (library.getFileNameWithoutExtension().substring (0, 3) == "lib");
  392. flags.add ("-l" + library.getFileNameWithoutExtension().substring (3));
  393. String searchPath (library.toUnixStyle().upToLastOccurrenceOf ("/", false, false));
  394. if (! library.isAbsolute())
  395. searchPath = "$(SRCROOT)/" + searchPath;
  396. librarySearchPaths.add (sanitisePath (searchPath));
  397. }
  398. void getLinkerFlags (const BuildConfiguration& config, StringArray& flags, StringArray& librarySearchPaths)
  399. {
  400. if (xcodeIsBundle)
  401. flags.add ("-bundle");
  402. const Array<RelativePath>& extraLibs = config.isDebug().getValue() ? xcodeExtraLibrariesDebug
  403. : xcodeExtraLibrariesRelease;
  404. for (int i = 0; i < extraLibs.size(); ++i)
  405. getLinkerFlagsForStaticLibrary (extraLibs.getReference(i), flags, librarySearchPaths);
  406. flags.add (replacePreprocessorTokens (config, getExtraLinkerFlags().toString()));
  407. flags.removeEmptyStrings (true);
  408. }
  409. StringArray getProjectSettings (const BuildConfiguration& config)
  410. {
  411. StringArray s;
  412. s.add ("ALWAYS_SEARCH_USER_PATHS = NO");
  413. s.add ("GCC_C_LANGUAGE_STANDARD = c99");
  414. s.add ("GCC_WARN_ABOUT_RETURN_TYPE = YES");
  415. s.add ("GCC_WARN_CHECK_SWITCH_STATEMENTS = YES");
  416. s.add ("GCC_WARN_UNUSED_VARIABLE = YES");
  417. s.add ("GCC_WARN_MISSING_PARENTHESES = YES");
  418. s.add ("GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES");
  419. s.add ("GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES");
  420. s.add ("WARNING_CFLAGS = -Wreorder");
  421. s.add ("GCC_MODEL_TUNING = G5");
  422. if (projectType.isLibrary())
  423. {
  424. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = NO");
  425. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = NO");
  426. }
  427. else
  428. {
  429. s.add ("GCC_INLINES_ARE_PRIVATE_EXTERN = YES");
  430. }
  431. if (iOS)
  432. {
  433. s.add ("\"CODE_SIGN_IDENTITY[sdk=iphoneos*]\" = \"iPhone Developer\"");
  434. s.add ("SDKROOT = iphoneos");
  435. s.add ("TARGETED_DEVICE_FAMILY = \"1,2\"");
  436. }
  437. s.add ("ZERO_LINK = NO");
  438. if (xcodeCanUseDwarf)
  439. s.add ("DEBUG_INFORMATION_FORMAT = \"dwarf\"");
  440. s.add ("PRODUCT_NAME = \"" + config.getTargetBinaryName().toString() + "\"");
  441. return s;
  442. }
  443. StringArray getTargetSettings (const XcodeBuildConfiguration& config)
  444. {
  445. StringArray s;
  446. {
  447. String srcRoot = rebaseFromProjectFolderToBuildTarget (RelativePath (".", RelativePath::projectFolder)).toUnixStyle();
  448. if (srcRoot.endsWith ("/."))
  449. srcRoot = srcRoot.dropLastCharacters (2);
  450. s.add ("SRCROOT = " + srcRoot.quoted());
  451. }
  452. const String arch (config.getMacArchitecture().toString());
  453. if (arch == osxArch_Native) s.add ("ARCHS = \"$(ARCHS_NATIVE)\"");
  454. else if (arch == osxArch_32BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_BIT)\"");
  455. else if (arch == osxArch_64BitUniversal) s.add ("ARCHS = \"$(ARCHS_STANDARD_32_64_BIT)\"");
  456. else if (arch == osxArch_64Bit) s.add ("ARCHS = \"$(ARCHS_STANDARD_64_BIT)\"");
  457. s.add ("HEADER_SEARCH_PATHS = \"" + replacePreprocessorTokens (config, getHeaderSearchPaths (config).joinIntoString (" ")) + " $(inherited)\"");
  458. s.add ("GCC_OPTIMIZATION_LEVEL = " + config.getGCCOptimisationFlag());
  459. s.add ("INFOPLIST_FILE = " + infoPlistFile.getFileName());
  460. const String extraFlags (replacePreprocessorTokens (config, getExtraCompilerFlags().toString()).trim());
  461. if (extraFlags.isNotEmpty())
  462. s.add ("OTHER_CPLUSPLUSFLAGS = \"" + extraFlags + "\"");
  463. if (xcodeProductInstallPath.isNotEmpty())
  464. s.add ("INSTALL_PATH = \"" + xcodeProductInstallPath + "\"");
  465. if (xcodeIsBundle)
  466. {
  467. s.add ("LIBRARY_STYLE = Bundle");
  468. s.add ("WRAPPER_EXTENSION = " + xcodeBundleExtension.substring (1));
  469. s.add ("GENERATE_PKGINFO_FILE = YES");
  470. }
  471. if (xcodeOtherRezFlags.isNotEmpty())
  472. s.add ("OTHER_REZFLAGS = \"" + xcodeOtherRezFlags + "\"");
  473. if (projectType.isLibrary())
  474. {
  475. if (config.getTargetBinaryRelativePath().toString().isNotEmpty())
  476. {
  477. RelativePath binaryPath (config.getTargetBinaryRelativePath().toString(), RelativePath::projectFolder);
  478. binaryPath = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder);
  479. s.add ("DSTROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  480. s.add ("SYMROOT = " + sanitisePath (binaryPath.toUnixStyle()));
  481. }
  482. s.add ("CONFIGURATION_BUILD_DIR = \"$(BUILD_DIR)\"");
  483. s.add ("DEPLOYMENT_LOCATION = YES");
  484. }
  485. String gccVersion ("com.apple.compilers.llvm.clang.1_0");
  486. if (! iOS)
  487. {
  488. const String sdk (config.getMacSDKVersion().toString());
  489. const String sdkCompat (config.getMacCompatibilityVersion().toString());
  490. if (sdk == osxVersion10_4) { s.add ("SDKROOT = macosx10.4"); gccVersion = "4.0"; }
  491. else if (sdk == osxVersion10_5) s.add ("SDKROOT = macosx10.5");
  492. else if (sdk == osxVersion10_6) s.add ("SDKROOT = macosx10.6");
  493. else if (sdk == osxVersion10_7) s.add ("SDKROOT = macosx10.7");
  494. if (sdkCompat == osxVersion10_4) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.4");
  495. else if (sdkCompat == osxVersion10_5) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.5");
  496. else if (sdkCompat == osxVersion10_6) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.6");
  497. else if (sdkCompat == osxVersion10_7) s.add ("MACOSX_DEPLOYMENT_TARGET = 10.7");
  498. s.add ("MACOSX_DEPLOYMENT_TARGET_ppc = 10.4");
  499. if (xcodeExcludedFiles64Bit.isNotEmpty())
  500. {
  501. s.add ("EXCLUDED_SOURCE_FILE_NAMES = \"$(EXCLUDED_SOURCE_FILE_NAMES_$(CURRENT_ARCH))\"");
  502. s.add ("EXCLUDED_SOURCE_FILE_NAMES_x86_64 = " + xcodeExcludedFiles64Bit);
  503. }
  504. }
  505. s.add ("GCC_VERSION = " + gccVersion);
  506. s.add ("CLANG_CXX_LANGUAGE_STANDARD = \"c++0x\"");
  507. {
  508. StringArray linkerFlags, librarySearchPaths;
  509. getLinkerFlags (config, linkerFlags, librarySearchPaths);
  510. if (linkerFlags.size() > 0)
  511. s.add ("OTHER_LDFLAGS = \"" + linkerFlags.joinIntoString (" ") + "\"");
  512. librarySearchPaths.addArray (config.getLibrarySearchPaths());
  513. librarySearchPaths.removeDuplicates (false);
  514. if (librarySearchPaths.size() > 0)
  515. {
  516. String libPaths ("LIBRARY_SEARCH_PATHS = (\"$(inherited)\"");
  517. for (int i = 0; i < librarySearchPaths.size(); ++i)
  518. libPaths += ", \"\\\"" + librarySearchPaths[i] + "\\\"\"";
  519. s.add (libPaths + ")");
  520. }
  521. }
  522. StringPairArray defines;
  523. if (config.isDebug().getValue())
  524. {
  525. defines.set ("_DEBUG", "1");
  526. defines.set ("DEBUG", "1");
  527. s.add ("ONLY_ACTIVE_ARCH = YES");
  528. s.add ("COPY_PHASE_STRIP = NO");
  529. s.add ("GCC_DYNAMIC_NO_PIC = NO");
  530. }
  531. else
  532. {
  533. defines.set ("_NDEBUG", "1");
  534. defines.set ("NDEBUG", "1");
  535. s.add ("GCC_GENERATE_DEBUGGING_SYMBOLS = NO");
  536. s.add ("GCC_SYMBOLS_PRIVATE_EXTERN = YES");
  537. }
  538. {
  539. const String objCSuffix (getObjCSuffix().toString().trim());
  540. if (objCSuffix.isNotEmpty())
  541. defines.set ("JUCE_ObjCExtraSuffix", replacePreprocessorTokens (config, objCSuffix));
  542. }
  543. {
  544. defines = mergePreprocessorDefs (defines, getAllPreprocessorDefs (config));
  545. StringArray defsList;
  546. for (int i = 0; i < defines.size(); ++i)
  547. {
  548. String def (defines.getAllKeys()[i]);
  549. const String value (defines.getAllValues()[i]);
  550. if (value.isNotEmpty())
  551. def << "=" << value;
  552. defsList.add (def.quoted());
  553. }
  554. s.add ("GCC_PREPROCESSOR_DEFINITIONS = (" + indentList (defsList, ",") + ")");
  555. }
  556. s.addTokens (config.getCustomXcodeFlags().toString(), ",", "\"'");
  557. s.trim();
  558. s.removeEmptyStrings();
  559. s.removeDuplicates (false);
  560. return s;
  561. }
  562. void addFrameworks()
  563. {
  564. if (! projectType.isLibrary())
  565. {
  566. StringArray s (xcodeFrameworks);
  567. s.addTokens (getExtraFrameworks().toString(), ",;", "\"'");
  568. s.trim();
  569. s.removeDuplicates (true);
  570. s.sort (true);
  571. for (int i = 0; i < s.size(); ++i)
  572. addFramework (s[i]);
  573. }
  574. }
  575. //==============================================================================
  576. void writeProjectFile (OutputStream& output)
  577. {
  578. output << "// !$*UTF8*$!\n{\n"
  579. "\tarchiveVersion = 1;\n"
  580. "\tclasses = {\n\t};\n"
  581. "\tobjectVersion = 46;\n"
  582. "\tobjects = {\n\n";
  583. Array <ValueTree*> objects;
  584. objects.addArray (pbxBuildFiles);
  585. objects.addArray (pbxFileReferences);
  586. objects.addArray (pbxGroups);
  587. objects.addArray (targetConfigs);
  588. objects.addArray (projectConfigs);
  589. objects.addArray (misc);
  590. for (int i = 0; i < objects.size(); ++i)
  591. {
  592. ValueTree& o = *objects.getUnchecked(i);
  593. output << "\t\t" << o.getType().toString() << " = { ";
  594. for (int j = 0; j < o.getNumProperties(); ++j)
  595. {
  596. const Identifier propertyName (o.getPropertyName(j));
  597. String val (o.getProperty (propertyName).toString());
  598. if (val.isEmpty() || (val.containsAnyOf (" \t;<>()=,&+-_\r\n")
  599. && ! (val.trimStart().startsWithChar ('(')
  600. || val.trimStart().startsWithChar ('{'))))
  601. val = val.quoted();
  602. output << propertyName.toString() << " = " << val << "; ";
  603. }
  604. output << "};\n";
  605. }
  606. output << "\t};\n\trootObject = " << createID ("__root") << ";\n}\n";
  607. }
  608. static void addPlistDictionaryKey (XmlElement* xml, const String& key, const String& value)
  609. {
  610. forEachXmlChildElementWithTagName (*xml, e, "key")
  611. {
  612. if (e->getAllSubText().trim().equalsIgnoreCase (key))
  613. {
  614. if (e->getNextElement() != nullptr && e->getNextElement()->hasTagName ("key"))
  615. {
  616. // try to fix broken plist format..
  617. xml->removeChildElement (e, true);
  618. break;
  619. }
  620. else
  621. {
  622. return; // (value already exists)
  623. }
  624. }
  625. }
  626. xml->createNewChildElement ("key") ->addTextElement (key);
  627. xml->createNewChildElement ("string")->addTextElement (value);
  628. }
  629. static void addPlistDictionaryKeyBool (XmlElement* xml, const String& key, const bool value)
  630. {
  631. xml->createNewChildElement ("key")->addTextElement (key);
  632. xml->createNewChildElement (value ? "true" : "false");
  633. }
  634. String addBuildFile (const String& path, const String& fileRefID, bool addToSourceBuildPhase, bool inhibitWarnings)
  635. {
  636. String fileID (createID (path + "buildref"));
  637. if (addToSourceBuildPhase)
  638. sourceIDs.add (fileID);
  639. ValueTree* v = new ValueTree (fileID);
  640. v->setProperty ("isa", "PBXBuildFile", 0);
  641. v->setProperty ("fileRef", fileRefID, 0);
  642. if (inhibitWarnings)
  643. v->setProperty ("settings", "{COMPILER_FLAGS = \"-w\"; }", 0);
  644. pbxBuildFiles.add (v);
  645. return fileID;
  646. }
  647. String addBuildFile (const RelativePath& path, bool addToSourceBuildPhase, bool inhibitWarnings)
  648. {
  649. return addBuildFile (path.toUnixStyle(), createFileRefID (path), addToSourceBuildPhase, inhibitWarnings);
  650. }
  651. String addFileReference (String pathString)
  652. {
  653. String sourceTree ("SOURCE_ROOT");
  654. RelativePath path (pathString, RelativePath::unknown);
  655. if (pathString.startsWith ("${"))
  656. {
  657. sourceTree = pathString.substring (2).upToFirstOccurrenceOf ("}", false, false);
  658. pathString = pathString.fromFirstOccurrenceOf ("}/", false, false);
  659. }
  660. else if (path.isAbsolute())
  661. {
  662. sourceTree = "<absolute>";
  663. }
  664. const String fileRefID (createFileRefID (pathString));
  665. ScopedPointer<ValueTree> v (new ValueTree (fileRefID));
  666. v->setProperty ("isa", "PBXFileReference", 0);
  667. v->setProperty ("lastKnownFileType", getFileType (path), 0);
  668. v->setProperty (Ids::name, pathString.fromLastOccurrenceOf ("/", false, false), 0);
  669. v->setProperty ("path", sanitisePath (pathString), 0);
  670. v->setProperty ("sourceTree", sourceTree, 0);
  671. const int existing = pbxFileReferences.indexOfSorted (*this, v);
  672. if (existing >= 0)
  673. {
  674. // If this fails, there's either a string hash collision, or the same file is being added twice (incorrectly)
  675. jassert (pbxFileReferences.getUnchecked (existing)->isEquivalentTo (*v));
  676. }
  677. else
  678. {
  679. pbxFileReferences.addSorted (*this, v.release());
  680. }
  681. return fileRefID;
  682. }
  683. public:
  684. static int compareElements (const ValueTree* first, const ValueTree* second)
  685. {
  686. return first->getType().getCharPointer().compare (second->getType().getCharPointer());
  687. }
  688. private:
  689. static String getFileType (const RelativePath& file)
  690. {
  691. if (file.hasFileExtension ("cpp;cc;cxx")) return "sourcecode.cpp.cpp";
  692. else if (file.hasFileExtension (".mm")) return "sourcecode.cpp.objcpp";
  693. else if (file.hasFileExtension (".m")) return "sourcecode.c.objc";
  694. else if (file.hasFileExtension (headerFileExtensions)) return "sourcecode.c.h";
  695. else if (file.hasFileExtension (".framework")) return "wrapper.framework";
  696. else if (file.hasFileExtension (".jpeg;.jpg")) return "image.jpeg";
  697. else if (file.hasFileExtension ("png;gif")) return "image" + file.getFileExtension();
  698. else if (file.hasFileExtension ("html;htm")) return "text.html";
  699. else if (file.hasFileExtension ("xml;zip;wav")) return "file" + file.getFileExtension();
  700. else if (file.hasFileExtension ("txt;rtf")) return "text" + file.getFileExtension();
  701. else if (file.hasFileExtension ("plist")) return "text.plist.xml";
  702. else if (file.hasFileExtension ("app")) return "wrapper.application";
  703. else if (file.hasFileExtension ("component;vst;plugin")) return "wrapper.cfbundle";
  704. else if (file.hasFileExtension ("xcodeproj")) return "wrapper.pb-project";
  705. else if (file.hasFileExtension ("a")) return "archive.ar";
  706. return "file" + file.getFileExtension();
  707. }
  708. String addFile (const RelativePath& path, bool shouldBeCompiled, bool inhibitWarnings)
  709. {
  710. const String pathAsString (path.toUnixStyle());
  711. const String refID (addFileReference (path.toUnixStyle()));
  712. if (shouldBeCompiled)
  713. {
  714. if (path.hasFileExtension (".r"))
  715. rezFileIDs.add (addBuildFile (pathAsString, refID, false, inhibitWarnings));
  716. else
  717. addBuildFile (pathAsString, refID, true, inhibitWarnings);
  718. }
  719. return refID;
  720. }
  721. String addProjectItem (const Project::Item& projectItem)
  722. {
  723. if (projectItem.isGroup())
  724. {
  725. StringArray childIDs;
  726. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  727. {
  728. const String childID (addProjectItem (projectItem.getChild(i)));
  729. if (childID.isNotEmpty())
  730. childIDs.add (childID);
  731. }
  732. return addGroup (projectItem, childIDs);
  733. }
  734. else
  735. {
  736. if (projectItem.shouldBeAddedToTargetProject())
  737. {
  738. String itemPath (projectItem.getFilePath());
  739. bool inhibitWarnings = projectItem.getShouldInhibitWarningsValue().getValue();
  740. if (itemPath.startsWith ("${"))
  741. {
  742. const RelativePath path (itemPath, RelativePath::unknown);
  743. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  744. }
  745. else
  746. {
  747. const RelativePath path (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder);
  748. return addFile (path, projectItem.shouldBeCompiled(), inhibitWarnings);
  749. }
  750. }
  751. }
  752. return String::empty;
  753. }
  754. void addFramework (const String& frameworkName)
  755. {
  756. const String path ("System/Library/Frameworks/" + frameworkName + ".framework");
  757. const String fileRefID (createFileRefID (path));
  758. addFileReference ("${SDKROOT}/" + path);
  759. frameworkIDs.add (addBuildFile (path, fileRefID, false, false));
  760. frameworkFileIDs.add (fileRefID);
  761. }
  762. void addGroup (const String& groupID, const String& groupName, const StringArray& childIDs)
  763. {
  764. ValueTree* v = new ValueTree (groupID);
  765. v->setProperty ("isa", "PBXGroup", 0);
  766. v->setProperty ("children", "(" + indentList (childIDs, ",") + " )", 0);
  767. v->setProperty (Ids::name, groupName, 0);
  768. v->setProperty ("sourceTree", "<group>", 0);
  769. pbxGroups.add (v);
  770. }
  771. String addGroup (const Project::Item& item, StringArray& childIDs)
  772. {
  773. const String groupName (item.getName().toString());
  774. const String groupID (getIDForGroup (item));
  775. addGroup (groupID, groupName, childIDs);
  776. return groupID;
  777. }
  778. void addMainBuildProduct()
  779. {
  780. jassert (xcodeFileType.isNotEmpty());
  781. jassert (xcodeBundleExtension.isEmpty() || xcodeBundleExtension.startsWithChar('.'));
  782. String productName (getConfiguration(0)->getTargetBinaryName().toString());
  783. if (xcodeFileType == "archive.ar")
  784. productName = getLibbedFilename (productName);
  785. else
  786. productName += xcodeBundleExtension;
  787. addBuildProduct (xcodeFileType, productName);
  788. }
  789. void addBuildProduct (const String& fileType, const String& binaryName)
  790. {
  791. ValueTree* v = new ValueTree (createID ("__productFileID"));
  792. v->setProperty ("isa", "PBXFileReference", 0);
  793. v->setProperty ("explicitFileType", fileType, 0);
  794. v->setProperty ("includeInIndex", (int) 0, 0);
  795. v->setProperty ("path", sanitisePath (binaryName), 0);
  796. v->setProperty ("sourceTree", "BUILT_PRODUCTS_DIR", 0);
  797. pbxFileReferences.add (v);
  798. }
  799. void addTargetConfig (const String& configName, const StringArray& buildSettings)
  800. {
  801. ValueTree* v = new ValueTree (createID ("targetconfigid_" + configName));
  802. v->setProperty ("isa", "XCBuildConfiguration", 0);
  803. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  804. v->setProperty (Ids::name, configName, 0);
  805. targetConfigs.add (v);
  806. }
  807. void addProjectConfig (const String& configName, const StringArray& buildSettings)
  808. {
  809. ValueTree* v = new ValueTree (createID ("projectconfigid_" + configName));
  810. v->setProperty ("isa", "XCBuildConfiguration", 0);
  811. v->setProperty ("buildSettings", "{" + indentList (buildSettings, ";") + " }", 0);
  812. v->setProperty (Ids::name, configName, 0);
  813. projectConfigs.add (v);
  814. }
  815. void addConfigList (const OwnedArray <ValueTree>& configsToUse, const String& listID)
  816. {
  817. StringArray configIDs;
  818. for (int i = 0; i < configsToUse.size(); ++i)
  819. configIDs.add (configsToUse[i]->getType().toString());
  820. ValueTree* v = new ValueTree (listID);
  821. v->setProperty ("isa", "XCConfigurationList", 0);
  822. v->setProperty ("buildConfigurations", "(" + indentList (configIDs, ",") + " )", 0);
  823. v->setProperty ("defaultConfigurationIsVisible", (int) 0, 0);
  824. if (configsToUse[0] != nullptr)
  825. v->setProperty ("defaultConfigurationName", configsToUse[0]->getProperty (Ids::name), 0);
  826. misc.add (v);
  827. }
  828. ValueTree* addBuildPhase (const String& phaseType, const StringArray& fileIds)
  829. {
  830. String phaseId (createID (phaseType + "resbuildphase"));
  831. buildPhaseIDs.add (phaseId);
  832. ValueTree* v = new ValueTree (phaseId);
  833. v->setProperty ("isa", phaseType, 0);
  834. v->setProperty ("buildActionMask", "2147483647", 0);
  835. v->setProperty ("files", "(" + indentList (fileIds, ",") + " )", 0);
  836. v->setProperty ("runOnlyForDeploymentPostprocessing", (int) 0, 0);
  837. misc.add (v);
  838. return v;
  839. }
  840. void addTargetObject()
  841. {
  842. ValueTree* const v = new ValueTree (createID ("__target"));
  843. v->setProperty ("isa", "PBXNativeTarget", 0);
  844. v->setProperty ("buildConfigurationList", createID ("__configList"), 0);
  845. v->setProperty ("buildPhases", "(" + indentList (buildPhaseIDs, ",") + " )", 0);
  846. v->setProperty ("buildRules", "( )", 0);
  847. v->setProperty ("dependencies", "( )", 0);
  848. v->setProperty (Ids::name, projectName, 0);
  849. v->setProperty ("productName", projectName, 0);
  850. v->setProperty ("productReference", createID ("__productFileID"), 0);
  851. if (xcodeProductInstallPath.isNotEmpty())
  852. v->setProperty ("productInstallPath", xcodeProductInstallPath, 0);
  853. jassert (xcodeProductType.isNotEmpty());
  854. v->setProperty ("productType", xcodeProductType, 0);
  855. misc.add (v);
  856. }
  857. void addProjectObject()
  858. {
  859. ValueTree* const v = new ValueTree (createID ("__root"));
  860. v->setProperty ("isa", "PBXProject", 0);
  861. v->setProperty ("buildConfigurationList", createID ("__projList"), 0);
  862. v->setProperty ("compatibilityVersion", "Xcode 3.2", 0);
  863. v->setProperty ("hasScannedForEncodings", (int) 0, 0);
  864. v->setProperty ("mainGroup", createID ("__mainsourcegroup"), 0);
  865. v->setProperty ("projectDirPath", "\"\"", 0);
  866. v->setProperty ("projectRoot", "\"\"", 0);
  867. v->setProperty ("targets", "( " + createID ("__target") + " )", 0);
  868. misc.add (v);
  869. }
  870. void addShellScriptPhase()
  871. {
  872. if (xcodeShellScript.isNotEmpty())
  873. {
  874. ValueTree* const v = addBuildPhase ("PBXShellScriptBuildPhase", StringArray());
  875. v->setProperty (Ids::name, xcodeShellScriptTitle, 0);
  876. v->setProperty ("shellPath", "/bin/sh", 0);
  877. v->setProperty ("shellScript", xcodeShellScript.replace ("\\", "\\\\")
  878. .replace ("\"", "\\\"")
  879. .replace ("\r\n", "\\n")
  880. .replace ("\n", "\\n"), 0);
  881. }
  882. }
  883. //==============================================================================
  884. static String indentList (const StringArray& list, const String& separator)
  885. {
  886. if (list.size() == 0)
  887. return " ";
  888. return "\n\t\t\t\t" + list.joinIntoString (separator + "\n\t\t\t\t")
  889. + (separator == ";" ? separator : String::empty);
  890. }
  891. String createID (String rootString) const
  892. {
  893. if (rootString.startsWith ("${"))
  894. rootString = rootString.fromFirstOccurrenceOf ("}/", false, false);
  895. rootString += project.getProjectUID();
  896. return MD5 (rootString.toUTF8()).toHexString().substring (0, 24).toUpperCase();
  897. }
  898. String createFileRefID (const RelativePath& path) const
  899. {
  900. return createFileRefID (path.toUnixStyle());
  901. }
  902. String createFileRefID (const String& path) const
  903. {
  904. return createID ("__fileref_" + path);
  905. }
  906. String getIDForGroup (const Project::Item& item) const
  907. {
  908. return createID (item.getID());
  909. }
  910. bool shouldFileBeCompiledByDefault (const RelativePath& file) const
  911. {
  912. return file.hasFileExtension (sourceFileExtensions);
  913. }
  914. };
  915. #endif // __JUCER_PROJECTEXPORT_XCODE_JUCEHEADER__