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.

1022 lines
40KB

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