XQuilla/wintools/genproject.xq

436 lines
17 KiB
Text
Raw Normal View History

2020-02-17 21:12:51 +00:00
(:
* IMPORTANT: this file is shared (identical) between the BDB XML and XQilla
* projects. It should not be changed independently or in any way that makes
* it project-dependent.
*
* TBD:
* 1. Could drive the remaining variables, including Visual Studio
* variables, off of the xml file and provide the XML input
* file as an external variable to the query. E.g. some projects
* may want to parameterize the optimization and warnings levels
* 2. Generate AdditionalIncludeDirectories based on the <depends> elements for
* projects rather than just including "all"
:)
2020-02-17 21:17:06 +00:00
declare variable $projectFile external;
declare variable $outputPath external;
declare variable $projectDoc := doc($projectFile);
declare variable $sourcePath := $projectDoc/projects/variable[@name="sourcePath"];
2020-02-17 21:12:51 +00:00
(: Visual Studio Variables --relatively static :)
declare variable $debugOptLevel := "0"; (: VS -- no optimization :)
declare variable $releaseOptLevel := "2"; (: VS -- level 2 opt :)
declare variable $warnLevel := "3"; (: VS warning level :)
declare variable $appType := "1"; (: VS type 1 is a program :)
declare variable $dllType := "2"; (: VS type 2 is a DLL :)
declare variable $staticType := "4"; (: VS type 4 is a static lib :)
declare variable $debugInfo := "3"; (: VS debug information format :)
declare variable $libExtension := ".lib";
2020-02-17 21:13:50 +00:00
(: formatting :)
declare function local:indent($n)
{
concat("&#xa;",string-join(for $i in (1 to $n) return " ", ""))
};
2020-02-17 21:12:51 +00:00
(: is it a DLL, Application, or static library? :)
2020-02-17 21:19:08 +00:00
declare function local:configurationType($project, $config)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:19:08 +00:00
if ($project/type eq "library") then if (contains($config,"Static")) then
$staticType else $dllType
else if ($project/type eq "app") then $appType
else error(QName("", "xqilla"),"configurationType: unknown project type")
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:13:50 +00:00
(: "normalize" Windows file paths :)
declare function local:windowsPath($path) as xs:string
{
translate($path,"/","\\")
};
2020-02-17 21:12:51 +00:00
(: debug vs release :)
declare function local:isDebug($config) as xs:boolean
{
contains($config,"Debug")
};
declare function local:isRelease($config) as xs:boolean
{
contains($config,"Release")
};
(: machine-target-dependent link options :)
2020-02-17 21:13:50 +00:00
declare function local:addLinkOptions($project, $platform, $config)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
let $machine := if (contains($platform,"Win32")) then "/machine:x86"
else concat("/machine:",$platform)
let $opt := string-join(($machine,$project/options/link[contains(@config,$config)])," ")
return if (not($opt eq "")) then attribute{"AdditionalOptions"}{$opt}
2020-02-17 21:12:51 +00:00
else ()
};
declare function local:addDebugInformation($config)
{
attribute{"DebugInformationFormat"}{$debugInfo},
if (local:isDebug($config)) then attribute{"BasicRuntimeChecks"}{"3"} else ()
};
2020-02-17 21:19:08 +00:00
(: return "dll", "app", "static_lib", or "static_app" :)
declare function local:getProjectType($project, $config)
{
if ($project/type eq "library") then if (contains($config,"Static")) then "static_lib"
else "dll"
else if ($project/type eq "app") then if (contains($config,"Static")) then "static_app"
else "app"
else error(QName("", "dbxml"),"getProjectType: unknown project type")
};
2020-02-17 21:12:51 +00:00
declare function local:generateCompilerPreprocessorDefs($project, $config)
{
2020-02-17 21:19:08 +00:00
let $ptype := local:getProjectType($project,$config)
2020-02-17 21:17:06 +00:00
let $generic := $projectDoc/projects/preprocessor[@config="all" or contains($config,@config)]
2020-02-17 21:19:08 +00:00
let $genericType := $projectDoc/projects/preprocessor[@config=$ptype]
2020-02-17 21:13:50 +00:00
let $proj := $project/preprocessor[@config="all" or contains(@config,$config)]
2020-02-17 21:19:08 +00:00
let $projType := $project/preprocessor[@config=$ptype]
2020-02-17 21:12:51 +00:00
2020-02-17 21:19:08 +00:00
return string-join(($generic,$genericType,$proj,$projType),";")
2020-02-17 21:12:51 +00:00
};
(:
declare function local:generateResourcePreprocessorDefs($config)
{
let $dbg := if (local:isDebug($config)) then "_DEBUG" else "NDEBUG"
return concat($dbg,";_CRT_SECURE_NO_DEPRECATE=1")
};
:)
(: Optimization level :)
declare function local:optLevel($config)
{
if (local:isDebug($config)) then $debugOptLevel
else $releaseOptLevel
};
(: MDd is 3, MD is 2, MTd is 1, MT is 0 :)
declare function local:runtimeLibrary($config,$static as xs:boolean)
{
if (local:isDebug($config)) then
if ($static) then "1" else"3"
else
if ($static) then "0" else"2"
};
2020-02-17 21:13:50 +00:00
declare function local:getLibName($name, $config)
{
2020-02-17 21:17:06 +00:00
$projectDoc/projects/library[@name=$name]/libname[@config=$config]
2020-02-17 21:13:50 +00:00
};
2020-02-17 21:12:51 +00:00
declare function local:makeStaticOutputFile($project, $config)
{
attribute{"OutputFile"}
2020-02-17 21:13:50 +00:00
{concat("$(OutDir)/", local:getLibName($project/@name, $config),".lib")
2020-02-17 21:12:51 +00:00
}
};
declare function local:makeImportLibrary($project, $config)
{
2020-02-17 21:19:08 +00:00
if ($project/type eq "library" and not(contains($config,"Static"))) then
2020-02-17 21:13:50 +00:00
attribute{"ImportLibrary"}{concat("$(OutDir)/", local:getLibName($project/@name, $config),".lib")}
else ()
};
declare function local:makeModuleDefinition($project, $config)
{
if (not(empty($project/moddef))) then
attribute{"ModuleDefinitionFile"}{local:windowsPath(concat($sourcePath,$project/moddef/@file))}
2020-02-17 21:12:51 +00:00
else ()
};
declare function local:makeOutputPDBFile($project, $config)
{
attribute{"ProgramDatabaseFile"}
{
2020-02-17 21:19:08 +00:00
if ($project/type eq "library") then
2020-02-17 21:13:50 +00:00
concat("$(OutDir)/", local:getLibName($project/@name, $config),".pdb")
2020-02-17 21:19:08 +00:00
else
concat("$(OutDir)/",if (not(empty($project/@outputpdb))) then $project/@outputpdb else $project/@name,".pdb")
2020-02-17 21:12:51 +00:00
}
};
declare function local:makeOutputFile($project, $config)
{
attribute{"OutputFile"}
{
2020-02-17 21:19:08 +00:00
if ($project/type eq "library") then
2020-02-17 21:13:50 +00:00
concat("$(OutDir)/", local:getLibName($project/@name, $config), ".dll")
2020-02-17 21:12:51 +00:00
else
concat("$(OutDir)/",if (not(empty($project/@output))) then $project/@output else $project/@name,".exe")
}
};
(: The simple thing is to add all libraries for all projects :)
declare function local:addLibraryDependencies($project,$config)
{
attribute{"AdditionalDependencies"}{string-join(for $dep in $project/depends
2020-02-17 21:13:50 +00:00
return concat(local:getLibName($dep, $config),".lib")," ")}
};
2020-02-17 21:17:06 +00:00
declare function local:makeLibraryDirectory($lib,$platform,$config,$vsversion)
2020-02-17 21:13:50 +00:00
{
2020-02-17 21:19:08 +00:00
for $dir in $lib/platform[contains(@name,$platform) and contains(@vsver,$vsversion)]/config[$config=./@type]/libdir
2020-02-17 21:13:50 +00:00
return
if (not(empty($lib/libbase[@vsver=$vsversion]))) then
concat($lib/libbase[@vsver=$vsversion],"/", $dir)
else $dir
2020-02-17 21:12:51 +00:00
};
(: The simple thing is to add all libraries for all projects :)
2020-02-17 21:17:06 +00:00
declare function local:addLibraryDirectories($project,$platform,$config,$vsversion)
2020-02-17 21:12:51 +00:00
{
attribute{"AdditionalLibraryDirectories"}{string-join(for $dep in $project/depends
2020-02-17 21:17:06 +00:00
return local:makeLibraryDirectory($projectDoc/projects/library[@name=$dep],$platform,$config,$vsversion),";")}
2020-02-17 21:12:51 +00:00
};
(: The simple thing is to add all libraries for all projects :)
declare function local:addIncludeDirectories($project,$config)
{
2020-02-17 21:17:06 +00:00
let $incref := for $inc in $project/include[@type="ref"] return $projectDoc/projects/include[@name=$inc]
2020-02-17 21:12:51 +00:00
let $increl := $project/include[@type="rel"]
return
attribute{"AdditionalIncludeDirectories"}{string-join(($incref,$increl),",")}
};
(: look for project-specific tool, then a type-specific event :)
declare function local:generatePostBuildEvent($project, $config)
{
2020-02-17 21:13:50 +00:00
local:indent(6),<Tool>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{"VCPostBuildEventTool"}}
{if (not(empty($project/event[@name="postbuild"]))) then
(attribute{"CommandLine"}{$project/event[@name="postbuild"]/command[@config=$config]},
attribute{"Description"}{$project/event[@name="postbuild"]/description})
2020-02-17 21:17:06 +00:00
else let $ev := $projectDoc/projects/event[@name="postbuild" and @type=$project/type]
2020-02-17 21:12:51 +00:00
return if (not(empty($ev))) then
(attribute{"CommandLine"}{replace($ev/command[@config=$config],"@pname@",if (not(empty($project/@output))) then $project/@output else $project/@name)},
attribute{"Description"}{$ev/description})
else ()
}
</Tool>
};
declare function local:generateCustomBuildTool($project, $config)
{
2020-02-17 21:13:50 +00:00
local:indent(6),<Tool>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{"VCCustomBuildTool"}}
{if (not(empty($project/event[@name="custom"]))) then
(attribute{"CommandLine"}{$project/event[@name="custom"]/command[contains(@config,$config)]},
attribute{"Outputs"}{$project/event[@name="custom"]/output})
else ()}
</Tool>
};
(:
static build of libraries; at this time it does *not* add additional
dependent libraries. This means that applications need to include them
for the time being.
:)
2020-02-17 21:19:08 +00:00
declare function local:generateConfigLibrarian($project, $platform, $config)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
local:indent(6),<Tool>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{"VCLibrarianTool"}}
2020-02-17 21:19:08 +00:00
{local:addLinkOptions($project,$platform,$config)}
2020-02-17 21:12:51 +00:00
{local:makeStaticOutputFile($project,$config)}
</Tool>
};
2020-02-17 21:17:06 +00:00
declare function local:generateConfigLinkerAndMidl($project, $platform, $config, $vsversion)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
local:indent(6),<Tool>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{"VCLinkerTool"}}
2020-02-17 21:13:50 +00:00
{local:addLinkOptions($project, $platform, $config)}
2020-02-17 21:12:51 +00:00
{local:addLibraryDependencies($project,$config)}
2020-02-17 21:17:06 +00:00
{local:addLibraryDirectories($project,$platform,$config,$vsversion)}
2020-02-17 21:12:51 +00:00
{local:makeOutputFile($project, $config)}
{local:makeOutputPDBFile($project, $config)}
2020-02-17 21:19:08 +00:00
{if (local:isDebug($config)) then attribute{"LinkIncremental"}{"2"} else attribute{"LinkIncremental"}{"1"}}
2020-02-17 21:12:51 +00:00
{attribute{"GenerateDebugInformation"}{"TRUE"}}
{attribute{"SuppressStartupBanner"}{"TRUE"}}
2020-02-17 21:19:08 +00:00
{if (local:isDebug($config)) then attribute{"OptimizeReferences"}{"1"} else attribute{"OptimizeReferences"}{"2"}}
2020-02-17 21:12:51 +00:00
{local:makeImportLibrary($project,$config)}
2020-02-17 21:13:50 +00:00
{local:makeModuleDefinition($project,$config)}
{attribute{"TargetMachine"}{"0"}}
2020-02-17 21:12:51 +00:00
</Tool>
};
2020-02-17 21:17:06 +00:00
declare function local:generateConfigCompiler($project, $platform, $config, $static as xs:boolean, $vsversion)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
local:indent(6),<Tool>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{"VCCLCompilerTool"}}
{attribute{"Optimization"}{local:optLevel($config)}}
{attribute{"MinimalRebuild"}{"TRUE"}}
{if (local:isRelease($config)) then attribute{"InlineFunctionExpansion"}{"1"} else ()}
{local:addIncludeDirectories($project,$config)}
{attribute{"PreprocessorDefinitions"}{local:generateCompilerPreprocessorDefs($project, $config)}}
{attribute{"StringPooling"}{"TRUE"}}
{if (not(empty($project/options/rtti))) then attribute{"RuntimeTypeInfo"}{"TRUE"} else ()}
{attribute{"RuntimeLibrary"}{local:runtimeLibrary($config,$static)}}
{if (local:isRelease($config)) then attribute{"EnableFunctionLevelLinking"}{"TRUE"} else ()}
2020-02-17 21:13:50 +00:00
{attribute{"UsePrecompiledHeader"}{"0"}}
2020-02-17 21:12:51 +00:00
{if ($project/@name eq "dbxml") then attribute{"PrecompiledHeaderThrough"}{"DbXmlInternal.hpp"} else ()}
2020-02-17 21:19:08 +00:00
{attribute{"PrecompiledHeaderFile"}{concat("./$(IntDir)/",$project/@name,".pch")}}
{attribute{"AssemblerListingLocation"}{"./$(IntDir)/"}}
{attribute{"ObjectFile"}{"./$(IntDir)/"}}
2020-02-17 21:12:51 +00:00
{attribute{"WarningLevel"}{$warnLevel}}
2020-02-17 21:13:50 +00:00
{if (not($vsversion eq "8.00") and not($project/options/nowp64)) then attribute{"Detect64BitPortabilityProblems"}{"TRUE"} else ()}
2020-02-17 21:12:51 +00:00
{attribute{"SuppressStartupBanner"}{"TRUE"}}
{local:addDebugInformation($config)}
{attribute{"CompileAs"}{"0"}}
</Tool>
};
declare function local:generateConfigBoilerplate($config)
{
2020-02-17 21:13:50 +00:00
local:indent(6),<Tool Name="VCPreBuildEventTool"/>,
local:indent(6),<Tool Name="VCPreLinkEventTool"/>,
local:indent(6),<Tool Name="VCResourceCompilerTool"/>,
local:indent(6),<Tool Name="VCXMLDataGeneratorTool"/>,
local:indent(6),<Tool Name="VCManagedWrapperGeneratorTool"/>,
local:indent(6),<Tool Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:13:50 +00:00
(: use "platform/configuration" :)
2020-02-17 21:19:08 +00:00
declare function local:generateOutputDirectory($platform,$config,$vsversion)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:17:06 +00:00
let $outputBase := $projectDoc/projects/variable[@name=concat("outputBase.", $vsversion)]
2020-02-17 21:19:08 +00:00
return attribute{"OutputDirectory"}{local:windowsPath(concat($outputBase,"$(PlatformName)","/",$config))}
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:17:06 +00:00
declare function local:generateConfig($project, $platform, $config, $vsversion)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
local:indent(4),<Configuration>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{concat($config,"|",$platform)}}
2020-02-17 21:19:08 +00:00
{local:generateOutputDirectory($platform,$config,$vsversion)}
2020-02-17 21:12:51 +00:00
{attribute{"IntermediateDirectory"}{concat("./$(OutDir)/",$project/@name)}}
2020-02-17 21:19:08 +00:00
{attribute{"ConfigurationType"}{local:configurationType($project,$config)}}
2020-02-17 21:12:51 +00:00
{attribute{"UseOfMFC"}{"0"}}
{attribute{"ATLMinimizesCRunTimeLibraryUsage"}{"FALSE"}}
{attribute{"CharacterSet"}{"2"}}
{local:generateConfigBoilerplate($config)}
2020-02-17 21:17:06 +00:00
{local:generateConfigCompiler($project, $platform, $config,false(),$vsversion)}
{local:generateConfigLinkerAndMidl($project, $platform, $config, $vsversion)}
2020-02-17 21:12:51 +00:00
{local:generatePostBuildEvent($project,if (local:isDebug($config)) then "Debug" else "Release")}
{local:generateCustomBuildTool($project,if (local:isDebug($config)) then "Debug" else "Release")}
</Configuration>
};
2020-02-17 21:17:06 +00:00
declare function local:generateStaticConfig($project, $platform, $config, $vsversion)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
local:indent(4),<Configuration>
2020-02-17 21:12:51 +00:00
{attribute{"Name"}{concat($config,"|",$platform)}}
2020-02-17 21:19:08 +00:00
{local:generateOutputDirectory($platform,$config,$vsversion)}
2020-02-17 21:12:51 +00:00
{attribute{"IntermediateDirectory"}{concat("./$(OutDir)/",$project/@name)}}
2020-02-17 21:19:08 +00:00
{attribute{"ConfigurationType"}{local:configurationType($project,$config)}}
2020-02-17 21:12:51 +00:00
{attribute{"UseOfMFC"}{"0"}}
{attribute{"ATLMinimizesCRunTimeLibraryUsage"}{"FALSE"}}
{attribute{"CharacterSet"}{"2"}}
{local:generateConfigBoilerplate($config)}
2020-02-17 21:17:06 +00:00
{local:generateConfigCompiler($project, $platform, $config,true(),$vsversion)}
2020-02-17 21:12:51 +00:00
{if (contains($project/type,"lib")) then
2020-02-17 21:19:08 +00:00
local:generateConfigLibrarian($project, $platform, $config)
2020-02-17 21:12:51 +00:00
else
2020-02-17 21:17:06 +00:00
local:generateConfigLinkerAndMidl($project, $platform, $config, $vsversion)
2020-02-17 21:12:51 +00:00
}
</Configuration>
};
2020-02-17 21:17:06 +00:00
declare function local:generateRcFile($file, $vsversion)
2020-02-17 21:13:50 +00:00
{
local:indent(4),<File RelativePath="{local:windowsPath(concat($sourcePath,$file/@name))}">
{ for $platform in local:getPlatforms($vsversion) return
for $config in ("Debug","Release") return
(local:indent(6),<FileConfiguration Name="{concat($config,"|",$platform)}">
<Tool Name="VCResourceCompilerTool" PreprocessorDefinitions="{concat(if ($config="Debug") then "_DEBUG" else "NDEBUG",";","$(NoInherit))")}"/>
{local:indent(6)}</FileConfiguration>)
}
{local:indent(4)}</File>
};
2020-02-17 21:17:06 +00:00
declare function local:generateFilesNoFilter($project, $vsversion)
2020-02-17 21:12:51 +00:00
{
for $file in $project/files/file
2020-02-17 21:17:06 +00:00
return if (ends-with($file/@name,".rc")) then local:generateRcFile($file, $vsversion)
2020-02-17 21:13:50 +00:00
else (local:indent(4),<File RelativePath="{local:windowsPath(concat($sourcePath,$file/@name))}"/>)
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:17:06 +00:00
declare function local:generateFilesWithFilter($project,$filter,$vsversion)
2020-02-17 21:12:51 +00:00
{
for $file in $project/files/filter[@name=$filter]/file
2020-02-17 21:17:06 +00:00
return if (ends-with($file/@name,".rc")) then local:generateRcFile($file, $vsversion)
2020-02-17 21:13:50 +00:00
else (local:indent(6),<File RelativePath="{local:windowsPath(concat($sourcePath,$file/@name))}"/>)
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:17:06 +00:00
declare function local:generateFiles($project, $vsversion)
2020-02-17 21:12:51 +00:00
{
let $filters := $project/files/filter/@name
return if (empty($filters)) then
2020-02-17 21:17:06 +00:00
local:generateFilesNoFilter($project, $vsversion)
2020-02-17 21:12:51 +00:00
else
for $filter in $filters
2020-02-17 21:13:50 +00:00
return (local:indent(4),<Filter Name="{$filter}" Filter="">
2020-02-17 21:17:06 +00:00
{local:generateFilesWithFilter($project,$filter,$vsversion)}
2020-02-17 21:13:50 +00:00
{local:indent(4)}</Filter>)
};
declare function local:getGuid($project)
{
concat("{",$project/@guid,"}")
};
declare function local:getPlatforms($version)
{
if ($version eq "7.10") then ("Win32")
else ("Win32", "x64", "IA64")
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:17:06 +00:00
declare function local:getOutputName($project, $vsversion)
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:17:06 +00:00
let $vsname := if($vsversion = "7.10") then "VC7.1" else "VC8"
return
concat($outputPath, "/", $vsname, "/", $project/@name, ".vcproj")
2020-02-17 21:12:51 +00:00
};
2020-02-17 21:19:08 +00:00
declare function local:getConfigurations($project)
{
for $compType in ("Debug","Release") return
for $config in $project/configuration return concat($config,$compType)
};
for $vsversion in distinct-values($projectDoc//visualstudioversion)
2020-02-17 21:17:06 +00:00
for $project in $projectDoc/projects/project
2020-02-17 21:12:51 +00:00
let $static := contains($project/@name,"static")
2020-02-17 21:19:08 +00:00
let $proj := $project
2020-02-17 21:22:42 +00:00
return
if ($vsversion = "10.0") then ()
else (
2020-02-17 21:17:06 +00:00
put(<VisualStudioProject
2020-02-17 21:12:51 +00:00
ProjectType="Visual C++"
2020-02-17 21:13:50 +00:00
Version="{$vsversion}"
2020-02-17 21:12:51 +00:00
Name="{string($project/@name)}"
2020-02-17 21:13:50 +00:00
ProjectGUID="{local:getGuid($project)}">
{local:indent(2)}<Platforms>
{local:indent(4)}<Platform Name="Win32"/>
{local:indent(4)}<Platform Name="x64"/>
{local:indent(4)}<Platform Name="IA64"/>
{local:indent(2)}</Platforms>
{local:indent(2)}<Configurations>
2020-02-17 21:12:51 +00:00
{
2020-02-17 21:13:50 +00:00
for $platform in local:getPlatforms($vsversion) return
2020-02-17 21:19:08 +00:00
for $config in local:getConfigurations($proj) return if (contains($config, "Static")) then local:generateStaticConfig($project, $platform, $config, $vsversion) else local:generateConfig($project, $platform, $config, $vsversion)
2020-02-17 21:12:51 +00:00
}
2020-02-17 21:13:50 +00:00
{local:indent(2)}</Configurations>
{local:indent(2)}<References/>
{local:indent(2)}<Files>
2020-02-17 21:17:06 +00:00
{local:generateFiles($proj, $vsversion)}
2020-02-17 21:13:50 +00:00
{local:indent(2)}</Files>
{local:indent(2)}<Globals/>
2020-02-17 21:17:06 +00:00
{"&#xa;"}</VisualStudioProject>, local:getOutputName($project, $vsversion))
2020-02-17 21:22:42 +00:00
)