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.

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