"""SCons.Tool.msvsTool-specific initialization for Microsoft Visual Studio project files.There normally shouldn't be any need to import this module directly.It will usually be imported through the generic SCons.Tool.Tool()selection method."""## __COPYRIGHT__## Permission is hereby granted, free of charge, to any person obtaining# a copy of this software and associated documentation files (the# "Software"), to deal in the Software without restriction, including# without limitation the rights to use, copy, modify, merge, publish,# distribute, sublicense, and/or sell copies of the Software, and to# permit persons to whom the Software is furnished to do so, subject to# the following conditions:## The above copyright notice and this permission notice shall be included# in all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.__revision__="__FILE__ __REVISION__ __DATE__ __DEVELOPER__"importSCons.compatimportbase64importhashlibimportntpathimportos# compat layer imports "cPickle" for us if it's available.importpickleimportreimportsysimportSCons.BuilderimportSCons.Node.FSimportSCons.Platform.win32importSCons.Script.SConscriptimportSCons.PathListimportSCons.UtilimportSCons.WarningsfromMSCommonimportmsvc_exists,msvc_setup_env_oncefromSCons.DefaultsimportprocessDefines############################################################################### Below here are the classes and functions for generation of# DSP/DSW/SLN/VCPROJ files.##############################################################################defxmlify(s):s=s.replace("&","&amp;")# do this firsts=s.replace("'","&apos;")s=s.replace('"',"&quot;")returns# Process a CPPPATH list in includes, given the env, target and source.# Returns a tuple of nodes.defprocessIncludes(includes,env,target,source):returnSCons.PathList.PathList(includes).subst_path(env,target,source)external_makefile_guid='{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}'def_generateGUID(slnfile,name):"""This generates a dummy GUID for the sln file to use. It is based on the MD5 signatures of the sln filename plus the name of the project. It basically just needs to be unique, and not change with each invocation."""m=hashlib.md5()# Normalize the slnfile path to a Windows path (\ separators) so# the generated file has a consistent GUID even if we generate# it on a non-Windows platform.m.update(ntpath.normpath(str(slnfile))+str(name))solution=m.hexdigest().upper()# convert most of the signature to GUID form (discard the rest)solution="{"+solution[:8]+"-"+solution[8:12]+"-"+solution[12:16]+"-"+solution[16:20]+"-"+solution[20:32]+"}"returnsolutionversion_re=re.compile(r'(\d+\.\d+)(.*)')defmsvs_parse_version(s):""" Split a Visual Studio version, which may in fact be something like '7.0Exp', into is version number (returned as a float) and trailing "suite" portion. """num,suite=version_re.match(s).groups()returnfloat(num),suite# os.path.relpath has been introduced in Python 2.6# We define it locally for earlier versions of Pythondefrelpath(path,start=os.path.curdir):"""Return a relative version of a path"""importsysifnotpath:raiseValueError("no path specified")start_list=os.path.abspath(start).split(os.sep)path_list=os.path.abspath(path).split(os.sep)if'posix'insys.builtin_module_names:# Work out how much of the filepath is shared by start and path.i=len(os.path.commonprefix([start_list,path_list]))else:ifstart_list[0].lower()!=path_list[0].lower():unc_path,rest=os.path.splitunc(path)unc_start,rest=os.path.splitunc(start)ifbool(unc_path)^bool(unc_start):raiseValueError("Cannot mix UNC and non-UNC paths (%s and %s)"%(path,start))else:raiseValueError("path is on drive %s, start on drive %s"%(path_list[0],start_list[0]))# Work out how much of the filepath is shared by start and path.foriinrange(min(len(start_list),len(path_list))):ifstart_list[i].lower()!=path_list[i].lower():breakelse:i+=1rel_list=[os.pardir]*(len(start_list)-i)+path_list[i:]ifnotrel_list:returnos.path.curdirreturnos.path.join(*rel_list)ifnot"relpath"inos.path.__all__:os.path.relpath=relpath# This is how we re-invoke SCons from inside MSVS Project files.# The problem is that we might have been invoked as either scons.bat# or scons.py. If we were invoked directly as scons.py, then we could# use sys.argv[0] to find the SCons "executable," but that doesn't work# if we were invoked as scons.bat, which uses "python -c" to execute# things and ends up with "-c" as sys.argv[0]. Consequently, we have# the MSVS Project file invoke SCons the same way that scons.bat does,# which works regardless of how we were invoked.defgetExecScriptMain(env,xml=None):scons_home=env.get('SCONS_HOME')ifnotscons_homeand'SCONS_LIB_DIR'inos.environ:scons_home=os.environ['SCONS_LIB_DIR']ifscons_home:exec_script_main="from os.path import join; import sys; sys.path = [ r'%s' ] + sys.path; import SCons.Script; SCons.Script.main()"%scons_homeelse:version=SCons.__version__exec_script_main="from os.path import join; import sys; sys.path = [ join(sys.prefix, 'Lib', 'site-packages', 'scons-%(version)s'), join(sys.prefix, 'scons-%(version)s'), join(sys.prefix, 'Lib', 'site-packages', 'scons'), join(sys.prefix, 'scons') ] + sys.path; import SCons.Script; SCons.Script.main()"%locals()ifxml:exec_script_main=xmlify(exec_script_main)returnexec_script_main# The string for the Python executable we tell the Project file to use# is either sys.executable or, if an external PYTHON_ROOT environment# variable exists, $(PYTHON)ROOT\\python.exe (generalized a little to# pluck the actual executable name from sys.executable).try:python_root=os.environ['PYTHON_ROOT']exceptKeyError:python_executable=sys.executableelse:python_executable=os.path.join('$$(PYTHON_ROOT)',os.path.split(sys.executable)[1])classConfig(object):passdefsplitFully(path):dir,base=os.path.split(path)ifdiranddir!=''anddir!=path:returnsplitFully(dir)+[base]ifbase=='':return[]return[base]defmakeHierarchy(sources):'''Break a list of files into a hierarchy; for each value, if it is a string, then it is a file. If it is a dictionary, it is a folder. The string is the original path of the file.'''hierarchy={}forfileinsources:path=splitFully(file)iflen(path):dict=hierarchyforpartinpath[:-1]:ifpartnotindict:dict[part]={}dict=dict[part]dict[path[-1]]=file#else:# print 'Warning: failed to decompose path for '+str(file)returnhierarchyclass_DSPGenerator(object):""" Base class for DSP generators """srcargs=['srcs','incs','localincs','resources','misc']def__init__(self,dspfile,source,env):self.dspfile=str(dspfile)try:get_abspath=dspfile.get_abspathexceptAttributeError:self.dspabs=os.path.abspath(dspfile)else:self.dspabs=get_abspath()if'variant'notinenv:raiseSCons.Errors.InternalError("You must specify a 'variant' argument (i.e. 'Debug' or "+\
"'Release') to create an MSVSProject.")elifSCons.Util.is_String(env['variant']):variants=[env['variant']]elifSCons.Util.is_List(env['variant']):variants=env['variant']if'buildtarget'notinenvorenv['buildtarget']==None:buildtarget=['']elifSCons.Util.is_String(env['buildtarget']):buildtarget=[env['buildtarget']]elifSCons.Util.is_List(env['buildtarget']):iflen(env['buildtarget'])!=len(variants):raiseSCons.Errors.InternalError("Sizes of 'buildtarget' and 'variant' lists must be the same.")buildtarget=[]forbtinenv['buildtarget']:ifSCons.Util.is_String(bt):buildtarget.append(bt)else:buildtarget.append(bt.get_abspath())else:buildtarget=[env['buildtarget'].get_abspath()]iflen(buildtarget)==1:bt=buildtarget[0]buildtarget=[]for_invariants:buildtarget.append(bt)if'outdir'notinenvorenv['outdir']==None:outdir=['']elifSCons.Util.is_String(env['outdir']):outdir=[env['outdir']]elifSCons.Util.is_List(env['outdir']):iflen(env['outdir'])!=len(variants):raiseSCons.Errors.InternalError("Sizes of 'outdir' and 'variant' lists must be the same.")outdir=[]forsinenv['outdir']:ifSCons.Util.is_String(s):outdir.append(s)else:outdir.append(s.get_abspath())else:outdir=[env['outdir'].get_abspath()]iflen(outdir)==1:s=outdir[0]outdir=[]forvinvariants:outdir.append(s)if'runfile'notinenvorenv['runfile']==None:runfile=buildtarget[-1:]elifSCons.Util.is_String(env['runfile']):runfile=[env['runfile']]elifSCons.Util.is_List(env['runfile']):iflen(env['runfile'])!=len(variants):raiseSCons.Errors.InternalError("Sizes of 'runfile' and 'variant' lists must be the same.")runfile=[]forsinenv['runfile']:ifSCons.Util.is_String(s):runfile.append(s)else:runfile.append(s.get_abspath())else:runfile=[env['runfile'].get_abspath()]iflen(runfile)==1:s=runfile[0]runfile=[]forvinvariants:runfile.append(s)self.sconscript=env['MSVSSCONSCRIPT']cmdargs=env.get('cmdargs','')self.env=envif'name'inself.env:self.name=self.env['name']else:self.name=os.path.basename(SCons.Util.splitext(self.dspfile)[0])self.name=self.env.subst(self.name)sourcenames=['Source Files','Header Files','Local Headers','Resource Files','Other Files']self.sources={}forninsourcenames:self.sources[n]=[]self.configs={}self.nokeep=0if'nokeep'inenvandenv['variant']!=0:self.nokeep=1ifself.nokeep==0andos.path.exists(self.dspabs):self.Parse()fortinzip(sourcenames,self.srcargs):ift[1]inself.env:ifSCons.Util.is_List(self.env[t[1]]):foriinself.env[t[1]]:ifnotiinself.sources[t[0]]:self.sources[t[0]].append(i)else:ifnotself.env[t[1]]inself.sources[t[0]]:self.sources[t[0]].append(self.env[t[1]])forninsourcenames:#TODO 2.4: compat layer supports sorted(key=) but not sort(key=)#TODO 2.4: self.sources[n].sort(key=lambda a: a.lower())self.sources[n]=sorted(self.sources[n],key=lambdaa:a.lower())defAddConfig(self,variant,buildtarget,outdir,runfile,cmdargs,dspfile=dspfile):config=Config()config.buildtarget=buildtargetconfig.outdir=outdirconfig.cmdargs=cmdargsconfig.runfile=runfilematch=re.match('(.*)\|(.*)',variant)ifmatch:config.variant=match.group(1)config.platform=match.group(2)else:config.variant=variantconfig.platform='Win32'self.configs[variant]=configprint"Adding '"+self.name+' - '+config.variant+'|'+config.platform+"' to '"+str(dspfile)+"'"foriinrange(len(variants)):AddConfig(self,variants[i],buildtarget[i],outdir[i],runfile[i],cmdargs)self.platforms=[]forkeyinself.configs.keys():platform=self.configs[key].platformifnotplatforminself.platforms:self.platforms.append(platform)defBuild(self):passV6DSPHeader="""\# Microsoft Developer Studio Project File - Name="%(name)s" - Package Owner=<4># Microsoft Developer Studio Generated Build File, Format Version 6.00# ** DO NOT EDIT **# TARGTYPE "Win32 (x86) External Target" 0x0106CFG=%(name)s - Win32 %(confkey)s!MESSAGE This is not a valid makefile. To build this project using NMAKE,!MESSAGE use the Export Makefile command and run!MESSAGE !MESSAGE NMAKE /f "%(name)s.mak".!MESSAGE !MESSAGE You can specify a configuration when running NMAKE!MESSAGE by defining the macro CFG on the command line. For example:!MESSAGE !MESSAGE NMAKE /f "%(name)s.mak" CFG="%(name)s - Win32 %(confkey)s"!MESSAGE !MESSAGE Possible choices for configuration are:!MESSAGE """class_GenerateV6DSP(_DSPGenerator):"""Generates a Project file for MSVS 6.0"""defPrintHeader(self):# pick a default configconfkeys=sorted(self.configs.keys())name=self.nameconfkey=confkeys[0]self.file.write(V6DSPHeader%locals())forkindinconfkeys:self.file.write('!MESSAGE "%s - Win32 %s" (based on "Win32 (x86) External Target")\n'%(name,kind))self.file.write('!MESSAGE \n\n')defPrintProject(self):name=self.nameself.file.write('# Begin Project\n''# PROP AllowPerConfigDependencies 0\n''# PROP Scc_ProjName ""\n''# PROP Scc_LocalPath ""\n\n')first=1confkeys=sorted(self.configs.keys())forkindinconfkeys:outdir=self.configs[kind].outdirbuildtarget=self.configs[kind].buildtargetiffirst==1:self.file.write('!IF "$(CFG)" == "%s - Win32 %s"\n\n'%(name,kind))first=0else:self.file.write('\n!ELSEIF "$(CFG)" == "%s - Win32 %s"\n\n'%(name,kind))env_has_buildtarget='MSVSBUILDTARGET'inself.envifnotenv_has_buildtarget:self.env['MSVSBUILDTARGET']=buildtarget# have to write this twice, once with the BASE settings, and once withoutforbasein("BASE ",""):self.file.write('# PROP %sUse_MFC 0\n''# PROP %sUse_Debug_Libraries '%(base,base))ifkind.lower().find('debug')<0:self.file.write('0\n')else:self.file.write('1\n')self.file.write('# PROP %sOutput_Dir "%s"\n''# PROP %sIntermediate_Dir "%s"\n'%(base,outdir,base,outdir))cmd='echo Starting SCons && '+self.env.subst('$MSVSBUILDCOM',1)self.file.write('# PROP %sCmd_Line "%s"\n''# PROP %sRebuild_Opt "-c && %s"\n''# PROP %sTarget_File "%s"\n''# PROP %sBsc_Name ""\n''# PROP %sTarget_Dir ""\n'\
%(base,cmd,base,cmd,base,buildtarget,base,base))ifnotenv_has_buildtarget:delself.env['MSVSBUILDTARGET']self.file.write('\n!ENDIF\n\n''# Begin Target\n\n')forkindinconfkeys:self.file.write('# Name "%s - Win32 %s"\n'%(name,kind))self.file.write('\n')first=0forkindinconfkeys:iffirst==0:self.file.write('!IF "$(CFG)" == "%s - Win32 %s"\n\n'%(name,kind))first=1else:self.file.write('!ELSEIF "$(CFG)" == "%s - Win32 %s"\n\n'%(name,kind))self.file.write('!ENDIF \n\n')self.PrintSourceFiles()self.file.write('# End Target\n''# End Project\n')ifself.nokeep==0:# now we pickle some data and add it to the file -- MSDEV will ignore it.pdata=pickle.dumps(self.configs,1)pdata=base64.encodestring(pdata)self.file.write(pdata+'\n')pdata=pickle.dumps(self.sources,1)pdata=base64.encodestring(pdata)self.file.write(pdata+'\n')defPrintSourceFiles(self):categories={'Source Files':'cpp|c|cxx|l|y|def|odl|idl|hpj|bat','Header Files':'h|hpp|hxx|hm|inl','Local Headers':'h|hpp|hxx|hm|inl','Resource Files':'r|rc|ico|cur|bmp|dlg|rc2|rct|bin|cnt|rtf|gif|jpg|jpeg|jpe','Other Files':''}forkindinsorted(categories.keys(),key=lambdaa:a.lower()):ifnotself.sources[kind]:continue# skip empty groupsself.file.write('# Begin Group "'+kind+'"\n\n')typelist=categories[kind].replace('|',';')self.file.write('# PROP Default_Filter "'+typelist+'"\n')forfileinself.sources[kind]:file=os.path.normpath(file)self.file.write('# Begin Source File\n\n''SOURCE="'+file+'"\n''# End Source File\n')self.file.write('# End Group\n')# add the SConscript file outside of the groupsself.file.write('# Begin Source File\n\n''SOURCE="'+str(self.sconscript)+'"\n''# End Source File\n')defParse(self):try:dspfile=open(self.dspabs,'r')exceptIOError:return# doesn't exist yet, so can't add anything to configs.line=dspfile.readline()whileline:ifline.find("# End Project")>-1:breakline=dspfile.readline()line=dspfile.readline()datas=linewhilelineandline!='\n':line=dspfile.readline()datas=datas+line# OK, we've found our little pickled cache of data.try:datas=base64.decodestring(datas)data=pickle.loads(datas)exceptKeyboardInterrupt:raiseexcept:return# unable to unpickle any data for some reasonself.configs.update(data)data=Noneline=dspfile.readline()datas=linewhilelineandline!='\n':line=dspfile.readline()datas=datas+line# OK, we've found our little pickled cache of data.# it has a "# " in front of it, so we strip that.try:datas=base64.decodestring(datas)data=pickle.loads(datas)exceptKeyboardInterrupt:raiseexcept:return# unable to unpickle any data for some reasonself.sources.update(data)defBuild(self):try:self.file=open(self.dspabs,'w')exceptIOError,detail:raiseSCons.Errors.InternalError('Unable to open "'+self.dspabs+'" for writing:'+str(detail))else:self.PrintHeader()self.PrintProject()self.file.close()V7DSPHeader="""\<?xml version="1.0" encoding="%(encoding)s"?><VisualStudioProject\tProjectType="Visual C++"\tVersion="%(versionstr)s"\tName="%(name)s"\tProjectGUID="%(project_guid)s"%(scc_attrs)s\tKeyword="MakeFileProj">"""V7DSPConfiguration="""\\t\t<Configuration\t\t\tName="%(variant)s|%(platform)s"\t\t\tOutputDirectory="%(outdir)s"\t\t\tIntermediateDirectory="%(outdir)s"\t\t\tConfigurationType="0"\t\t\tUseOfMFC="0"\t\t\tATLMinimizesCRunTimeLibraryUsage="FALSE">\t\t\t<Tool\t\t\t\tName="VCNMakeTool"\t\t\t\tBuildCommandLine="%(buildcmd)s"\t\t\t\tReBuildCommandLine="%(rebuildcmd)s"\t\t\t\tCleanCommandLine="%(cleancmd)s"\t\t\t\tOutput="%(runfile)s"/>\t\t</Configuration>"""V8DSPHeader="""\<?xml version="1.0" encoding="%(encoding)s"?><VisualStudioProject\tProjectType="Visual C++"\tVersion="%(versionstr)s"\tName="%(name)s"\tProjectGUID="%(project_guid)s"\tRootNamespace="%(name)s"%(scc_attrs)s\tKeyword="MakeFileProj">"""V8DSPConfiguration="""\\t\t<Configuration\t\t\tName="%(variant)s|%(platform)s"\t\t\tConfigurationType="0"\t\t\tUseOfMFC="0"\t\t\tATLMinimizesCRunTimeLibraryUsage="false"\t\t\t>\t\t\t<Tool\t\t\t\tName="VCNMakeTool"\t\t\t\tBuildCommandLine="%(buildcmd)s"\t\t\t\tReBuildCommandLine="%(rebuildcmd)s"\t\t\t\tCleanCommandLine="%(cleancmd)s"\t\t\t\tOutput="%(runfile)s"\t\t\t\tPreprocessorDefinitions="%(preprocdefs)s"\t\t\t\tIncludeSearchPath="%(includepath)s"\t\t\t\tForcedIncludes=""\t\t\t\tAssemblySearchPath=""\t\t\t\tForcedUsingAssemblies=""\t\t\t\tCompileAsManaged=""\t\t\t/>\t\t</Configuration>"""class_GenerateV7DSP(_DSPGenerator):"""Generates a Project file for MSVS .NET"""def__init__(self,dspfile,source,env):_DSPGenerator.__init__(self,dspfile,source,env)self.version=env['MSVS_VERSION']self.version_num,self.suite=msvs_parse_version(self.version)ifself.version_num>=9.0:self.versionstr='9.00'self.dspheader=V8DSPHeaderself.dspconfiguration=V8DSPConfigurationelifself.version_num>=8.0:self.versionstr='8.00'self.dspheader=V8DSPHeaderself.dspconfiguration=V8DSPConfigurationelse:ifself.version_num>=7.1:self.versionstr='7.10'else:self.versionstr='7.00'self.dspheader=V7DSPHeaderself.dspconfiguration=V7DSPConfigurationself.file=NonedefPrintHeader(self):env=self.envversionstr=self.versionstrname=self.nameencoding=self.env.subst('$MSVSENCODING')scc_provider=env.get('MSVS_SCC_PROVIDER','')scc_project_name=env.get('MSVS_SCC_PROJECT_NAME','')scc_aux_path=env.get('MSVS_SCC_AUX_PATH','')# MSVS_SCC_LOCAL_PATH is kept for backwards compatibility purpose and should# be deprecated as soon as possible.scc_local_path_legacy=env.get('MSVS_SCC_LOCAL_PATH','')scc_connection_root=env.get('MSVS_SCC_CONNECTION_ROOT',os.curdir)scc_local_path=os.path.relpath(scc_connection_root,os.path.dirname(self.dspabs))project_guid=env.get('MSVS_PROJECT_GUID','')ifnotproject_guid:project_guid=_generateGUID(self.dspfile,'')ifscc_provider!='':scc_attrs='\tSccProjectName="%s"\n'%scc_project_nameifscc_aux_path!='':scc_attrs+='\tSccAuxPath="%s"\n'%scc_aux_pathscc_attrs+=('\tSccLocalPath="%s"\n''\tSccProvider="%s"'%(scc_local_path,scc_provider))elifscc_local_path_legacy!='':# This case is kept for backwards compatibility purpose and should# be deprecated as soon as possible.scc_attrs=('\tSccProjectName="%s"\n''\tSccLocalPath="%s"'%(scc_project_name,scc_local_path_legacy))else:self.dspheader=self.dspheader.replace('%(scc_attrs)s\n','')self.file.write(self.dspheader%locals())self.file.write('\t<Platforms>\n')forplatforminself.platforms:self.file.write('\t\t<Platform\n''\t\t\tName="%s"/>\n'%platform)self.file.write('\t</Platforms>\n')ifself.version_num>=8.0:self.file.write('\t<ToolFiles>\n''\t</ToolFiles>\n')defPrintProject(self):self.file.write('\t<Configurations>\n')confkeys=sorted(self.configs.keys())forkindinconfkeys:variant=self.configs[kind].variantplatform=self.configs[kind].platformoutdir=self.configs[kind].outdirbuildtarget=self.configs[kind].buildtargetrunfile=self.configs[kind].runfilecmdargs=self.configs[kind].cmdargsenv_has_buildtarget='MSVSBUILDTARGET'inself.envifnotenv_has_buildtarget:self.env['MSVSBUILDTARGET']=buildtargetstarting='echo Starting SCons && 'ifcmdargs:cmdargs=' '+cmdargselse:cmdargs=''buildcmd=xmlify(starting+self.env.subst('$MSVSBUILDCOM',1)+cmdargs)rebuildcmd=xmlify(starting+self.env.subst('$MSVSREBUILDCOM',1)+cmdargs)cleancmd=xmlify(starting+self.env.subst('$MSVSCLEANCOM',1)+cmdargs)# This isn't perfect; CPPDEFINES and CPPPATH can contain $TARGET and $SOURCE,# so they could vary depending on the command being generated. This code# assumes they don't.preprocdefs=xmlify(';'.join(processDefines(self.env.get('CPPDEFINES',[]))))includepath_Dirs=processIncludes(self.env.get('CPPPATH',[]),self.env,None,None)includepath=xmlify(';'.join([str(x)forxinincludepath_Dirs]))ifnotenv_has_buildtarget:delself.env['MSVSBUILDTARGET']self.file.write(self.dspconfiguration%locals())self.file.write('\t</Configurations>\n')ifself.version_num>=7.1:self.file.write('\t<References>\n''\t</References>\n')self.PrintSourceFiles()self.file.write('</VisualStudioProject>\n')ifself.nokeep==0:# now we pickle some data and add it to the file -- MSDEV will ignore it.pdata=pickle.dumps(self.configs,1)pdata=base64.encodestring(pdata)self.file.write('<!-- SCons Data:\n'+pdata+'\n')pdata=pickle.dumps(self.sources,1)pdata=base64.encodestring(pdata)self.file.write(pdata+'-->\n')defprintSources(self,hierarchy,commonprefix):sorteditems=sorted(hierarchy.items(),key=lambdaa:a[0].lower())# First folders, then filesforkey,valueinsorteditems:ifSCons.Util.is_Dict(value):self.file.write('\t\t\t<Filter\n''\t\t\t\tName="%s"\n''\t\t\t\tFilter="">\n'%(key))self.printSources(value,commonprefix)self.file.write('\t\t\t</Filter>\n')forkey,valueinsorteditems:ifSCons.Util.is_String(value):file=valueifcommonprefix:file=os.path.join(commonprefix,value)file=os.path.normpath(file)self.file.write('\t\t\t<File\n''\t\t\t\tRelativePath="%s">\n''\t\t\t</File>\n'%(file))defPrintSourceFiles(self):categories={'Source Files':'cpp;c;cxx;l;y;def;odl;idl;hpj;bat','Header Files':'h;hpp;hxx;hm;inl','Local Headers':'h;hpp;hxx;hm;inl','Resource Files':'r;rc;ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe','Other Files':''}self.file.write('\t<Files>\n')cats=sorted([kforkincategories.keys()ifself.sources[k]],key=lambdaa:a.lower())forkindincats:iflen(cats)>1:self.file.write('\t\t<Filter\n''\t\t\tName="%s"\n''\t\t\tFilter="%s">\n'%(kind,categories[kind]))sources=self.sources[kind]# First remove any common prefixcommonprefix=Nones=list(map(os.path.normpath,sources))# take the dirname because the prefix may include parts# of the filenames (e.g. if you have 'dir\abcd' and# 'dir\acde' then the cp will be 'dir\a' )cp=os.path.dirname(os.path.commonprefix(s))ifcpands[0][len(cp)]==os.sep:# +1 because the filename starts after the separatorsources=[s[len(cp)+1:]forsinsources]commonprefix=cphierarchy=makeHierarchy(sources)self.printSources(hierarchy,commonprefix=commonprefix)iflen(cats)>1:self.file.write('\t\t</Filter>\n')# add the SConscript file outside of the groupsself.file.write('\t\t<File\n''\t\t\tRelativePath="%s">\n''\t\t</File>\n'%str(self.sconscript))self.file.write('\t</Files>\n''\t<Globals>\n''\t</Globals>\n')defParse(self):try:dspfile=open(self.dspabs,'r')exceptIOError:return# doesn't exist yet, so can't add anything to configs.line=dspfile.readline()whileline:ifline.find('<!-- SCons Data:')>-1:breakline=dspfile.readline()line=dspfile.readline()datas=linewhilelineandline!='\n':line=dspfile.readline()datas=datas+line# OK, we've found our little pickled cache of data.try:datas=base64.decodestring(datas)data=pickle.loads(datas)exceptKeyboardInterrupt:raiseexcept:return# unable to unpickle any data for some reasonself.configs.update(data)data=Noneline=dspfile.readline()datas=linewhilelineandline!='\n':line=dspfile.readline()datas=datas+line# OK, we've found our little pickled cache of data.try:datas=base64.decodestring(datas)data=pickle.loads(datas)exceptKeyboardInterrupt:raiseexcept:return# unable to unpickle any data for some reasonself.sources.update(data)defBuild(self):try:self.file=open(self.dspabs,'w')exceptIOError,detail:raiseSCons.Errors.InternalError('Unable to open "'+self.dspabs+'" for writing:'+str(detail))else:self.PrintHeader()self.PrintProject()self.file.close()V10DSPHeader="""\<?xml version="1.0" encoding="%(encoding)s"?><Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">"""V10DSPProjectConfiguration="""\\t\t<ProjectConfiguration Include="%(variant)s|%(platform)s">\t\t\t<Configuration>%(variant)s</Configuration>\t\t\t<Platform>%(platform)s</Platform>\t\t</ProjectConfiguration>"""V10DSPGlobals="""\\t<PropertyGroup Label="Globals">\t\t<ProjectGuid>%(project_guid)s</ProjectGuid>%(scc_attrs)s\t\t<RootNamespace>%(name)s</RootNamespace>\t\t<Keyword>MakeFileProj</Keyword>\t</PropertyGroup>"""V10DSPPropertyGroupCondition="""\\t<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'" Label="Configuration">\t\t<ConfigurationType>Makefile</ConfigurationType>\t\t<UseOfMfc>false</UseOfMfc>\t</PropertyGroup>"""V10DSPImportGroupCondition="""\\t<ImportGroup Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'" Label="PropertySheets">\t\t<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\t</ImportGroup>"""V10DSPCommandLine="""\\t\t<NMakeBuildCommandLine Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">%(buildcmd)s</NMakeBuildCommandLine>\t\t<NMakeReBuildCommandLine Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">%(rebuildcmd)s</NMakeReBuildCommandLine>\t\t<NMakeCleanCommandLine Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">%(cleancmd)s</NMakeCleanCommandLine>\t\t<NMakeOutput Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">%(runfile)s</NMakeOutput>\t\t<NMakePreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">%(preprocdefs)s</NMakePreprocessorDefinitions>\t\t<NMakeIncludeSearchPath Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">%(includepath)s</NMakeIncludeSearchPath>\t\t<NMakeForcedIncludes Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">$(NMakeForcedIncludes)</NMakeForcedIncludes>\t\t<NMakeAssemblySearchPath Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">$(NMakeAssemblySearchPath)</NMakeAssemblySearchPath>\t\t<NMakeForcedUsingAssemblies Condition="'$(Configuration)|$(Platform)'=='%(variant)s|%(platform)s'">$(NMakeForcedUsingAssemblies)</NMakeForcedUsingAssemblies>"""class_GenerateV10DSP(_DSPGenerator):"""Generates a Project file for MSVS 2010"""def__init__(self,dspfile,source,env):_DSPGenerator.__init__(self,dspfile,source,env)self.dspheader=V10DSPHeaderself.dspconfiguration=V10DSPProjectConfigurationself.dspglobals=V10DSPGlobalsdefPrintHeader(self):env=self.envname=self.nameencoding=env.subst('$MSVSENCODING')project_guid=env.get('MSVS_PROJECT_GUID','')scc_provider=env.get('MSVS_SCC_PROVIDER','')scc_project_name=env.get('MSVS_SCC_PROJECT_NAME','')scc_aux_path=env.get('MSVS_SCC_AUX_PATH','')# MSVS_SCC_LOCAL_PATH is kept for backwards compatibility purpose and should# be deprecated as soon as possible.scc_local_path_legacy=env.get('MSVS_SCC_LOCAL_PATH','')scc_connection_root=env.get('MSVS_SCC_CONNECTION_ROOT',os.curdir)scc_local_path=os.path.relpath(scc_connection_root,os.path.dirname(self.dspabs))ifnotproject_guid:project_guid=_generateGUID(self.dspfile,'')ifscc_provider!='':scc_attrs='\t\t<SccProjectName>%s</SccProjectName>\n'%scc_project_nameifscc_aux_path!='':scc_attrs+='\t\t<SccAuxPath>%s</SccAuxPath>\n'%scc_aux_pathscc_attrs+=('\t\t<SccLocalPath>%s</SccLocalPath>\n''\t\t<SccProvider>%s</SccProvider>\n'%(scc_local_path,scc_provider))elifscc_local_path_legacy!='':# This case is kept for backwards compatibility purpose and should# be deprecated as soon as possible.scc_attrs=('\t\t<SccProjectName>%s</SccProjectName>\n''\t\t<SccLocalPath>%s</SccLocalPath>\n'%(scc_project_name,scc_local_path_legacy))else:self.dspglobals=self.dspglobals.replace('%(scc_attrs)s','')self.file.write(self.dspheader%locals())self.file.write('\t<ItemGroup Label="ProjectConfigurations">\n')confkeys=sorted(self.configs.keys())forkindinconfkeys:variant=self.configs[kind].variantplatform=self.configs[kind].platformself.file.write(self.dspconfiguration%locals())self.file.write('\t</ItemGroup>\n')self.file.write(self.dspglobals%locals())defPrintProject(self):name=self.nameconfkeys=sorted(self.configs.keys())self.file.write('\t<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />\n')forkindinconfkeys:variant=self.configs[kind].variantplatform=self.configs[kind].platformself.file.write(V10DSPPropertyGroupCondition%locals())self.file.write('\t<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />\n')self.file.write('\t<ImportGroup Label="ExtensionSettings">\n')self.file.write('\t</ImportGroup>\n')forkindinconfkeys:variant=self.configs[kind].variantplatform=self.configs[kind].platformself.file.write(V10DSPImportGroupCondition%locals())self.file.write('\t<PropertyGroup Label="UserMacros" />\n')self.file.write('\t<PropertyGroup>\n')self.file.write('\t<_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>\n')forkindinconfkeys:variant=self.configs[kind].variantplatform=self.configs[kind].platformoutdir=self.configs[kind].outdirbuildtarget=self.configs[kind].buildtargetrunfile=self.configs[kind].runfilecmdargs=self.configs[kind].cmdargsenv_has_buildtarget='MSVSBUILDTARGET'inself.envifnotenv_has_buildtarget:self.env['MSVSBUILDTARGET']=buildtargetstarting='echo Starting SCons && 'ifcmdargs:cmdargs=' '+cmdargselse:cmdargs=''buildcmd=xmlify(starting+self.env.subst('$MSVSBUILDCOM',1)+cmdargs)rebuildcmd=xmlify(starting+self.env.subst('$MSVSREBUILDCOM',1)+cmdargs)cleancmd=xmlify(starting+self.env.subst('$MSVSCLEANCOM',1)+cmdargs)# This isn't perfect; CPPDEFINES and CPPPATH can contain $TARGET and $SOURCE,# so they could vary depending on the command being generated. This code# assumes they don't.preprocdefs=xmlify(';'.join(processDefines(self.env.get('CPPDEFINES',[]))))includepath_Dirs=processIncludes(self.env.get('CPPPATH',[]),self.env,None,None)includepath=xmlify(';'.join([str(x)forxinincludepath_Dirs]))ifnotenv_has_buildtarget:delself.env['MSVSBUILDTARGET']self.file.write(V10DSPCommandLine%locals())self.file.write('\t</PropertyGroup>\n')#filter settings in MSVS 2010 are stored in separate fileself.filtersabs=self.dspabs+'.filters'try:self.filters_file=open(self.filtersabs,'w')exceptIOError,detail:raiseSCons.Errors.InternalError('Unable to open "'+self.filtersabs+'" for writing:'+str(detail))self.filters_file.write('<?xml version="1.0" encoding="utf-8"?>\n''<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\n')self.PrintSourceFiles()self.filters_file.write('</Project>')self.filters_file.close()self.file.write('\t<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\n''\t<ImportGroup Label="ExtensionTargets">\n''\t</ImportGroup>\n''</Project>\n')ifself.nokeep==0:# now we pickle some data and add it to the file -- MSDEV will ignore it.pdata=pickle.dumps(self.configs,1)pdata=base64.encodestring(pdata)self.file.write('<!-- SCons Data:\n'+pdata+'\n')pdata=pickle.dumps(self.sources,1)pdata=base64.encodestring(pdata)self.file.write(pdata+'-->\n')defprintFilters(self,hierarchy,name):sorteditems=sorted(hierarchy.items(),key=lambdaa:a[0].lower())forkey,valueinsorteditems:ifSCons.Util.is_Dict(value):filter_name=name+'\\'+keyself.filters_file.write('\t\t<Filter Include="%s">\n''\t\t\t<UniqueIdentifier>%s</UniqueIdentifier>\n''\t\t</Filter>\n'%(filter_name,_generateGUID(self.dspabs,filter_name)))self.printFilters(value,filter_name)defprintSources(self,hierarchy,kind,commonprefix,filter_name):keywords={'Source Files':'ClCompile','Header Files':'ClInclude','Local Headers':'ClInclude','Resource Files':'None','Other Files':'None'}sorteditems=sorted(hierarchy.items(),key=lambdaa:a[0].lower())# First folders, then filesforkey,valueinsorteditems:ifSCons.Util.is_Dict(value):self.printSources(value,kind,commonprefix,filter_name+'\\'+key)forkey,valueinsorteditems:ifSCons.Util.is_String(value):file=valueifcommonprefix:file=os.path.join(commonprefix,value)file=os.path.normpath(file)self.file.write('\t\t<%s Include="%s" />\n'%(keywords[kind],file))self.filters_file.write('\t\t<%s Include="%s">\n''\t\t\t<Filter>%s</Filter>\n''\t\t</%s>\n'%(keywords[kind],file,filter_name,keywords[kind]))defPrintSourceFiles(self):categories={'Source Files':'cpp;c;cxx;l;y;def;odl;idl;hpj;bat','Header Files':'h;hpp;hxx;hm;inl','Local Headers':'h;hpp;hxx;hm;inl','Resource Files':'r;rc;ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe','Other Files':''}cats=sorted([kforkincategories.keys()ifself.sources[k]],key=lambdaa:a.lower())# print vcxproj.filters file firstself.filters_file.write('\t<ItemGroup>\n')forkindincats:self.filters_file.write('\t\t<Filter Include="%s">\n''\t\t\t<UniqueIdentifier>{7b42d31d-d53c-4868-8b92-ca2bc9fc052f}</UniqueIdentifier>\n''\t\t\t<Extensions>%s</Extensions>\n''\t\t</Filter>\n'%(kind,categories[kind]))# First remove any common prefixsources=self.sources[kind]commonprefix=Nones=list(map(os.path.normpath,sources))# take the dirname because the prefix may include parts# of the filenames (e.g. if you have 'dir\abcd' and# 'dir\acde' then the cp will be 'dir\a' )cp=os.path.dirname(os.path.commonprefix(s))ifcpands[0][len(cp)]==os.sep:# +1 because the filename starts after the separatorsources=[s[len(cp)+1:]forsinsources]commonprefix=cphierarchy=makeHierarchy(sources)self.printFilters(hierarchy,kind)self.filters_file.write('\t</ItemGroup>\n')# then print files and filtersforkindincats:self.file.write('\t<ItemGroup>\n')self.filters_file.write('\t<ItemGroup>\n')# First remove any common prefixsources=self.sources[kind]commonprefix=Nones=list(map(os.path.normpath,sources))# take the dirname because the prefix may include parts# of the filenames (e.g. if you have 'dir\abcd' and# 'dir\acde' then the cp will be 'dir\a' )cp=os.path.dirname(os.path.commonprefix(s))ifcpands[0][len(cp)]==os.sep:# +1 because the filename starts after the separatorsources=[s[len(cp)+1:]forsinsources]commonprefix=cphierarchy=makeHierarchy(sources)self.printSources(hierarchy,kind,commonprefix,kind)self.file.write('\t</ItemGroup>\n')self.filters_file.write('\t</ItemGroup>\n')# add the SConscript file outside of the groupsself.file.write('\t<ItemGroup>\n''\t\t<None Include="%s" />\n'#'\t\t<None Include="SConstruct" />\n''\t</ItemGroup>\n'%str(self.sconscript))defParse(self):print"_GenerateV10DSP.Parse()"defBuild(self):try:self.file=open(self.dspabs,'w')exceptIOError,detail:raiseSCons.Errors.InternalError('Unable to open "'+self.dspabs+'" for writing:'+str(detail))else:self.PrintHeader()self.PrintProject()self.file.close()class_DSWGenerator(object):""" Base class for DSW generators """def__init__(self,dswfile,source,env):self.dswfile=os.path.normpath(str(dswfile))self.dsw_folder_path=os.path.dirname(os.path.abspath(self.dswfile))self.env=envif'projects'notinenv:raiseSCons.Errors.UserError("You must specify a 'projects' argument to create an MSVSSolution.")projects=env['projects']ifnotSCons.Util.is_List(projects):raiseSCons.Errors.InternalError("The 'projects' argument must be a list of nodes.")projects=SCons.Util.flatten(projects)iflen(projects)<1:raiseSCons.Errors.UserError("You must specify at least one project to create an MSVSSolution.")self.dspfiles=list(map(str,projects))if'name'inself.env:self.name=self.env['name']else:self.name=os.path.basename(SCons.Util.splitext(self.dswfile)[0])self.name=self.env.subst(self.name)defBuild(self):passclass_GenerateV7DSW(_DSWGenerator):"""Generates a Solution file for MSVS .NET"""def__init__(self,dswfile,source,env):_DSWGenerator.__init__(self,dswfile,source,env)self.file=Noneself.version=self.env['MSVS_VERSION']self.version_num,self.suite=msvs_parse_version(self.version)self.versionstr='7.00'ifself.version_num>=11.0:self.versionstr='12.00'elifself.version_num>=10.0:self.versionstr='11.00'elifself.version_num>=9.0:self.versionstr='10.00'elifself.version_num>=8.0:self.versionstr='9.00'elifself.version_num>=7.1:self.versionstr='8.00'if'slnguid'inenvandenv['slnguid']:self.slnguid=env['slnguid']else:self.slnguid=_generateGUID(dswfile,self.name)self.configs={}self.nokeep=0if'nokeep'inenvandenv['variant']!=0:self.nokeep=1ifself.nokeep==0andos.path.exists(self.dswfile):self.Parse()defAddConfig(self,variant,dswfile=dswfile):config=Config()match=re.match('(.*)\|(.*)',variant)ifmatch:config.variant=match.group(1)config.platform=match.group(2)else:config.variant=variantconfig.platform='Win32'self.configs[variant]=configprint"Adding '"+self.name+' - '+config.variant+'|'+config.platform+"' to '"+str(dswfile)+"'"if'variant'notinenv:raiseSCons.Errors.InternalError("You must specify a 'variant' argument (i.e. 'Debug' or "+\
"'Release') to create an MSVS Solution File.")elifSCons.Util.is_String(env['variant']):AddConfig(self,env['variant'])elifSCons.Util.is_List(env['variant']):forvariantinenv['variant']:AddConfig(self,variant)self.platforms=[]forkeyinself.configs.keys():platform=self.configs[key].platformifnotplatforminself.platforms:self.platforms.append(platform)defGenerateProjectFilesInfo(self):fordspfileinself.dspfiles:dsp_folder_path,name=os.path.split(dspfile)dsp_folder_path=os.path.abspath(dsp_folder_path)dsp_relative_folder_path=os.path.relpath(dsp_folder_path,self.dsw_folder_path)ifdsp_relative_folder_path==os.curdir:dsp_relative_file_path=nameelse:dsp_relative_file_path=os.path.join(dsp_relative_folder_path,name)dspfile_info={'NAME':name,'GUID':_generateGUID(dspfile,''),'FOLDER_PATH':dsp_folder_path,'FILE_PATH':dspfile,'SLN_RELATIVE_FOLDER_PATH':dsp_relative_folder_path,'SLN_RELATIVE_FILE_PATH':dsp_relative_file_path}self.dspfiles_info.append(dspfile_info)self.dspfiles_info=[]GenerateProjectFilesInfo(self)defParse(self):try:dswfile=open(self.dswfile,'r')exceptIOError:return# doesn't exist yet, so can't add anything to configs.line=dswfile.readline()whileline:ifline[:9]=="EndGlobal":breakline=dswfile.readline()line=dswfile.readline()datas=linewhileline:line=dswfile.readline()datas=datas+line# OK, we've found our little pickled cache of data.try:datas=base64.decodestring(datas)data=pickle.loads(datas)exceptKeyboardInterrupt:raiseexcept:return# unable to unpickle any data for some reasonself.configs.update(data)defPrintSolution(self):"""Writes a solution file"""self.file.write('Microsoft Visual Studio Solution File, Format Version %s\n'%self.versionstr)ifself.version_num>=11.0:self.file.write('# Visual Studio 11\n')elifself.version_num>=10.0:self.file.write('# Visual Studio 2010\n')elifself.version_num>=9.0:self.file.write('# Visual Studio 2008\n')elifself.version_num>=8.0:self.file.write('# Visual Studio 2005\n')fordspinfoinself.dspfiles_info:name=dspinfo['NAME']base,suffix=SCons.Util.splitext(name)ifsuffix=='.vcproj':name=baseself.file.write('Project("%s") = "%s", "%s", "%s"\n'%(external_makefile_guid,name,dspinfo['SLN_RELATIVE_FILE_PATH'],dspinfo['GUID']))ifself.version_num>=7.1andself.version_num<8.0:self.file.write('\tProjectSection(ProjectDependencies) = postProject\n''\tEndProjectSection\n')self.file.write('EndProject\n')self.file.write('Global\n')env=self.envif'MSVS_SCC_PROVIDER'inenv:scc_number_of_projects=len(self.dspfiles)+1slnguid=self.slnguidscc_provider=env.get('MSVS_SCC_PROVIDER','').replace(' ',r'\u0020')scc_project_name=env.get('MSVS_SCC_PROJECT_NAME','').replace(' ',r'\u0020')scc_connection_root=env.get('MSVS_SCC_CONNECTION_ROOT',os.curdir)scc_local_path=os.path.relpath(scc_connection_root,self.dsw_folder_path).replace('\\','\\\\')self.file.write('\tGlobalSection(SourceCodeControl) = preSolution\n''\t\tSccNumberOfProjects = %(scc_number_of_projects)d\n''\t\tSccProjectName0 = %(scc_project_name)s\n''\t\tSccLocalPath0 = %(scc_local_path)s\n''\t\tSccProvider0 = %(scc_provider)s\n''\t\tCanCheckoutShared = true\n'%locals())sln_relative_path_from_scc=os.path.relpath(self.dsw_folder_path,scc_connection_root)ifsln_relative_path_from_scc!=os.curdir:self.file.write('\t\tSccProjectFilePathRelativizedFromConnection0 = %s\\\\\n'%sln_relative_path_from_scc.replace('\\','\\\\'))ifself.version_num<8.0:# When present, SolutionUniqueID is automatically removed by VS 2005# TODO: check for Visual Studio versions newer than 2005self.file.write('\t\tSolutionUniqueID = %s\n'%slnguid)fordspinfoinself.dspfiles_info:i=self.dspfiles_info.index(dspinfo)+1dsp_relative_file_path=dspinfo['SLN_RELATIVE_FILE_PATH'].replace('\\','\\\\')dsp_scc_relative_folder_path=os.path.relpath(dspinfo['FOLDER_PATH'],scc_connection_root).replace('\\','\\\\')self.file.write('\t\tSccProjectUniqueName%(i)s = %(dsp_relative_file_path)s\n''\t\tSccLocalPath%(i)d = %(scc_local_path)s\n''\t\tCanCheckoutShared = true\n''\t\tSccProjectFilePathRelativizedFromConnection%(i)s = %(dsp_scc_relative_folder_path)s\\\\\n'%locals())self.file.write('\tEndGlobalSection\n')ifself.version_num>=8.0:self.file.write('\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n')else:self.file.write('\tGlobalSection(SolutionConfiguration) = preSolution\n')confkeys=sorted(self.configs.keys())cnt=0fornameinconfkeys:variant=self.configs[name].variantplatform=self.configs[name].platformifself.version_num>=8.0:self.file.write('\t\t%s|%s = %s|%s\n'%(variant,platform,variant,platform))else:self.file.write('\t\tConfigName.%d = %s\n'%(cnt,variant))cnt=cnt+1self.file.write('\tEndGlobalSection\n')ifself.version_num<=7.1:self.file.write('\tGlobalSection(ProjectDependencies) = postSolution\n''\tEndGlobalSection\n')ifself.version_num>=8.0:self.file.write('\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n')else:self.file.write('\tGlobalSection(ProjectConfiguration) = postSolution\n')fornameinconfkeys:variant=self.configs[name].variantplatform=self.configs[name].platformifself.version_num>=8.0:fordspinfoinself.dspfiles_info:guid=dspinfo['GUID']self.file.write('\t\t%s.%s|%s.ActiveCfg = %s|%s\n''\t\t%s.%s|%s.Build.0 = %s|%s\n'%(guid,variant,platform,variant,platform,guid,variant,platform,variant,platform))else:fordspinfoinself.dspfiles_info:guid=dspinfo['GUID']self.file.write('\t\t%s.%s.ActiveCfg = %s|%s\n''\t\t%s.%s.Build.0 = %s|%s\n'%(guid,variant,variant,platform,guid,variant,variant,platform))self.file.write('\tEndGlobalSection\n')ifself.version_num>=8.0:self.file.write('\tGlobalSection(SolutionProperties) = preSolution\n''\t\tHideSolutionNode = FALSE\n''\tEndGlobalSection\n')else:self.file.write('\tGlobalSection(ExtensibilityGlobals) = postSolution\n''\tEndGlobalSection\n''\tGlobalSection(ExtensibilityAddIns) = postSolution\n''\tEndGlobalSection\n')self.file.write('EndGlobal\n')ifself.nokeep==0:pdata=pickle.dumps(self.configs,1)pdata=base64.encodestring(pdata)self.file.write(pdata+'\n')defBuild(self):try:self.file=open(self.dswfile,'w')exceptIOError,detail:raiseSCons.Errors.InternalError('Unable to open "'+self.dswfile+'" for writing:'+str(detail))else:self.PrintSolution()self.file.close()V6DSWHeader="""\Microsoft Developer Studio Workspace File, Format Version 6.00# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!###############################################################################Project: "%(name)s"="%(dspfile)s" - Package Owner=<4>Package=<5>{{{}}}Package=<4>{{{}}}###############################################################################Global:Package=<5>{{{}}}Package=<3>{{{}}}###############################################################################"""class_GenerateV6DSW(_DSWGenerator):"""Generates a Workspace file for MSVS 6.0"""defPrintWorkspace(self):""" writes a DSW file """name=self.namedspfile=os.path.relpath(self.dspfiles[0],self.dsw_folder_path)self.file.write(V6DSWHeader%locals())defBuild(self):try:self.file=open(self.dswfile,'w')exceptIOError,detail:raiseSCons.Errors.InternalError('Unable to open "'+self.dswfile+'" for writing:'+str(detail))else:self.PrintWorkspace()self.file.close()defGenerateDSP(dspfile,source,env):"""Generates a Project file based on the version of MSVS that is being used"""version_num=6.0if'MSVS_VERSION'inenv:version_num,suite=msvs_parse_version(env['MSVS_VERSION'])ifversion_num>=10.0:g=_GenerateV10DSP(dspfile,source,env)g.Build()elifversion_num>=7.0:g=_GenerateV7DSP(dspfile,source,env)g.Build()else:g=_GenerateV6DSP(dspfile,source,env)g.Build()defGenerateDSW(dswfile,source,env):"""Generates a Solution/Workspace file based on the version of MSVS that is being used"""version_num=6.0if'MSVS_VERSION'inenv:version_num,suite=msvs_parse_version(env['MSVS_VERSION'])ifversion_num>=7.0:g=_GenerateV7DSW(dswfile,source,env)g.Build()else:g=_GenerateV6DSW(dswfile,source,env)g.Build()############################################################################### Above here are the classes and functions for generation of# DSP/DSW/SLN/VCPROJ files.##############################################################################defGetMSVSProjectSuffix(target,source,env,for_signature):returnenv['MSVS']['PROJECTSUFFIX']defGetMSVSSolutionSuffix(target,source,env,for_signature):returnenv['MSVS']['SOLUTIONSUFFIX']defGenerateProject(target,source,env):# generate the dsp file, according to the version of MSVS.builddspfile=target[0]dspfile=builddspfile.srcnode()# this detects whether or not we're using a VariantDirifnotdspfileisbuilddspfile:try:bdsp=open(str(builddspfile),"w+")exceptIOError,detail:print'Unable to open "'+str(dspfile)+'" for writing:',detail,'\n'raisebdsp.write("This is just a placeholder file.\nThe real project file is here:\n%s\n"%dspfile.get_abspath())GenerateDSP(dspfile,source,env)ifenv.get('auto_build_solution',1):builddswfile=target[1]dswfile=builddswfile.srcnode()ifnotdswfileisbuilddswfile:try:bdsw=open(str(builddswfile),"w+")exceptIOError,detail:print'Unable to open "'+str(dspfile)+'" for writing:',detail,'\n'raisebdsw.write("This is just a placeholder file.\nThe real workspace file is here:\n%s\n"%dswfile.get_abspath())GenerateDSW(dswfile,source,env)defGenerateSolution(target,source,env):GenerateDSW(target[0],source,env)defprojectEmitter(target,source,env):"""Sets up the DSP dependencies."""# todo: Not sure what sets source to what user has passed as target,# but this is what happens. When that is fixed, we also won't have# to make the user always append env['MSVSPROJECTSUFFIX'] to target.ifsource[0]==target[0]:source=[]# make sure the suffix is correct for the version of MSVS we're running.(base,suff)=SCons.Util.splitext(str(target[0]))suff=env.subst('$MSVSPROJECTSUFFIX')target[0]=base+suffifnotsource:source='prj_inputs:'source=source+env.subst('$MSVSSCONSCOM',1)source=source+env.subst('$MSVSENCODING',1)# Project file depends on CPPDEFINES and CPPPATHpreprocdefs=xmlify(';'.join(processDefines(env.get('CPPDEFINES',[]))))includepath_Dirs=processIncludes(env.get('CPPPATH',[]),env,None,None)includepath=xmlify(';'.join([str(x)forxinincludepath_Dirs]))source=source+"; ppdefs:%s incpath:%s"%(preprocdefs,includepath)if'buildtarget'inenvandenv['buildtarget']!=None:ifSCons.Util.is_String(env['buildtarget']):source=source+' "%s"'%env['buildtarget']elifSCons.Util.is_List(env['buildtarget']):forbtinenv['buildtarget']:ifSCons.Util.is_String(bt):source=source+' "%s"'%btelse:try:source=source+' "%s"'%bt.get_abspath()exceptAttributeError:raiseSCons.Errors.InternalError("buildtarget can be a string, a node, a list of strings or nodes, or None")else:try:source=source+' "%s"'%env['buildtarget'].get_abspath()exceptAttributeError:raiseSCons.Errors.InternalError("buildtarget can be a string, a node, a list of strings or nodes, or None")if'outdir'inenvandenv['outdir']!=None:ifSCons.Util.is_String(env['outdir']):source=source+' "%s"'%env['outdir']elifSCons.Util.is_List(env['outdir']):forsinenv['outdir']:ifSCons.Util.is_String(s):source=source+' "%s"'%selse:try:source=source+' "%s"'%s.get_abspath()exceptAttributeError:raiseSCons.Errors.InternalError("outdir can be a string, a node, a list of strings or nodes, or None")else:try:source=source+' "%s"'%env['outdir'].get_abspath()exceptAttributeError:raiseSCons.Errors.InternalError("outdir can be a string, a node, a list of strings or nodes, or None")if'name'inenv:ifSCons.Util.is_String(env['name']):source=source+' "%s"'%env['name']else:raiseSCons.Errors.InternalError("name must be a string")if'variant'inenv:ifSCons.Util.is_String(env['variant']):source=source+' "%s"'%env['variant']elifSCons.Util.is_List(env['variant']):forvariantinenv['variant']:ifSCons.Util.is_String(variant):source=source+' "%s"'%variantelse:raiseSCons.Errors.InternalError("name must be a string or a list of strings")else:raiseSCons.Errors.InternalError("variant must be a string or a list of strings")else:raiseSCons.Errors.InternalError("variant must be specified")forsin_DSPGenerator.srcargs:ifsinenv:ifSCons.Util.is_String(env[s]):source=source+' "%s'%env[s]elifSCons.Util.is_List(env[s]):fortinenv[s]:ifSCons.Util.is_String(t):source=source+' "%s"'%telse:raiseSCons.Errors.InternalError(s+" must be a string or a list of strings")else:raiseSCons.Errors.InternalError(s+" must be a string or a list of strings")source=source+' "%s"'%str(target[0])source=[SCons.Node.Python.Value(source)]targetlist=[target[0]]sourcelist=sourceifenv.get('auto_build_solution',1):env['projects']=[env.File(t).srcnode()fortintargetlist]t,s=solutionEmitter(target,target,env)targetlist=targetlist+treturn(targetlist,sourcelist)defsolutionEmitter(target,source,env):"""Sets up the DSW dependencies."""# todo: Not sure what sets source to what user has passed as target,# but this is what happens. When that is fixed, we also won't have# to make the user always append env['MSVSSOLUTIONSUFFIX'] to target.ifsource[0]==target[0]:source=[]# make sure the suffix is correct for the version of MSVS we're running.(base,suff)=SCons.Util.splitext(str(target[0]))suff=env.subst('$MSVSSOLUTIONSUFFIX')target[0]=base+suffifnotsource:source='sln_inputs:'if'name'inenv:ifSCons.Util.is_String(env['name']):source=source+' "%s"'%env['name']else:raiseSCons.Errors.InternalError("name must be a string")if'variant'inenv:ifSCons.Util.is_String(env['variant']):source=source+' "%s"'%env['variant']elifSCons.Util.is_List(env['variant']):forvariantinenv['variant']:ifSCons.Util.is_String(variant):source=source+' "%s"'%variantelse:raiseSCons.Errors.InternalError("name must be a string or a list of strings")else:raiseSCons.Errors.InternalError("variant must be a string or a list of strings")else:raiseSCons.Errors.InternalError("variant must be specified")if'slnguid'inenv:ifSCons.Util.is_String(env['slnguid']):source=source+' "%s"'%env['slnguid']else:raiseSCons.Errors.InternalError("slnguid must be a string")if'projects'inenv:ifSCons.Util.is_String(env['projects']):source=source+' "%s"'%env['projects']elifSCons.Util.is_List(env['projects']):fortinenv['projects']:ifSCons.Util.is_String(t):source=source+' "%s"'%tsource=source+' "%s"'%str(target[0])source=[SCons.Node.Python.Value(source)]return([target[0]],source)projectAction=SCons.Action.Action(GenerateProject,None)solutionAction=SCons.Action.Action(GenerateSolution,None)projectBuilder=SCons.Builder.Builder(action='$MSVSPROJECTCOM',suffix='$MSVSPROJECTSUFFIX',emitter=projectEmitter)solutionBuilder=SCons.Builder.Builder(action='$MSVSSOLUTIONCOM',suffix='$MSVSSOLUTIONSUFFIX',emitter=solutionEmitter)default_MSVS_SConscript=Nonedefgenerate(env):"""Add Builders and construction variables for Microsoft Visual Studio project files to an Environment."""try:env['BUILDERS']['MSVSProject']exceptKeyError:env['BUILDERS']['MSVSProject']=projectBuildertry:env['BUILDERS']['MSVSSolution']exceptKeyError:env['BUILDERS']['MSVSSolution']=solutionBuilderenv['MSVSPROJECTCOM']=projectActionenv['MSVSSOLUTIONCOM']=solutionActionifSCons.Script.call_stack:# XXX Need to find a way to abstract this; the build engine# shouldn't depend on anything in SCons.Script.env['MSVSSCONSCRIPT']=SCons.Script.call_stack[0].sconscriptelse:globaldefault_MSVS_SConscriptifdefault_MSVS_SConscriptisNone:default_MSVS_SConscript=env.File('SConstruct')env['MSVSSCONSCRIPT']=default_MSVS_SConscriptenv['MSVSSCONS']='"%s" -c "%s"'%(python_executable,getExecScriptMain(env))env['MSVSSCONSFLAGS']='-C "${MSVSSCONSCRIPT.dir.abspath}" -f ${MSVSSCONSCRIPT.name}'env['MSVSSCONSCOM']='$MSVSSCONS $MSVSSCONSFLAGS'env['MSVSBUILDCOM']='$MSVSSCONSCOM "$MSVSBUILDTARGET"'env['MSVSREBUILDCOM']='$MSVSSCONSCOM "$MSVSBUILDTARGET"'env['MSVSCLEANCOM']='$MSVSSCONSCOM -c "$MSVSBUILDTARGET"'# Set-up ms tools paths for default versionmsvc_setup_env_once(env)if'MSVS_VERSION'inenv:version_num,suite=msvs_parse_version(env['MSVS_VERSION'])else:(version_num,suite)=(7.0,None)# guess at a defaultif'MSVS'notinenv:env['MSVS']={}if(version_num<7.0):env['MSVS']['PROJECTSUFFIX']='.dsp'env['MSVS']['SOLUTIONSUFFIX']='.dsw'elif(version_num<10.0):env['MSVS']['PROJECTSUFFIX']='.vcproj'env['MSVS']['SOLUTIONSUFFIX']='.sln'else:env['MSVS']['PROJECTSUFFIX']='.vcxproj'env['MSVS']['SOLUTIONSUFFIX']='.sln'if(version_num>=10.0):env['MSVSENCODING']='utf-8'else:env['MSVSENCODING']='Windows-1252'env['GET_MSVSPROJECTSUFFIX']=GetMSVSProjectSuffixenv['GET_MSVSSOLUTIONSUFFIX']=GetMSVSSolutionSuffixenv['MSVSPROJECTSUFFIX']='${GET_MSVSPROJECTSUFFIX}'env['MSVSSOLUTIONSUFFIX']='${GET_MSVSSOLUTIONSUFFIX}'env['SCONS_HOME']=os.environ.get('SCONS_HOME')defexists(env):returnmsvc_exists()# Local Variables:# tab-width:4# indent-tabs-mode:nil# End:# vim: set expandtab tabstop=4 shiftwidth=4: