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.

340 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class MakefileProjectExporter : public ProjectExporter
  18. {
  19. public:
  20. //==============================================================================
  21. static const char* getNameLinux() { return "Linux Makefile"; }
  22. static const char* getValueTreeTypeName() { return "LINUX_MAKE"; }
  23. static MakefileProjectExporter* createForSettings (Project& project, const ValueTree& settings)
  24. {
  25. if (settings.hasType (getValueTreeTypeName()))
  26. return new MakefileProjectExporter (project, settings);
  27. return nullptr;
  28. }
  29. //==============================================================================
  30. MakefileProjectExporter (Project& p, const ValueTree& t) : ProjectExporter (p, t)
  31. {
  32. name = getNameLinux();
  33. if (getTargetLocationString().isEmpty())
  34. getTargetLocationValue() = getDefaultBuildsRootFolder() + "Linux";
  35. }
  36. //==============================================================================
  37. bool canLaunchProject() override { return false; }
  38. bool launchProject() override { return false; }
  39. bool usesMMFiles() const override { return false; }
  40. bool isLinux() const override { return true; }
  41. bool canCopeWithDuplicateFiles() override { return false; }
  42. void createExporterProperties (PropertyListBuilder&) override
  43. {
  44. }
  45. //==============================================================================
  46. void create (const OwnedArray<LibraryModule>&) const override
  47. {
  48. Array<RelativePath> files;
  49. for (int i = 0; i < getAllGroups().size(); ++i)
  50. findAllFilesToCompile (getAllGroups().getReference(i), files);
  51. MemoryOutputStream mo;
  52. writeMakefile (mo, files);
  53. overwriteFileIfDifferentOrThrow (getTargetFolder().getChildFile ("Makefile"), mo);
  54. }
  55. protected:
  56. //==============================================================================
  57. class MakeBuildConfiguration : public BuildConfiguration
  58. {
  59. public:
  60. MakeBuildConfiguration (Project& p, const ValueTree& settings)
  61. : BuildConfiguration (p, settings)
  62. {
  63. setValueIfVoid (getLibrarySearchPathValue(), "/usr/X11R6/lib/");
  64. }
  65. Value getArchitectureType() { return getValue (Ids::linuxArchitecture); }
  66. var getArchitectureTypeVar() const { return config [Ids::linuxArchitecture]; }
  67. var getDefaultOptimisationLevel() const override { return var ((int) (isDebug() ? gccO0 : gccO3)); }
  68. void createConfigProperties (PropertyListBuilder& props) override
  69. {
  70. addGCCOptimisationProperty (props);
  71. static const char* const archNames[] = { "(Default)", "<None>", "32-bit (-m32)", "64-bit (-m64)", "ARM v6", "ARM v7" };
  72. const var archFlags[] = { var(), var (String()), "-m32", "-m64", "-march=armv6", "-march=armv7" };
  73. props.add (new ChoicePropertyComponent (getArchitectureType(), "Architecture",
  74. StringArray (archNames, numElementsInArray (archNames)),
  75. Array<var> (archFlags, numElementsInArray (archFlags))));
  76. }
  77. };
  78. BuildConfiguration::Ptr createBuildConfig (const ValueTree& tree) const override
  79. {
  80. return new MakeBuildConfiguration (project, tree);
  81. }
  82. private:
  83. //==============================================================================
  84. void findAllFilesToCompile (const Project::Item& projectItem, Array<RelativePath>& results) const
  85. {
  86. if (projectItem.isGroup())
  87. {
  88. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  89. findAllFilesToCompile (projectItem.getChild(i), results);
  90. }
  91. else
  92. {
  93. if (projectItem.shouldBeCompiled())
  94. results.add (RelativePath (projectItem.getFile(), getTargetFolder(), RelativePath::buildTargetFolder));
  95. }
  96. }
  97. void writeDefineFlags (OutputStream& out, const BuildConfiguration& config) const
  98. {
  99. StringPairArray defines;
  100. defines.set ("LINUX", "1");
  101. if (config.isDebug())
  102. {
  103. defines.set ("DEBUG", "1");
  104. defines.set ("_DEBUG", "1");
  105. }
  106. else
  107. {
  108. defines.set ("NDEBUG", "1");
  109. }
  110. out << createGCCPreprocessorFlags (mergePreprocessorDefs (defines, getAllPreprocessorDefs (config)));
  111. }
  112. void writeHeaderPathFlags (OutputStream& out, const BuildConfiguration& config) const
  113. {
  114. StringArray searchPaths (extraSearchPaths);
  115. searchPaths.addArray (config.getHeaderSearchPaths());
  116. searchPaths.insert (0, "/usr/include/freetype2");
  117. searchPaths.insert (0, "/usr/include");
  118. searchPaths.removeDuplicates (false);
  119. for (int i = 0; i < searchPaths.size(); ++i)
  120. out << " -I " << escapeSpaces (FileHelpers::unixStylePath (replacePreprocessorTokens (config, searchPaths[i])));
  121. }
  122. void writeCppFlags (OutputStream& out, const BuildConfiguration& config) const
  123. {
  124. out << " CPPFLAGS := $(DEPFLAGS) -std=c++11";
  125. writeDefineFlags (out, config);
  126. writeHeaderPathFlags (out, config);
  127. out << newLine;
  128. }
  129. void writeLinkerFlags (OutputStream& out, const BuildConfiguration& config) const
  130. {
  131. out << " LDFLAGS += $(TARGET_ARCH) -L$(BINDIR) -L$(LIBDIR)";
  132. if (makefileIsDLL)
  133. out << " -shared";
  134. if (! config.isDebug())
  135. out << " -fvisibility=hidden";
  136. out << config.getGCCLibraryPathFlags();
  137. for (int i = 0; i < linuxLibs.size(); ++i)
  138. out << " -l" << linuxLibs[i];
  139. StringArray libraries;
  140. libraries.addTokens (getExternalLibrariesString(), ";", "\"'");
  141. libraries.removeEmptyStrings();
  142. if (libraries.size() != 0)
  143. out << " -l" << replacePreprocessorTokens (config, libraries.joinIntoString (" -l")).trim();
  144. out << " " << replacePreprocessorTokens (config, getExtraLinkerFlagsString()).trim()
  145. << newLine;
  146. }
  147. void writeConfig (OutputStream& out, const BuildConfiguration& config) const
  148. {
  149. const String buildDirName ("build");
  150. const String intermediatesDirName (buildDirName + "/intermediate/" + config.getName());
  151. String outputDir (buildDirName);
  152. if (config.getTargetBinaryRelativePathString().isNotEmpty())
  153. {
  154. RelativePath binaryPath (config.getTargetBinaryRelativePathString(), RelativePath::projectFolder);
  155. outputDir = binaryPath.rebased (projectFolder, getTargetFolder(), RelativePath::buildTargetFolder).toUnixStyle();
  156. }
  157. out << "ifeq ($(CONFIG)," << escapeSpaces (config.getName()) << ")" << newLine;
  158. out << " BINDIR := " << escapeSpaces (buildDirName) << newLine
  159. << " LIBDIR := " << escapeSpaces (buildDirName) << newLine
  160. << " OBJDIR := " << escapeSpaces (intermediatesDirName) << newLine
  161. << " OUTDIR := " << escapeSpaces (outputDir) << newLine
  162. << newLine
  163. << " ifeq ($(TARGET_ARCH),)" << newLine
  164. << " TARGET_ARCH := " << getArchFlags (config) << newLine
  165. << " endif" << newLine
  166. << newLine;
  167. writeCppFlags (out, config);
  168. out << " CFLAGS += $(CPPFLAGS) $(TARGET_ARCH)";
  169. if (config.isDebug())
  170. out << " -g -ggdb";
  171. if (makefileIsDLL)
  172. out << " -fPIC";
  173. out << " -O" << config.getGCCOptimisationFlag()
  174. << (" " + replacePreprocessorTokens (config, getExtraCompilerFlagsString())).trimEnd()
  175. << newLine;
  176. out << " CXXFLAGS += $(CFLAGS)" << newLine;
  177. writeLinkerFlags (out, config);
  178. out << newLine;
  179. String targetName (replacePreprocessorTokens (config, config.getTargetBinaryNameString()));
  180. if (projectType.isStaticLibrary() || projectType.isDynamicLibrary())
  181. targetName = getLibbedFilename (targetName);
  182. else
  183. targetName = targetName.upToLastOccurrenceOf (".", false, false) + makefileTargetSuffix;
  184. out << " TARGET := " << escapeSpaces (targetName) << newLine;
  185. if (projectType.isStaticLibrary())
  186. out << " BLDCMD = ar -rcs $(OUTDIR)/$(TARGET) $(OBJECTS)" << newLine;
  187. else
  188. out << " BLDCMD = $(CXX) -o $(OUTDIR)/$(TARGET) $(OBJECTS) $(LDFLAGS) $(RESOURCES) $(TARGET_ARCH)" << newLine;
  189. out << " CLEANCMD = rm -rf $(OUTDIR)/$(TARGET) $(OBJDIR)" << newLine
  190. << "endif" << newLine
  191. << newLine;
  192. }
  193. void writeObjects (OutputStream& out, const Array<RelativePath>& files) const
  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) const
  202. {
  203. out << "# Automatically generated makefile, created by the Introjucer" << newLine
  204. << "# Don't edit this file! Your changes will be overwritten when you re-save the Introjucer project!" << newLine
  205. << newLine;
  206. out << "# (this disables dependency generation if multiple architectures are set)" << newLine
  207. << "DEPFLAGS := $(if $(word 2, $(TARGET_ARCH)), , -MMD)" << newLine
  208. << newLine;
  209. out << "ifndef CONFIG" << newLine
  210. << " CONFIG=" << escapeSpaces (getConfiguration(0)->getName()) << newLine
  211. << "endif" << newLine
  212. << newLine;
  213. for (ConstConfigIterator config (*this); config.next();)
  214. writeConfig (out, *config);
  215. writeObjects (out, files);
  216. out << ".PHONY: clean" << newLine
  217. << newLine;
  218. out << "$(OUTDIR)/$(TARGET): $(OBJECTS) $(RESOURCES)" << newLine
  219. << "\t@echo Linking " << projectName << newLine
  220. << "\t-@mkdir -p $(BINDIR)" << newLine
  221. << "\t-@mkdir -p $(LIBDIR)" << newLine
  222. << "\t-@mkdir -p $(OUTDIR)" << newLine
  223. << "\t@$(BLDCMD)" << newLine
  224. << newLine;
  225. out << "clean:" << newLine
  226. << "\t@echo Cleaning " << projectName << newLine
  227. << "\t@$(CLEANCMD)" << newLine
  228. << newLine;
  229. out << "strip:" << newLine
  230. << "\t@echo Stripping " << projectName << newLine
  231. << "\t-@strip --strip-unneeded $(OUTDIR)/$(TARGET)" << 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;s;S") ? "\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 getArchFlags (const BuildConfiguration& config) const
  250. {
  251. if (const MakeBuildConfiguration* makeConfig = dynamic_cast<const MakeBuildConfiguration*> (&config))
  252. if (! makeConfig->getArchitectureTypeVar().isVoid())
  253. return makeConfig->getArchitectureTypeVar();
  254. return "-march=native";
  255. }
  256. String getObjectFileFor (const RelativePath& file) const
  257. {
  258. return file.getFileNameWithoutExtension()
  259. + "_" + String::toHexString (file.toUnixStyle().hashCode()) + ".o";
  260. }
  261. JUCE_DECLARE_NON_COPYABLE (MakefileProjectExporter)
  262. };