[Cmake] howto sort a cmake list ?

Michael T. Wagner Michael T. Wagner" <mtw at shared-reality.com
Sat, 21 Feb 2004 16:22:01 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_000C_01C3F896.D6074790
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi Bill,
I've managed to add the SORT command, it works like this: SORT(CTRLGUI_SRC
ASC), CTRLGUI_SRC is a variable containing the list of filenames to be
sorted, you can specify ASC or DESC to specify the sort order.
I've looked into the  vc7 generator code but didnt find the place to put the
code so I ended up adding the command which was quite straight forward)
Anyway, I've attached the source if you want to consider adding it
somewhere.
Greetings,
Mike

----- Original Message ----- 
From: "William A. Hoffman" <billlist at nycap.rr.com>
To: "Michael T. Wagner" <mtw at shared-reality.com>; <cmake at www.cmake.org>
Sent: Friday, February 20, 2004 5:38 PM
Subject: Re: [Cmake] howto sort a cmake list ?


> Thanks for the feedback.  Perhaps the answer is to change the visual
> studio generator to sort the files before it puts them in the solution.
>
> -Bill
>
>
> At 10:04 AM 2/20/2004, Michael T. Wagner wrote:
> >Hi Bill,
> >
> >thanks for answering - I'm using CMake to generate a VC7 project from a
list
> >${CTRLGUI_SRC} which contains like 60 files.
> >When opening the project in VC7, the source files show up in the "Source
> >Files"- and "Header Files"-Folder of the Solution Explorer in the same
order
> >they are listed in the ${CTRLGUI_SRC} list.
> >There are QT_WRAPPER macros and other special handling involved for some
> >files so its not possible to assemble the list alphabetically in the
> >CMakeLists-file just by hand.
> >Well to make a long story short it would be just more convenient to have
the
> >files in the "Solution Explorer"-folders sorted alphabetically.
> >I guess i go ahead then and try to add a SORT command myself... cross
your
> >fingers for me ;-)
> >
> >Thanks again,
> >
> >Mike
> >
> >----- Original Message ----- 
> >From: "William A. Hoffman" <billlist at nycap.rr.com>
> >To: "Michael T. Wagner" <mtw at shared-reality.com>; <cmake at www.cmake.org>
> >Sent: Friday, February 20, 2004 2:39 PM
> >Subject: Re: [Cmake] howto sort a cmake list ?
> >
> >
> >> I guess you could do it with a TRY_COMPILE and an EXEC_PROGRAM.
> >> But, I do not think there is a way to sort a list in the cmake language
> >> right now.   Just curious, why would you want to sort a list in a cmake
> >file?
> >>
> >> -Bill
> >>
> >>
> >> At 05:53 AM 2/20/2004, Michael T. Wagner wrote:
> >> >Hi all,
> >> >
> >> >this might be a dumb question but I didnt figure out how to do it -
I've
> >set
> >> >a list with SET (list blah dablah ..) and want to sort it
alphabetically.
> >> >So is there any SORT command in CMake for lists ?
> >> >I tried to do a FOREACH loop with the list elements and compare them
with
> >IF
> >> >(${var} STRLESS ${var}) but it didnt work with two variables.
> >> >What am I missing ?
> >> >
> >> >Thanks alot for any answer,
> >> >
> >> >Mike
> >> >
> >> >
> >> >
> >> >
> >> >
> >> >_______________________________________________
> >> >Cmake mailing list
> >> >Cmake at www.cmake.org
> >> >http://www.cmake.org/mailman/listinfo/cmake
> >>
> >> _______________________________________________
> >> Cmake mailing list
> >> Cmake at www.cmake.org
> >> http://www.cmake.org/mailman/listinfo/cmake
> >
> >_______________________________________________
> >Cmake mailing list
> >Cmake at www.cmake.org
> >http://www.cmake.org/mailman/listinfo/cmake
>

------=_NextPart_000_000C_01C3F896.D6074790
Content-Type: text/plain;
	name="cmSortCommand.h"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="cmSortCommand.h"

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
=0A=
  Program:   CMake - Cross-Platform Makefile Generator=0A=
  Module:    $RCSfile: cmSortCommand.h,v $=0A=
  Language:  C++=0A=
  Date:      $Date: 2002/10/23 22:03:26 $=0A=
  Version:   $Revision: 1.2 $=0A=
=0A=
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights =
reserved.=0A=
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for =
details.=0A=
=0A=
     This software is distributed WITHOUT ANY WARRANTY; without even =0A=
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR =0A=
     PURPOSE.  See the above copyright notices for more information.=0A=
=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/=0A=
#ifndef cmSortCommand_h=0A=
#define cmSortCommand_h=0A=
=0A=
#include "cmStandardIncludes.h"=0A=
#include "cmCommand.h"=0A=
=0A=
/** \class cmSortCommand=0A=
 * \brief Set a CMAKE variable=0A=
 *=0A=
 * cmSortCommand sets a variable to a value with expansion.  =0A=
 */=0A=
class cmSortCommand : public cmCommand=0A=
{=0A=
public:=0A=
  /**=0A=
   * This is a virtual constructor for the command.=0A=
   */=0A=
  virtual cmCommand* Clone() =0A=
    {=0A=
    return new cmSortCommand;=0A=
    }=0A=
=0A=
  /**=0A=
   * This is called when the command is first encountered in=0A=
   * the CMakeLists.txt file.=0A=
   */=0A=
  virtual bool InitialPass(std::vector<std::string> const& args);=0A=
=0A=
  /**=0A=
   * This determines if the command gets propagated down=0A=
   * to makefiles located in subdirectories.=0A=
   */=0A=
  virtual bool IsInherited() {return true;}=0A=
=0A=
  /**=0A=
   * The name of the command as specified in CMakeList.txt.=0A=
   */=0A=
  virtual const char* GetName() {return "SORT";}=0A=
  =0A=
  /**=0A=
   * Succinct documentation.=0A=
   */=0A=
  virtual const char* GetTerseDocumentation() =0A=
    {=0A=
    return "Sort a list of values from a CMAKE variable";=0A=
    }=0A=
  =0A=
  /**=0A=
   * More documentation.=0A=
   */=0A=
  virtual const char* GetFullDocumentation()=0A=
    {=0A=
    return=0A=
		"SORT(VALUE [ASC, DESC])\n"=0A=
      "Sorts list in VALUE in given order "=0A=
      "default is ascending."=0A=
      "(e.g. semicolon separated list). VALUE is expanded. ";=0A=
    }=0A=
  static bool sortDesc ( std::string fileA, std::string fileB );=0A=
  static bool sortAsc ( std::string fileA, std::string fileB );=0A=
=0A=
  cmTypeMacro(cmSortCommand, cmCommand);=0A=
};=0A=
=0A=
=0A=
=0A=
#endif=0A=

------=_NextPart_000_000C_01C3F896.D6074790
Content-Type: text/plain;
	name="cmSortCommand.cxx"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="cmSortCommand.cxx"

/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
=0A=
  Program:   CMake - Cross-Platform Makefile Generator=0A=
  Module:    $RCSfile: cmSortCommand.cxx,v $=0A=
  Language:  C++=0A=
  Date:      $Date: 2003/05/13 16:04:46 $=0A=
  Version:   $Revision: 1.4.2.1 $=0A=
=0A=
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights =
reserved.=0A=
  =0A=
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for =
details.=0A=
  =0A=
     This software is distributed WITHOUT ANY WARRANTY; without even =0A=
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR =0A=
     PURPOSE.  See the above copyright notices for more information.=0A=
=0A=
  Extension cmSortCommand written by Michael T. Wagner, =
shared-reality.com 2004=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/=0A=
#include "cmSortCommand.h"=0A=
=0A=
// cmSortCommand=0A=
=0A=

bool cmSortCommand::sortDesc ( std::string fileA, std::string fileB )
{
	std::string fa =3D cmsys::SystemTools::GetFilenameName(fileA);=0A=
	std::string fb =3D cmsys::SystemTools::GetFilenameName(fileB);
	// transform to uppercase for compare=20
	int (*pf)(int)=3Dtoupper; // use function pointer on toupper() func to =
avoid compile ambiguity
	std::transform(fa.begin(), fa.end(), fa.begin(), pf);=20
	std::transform(fb.begin(), fb.end(), fb.begin(), pf);=20

	return fa > fb;
}
=0A=
bool cmSortCommand::sortAsc ( std::string fileA, std::string fileB )
{
	std::string fa =3D cmsys::SystemTools::GetFilenameName(fileA);=0A=
	std::string fb =3D cmsys::SystemTools::GetFilenameName(fileB);
	// transform to uppercase for compare=20
	int (*pf)(int)=3Dtoupper; // use function pointer on toupper() func to =
avoid compile ambiguity
	std::transform(fa.begin(), fa.end(), fa.begin(), pf);=20
	std::transform(fb.begin(), fb.end(), fb.begin(), pf);=20

	return fa < fb;
}
=0A=
bool cmSortCommand::InitialPass(std::vector<std::string> const& args)=0A=
{=0A=
  if(args.size() < 1)=0A=
    {=0A=
    return true;=0A=
    }=0A=
  =0A=
  =0A=
  const char* variable =3D args[0].c_str(); // VAR is always first=0A=
  =0A=
  std::string dir =3D args[1];=0A=
  if (dir =3D=3D "") dir =3D "ASC";=0A=
  	=0A=
  // get the old value=0A=
  const char* cacheValue =3D m_Makefile->GetDefinition(variable);=0A=
=0A=
  // if there is no old value then return=0A=
  if (!cacheValue)=0A=
    {=0A=
    return true;=0A=
    }=0A=
  =0A=
  // expand the variable=0A=
  std::vector<std::string> varArgsExpanded;=0A=
  cmSystemTools::ExpandListArgument(cacheValue, varArgsExpanded);=0A=
  // sort the list =0A=
 int size =3D varArgsExpanded.size();=0A=
 // sort the stuff =0A=
  if (dir=3D=3D"DESC") =0A=
	  std::sort(varArgsExpanded.begin(), varArgsExpanded.end(), sortDesc);=0A=
  else =0A=
	  std::sort(varArgsExpanded.begin(), varArgsExpanded.end(), sortAsc);=0A=
  // now create the new value=0A=
  std::string value; =0A=
  for(unsigned int j =3D 0; j < varArgsExpanded.size(); ++j)=0A=
    {=0A=
	  if (value.size())=0A=
        {=0A=
        value +=3D ";";=0A=
        }=0A=
      value +=3D varArgsExpanded[j];=0A=
	}=0A=
  // add the definition=0A=
  m_Makefile->AddDefinition(variable, value.c_str());=0A=
=0A=
  return true;=0A=
}=0A=
=0A=

------=_NextPart_000_000C_01C3F896.D6074790--