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.

336 lines
13KB

  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_MAKE_JUCEHEADER__
  19. #define __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__
  20. #include "jucer_ProjectExporter.h"
  21. //==============================================================================
  22. class MakefileProjectExporter : public ProjectExporter
  23. {
  24. public:
  25. //==============================================================================
  26. static const char* getNameLinux() { return "Linux Makefile"; }
  27. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  28. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  29. {
  30. if (settings.hasType (getValueTreeTypeName()))
  31. return new MakefileProjectExporter (project, settings);
  32. return nullptr;
  33. }
  34. //==============================================================================
  35. MakefileProjectExporter (Project& project_, const ValueTree& settings_)
  36. : ProjectExporter (project_, settings_)
  37. {
  38. name = getNameLinux();
  39. if (getTargetLocationString().isEmpty())
  40. getTargetLocationValue() = getDefaultBuildsRootFolder() + "Linux";
  41. }
  42. //==============================================================================
  43. int getLaunchPreferenceOrderForCurrentOS()
  44. {
  45. #if JUCE_LINUX
  46. return 1;
  47. #else
  48. return 0;
  49. #endif
  50. }
  51. bool isPossibleForCurrentProject() { return true; }
  52. bool usesMMFiles() const { return false; }
  53. bool isLinux() const { return true; }
  54. bool canCopeWithDuplicateFiles() { return false; }
  55. void launchProject()
  56. {
  57. // what to do on linux?
  58. }
  59. void createPropertyEditors (PropertyListBuilder& props)
  60. {
  61. ProjectExporter::createPropertyEditors (props);
  62. }
  63. //==============================================================================
  64. void create (const OwnedArray<LibraryModule>&) const
  65. {
  66. Array<RelativePath> files;
  67. for (int i = 0; i < groups.size(); ++i)
  68. findAllFilesToCompile (groups.getReference(i), files);
  69. MemoryOutputStream mo;
  70. writeMakefile (mo, files);
  71. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  72. }
  73. protected:
  74. //==============================================================================
  75. class MakeBuildConfiguration : public BuildConfiguration
  76. {
  77. public:
  78. MakeBuildConfiguration (Project& project, const ValueTree& settings)
  79. : BuildConfiguration (project, settings)
  80. {
  81. if (getLibrarySearchPathValue().getValue().isVoid())
  82. getLibrarySearchPathValue() = "/usr/X11R6/lib/";
  83. }
  84. void createPropertyEditors (PropertyListBuilder& props)
  85. {
  86. createBasicPropertyEditors (props);
  87. }
  88. };
  89. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  90. {
  91. return new MakeBuildConfiguration (project, settings);
  92. }
  93. private:
  94. //==============================================================================
  95. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  96. {
  97. if (projectItem.isGroup())
  98. {
  99. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  100. findAllFilesToCompile (projectItem.getChild(i), results);
  101. }
  102. else
  103. {
  104. if (projectItem.shouldBeCompiled())
  105. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  106. }
  107. }
  108. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  109. {
  110. StringPairArray defines;
  111. defines.set ("LINUX", "1");
  112. if (config.isDebug())
  113. {
  114. defines.set ("DEBUG", "1");
  115. defines.set ("_DEBUG", "1");
  116. }
  117. else
  118. {
  119. defines.set ("NDEBUG", "1");
  120. }
  121. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  122. }
  123. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  124. {
  125. StringArray searchPaths (extraSearchPaths);
  126. searchPaths.addArray (config.getHeaderSearchPaths());
  127. searchPaths.insert (0, "/usr/include/freetype2");
  128. searchPaths.insert (0, "/usr/include");
  129. searchPaths.removeDuplicates (false);
  130. for (int i = 0; i < searchPaths.size(); ++i)
  131. out << " -I " << addQuotesIfContainsSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  132. }
  133. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  134. {
  135. out << " CPPFLAGS := $(DEPFLAGS)";
  136. writeDefineFlags (out, config);
  137. writeHeaderPathFlags (out, config);
  138. out << newLine;
  139. }
  140. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  141. {
  142. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  143. if (makefileIsDLL)
  144. out << " -shared";
  145. out << config.getGCCLibraryPathFlags();
  146. const char* defaultLibs[] = { "dl", "freetype", "pthread", "rt", "X11", "GL", "GLU", "Xinerama", "asound", "Xext", 0 };
  147. StringArray libs (defaultLibs);
  148. for (int i = 0; i < libs.size(); ++i)
  149. out << " -l" << libs[i];
  150. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  151. << newLine;
  152. }
  153. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  154. {
  155. const String buildDirName ("build");
  156. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  157. String outputDir (buildDirName);
  158. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  159. {
  160. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  161. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  162. }
  163. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  164. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  165. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  166. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  167. << " OUTDIR := " << escapeSpaces (outputDir) << newLine;
  168. writeCppFlags (out, config);
  169. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  170. if (config.isDebug())
  171. out << " -g -ggdb";
  172. if (makefileIsDLL)
  173. out << " -fPIC";
  174. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  175. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim() << newLine;
  176. writeLinkerFlags (out, config);
  177. out << " LDDEPS :=" << newLine
  178. << " RESFLAGS := ";
  179. writeDefineFlags (out, config);
  180. writeHeaderPathFlags (out, config);
  181. out << newLine;
  182. String targetName (config.getTargetBinaryNameString());
  183. if (projectType.isLibrary())
  184. targetName = getLibbedFilename (targetName);
  185. else
  186. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  187. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  188. if (projectType.isLibrary())
  189. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  190. else
  191. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  192. out << "endif" << newLine << newLine;
  193. }
  194. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  195. {
  196. out << "OBJECTS := \\" << newLine;
  197. for (int i = 0; i < files.size(); ++i)
  198. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  199. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  200. out << newLine;
  201. }
  202. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  203. {
  204. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  205. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  206. << newLine;
  207. out << "ifndef CONFIG" << newLine
  208. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  209. << "endif" << newLine
  210. << newLine;
  211. if (! projectType.isLibrary())
  212. out << "ifeq ($(TARGET_ARCH),)" << newLine
  213. << " TARGET_ARCH := -march=native" << newLine
  214. << "endif" << newLine << newLine;
  215. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  216. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  217. << newLine;
  218. for (ConstConfigIterator config (*this); config.next();)
  219. writeConfig (out, *config);
  220. writeObjects (out, files);
  221. out << ".PHONY: clean" << newLine
  222. << newLine;
  223. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  224. << "\t@echo Linking " << projectName << newLine
  225. << "\t-@mkdir -p $(BINDIR)" << newLine
  226. << "\t-@mkdir -p $(LIBDIR)" << newLine
  227. << "\t-@mkdir -p $(OUTDIR)" << newLine
  228. << "\t@$(BLDCMD)" << newLine
  229. << newLine;
  230. out << "clean:" << newLine
  231. << "\t@echo Cleaning " << projectName << newLine
  232. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  233. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  234. << "\t-@rm -rf $(OBJDIR)" << newLine
  235. << newLine;
  236. for (int i = 0; i < files.size(); ++i)
  237. {
  238. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  239. {
  240. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  241. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  242. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  243. << "\t-@mkdir -p $(OBJDIR)" << newLine
  244. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  245. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  246. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  247. << newLine << newLine;
  248. }
  249. }
  250. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  251. }
  252. String getObjectFileFor (const RelativePath& file) const
  253. {
  254. return file.getFileNameWithoutExtension()
  255. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  256. }
  257. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  258. };
  259. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__