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.

346 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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 0;
  33. }
  34. //==============================================================================
  35. MakefileProjectExporter (Project& project_, const ValueTree& settings_)
  36. : ProjectExporter (project_, settings_)
  37. {
  38. name = getNameLinux();
  39. if (getTargetLocation().toString().isEmpty())
  40. getTargetLocation() = getDefaultBuildsRootFolder() + "Linux";
  41. if (getVSTFolder().toString().isEmpty())
  42. getVSTFolder() = "~/SDKs/vstsdk2.4";
  43. }
  44. ~MakefileProjectExporter()
  45. {
  46. }
  47. //==============================================================================
  48. bool isDefaultFormatForCurrentOS()
  49. {
  50. #if JUCE_LINUX
  51. return true;
  52. #else
  53. return false;
  54. #endif
  55. }
  56. bool isPossibleForCurrentProject() { return true; }
  57. bool usesMMFiles() const { return false; }
  58. void launchProject()
  59. {
  60. // what to do on linux?
  61. }
  62. void createPropertyEditors (Array <PropertyComponent*>& props)
  63. {
  64. ProjectExporter::createPropertyEditors (props);
  65. }
  66. //==============================================================================
  67. const String create()
  68. {
  69. Array<RelativePath> files;
  70. findAllFilesToCompile (project.getMainGroup(), files);
  71. for (int i = 0; i < juceWrapperFiles.size(); ++i)
  72. if (shouldFileBeCompiledByDefault (juceWrapperFiles.getReference(i)))
  73. files.add (juceWrapperFiles.getReference(i));
  74. const Array<RelativePath> vstFiles (getVSTFilesRequired());
  75. for (int i = 0; i < vstFiles.size(); i++)
  76. files.add (vstFiles.getReference(i));
  77. MemoryOutputStream mo;
  78. writeMakefile (mo, files);
  79. const File makefile (getTargetFolder().getChildFile ("Makefile"));
  80. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (makefile, mo))
  81. return "Can't write to the Makefile: " + makefile.getFullPathName();
  82. return String::empty;
  83. }
  84. private:
  85. //==============================================================================
  86. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results)
  87. {
  88. if (projectItem.isGroup())
  89. {
  90. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  91. findAllFilesToCompile (projectItem.getChild(i), results);
  92. }
  93. else
  94. {
  95. if (projectItem.shouldBeCompiled())
  96. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  97. }
  98. }
  99. void writeDefineFlags (OutputStream& out, const Project::BuildConfiguration& config)
  100. {
  101. StringArray defines;
  102. defines.add (getExporterIdentifierMacro() + "=1");
  103. defines.add ("LINUX=1");
  104. if (config.isDebug().getValue())
  105. {
  106. defines.add ("DEBUG=1");
  107. defines.add ("_DEBUG=1");
  108. }
  109. else
  110. {
  111. defines.add ("NDEBUG=1");
  112. }
  113. defines.addArray (config.parsePreprocessorDefs());
  114. defines.addArray (parsePreprocessorDefs());
  115. for (int i = 0; i < defines.size(); ++i)
  116. out << " -D " << defines[i].quoted();
  117. }
  118. void writeHeaderPathFlags (OutputStream& out, const Project::BuildConfiguration& config)
  119. {
  120. StringArray headerPaths (config.getHeaderSearchPaths());
  121. headerPaths.insert (0, "/usr/include/freetype2");
  122. headerPaths.insert (0, "/usr/include");
  123. if (project.shouldAddVSTFolderToPath() && getVSTFolder().toString().isNotEmpty())
  124. headerPaths.insert (0, rebaseFromProjectFolderToBuildTarget (RelativePath (getVSTFolder().toString(), RelativePath::projectFolder)).toUnixStyle());
  125. if (isVST())
  126. headerPaths.insert (0, juceWrapperFolder.toUnixStyle());
  127. for (int i = 0; i < headerPaths.size(); ++i)
  128. out << " -I " << FileHelpers::unixStylePath (headerPaths[i]).quoted();
  129. }
  130. void writeCppFlags (OutputStream& out, const Project::BuildConfiguration& config)
  131. {
  132. out << " CPPFLAGS := $(DEPFLAGS)";
  133. writeDefineFlags (out, config);
  134. writeHeaderPathFlags (out, config);
  135. out << newLine;
  136. }
  137. void writeLinkerFlags (OutputStream& out, const Project::BuildConfiguration& config)
  138. {
  139. out << " LDFLAGS += -L$(BINDIR) -L$(LIBDIR)";
  140. if (project.isAudioPlugin())
  141. out << " -shared";
  142. {
  143. Array<RelativePath> libraryPaths;
  144. libraryPaths.add (RelativePath ("/usr/X11R6/lib/", RelativePath::unknown));
  145. libraryPaths.add (getJucePathFromTargetFolder().getChildFile ("bin"));
  146. for (int i = 0; i < libraryPaths.size(); ++i)
  147. out << " -L" << libraryPaths.getReference(i).toUnixStyle().quoted();
  148. }
  149. const char* defaultLibs[] = { "freetype", "pthread", "rt", "X11", "GL", "GLU", "Xinerama", "asound", 0 };
  150. StringArray libs (defaultLibs);
  151. if (project.getJuceLinkageMode() == Project::useLinkedJuce)
  152. libs.add ("juce");
  153. for (int i = 0; i < libs.size(); ++i)
  154. out << " -l" << libs[i];
  155. out << " " << getExtraLinkerFlags().toString().trim()
  156. << newLine;
  157. }
  158. void writeConfig (OutputStream& out, const Project::BuildConfiguration& config)
  159. {
  160. const String buildDirName ("build");
  161. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName().toString());
  162. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName().toString()) << ")" << newLine;
  163. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  164. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  165. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  166. << " OUTDIR := " << escapeSpaces (buildDirName) << newLine;
  167. writeCppFlags (out, config);
  168. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  169. if (config.isDebug().getValue())
  170. out << " -g -ggdb";
  171. if (project.isAudioPlugin())
  172. out << " -fPIC";
  173. out << " -O" << config.getGCCOptimisationFlag() << newLine;
  174. out << " CXXFLAGS += $(CFLAGS) " << getExtraCompilerFlags().toString().trim() << newLine;
  175. writeLinkerFlags (out, config);
  176. out << " LDDEPS :=" << newLine
  177. << " RESFLAGS := ";
  178. writeDefineFlags (out, config);
  179. writeHeaderPathFlags (out, config);
  180. out << newLine;
  181. String targetName (config.getTargetBinaryName().getValue().toString());
  182. if (project.isLibrary())
  183. targetName = getLibbedFilename (targetName);
  184. else if (isVST())
  185. targetName = targetName.upToLastOccurrenceOf (".", false, false) + ".so";
  186. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  187. if (project.isLibrary())
  188. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS) $(TARGET_ARCH)" << newLine;
  189. else
  190. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  191. out << "endif" << newLine << newLine;
  192. }
  193. void writeObjects (OutputStream& out, const Array<RelativePath>& files)
  194. {
  195. out << "OBJECTS := \\" << newLine;
  196. for (int i = 0; i < files.size(); ++i)
  197. if (shouldFileBeCompiledByDefault (files.getReference(i)))
  198. out << " $(OBJDIR)/" << escapeSpaces (getObjectFileFor (files.getReference(i))) << " \\" << newLine;
  199. out << newLine;
  200. }
  201. void writeMakefile (OutputStream& out, const Array<RelativePath>& files)
  202. {
  203. out << "# Automatically generated makefile, created by the Jucer" << newLine
  204. << "# Don't edit this file! Your changes will be overwritten when you re-save the Jucer project!" << newLine
  205. << newLine;
  206. out << "ifndef CONFIG" << newLine
  207. << " CONFIG=" << escapeSpaces (project.getConfiguration(0).getName().toString()) << newLine
  208. << "endif" << newLine
  209. << newLine;
  210. if (! project.isLibrary())
  211. out << "ifeq ($(TARGET_ARCH),)" << newLine
  212. << " TARGET_ARCH := -march=native" << newLine
  213. << "endif" << newLine << newLine;
  214. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  215. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  216. << newLine;
  217. int i;
  218. for (i = 0; i < project.getNumConfigurations(); ++i)
  219. writeConfig (out, project.getConfiguration(i));
  220. writeObjects (out, files);
  221. out << ".PHONY: clean" << newLine
  222. << newLine;
  223. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(LDDEPS) $(RESOURCES)" << newLine
  224. << "\t@echo Linking " << project.getProjectName() << 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 " << project.getProjectName() << newLine
  232. << "\t-@rm -f $(OUTDIR)/$(TARGET)" << newLine
  233. << "\t-@rm -rf $(OBJDIR)/*" << newLine
  234. << "\t-@rm -rf $(OBJDIR)" << newLine
  235. << newLine;
  236. for (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. static const String escapeSpaces (const String& s)
  253. {
  254. return s.replace (" ", "\\ ");
  255. }
  256. const String getObjectFileFor (const RelativePath& file) const
  257. {
  258. return file.getFileNameWithoutExtension()
  259. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  260. }
  261. MakefileProjectExporter (const MakefileProjectExporter&);
  262. MakefileProjectExporter& operator= (const MakefileProjectExporter&);
  263. };
  264. #endif // __JUCER_PROJECTEXPORT_MAKE_JUCEHEADER__