[CMake] Selecting runtime library on Visual Studio projects

Sylvain Benner benner at virtools.com
Tue Jul 18 06:11:21 EDT 2006


Hi,

I think you can't override a flag in the current version of CMake (release 
or CVS) because the loop that parses the flag string do not provide this.
I changed this function in order to override easily the flags in the low 
level configuration files.

I attached the modified flag table for compiler and the modified function 
that loop on this table
Note that there are imaginery flags that do not exist in VS. With these 
imaginery flags we can set the default options without forcing them in the 
code.
There are also some flags for VS2005 (especially for exception handling).

With this code you can override a flag with this macro :

MACRO(ADD_FLAG where flag)

SET(${where} "${flag} ${${where}}")

ENDMACRO(ADD_FLAG)



// example : override a subsystem flag

ADD_FLAG(CMAKE_EXE_LINKER_FLAGS /SUBSYSTEM:CONSOLE)
-------------- next part --------------
Flag Table ================================================================================
 
cmVS7FlagTable cmLocalVisualStudio7GeneratorMPCompilerFlagTable[] =

{

  // option flags (some flags map to the same option)

 

   // C/C++ ----------------------------------------------------------------------------------------------

   // General

   {"DebugInformationFormat",          "Z7",          "debug format",                              "1"},

   {"DebugInformationFormat",          "Zd",          "debug format",                              "2"},

   {"DebugInformationFormat",          "Zi",          "debug format",                              "3"},

   {"DebugInformationFormat",          "ZI",          "debug format",                              "4"},

   {"SuppressStartupBanner",           "logo",        "DO not SuppressStartupBanner",              "FALSE"},

   {"SuppressStartupBanner",           "nologo",      "SuppressStartupBanner",                     "TRUE"},

   {"WarningLevel",                    "W1",          "Warning level",                             "1"},

   {"WarningLevel",                    "W2",          "Warning level",                             "2"},

   {"WarningLevel",                    "W3",          "Warning level",                             "3"},

   {"WarningLevel",                    "W4",          "Warning level",                             "4"},

   {"Detect64BitPortabilityProblems",  "Wp64",        "Detect 64-bit Portability Issues",          "TRUE"},

   {"Detect64BitPortabilityProblems",  "noWp64",      "Do not Detect 64-bit Portability Issues",   "FALSE"},

   {"WarnAsError",                     "WX",          "Treat warnings as errors",                  "TRUE"},

   {"WarnAsError",                     "noWX",        "Do not Treat warnings as errors",           "FALSE"},

   // Optimization

   {"Optimization",                    "Od",          "Non Debug",                                 "0"},

   {"Optimization",                    "O1",          "Min Size",                                  "1"},

   {"Optimization",                    "O2",          "Max Speed",                                  "2"},

   {"Optimization",                    "Ox",          "Max Optimization",                         "3"},

   {"GlobalOptimizations",             "Og",          "Global Optimizations",                      "TRUE"},

   {"GlobalOptimizations",             "noOg",        "Do not Global Optimizations",               "FALSE"},

   {"InlineFunctionExpansion",         "Ob0",         "no inlines",                                "0"},

   {"InlineFunctionExpansion",         "Ob1",         "when inline keyword",                       "1"},

   {"InlineFunctionExpansion",         "Ob2",         "any time you can inline",                   "2"},

   {"EnableIntrinsicFunctions",        "Oi",          "EnableIntrinsicFunctions",                  "TRUE"},

   {"EnableIntrinsicFunctions",        "noOi",        "Do not EnableIntrinsicFunctions",           "FALSE"},

   {"ImproveFloatingPointConsistency", "Op",          "ImproveFloatingPointConsistency",           "TRUE"},

   {"ImproveFloatingPointConsistency", "noOp",        "Do not ImproveFloatingPointConsistency",    "FALSE"},

   {"FavorSizeOrSpeed",                "Ot",          "Favor fast code",                           "1"},

   {"FavorSizeOrSpeed",                "Os",          "Favor small code",                          "2"},

   {"OmitFramePointers",               "Oy",          "OmitFramePointers",                         "TRUE"},

   {"OmitFramePointers",               "noOy",        "Do not OmitFramePointers",                  "FALSE"},

   {"EnableFibreSafeOptimization",     "GT",          "OmitFramePointers",                         "TRUE"},

   {"EnableFibreSafeOptimization",     "noGT",        "Don't OmitFramePointers",                   "FALSE"},

   {"OptimizeForProcessor",            "GB",          "Blended processor mode",                    "0"},

   {"OptimizeForProcessor",            "G5",          "Pentium",                                   "1"},

   {"OptimizeForProcessor",            "G6",          "PPro PII PIII",                             "2"},

   {"OptimizeForProcessor",            "G7",          "Pentium 4 or Athlon",                       "3"},

   {"OptimizeForWindowsApplication",   "GA",          "Optimize for windows",                      "TRUE"},

   {"OptimizeForWindowsApplication",   "noGA",        "Do not Optimize for windows",               "FALSE"},

   // Code Generation

   {"StringPooling",                   "noGF",        "Disable read-only string pooling",          "FALSE"},

   {"StringPooling",                   "GF",          "Enable read-only string pooling",           "TRUE"},

   {"MinimalRebuild",                  "Gm",          "minimal rebuild",                           "TRUE"},

   {"MinimalRebuild",                  "noGm",        "Do not minimal rebuild",                    "FALSE"},

   {"ExceptionHandling",               "EHsc2005",    "Enable c++ exceptions",                     "1"},

   {"ExceptionHandling",               "noEHsc2005",  "Do not enable c++ exceptions",              "0"},

   {"ExceptionHandling",               "EHa",         "Enable c++ exceptions with SEH exceptions", "2"},

   {"ExceptionHandling",               "EHsc",        "enable c++ exceptions",                     "TRUE"},

   {"ExceptionHandling",               "noEHsc",      "Do not enable c++ exceptions",              "FALSE"},

   {"ExceptionHandling",               "GX",          "enable c++ exceptions",                     "TRUE"},

   {"ExceptionHandling",               "noGX",        "Do not enable c++ exceptions",              "FALSE"},

   {"SmallerTypeCheck",                "RTCc",        "smaller type check",                        "TRUE"},

   {"SmallerTypeCheck",                "noRTCc",      "Do not smaller type check",                 "FALSE"},

   {"BasicRuntimeChecks",              "GZ",          "Stack frame checks",                        "1"},

   {"BasicRuntimeChecks",              "RTCsu",       "Both stack and uninitialized checks ",      "3"},

   {"BasicRuntimeChecks",              "RTC1",        "Both stack and uninitialized checks ",      "3"},

   {"BasicRuntimeChecks",              "RTCs",        "Stack frame checks",                        "1"},

   {"BasicRuntimeChecks",              "RTCu",        "Uninitialized Variables ",                  "2"},

   {"RuntimeLibrary",                  "MTd",         "Multithreded debug",                        "1"},

   {"RuntimeLibrary",                  "MT",          "Multithreded",                              "0"},

   {"RuntimeLibrary",                  "MDd",         "Multithreded dll debug",                    "3"},

   {"RuntimeLibrary",                  "MD",          "Multithreded dll",                          "2"},

   {"RuntimeLibrary",                  "MLd",         "Sinble Thread debug",                       "5"},

   {"RuntimeLibrary",                  "ML",          "Sinble Thread",                             "4"},

   {"StructMemberAlignment",           "Zp16",        "struct align 16 byte ",                     "5"},

   {"StructMemberAlignment",           "Zp1",         "struct align 1 byte ",                      "1"},

   {"StructMemberAlignment",           "Zp2",         "struct align 2 byte ",                      "2"},

   {"StructMemberAlignment",           "Zp4",         "struct align 4 byte ",                      "3"},

   {"StructMemberAlignment",           "Zp8",         "struct align 8 byte ",                      "4"},

   {"BufferSecurityCheck",             "GS",          "Buffer security check",                     "TRUE"},

   {"BufferSecurityCheck",             "noGS",        "Don't Buffer security check",               "FALSE"},

   {"EnableFunctionLevelLinking",      "Gy",          "EnableFunctionLevelLinking",                "TRUE"},

   {"EnableFunctionLevelLinking",      "noGy",        "Do not EnableFunctionLevelLinking",         "FALSE"},

   {"EnableEnhancedInstructionSet",    "arch:SSE2",   "Use sse2 instructions",                     "2"},

   {"EnableEnhancedInstructionSet",    "arch:SSE",    "Use sse instructions",                      "1"},

   {"OpenMP",                          "openmp",      "Enable OpenMP support",                     "TRUE"},

   {"OpenMP",                          "noopenmp",    "Do not enable OpenMP support",              "FALSE"},

   // Language

   {"TreatWChar_tAsBuiltInType",         "Zc:wchar_t",   "Treats wchar_t as a built-in type",         "TRUE"},

   {"TreatWChar_tAsBuiltInType",         "noZc:wchar_t", "Do not Treats wchar_t as a built-in type",  "FALSE"},

   {"ForceConformanceInForLoopScope",    "Zc:forScope",  "force conform local scope in for loop",     "TRUE"},

   {"ForceConformanceInForLoopScope",    "noZc:forScope","No force conform local scope in for loop",  "FALSE"},

   {"RuntimeTypeInfo",                   "GR",           "Turn on Run time type information for c++", "TRUE"},

   {"RuntimeTypeInfo",                   "noGR",         "Turn off Run time type information for c++","FALSE"},

   // Precompiled Headers

   {"UsePrecompiledHeader",              "Yc",           "Create precompiled header",                 "1"},

   {"UsePrecompiledHeader",              "YX",           "Automatically generate precompiled header", "2"},

   {"UsePrecompiledHeader",              "Yu2005",       "Use precompiled header",                    "2"},

   {"UsePrecompiledHeader",              "Yu",           "Use precompiled header",                    "3"},

   {0,0,0,0 }

};

 
Loop Function ================================================================================
 
void cmLocalVisualStudio7GeneratorMP::FillFlagMapFromCommandFlags(

  std::map<cmStdString, cmStdString>& flagMap,

  cmVS7FlagTable* flagTable,

  std::string& flags)

{

  std::string option;

  std::string optionwithSpace;

 

  const int POS_MAX = 65535;

  int iBestPos = POS_MAX;

  int iCurPos = 0;

  std::string curCommand(flagTable->IDEName);

  std::string value;

 

   while(flagTable->IDEName)

   {

       while(flagTable->IDEName && (curCommand == flagTable->IDEName))

       {

          // while it's the same command, we seek the best flag to write

          option.reserve(strlen(flagTable->commandFlag)+1);

          optionwithSpace.reserve(strlen(flagTable->commandFlag)+2);

          // first do the - version

          option = "-";

          option += flagTable->commandFlag;

          optionwithSpace = option + " ";

          // check the option with a space first

          while((iCurPos = flags.find(optionwithSpace)) != flags.npos)

          {

            // replace the flag

            cmSystemTools::ReplaceString(flags, optionwithSpace.c_str(), "");

            // now put value into flag map

            if (iCurPos < iBestPos)

            {

               value = flagTable->value;

               iBestPos = iCurPos;

 

            }

          }

          // now do the / version

          optionwithSpace[0] = '/';

          while((iCurPos = flags.find(optionwithSpace)) != flags.npos)

          {

            // replace the flag

            cmSystemTools::ReplaceString(flags, optionwithSpace.c_str(), "");

            // now put value into flag map

            if (iCurPos < iBestPos)

            {

               value = flagTable->value;

               iBestPos = iCurPos;

            }

          }

          while((iCurPos = flags.find(option)) != flags.npos)

          {

            // replace the flag

            cmSystemTools::ReplaceString(flags, option.c_str(), "");

            // now put value into flag map

            if (iCurPos < iBestPos)

            {

               value = flagTable->value;

               iBestPos = iCurPos;

            }

          }

          // now do the / version

          option[0] = '/';

          while((iCurPos = flags.find(option)) != flags.npos)

          {

            // replace the flag

            cmSystemTools::ReplaceString(flags, option.c_str(), "");

            // now put value into flag map

            if (iCurPos < iBestPos)

            {

               value = flagTable->value;

               iBestPos = iCurPos;

            }

          }

          flagTable++;

       }

 

       if(iBestPos != POS_MAX)

       {

         flagMap[curCommand] = value;

       }

       if (flagTable->IDEName)

       {

          curCommand = flagTable->IDEName;

          iBestPos = POS_MAX;

       }

   }

 

  // If verbose makefiles have been requested and the /nologo option

  // was not given explicitly in the flags we want to add an attribute

  // to the generated project to disable logo suppression.  Otherwise

  // the GUI default is to enable suppression.

  if(this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"))

    {

    if(flagMap.find("SuppressStartupBanner") == flagMap.end())

      {

      flagMap["SuppressStartupBanner"] = "FALSE";

      }

    }

}



More information about the CMake mailing list