-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPBP_Projects.pbi
368 lines (332 loc) · 9.94 KB
/
PBP_Projects.pbi
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
; +--------------+
; | PBP_Projects |
; +--------------+
; | 2015.01.23 . Creation (PureBasic 5.31)
; | 2016.10.26 . Added "dll", "so", "dylib" recognition
; | 2017.05.08 . Multiple-include safe, added demo, path separator fixing,
; | added OS guessing based on icon file
; | 2019.05.24 . Added support for DPIAWARE compile flag (PB 5.70)
;-
CompilerIf (Not Defined(__PBP_Projects_Included, #PB_Constant))
#__PBP_Projects_Included = #True
CompilerIf (#PB_Compiler_IsMainFile)
EnableExplicit
CompilerEndIf
;- Procedures - PRIVATE
Procedure.i _PBP_IsValidProject(*Project)
Protected Result.i = #False
If (*Project And IsXML(*Project))
Protected *Node = MainXMLNode(*Project)
If (*Node)
If (GetXMLNodeName(*Node) = "project")
Result = #True
EndIf
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i _PBP_IsValidTarget(*Target)
Protected Result.i = #False
If (*Target)
If (XMLNodeType(*Target) = #PB_XML_Normal)
If (GetXMLNodeName(*Target) = "target")
Result = #True
EndIf
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i _PBP_GetTargetsNode(*Project)
Protected *Result = #Null
If (_PBP_IsValidProject(*Project))
*Result = ChildXMLNode(MainXMLNode(*Project))
While (*Result)
If (XMLNodeType(*Result) = #PB_XML_Normal)
If (GetXMLNodeName(*Result) = "section")
If (GetXMLAttribute(*Result, "name") = "targets")
Break
EndIf
EndIf
EndIf
*Result = NextXMLNode(*Result)
Wend
EndIf
ProcedureReturn (*Result)
EndProcedure
Procedure.s _PBP_GetTargetValue(*Target, Name.s, Attribute.s = "value")
Protected Result.s = ""
If (Name)
If (_PBP_IsValidTarget(*Target))
Protected *Node = XMLNodeFromPath(*Target, Name)
If (*Node)
Result = GetXMLAttribute(*Node, Attribute)
EndIf
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i _PBP_GetTargetOption(*Target, Name.s)
Protected Result.i = #False
If (Name)
If (_PBP_IsValidTarget(*Target))
Protected *Node = XMLNodeFromPath(*Target, "options")
If (*Node)
Result = Bool(GetXMLAttribute(*Node, Name) = "1")
EndIf
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.s _PBP_QuoteIfNeeded(Input.s)
If ((Input = "") Or (FindString(Input, " ")))
Input = #DQUOTE$ + Input + #DQUOTE$
EndIf
ProcedureReturn (Input)
EndProcedure
;-
;- Procedures - PUBLIC
Procedure.i PBP_LoadProject(File.s)
Protected XML.i = LoadXML(#PB_Any, File)
If (XML)
If (Not _PBP_IsValidProject(XML))
FreeXML(XML)
XML = #Null
EndIf
EndIf
ProcedureReturn (XML)
EndProcedure
Procedure.i PBP_FreeProject(*Project)
If (*Project And IsXML(*Project))
FreeXML(*Project)
EndIf
ProcedureReturn (#Null)
EndProcedure
Procedure.i PBP_CountTargets(*Project)
Protected Result.i = -1
Protected *Targets = _PBP_GetTargetsNode(*Project)
If (*Targets)
Result = 0
Protected *Child = ChildXMLNode(*Targets)
While (*Child)
If (XMLNodeType(*Child) = #PB_XML_Normal)
If (GetXMLNodeName(*Child) = "target")
Result + 1
EndIf
EndIf
*Child = NextXMLNode(*Child)
Wend
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i PBP_GetTarget(*Project, Index.i)
Protected *Result = #Null
If (Index >= 0)
Protected *Targets = _PBP_GetTargetsNode(*Project)
If (*Targets)
Protected Found.i = 0
Protected *Child = ChildXMLNode(*Targets)
While (*Child)
If (XMLNodeType(*Child) = #PB_XML_Normal)
If (GetXMLNodeName(*Child) = "target")
If (Found = Index)
*Result = *Child
Break
Else
Found + 1
EndIf
EndIf
EndIf
*Child = NextXMLNode(*Child)
Wend
EndIf
EndIf
ProcedureReturn (*Result)
EndProcedure
Procedure.s PBP_TargetName(*Target)
Protected Result.s = ""
If (_PBP_IsValidTarget(*Target))
Result = GetXMLAttribute(*Target, "name")
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.i PBP_TargetByName(*Project, Name.s)
Protected *Result = #Null
If (Name)
Protected n.i = PBP_CountTargets(*Project)
If (n > 0)
Protected i.i
For i = 0 To n -1
Protected *Target = PBP_GetTarget(*Project, i)
If (PBP_TargetName(*Target) = Name)
*Result = *Target
Break
EndIf
Next i
EndIf
EndIf
ProcedureReturn (*Result)
EndProcedure
Procedure.s PBP_TargetInputFile(*Target)
ProcedureReturn (_PBP_GetTargetValue(*Target, "inputfile"))
EndProcedure
Procedure.s PBP_TargetOutputFile(*Target)
ProcedureReturn (_PBP_GetTargetValue(*Target, "outputfile"))
EndProcedure
Procedure.i PBP_TargetCurrentOS(*Target)
Protected Result.i = #False
Protected OutputFile.s = PBP_TargetOutputFile(*Target)
If (OutputFile)
Select (LCase(GetExtensionPart(OutputFile)))
Case "exe", "dll"
Result = Bool(#PB_Compiler_OS = #PB_OS_Windows)
Case "app", "dylib"
Result = Bool(#PB_Compiler_OS = #PB_OS_MacOS)
Case "so"
Result = Bool(#PB_Compiler_OS = #PB_OS_Linux)
Default
Protected IconFile.s
Protected *IconNode = XMLNodeFromPath(*Target, "icon")
If (*IconNode)
IconFile = GetXMLNodeText(*IconNode)
EndIf
If (IconFile)
Select (LCase(GetExtensionPart(IconFile)))
Case "ico"
Result = Bool(#PB_Compiler_OS = #PB_OS_Windows)
Case "icns"
Result = Bool(#PB_Compiler_OS = #PB_OS_MacOS)
Default
Result = #True
EndSelect
Else
Result = #True
EndIf
EndSelect
EndIf
ProcedureReturn (Result)
EndProcedure
Procedure.s PBP_TargetBuildString(*Target, ProjectPath.s = "")
Protected Result.s = ""
If (_PBP_IsValidTarget(*Target))
Protected *Node
Protected Value.s
If (ProjectPath And (FileSize(ProjectPath) > 0))
ProjectPath = GetPathPart(ProjectPath)
EndIf
Value = _PBP_GetTargetValue(*Target, "inputfile")
If (Value)
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
ReplaceString(Value, "/", "\", #PB_String_InPlace)
CompilerEndIf
Result = _PBP_QuoteIfNeeded(ProjectPath + Value)
;
Value = _PBP_GetTargetValue(*Target, "outputfile")
If (Value)
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
ReplaceString(Value, "/", "\", #PB_String_InPlace)
CompilerEndIf
Result + " /EXE " + _PBP_QuoteIfNeeded(ProjectPath + Value)
EndIf
;
*Node = XMLNodeFromPath(*Target, "icon")
If (*Node)
Value = GetXMLNodeText(*Node)
If (Value)
CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
ReplaceString(Value, "/", "\", #PB_String_InPlace)
CompilerEndIf
If (GetXMLAttribute(*Node, "enable") = "1")
Result + " /ICON " + _PBP_QuoteIfNeeded(ProjectPath + Value)
EndIf
EndIf
EndIf
;
If (_PBP_GetTargetOption(*Target, "unicode"))
Result + " /UNICODE"
EndIf
If (_PBP_GetTargetOption(*Target, "thread"))
Result + " /THREAD"
EndIf
If (_PBP_GetTargetOption(*Target, "onerror"))
Result + " /LINENUMBERING"
EndIf
If (_PBP_GetTargetOption(*Target, "xpskin"))
Result + " /XP"
EndIf
If (_PBP_GetTargetOption(*Target, "dpiaware"))
Result + " /DPIAWARE"
EndIf
If (_PBP_GetTargetOption(*Target, "admin"))
Result + " /ADMINISTRATOR"
ElseIf (_PBP_GetTargetOption(*Target, "user"))
Result + " /USER"
EndIf
;
Value = _PBP_GetTargetValue(*Target, "subsystem")
If (Value)
Result + " /SUBSYSTEM " + _PBP_QuoteIfNeeded(Value)
EndIf
;
Select (_PBP_GetTargetValue(*Target, "format", "exe"))
Case "console"
Result + " /CONSOLE"
Case "dll"
Result + " /DLL"
Default
;
EndSelect
;
*Node = XMLNodeFromPath(*Target, "constants")
If (*Node)
*Node = ChildXMLNode(*Node)
While (*Node)
If (XMLNodeType(*Node) = #PB_XML_Normal)
If (GetXMLNodeName(*Node) = "constant")
If (GetXMLAttribute(*Node, "enable") = "1")
Value = GetXMLAttribute(*Node, "value")
Value = RemoveString(Value, " ")
Value = RemoveString(Value, "#")
Result + " /CONSTANT " + Value
EndIf
EndIf
EndIf
*Node = NextXMLNode(*Node)
Wend
EndIf
;
If (#True)
Result + " /QUIET"
EndIf
EndIf
EndIf
ProcedureReturn (Result)
EndProcedure
;-
;- Demo Program
CompilerIf (#PB_Compiler_IsMainFile)
DisableExplicit
ProjectFile.s = OpenFileRequester("Open PB Project", GetHomeDirectory(), "PureBasic Projects (*.pbp)|*.pbp", 0)
If (ProjectFile)
*Project = PBP_LoadProject(ProjectFile)
If (*Project)
NumTargets = PBP_CountTargets(*Project)
Debug ProjectFile
Debug "Targets: " + Str(NumTargets)
For i = 0 To NumTargets - 1
*Target = PBP_GetTarget(*Project, i)
Debug ""
Debug "[" + PBP_TargetName(*Target) + "]"
Debug "Targets this OS? Guess = " + Str(PBP_TargetCurrentOS(*Target))
Debug "Input = " + PBP_TargetInputFile(*Target)
Debug "Output = " + PBP_TargetOutputFile(*Target)
Debug "Compile params = " + PBP_TargetBuildString(*Target, ProjectFile)
Next i
PBP_FreeProject(*Project)
Else
Debug "Could not load project:"
Debug ProjectFile
EndIf
EndIf
CompilerEndIf
CompilerEndIf
;-