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.

1164 lines
47KB

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