Proposed patch for makefile variables

Hans hans at winternet.com
Tue Oct 11 18:37:02 UTC 2005


Hello,

I would like to get a patch into version 3.3 of kdevelop.  I am new to
this mailing list, so I'm not sure if this is the right way to go about
this, but I thought I would just send the patch and hope it gets in.

This is to fix a problem with custom makefiles in which variables in
targets and include statements are not being handled correctly.  This
patch will parse makefile variables when they are defined and substitute
them when they are used.  It also will substitute any environment
variables defined under the build options configuration box.

I don't know if there is a bug associated with this, but I searched for
one and couldn't find one.  Does there need to be one filed before this
patch is applied?

Feedback on the patch is welcome.  I tested it on a couple of makefiles,
but more review and testing would be welcome as well.

Thanks,

- Hans Erickson
  hans at winternet.com

-------------- next part --------------
--- kdevelop-3.2.91.old/buildtools/custommakefiles/customprojectpart.cpp	2005-09-10 03:21:46.000000000 -0500
+++ kdevelop-3.2.91.new/buildtools/custommakefiles/customprojectpart.cpp	2005-10-07 16:16:10.000000000 -0500
@@ -782,11 +782,36 @@
    QRegExp re("^([^($%.#][^)\\s]+) *:.*$");
    re.setMinimal(true);
 
+   QRegExp variablesRe("\\$\\(\\s*(.+)\\s*\\)");
+   QRegExp assignmentRe("^\\s*(.+)\\s*=\\s*(.+)\\s*$");
+   QMap<QString, QString> variables;
+   QStringList envVariables = QStringList::split(QRegExp("(=\\\")|(\\\"\\s*)"), makeEnvironment());
+   QStringList::const_iterator envIter = envVariables.begin();
+   while ( envIter != envVariables.end() )
+   {
+      QString key = *envIter++;
+      if (envIter != envVariables.end())
+      {
+         variables[key] = *envIter++;
+      }
+   }
+
    QRegExp includedMakefilesRe("^include\\s+(\\S+)");
    QString str = "";
    while (!f.atEnd()) {
       f.readLine(str, 200);
 
+      // Replace any variables in the current line
+      int offset = -1;
+      while ((offset = variablesRe.search(str, offset + 1)) != -1)
+      {
+         QString variableName=variablesRe.cap(1).simplifyWhiteSpace();
+         if (variables.contains(variableName))
+         {
+            str.replace(variablesRe, variables[variableName]);
+         }
+
+      }
       // Read all continuation lines
       // kdDebug(9025) << "Trying: " << str.simplifyWhiteSpace() << endl;
       //while (str.right(1) == "\\" && !stream.atEnd()) {
@@ -794,7 +819,13 @@
       //    str += stream.readLine();
       //}
 
-      if (includedMakefilesRe.search(str) != -1)
+      // Find any variables
+      if (assignmentRe.search(str) != -1)
+      {
+         variables[assignmentRe.cap(1).simplifyWhiteSpace()] =
+            assignmentRe.cap(2).simplifyWhiteSpace();
+      }
+      else if (includedMakefilesRe.search(str) != -1)
       {
          QString includedMakefile=includedMakefilesRe.cap(1).simplifyWhiteSpace();
          /*special optimization for makefiles generated with the new cmake makefile generator:


More information about the KDevelop-devel mailing list