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.

332 lines
12KB

  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. setValueIfVoid (getLibrarySearchPathValue(), "/usr/X11R6/lib/");
  82. }
  83. void createPropertyEditors (PropertyListBuilder& props)
  84. {
  85. createBasicPropertyEditors (props);
  86. }
  87. };
  88. BuildConfiguration::Ptr createBuildConfig (const ValueTree& settings) const
  89. {
  90. return new MakeBuildConfiguration (project, settings);
  91. }
  92. private:
  93. //==============================================================================
  94. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  95. {
  96. if (projectItem.isGroup())
  97. {
  98. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  99. findAllFilesToCompile (projectItem.getChild(i), results);
  100. }
  101. else
  102. {
  103. if (projectItem.shouldBeCompiled())
  104. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  105. }
  106. }
  107. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  108. {
  109. StringPairArray defines;
  110. defines.set ("LINUX", "1");
  111. if (config.isDebug())
  112. {
  113. defines.set ("DEBUG", "1");
  114. defines.set ("_DEBUG", "1");
  115. }
  116. else
  117. {
  118. defines.set ("NDEBUG", "1");
  119. }
  120. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  121. }
  122. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  123. {
  124. StringArray searchPaths (extraSearchPaths);
  125. searchPaths.addArray (config.getHeaderSearchPaths());
  126. searchPaths.insert (0, "/usr/include/freetype2");
  127. searchPaths.insert (0, "/usr/include");
  128. searchPaths.removeDuplicates (false);
  129. for (int i = 0; i < searchPaths.size(); ++i)
  130. out << " -I " << addQuotesIfContainsSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  131. }
  132. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  133. {
  134. out << " CPPFLAGS := $(DEPFLAGS)";
  135. writeDefineFlags (out, config);
  136. writeHeaderPathFlags (out, config);
  137. out << newLine;
  138. }
  139. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  140. {
  141. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  142. if (makefileIsDLL)
  143. out << " -shared";
  144. out << config.getGCCLibraryPathFlags();
  145. for (int i = 0; i < linuxLibs.size(); ++i)
  146. out << " -l" << linuxLibs[i];
  147. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  148. << newLine;
  149. }
  150. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  151. {
  152. const String buildDirName ("build");
  153. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  154. String outputDir (buildDirName);
  155. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  156. {
  157. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  158. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  159. }
  160. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  161. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  162. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  163. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  164. << " OUTDIR := " << escapeSpaces (outputDir) << newLine;
  165. writeCppFlags (out, config);
  166. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  167. if (config.isDebug())
  168. out << " -g -ggdb";
  169. if (makefileIsDLL)
  170. out << " -fPIC";
  171. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  172. out << " CXXFLAGS += $(CFLAGS) " << replacePreprocessorTokens (config, getExtraCompilerFlagsString()).trim() << newLine;
  173. writeLinkerFlags (out, config);
  174. out << " LDDEPS :=" << newLine
  175. << " RESFLAGS := ";
  176. writeDefineFlags (out, config);
  177. writeHeaderPathFlags (out, config);
  178. out << newLine;
  179. String targetName (config.getTargetBinaryNameString());
  180. if (projectType.isLibrary())
  181. targetName = getLibbedFilename (targetName);
  182. else
  183. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  184. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  185. if (projectType.isLibrary())
  186. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  187. else
  188. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  189. out << "endif" << newLine << newLine;
  190. }
  191. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  192. {
  193. out << "OBJECTS := \\" << newLine;
  194. for (int i = 0; i < files.size(); ++i)
  195. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  196. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  197. out << newLine;
  198. }
  199. void writeMakefile (OutputStream& out, const Array<RelativePath>& files) const
  200. {
  201. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  202. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  203. << newLine;
  204. out << "ifndef CONFIG" << newLine
  205. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  206. << "endif" << newLine
  207. << newLine;
  208. if (! projectType.isLibrary())
  209. out << "ifeq ($(TARGET_ARCH),)" << newLine
  210. << " TARGET_ARCH := -march=native" << newLine
  211. << "endif" << newLine << newLine;
  212. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  213. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  214. << newLine;
  215. for (ConstConfigIterator config (*this); config.next();)
  216. writeConfig (out, *config);
  217. writeObjects (out, files);
  218. out << ".PHONY: clean" << newLine
  219. << newLine;
  220. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  221. << "\t@echo Linking " << projectName << newLine
  222. << "\t-@mkdir -p $(BINDIR)" << newLine
  223. << "\t-@mkdir -p $(LIBDIR)" << newLine
  224. << "\t-@mkdir -p $(OUTDIR)" << newLine
  225. << "\t@$(BLDCMD)" << newLine
  226. << newLine;
  227. out << "clean:" << newLine
  228. << "\t@echo Cleaning " << projectName << newLine
  229. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  230. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  231. << "\t-@rm -rf $(OBJDIR)" << newLine
  232. << newLine;
  233. for (int i = 0; i < files.size(); ++i)
  234. {
  235. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  236. {
  237. jassert (files.getReference(i).getRoot() == RelativePath::buildTargetFolder);
  238. out << "$(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i)))
  239. << ": " << escapeSpaces (files.getReference(i).toUnixStyle()) << newLine
  240. << "\t-@mkdir -p $(OBJDIR)" << newLine
  241. << "\t@echo \"Compiling " << files.getReference(i).getFileName() << "\"" << newLine
  242. << (files.getReference(i).hasFileExtension (".c") ? "\t@$(CC) $(CFLAGS) -o \"$@\" -c \"$<\""
  243. : "\t@$(CXX) $(CXXFLAGS) -o \"$@\" -c \"$<\"")
  244. << newLine << newLine;
  245. }
  246. }
  247. out << "-include $(OBJECTS:%.o=%.d)" << newLine;
  248. }
  249. String getObjectFileFor (const RelativePath& file) const
  250. {
  251. return file.getFileNameWithoutExtension()
  252. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  253. }
  254. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter);
  255. };
  256. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__