-
Notifications
You must be signed in to change notification settings - Fork 63
/
DN.Compiler.MSBuild.pas
183 lines (159 loc) · 5.58 KB
/
DN.Compiler.MSBuild.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.Compiler.MSBuild;
interface
uses
Windows,
Classes,
SysUtils,
DN.Types,
DN.Compiler.Intf,
DN.Compiler,
DN.Compiler.ValueOverrides.Intf,
DN.VariableResolver.Compiler.Factory;
type
TDNMSBuildCompiler = class(TDNCompiler)
private
FEmbarcaderoBinFolder: string;
FLogFile: string;
FVersion: TCompilerVersion;
function BuildCommandLine(const AProjectfile: string): string;
function GetMSBuildProperties: string;
function Execute(const ACommandLine: string): Cardinal;
function BuildParameterOverrideString(const ADefaultOverrides: ICompilerValueOverrides): string;
function GetParameterOverrides(AConfig: TDNCompilerConfig): string;
protected
function GetVersion: TCompilerVersion; override;
public
constructor Create(const AVariableResolverFactory: TDNCompilerVariableResolverFacory;
const AEmbarcaderoBinFolder: string);
function Compile(const AProjectFile: string): Boolean; override;
end;
implementation
uses
IOUtils,
ShellApi,
DN.Utils,
DN.VariableResolver.Intf,
DN.Compiler.ValueOverrides.Factory;
{ TDNMSBuildCompiler }
function TDNMSBuildCompiler.BuildCommandLine(
const AProjectfile: string): string;
begin
Result := 'call "' + FEmbarcaderoBinFolder + '\RSVars.bat"';
Result := Result + '& msbuild "' + AProjectfile + '" ' + GetMSBuildProperties() + ' > "' + FLogFile + '"';
Result := 'cmd.exe /c ' + Result;
end;
function TDNMSBuildCompiler.BuildParameterOverrideString(
const ADefaultOverrides: ICompilerValueOverrides): string;
const
CParameterOverrides = '/p:%s=%s';
CDebugInformation = 'DCC_DebugInformation';
var
LParameter, LValue: string;
begin
if not FParameterOverrides.ContainsKey(CDebugInformation) then
Result := Format(CParameterOverrides, [CDebugInformation, ADefaultOverrides.DebugInformation])
else
Result := '';
for LParameter in FParameterOverrides.Keys do
begin
LValue := FParameterOverrides[LParameter];
if LValue = '' then
LValue := '""';
Result := Result + ' ' + Format(CParameterOverrides, [LParameter, LValue]);
end;
end;
function TDNMSBuildCompiler.Compile(const AProjectFile: string): Boolean;
begin
Result := Execute(BuildCommandLine(AProjectFile)) = 0;
end;
constructor TDNMSBuildCompiler.Create(const AVariableResolverFactory: TDNCompilerVariableResolverFacory;
const AEmbarcaderoBinFolder: string);
begin
inherited Create(AVariableResolverFactory);
FEmbarcaderoBinFolder := AEmbarcaderoBinFolder;
FLogFile := TPath.GetTempFileName();
end;
function TDNMSBuildCompiler.Execute(const ACommandLine: string): Cardinal;
var
LExecInfo: TShellExecuteInfo;
begin
Result := MaxInt;
LExecInfo.cbSize := sizeof(TShellExecuteInfo);
LExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
LExecInfo.Wnd := 0;
LExecInfo.lpVerb := nil;
LExecInfo.lpFile := 'cmd.exe';
LExecInfo.lpParameters := PChar(ACommandLine);
LExecInfo.lpDirectory := nil;
LExecInfo.nShow := SW_HIDE;
LExecInfo.hInstApp := 0;
ShellExecuteEx(@LExecInfo);
WaitForSingleObject(LExecInfo.hProcess,INFINITE);
GetExitCodeProcess(LExecInfo.hProcess, Result);
CloseHandle(LExecInfo.hProcess);
if TFile.Exists(FLogFile) then
begin
Log.LoadFromFile(FLogFile);
TFile.Delete(FLogFile);
end;
end;
function TDNMSBuildCompiler.GetMSBuildProperties: string;
var
LResolver: IVariableResolver;
begin
Result := '/target:' + TDNCompilerTargetName[Target];
Result := Result + ' /p:config=' + TDNCompilerConfigName[Config];
Result := Result + ' /P:platform=' + TDNCompilerPlatformName[Platform];
LResolver := CreateResolver();
if DCUOutput <> '' then
Result := Result + ' /p:DCC_DcuOutput="' + LResolver.Resolve(ExcludeTrailingPathDelimiter(DCUOutput)) + '"';
if DCPOutput <> '' then
Result := Result + ' /p:DCC_DcpOutput="' + LResolver.Resolve(ExcludeTrailingPathDelimiter(DCPOutput)) + '"';
if EXEOutput <> '' then
Result := Result + ' /p:DCC_ExeOutput="' + LResolver.Resolve(ExcludeTrailingPathDelimiter(ExeOutput)) + '"';
if BPLOutput <> '' then
Result := Result + ' /p:DCC_BplOutput="' + LResolver.Resolve(ExcludeTrailingPathDelimiter(BPLOutput)) + '"';
Result := Result + ' ' + GetParameterOverrides(Config);
end;
function TDNMSBuildCompiler.GetParameterOverrides(
AConfig: TDNCompilerConfig): string;
begin
Result := BuildParameterOverrideString(TValueOverridesFactory.CreateOverride(AConfig, Version));
end;
function TDNMSBuildCompiler.GetVersion: TCompilerVersion;
var
LPos: Integer;
LValue: string;
LSettings: TFormatSettings;
const
CWin32Compiler = 'dcc32.exe';
CCommandLine = 'cmd.exe /c ""%s" --version > "%s""';
begin
if FVersion = 0 then
begin
FVersion := -1;
if Execute(Format(CCommandLine, [TPath.Combine(FEmbarcaderoBinFolder, CWin32Compiler), FLogFile])) = 0 then
begin
if (Log.Count > 0) then
begin
LPos := Pos(') ', Log[0]);
if LPos > 1 then
begin
LValue := Copy(Log[0], LPos + 2, Length(Log[0]));
LSettings := TFormatSettings.Create();
LSettings.DecimalSeparator := '.';
FVersion := StrToFloatDef(LValue, -1, LSettings);
end;
end;
end;
end;
Result := FVersion;
end;
end.