File: find_compilers_mac.dpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (662 lines) | stat: -rw-r--r-- 13,850 bytes parent folder | download | duplicates (7)
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
{target:mac}
{mac_console_app}
//
// AggPas 2.4 RM3 Helper utility application
// Milan Marusinec alias Milano (c) 2006 - 2008
//
program
 find_compilers_mac ;

uses
 SysUtils ,
 agg_basics ,
 file_utils_ ,
 Carbon ;
 
{$I agg_mode.inc }

type
 src_key = record
   key ,
   val : string[99 ];
 
  end;

const  
 key_max  = 99; 
 pool_max = 65536;
 make_max = 99;
 
 fpc_comp = '/usr/local/bin/ppcppc';
 fpc_libs = '-Fu"src;src/ctrl;src/platform/mac;src/util;src/svg;upi;expat-wrap"';
 fpc_incl = '-Fisrc';
 fpc_outd = '-FU_debug';
 fpc_frmw = '-k"-framework Carbon -framework QuickTime"';
 fpc_conf = '-Mdelphi -Tdarwin -Sg -Se3 -XX -Xs -B -v0i';
 fpc_capp = '-WC';
 fpc_gapp = '-WG';

var
 key_array : array[0..key_max - 1 ] of src_key;
 key_count ,
 key_lastx : unsigned;
 key_scanx : shortstring;
 
 pool_buff : pointer;
 pool_aloc ,
 pool_size : unsigned;
 
 make_array : array[0..make_max - 1 ] of string[99 ];
 make_count : unsigned;
 
{ WRPOOL }
procedure WrPool(str : shortstring; crlf : boolean = false );
begin
 if crlf then
  str:=str + #10;

 if pool_size + length(str ) < pool_aloc then
  begin
   System.move(
    str[1 ] ,
    pointer(ptrcomp(pool_buff ) + pool_size )^ ,
    length(str ) );

   inc(pool_size ,length(str ) );
  
  end;

end;

{ WRFILE }
function WrFile(destDir : FSRefPtr; name : shortstring ) : boolean;
var
 i : unsigned;

 ossState : OSStatus;
 ossError : OSErr;
 forkName ,
 destName : HFSUniStr255;
 dstFSRef : FSRef;
 forkRef  : SInt16;
 written  : ByteCount;
 catInfo  : FSCatalogInfo;
 
begin
 result:=false;
  
// Fill in Unicode name
 for i:=1 to length(name ) do
  destName.unicode[i - 1 ]:=byte(name[i ] );
  
 destName.length:=length(name );
 
// Write the script to file
 ossError:=FSCreateFileUnicode(destDir^ ,destName.length ,destName.unicode[0 ] ,kFSCatInfoNone ,NIL ,@dstFSRef ,NIL );

 if ossError = noErr then
  begin
   FSGetDataForkName(forkName ); 
 
   ossError:=FSOpenFork(dstFSRef ,forkName.length ,forkName.unicode[0 ] ,fsWrPerm ,forkRef );
  
   if ossError = noErr then
    begin
     ossError:=FSWriteFork(forkRef ,fsFromStart + noCacheBit ,0 ,pool_size ,pool_buff ,written );  

     FSCloseFork(forkRef );
    
     if (ossError = noErr ) and
        (pool_size = written ) then

     else
      exit;
 
    end
   else
    begin write('[FSOpenFork:' ,ossError ,'] ' ); exit; end;
  
  end
 else
  if ossError = dupFNErr then
  else
   begin write('[FSCreateFileUnicode:' ,ossError ,'] ' ); exit; end;
  
// Set The File permissions
 CatInfo.permissions[0 ]:=0;
 CatInfo.permissions[1 ]:=0;
 CatInfo.permissions[2 ]:=0;
 CatInfo.permissions[3 ]:=0;
 
 FSPermissionInfoPtr(@CatInfo.permissions ).mode:=999;

 ossError:=FSSetCatalogInfo(dstFSRef ,kFSCatInfoPermissions ,CatInfo );
 
// OK 
 result:=true; 
 
end; 
 
{ NEXTKEY } 
function NextKey(var val : shortstring ) : boolean;
begin
 result:=false;
 
 while key_lastx < key_count do
  begin
   inc(key_lastx );
   
   if cmp_str(key_array[key_lastx - 1 ].key ) = key_scanx then
    begin
     val   :=key_array[key_lastx - 1 ].val;
     result:=true;

     break;

    end;
  
  end;

end;
 
{ FIRSTKEY } 
function FirstKey(key : shortstring; var val : shortstring ) : boolean;
begin
 key_lastx:=0;
 key_scanx:=cmp_str(key );
 
 result:=NextKey(val );

end;
 
{ LOADKEYS } 
procedure LoadKeys(buff : char_ptr; size : SInt64 );
type
 e_scan = (expect_lp ,load_key ,load_val ,next_ln ,expect_crlf );
 
var 
 scan : e_scan;
 key  ,
 val  : shortstring;
 
procedure add_key;
begin
 if key_count < key_max then
  begin
   key_array[key_count ].key:=key;
   key_array[key_count ].val:=val;
  
   inc(key_count );
  
  end;

 key:='';
 val:='';

end; 

begin
 key_count:=0;
 
 scan:=expect_lp;
 key :='';
 val :='';

 while size > 0 do
  begin
   case scan of
    expect_lp :
     case buff^ of
      '{' :
       scan:=load_key;
  
      else
       break;
 
     end;
 
    load_key : 
     case buff^ of
      #13 ,#10 :
       break;
   
      ':' :
       scan:=load_val;
  
      '}' :
       begin
        add_key;

        scan:=next_ln;
   
       end;
   
      else
       key:=key + buff^; 
 
     end;
 
    load_val :
     case buff^ of
      #13 ,#10 :
       break;
   
      '}' :
       begin
        add_key;

        scan:=next_ln;
   
       end;
   
      else
       val:=val + buff^; 
 
     end;

    next_ln :
     case buff^ of
      #13 ,#10 :
       scan:=expect_crlf;
  
      ' ' :
      else
       break;
 
     end;
 
    expect_crlf :
     case buff^ of
      '{' :
       scan:=load_key;
   
      #13 ,#10 :
      else
       break;
 
     end;
 
   end;
   
   dec(size );
   inc(ptrcomp(buff ) );
   
  end;

end;

{ WRITECOMPILESCRIPT }
function WriteCompileScript(destDir : FSRefPtr; name ,ext : shortstring ) : boolean;
var
 cp ,fp ,fn ,fx : shortstring;
 
begin
 result:=false;
 
// Create the script in memory
 pool_size:=0;

 WrPool(fpc_comp + ' ' );
 WrPool(fpc_libs + ' ' );
 WrPool(fpc_incl + ' ' );
 WrPool(fpc_outd + ' ' );
 WrPool(fpc_frmw + ' ' );
 WrPool(fpc_conf + ' ' );

 if FirstKey('mac_console_app' ,cp ) then
  WrPool(fpc_capp + ' ' )
 else
  WrPool(fpc_gapp + ' ' );

 WrPool(name + ext ,true );
 
 if not FirstKey('mac_console_app' ,cp ) then
  begin
   WrPool('mkdir -p ' + name + '.app/Contents/MacOS' ,true );
   WrPool('mv -f ' + name + ' ' + name + '.app/Contents/MacOS/' + name ,true );
   
  end; 

 if FirstKey('mac_copy' ,cp ) then
  repeat
   spread_name(cp ,fp ,fn ,fx );

   if cmp_str(fx ) = cmp_str('.bmp' ) then
    WrPool('cp -f bmp/' + cp + ' ' + name + '.app/Contents/MacOS/' + cp ,true )
   else
    if cmp_str(fx ) = cmp_str('.svg' ) then
     WrPool('cp -f svg/' + cp + ' ' + name + '.app/Contents/MacOS/' + cp ,true );

  until not NextKey(cp );
  
// WriteFile
 name:='compile-' + name;
 
 if WrFile(destDir ,name ) then
  begin
   if make_count < make_max then
    begin
     make_array[make_count ]:=name;	

     inc(make_count );
 
    end; 

   result:=true; 
   
  end; 
 
end;
  
{ CREATECOMPILESCRIPT }
procedure CreateCompileScript(destDir : FSRefPtr; name ,ext : shortstring; inRef : FSRefPtr );
var
 loaded : boolean;

 ossError : OSStatus;
 forkName : HFSUniStr255;
 forkSize : SInt64;
 forkRef  : SInt16;
 forkBuff : pointer;
 forkLoad : ByteCount;
 
 target ,value : shortstring;

begin
 write(' ' ,name ,ext ,' ... ' );
 
// Open Source .DPR file
 FSGetDataForkName(forkName ); 
 
 ossError:=FSOpenFork(inRef^ ,forkName.length ,forkName.unicode[0 ] ,fsRdPerm ,forkRef );

 if ossError = noErr then
  begin
   loaded:=false;
   
  // Load DPR keys
   FSGetForkSize(forkRef ,forkSize );
    
   if (forkSize > 0 ) and
      agg_getmem(forkBuff ,forkSize ) then
    begin
     ossError:=FSReadFork(forkRef ,fsAtMark + noCacheMask ,0 ,forkSize ,forkBuff ,forkLoad );

     if (ossError = noErr ) and
        (forkSize = forkLoad ) then
      begin
       loaded:=true;
   
       LoadKeys(forkBuff ,forkSize );
  
      end;
    
     agg_freemem(forkBuff ,forkSize );
 
    end;

  // Close DPR
   FSCloseFork(forkRef );
   
  // Create compilation script
   if loaded then
    begin
     if FirstKey('skip' ,value ) then
      writeln('to be not included -> skipped' )
     else
      begin
       target:='mac';
   
       FirstKey('target' ,target );
  
       if cmp_str(target ) = cmp_str('mac' ) then
        if WriteCompileScript(destDir ,name ,ext ) then
         writeln('OK' )
        else 
         writeln('Failed to generate compile script !' )
       else
        writeln('different target (' ,target ,') -> skipped' );

      end;
  
    end
   else
    writeln('Failed to read the source file !' );
   
  end
 else
  writeln('Failed to open !' ); 

end;
 
{ PROCESSOBJECT } 
procedure ProcessObject(destDir : FSRefPtr; inCatInfo : FSCatalogInfoPtr; inRef : FSRefPtr; inSpec : FSSpecPtr );
var
 file_path ,file_name ,file_ext : shortstring;

begin
 if inCatInfo.nodeFlags and kFSNodeIsDirectoryMask = kFSNodeIsDirectoryMask then
 else
  begin
   spread_name(inSpec.name ,file_path ,file_name ,file_ext );

   if cmp_str(file_ext ) = cmp_str('.dpr' ) then
    CreateCompileScript(destDir ,file_name ,file_ext ,inRef );

  end;

end;
 
{ ITERATEFOLDER }  
function IterateFolder(var inFolder : FSRef ) : OSStatus;
var
 kRequestCountPerIteration : size_t;
 
 outStatus : OSStatus;
 
 kCatalogInfoBitmap : FSCatalogInfoBitmap;
 
 iterator : FSIterator;
 
 catalogInfoArray : FSCatalogInfoPtr;
 
 FSRefArray  : FSRefPtr;
 FSSpecArray : FSSpecPtr;
 
 actualCount : ItemCount;
 
 index : UInt32;
 
 changed : boolean;
  
begin
 kRequestCountPerIteration:=((4096 * 4 ) div sizeof(FSCatalogInfo ) );

// Get permissions and node flags and Finder info
//
// For maximum performance, specify in the catalog
// bitmap only the information you need to know
 kCatalogInfoBitmap:=kFSCatInfoNodeFlags or kFSCatInfoFinderInfo;
 
// On each iteration of the do-while loop, retrieve this
// number of catalog infos
//
// We use the number of FSCatalogInfos that will fit in
// exactly four VM pages (#113). This is a good balance
// between the iteration I/O overhead and the risk of
// incurring additional I/O from additional memory
// allocation

// Create an iterator
 outStatus:=FSOpenIterator(inFolder ,kFSIterateFlat ,iterator );
 
 if outStatus = noErr then
  begin
  // Allocate storage for the returned information
   agg_getmem(pointer(catalogInfoArray ) ,sizeof(FSCatalogInfo ) * kRequestCountPerIteration );
   agg_getmem(pointer(FSRefArray ) ,sizeof(FSRef ) * kRequestCountPerIteration );
   agg_getmem(pointer(FSSpecArray ) ,sizeof(FSSpec ) * kRequestCountPerIteration );
 
   if catalogInfoArray = NIL then
    outStatus:=memFullErr

   else
    begin
    // Request information about files in the given directory,
    // until we get a status code back from the File Manager
     repeat
      changed:=false;
 
      outStatus:=
       FSGetCatalogInfoBulk(
        iterator ,
        kRequestCountPerIteration ,
        actualCount ,
        changed ,
        kCatalogInfoBitmap , 
        catalogInfoArray ,
        FSRefArray ,
        FSSpecArray ,
        NIL );
 
     // Process all items received
      if (outStatus = noErr ) or
         (outStatus = errFSNoMoreItems ) then
       for index:=0 to actualCount - 1 do
        ProcessObject(
         @inFolder ,
         FSCatalogInfoPtr(
          ptrcomp(catalogInfoArray ) + 
          index * sizeof(FSCatalogInfo ) ) ,
         FSRefPtr(
          ptrcomp(FSRefarray ) +
          index * sizeof(FSRef ) ) , 
         FSSpecPtr(
          ptrcomp(FSSpecArray ) +
          index * sizeof(FSSpec ) ) );

     until outStatus <> noErr;
 
    // errFSNoMoreItems tells us we have successfully processed all
    // items in the directory -- not really an error
     if outStatus = errFSNoMoreItems then
      outStatus:=noErr;
 
    // Free the array memory
     agg_freemem(pointer(catalogInfoArray ) ,sizeof(FSCatalogInfo ) * kRequestCountPerIteration );
     agg_freemem(pointer(FSRefArray ) ,sizeof(FSRef ) * kRequestCountPerIteration );
     agg_freemem(pointer(FSSpecArray ) ,sizeof(FSSpec ) * kRequestCountPerIteration );
 
    end;

  end;
 
 FSCloseIterator(iterator );
 
 result:=outStatus;

end;

{ CREATEMAKEFILE }
procedure CreateMakeFile(destDir : FSRefPtr );
var
 i : unsigned;

begin
 pool_size:=0;
 
 i:=0;
 
 while i < make_count do
  begin
   WrPool('./' + make_array[i ] ,true ); 
  
   inc(i );
  
  end;
  
 WrFile(destDir ,'compile_make_all' ); 

end;
 
{ SCANDEMOS }
procedure ScanDemos;
var
 outStatus : OSStatus;
 folderRef : FSRef;
 fileSpecs : FSSpec;
 
begin
 outStatus:=FSMakeFSSpec(0 ,0 ,'' ,fileSpecs );  

 if outStatus = noErr then
  begin 
   outStatus:=FSpMakeFSRef(fileSpecs ,folderRef );

   if outStatus = noErr then
    begin
     outStatus:=IterateFolder(folderRef );
 
     writeln;
 
     if make_count > 0 then
      begin
       CreateMakeFile(@folderRef );
   
       writeln('SUCCESS: FPC compilation script files were created' );
       writeln('         for the AggPas demos listed above.' );
       writeln;
       writeln('         To compile the demos, run Terminal, change to the current' );
       writeln('         directory and type "./compile_make_all"' );
       writeln('         or "./compile-xxx", where "xxx" is the name of the demo.' );
  
      end
     else
      writeln('MESSAGE: No AggPas demo files were found in current folder !' );
  
    end
   else
    writeln('ERROR: Failed to create FSRef structure for the current folder !' );
  
  end
 else
  writeln('ERROR: Failed to search for files in the current folder !' ); 

 writeln;

end;

BEGIN
 writeln;
 writeln('*************************************************************' );
 writeln('* Welcome to the AggPas 2.4 RM3 vector graphics library.    *' );
 writeln('*************************************************************' ); 
 writeln('*                                                           *' );
 writeln('* This helper utility will generate the compilation script  *' );
 writeln('* files with current paths and options needed to compile    *' );
 writeln('* properly all the AggPas demos on your Mac.                *' );
 writeln('*                                                           *' );
 writeln('* Currently the Free Pascal compiler is supported.          *' );
 writeln('* (www.freepascal.org)                                      *' ); 
 writeln('*                                                           *' );
 writeln('*************************************************************' );
 writeln;
 writeln('[Press ENTER key to continue ...]' );
 writeln; 
 readln;
 
 if agg_getmem(pool_buff ,pool_max ) then
  begin
   pool_aloc :=pool_max;
   pool_size :=0;
   make_count:=0;
 
   ScanDemos;
 
   agg_freemem(pool_buff ,pool_aloc );
 
  end
 else
  writeln('ERROR: Not enough memory for the pool buffer !' ); 

END.