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.

1133 lines
45KB

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