| 1 |
(* gm2lcc.mod generates the link command from a list of modules. |
| 2 |
|
| 3 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GNU Modula-2; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. *) |
| 21 |
|
| 22 |
MODULE gm2lcc ; |
| 23 |
|
| 24 |
(* |
| 25 |
Author : Gaius Mulley |
| 26 |
Title : gm2lcc |
| 27 |
Date : Fri Jul 24 11:45:08 BST 1992 |
| 28 |
Description: generates the link command from a list of modules. |
| 29 |
*) |
| 30 |
|
| 31 |
FROM libc IMPORT system, exit ; |
| 32 |
FROM SYSTEM IMPORT ADR ; |
| 33 |
FROM NameKey IMPORT Name, MakeKey, WriteKey, GetKey ; |
| 34 |
FROM M2Search IMPORT FindSourceFile, PrependSearchPath ; |
| 35 |
FROM M2FileName IMPORT CalculateFileName ; |
| 36 |
FROM SArgs IMPORT GetArg ; |
| 37 |
FROM StrLib IMPORT StrEqual, StrLen, StrCopy, StrConCat, StrRemoveWhitePrefix, IsSubString ; |
| 38 |
FROM FIO IMPORT File, StdIn, StdErr, StdOut, Close, IsNoError, EOF, WriteString, WriteLine ; |
| 39 |
FROM SFIO IMPORT OpenToRead, WriteS, ReadS ; |
| 40 |
FROM ASCII IMPORT nul ; |
| 41 |
FROM M2FileName IMPORT ExtractExtension ; |
| 42 |
FROM DynamicStrings IMPORT String, InitString, KillString, ConCat, ConCatChar, Length, Slice, Equal, EqualArray, RemoveWhitePrefix, RemoveWhitePostfix, RemoveComment, string, Mark, InitStringChar, Dup, Mult, Assign, char ; |
| 43 |
FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2 ; |
| 44 |
FROM M2Printf IMPORT fprintf0, fprintf1, fprintf2, fprintf3, fprintf4 ; |
| 45 |
FROM ObjectFiles IMPORT FileObjects, InitFileObject, KillFileObject, |
| 46 |
RegisterModuleObject, IsRegistered ; |
| 47 |
FROM Indexing IMPORT Index, InitIndex, GetIndice, |
| 48 |
HighIndice, LowIndice, IncludeIndiceIntoIndex, |
| 49 |
ForeachIndiceInIndexDo ; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
CONST |
| 54 |
Comment = '#' ; (* Comment leader. *) |
| 55 |
MaxSpaces = 20 ; (* Maximum spaces after a module *) |
| 56 |
(* name. *) |
| 57 |
Debugging = FALSE ; |
| 58 |
|
| 59 |
VAR |
| 60 |
DebugFound : BOOLEAN ; |
| 61 |
CheckFound : BOOLEAN ; |
| 62 |
VerboseFound : BOOLEAN ; |
| 63 |
ProfileFound : BOOLEAN ; |
| 64 |
LibrariesFound: BOOLEAN ; |
| 65 |
TargetFound : BOOLEAN ; |
| 66 |
ExecCommand : BOOLEAN ; (* should we execute the final cmd *) |
| 67 |
UseAr : BOOLEAN ; (* use 'ar' and create archive *) |
| 68 |
UseRanlib : BOOLEAN ; (* use 'ranlib' to index archive *) |
| 69 |
IgnoreMain : BOOLEAN ; (* ignore main module when linking *) |
| 70 |
UseLibtool : BOOLEAN ; (* use libtool and suffixes? *) |
| 71 |
Shared : BOOLEAN ; (* is a shared library required? *) |
| 72 |
BOption, (* the full -B option and directory. *) |
| 73 |
FOptions, |
| 74 |
CompilerDir, (* contains the directory after -B. *) |
| 75 |
RanlibProgram, |
| 76 |
ArProgram, |
| 77 |
Archives, |
| 78 |
Path, |
| 79 |
StartupFile, |
| 80 |
Libraries, |
| 81 |
MainModule, |
| 82 |
MainObject, |
| 83 |
Command, |
| 84 |
Target : String ; |
| 85 |
CmdLine, |
| 86 |
objects : FileObjects ; |
| 87 |
CmdLineObjects: Index ; |
| 88 |
fi, fo : File ; (* the input and output files *) |
| 89 |
|
| 90 |
|
| 91 |
(* |
| 92 |
FlushCommand - flush the command to the output file, |
| 93 |
or execute the command. |
| 94 |
*) |
| 95 |
|
| 96 |
PROCEDURE FlushCommand () : INTEGER ; |
| 97 |
BEGIN |
| 98 |
IF ExecCommand |
| 99 |
THEN |
| 100 |
IF VerboseFound |
| 101 |
THEN |
| 102 |
Command := WriteS (StdOut, Command) ; |
| 103 |
fprintf0 (StdOut, '\n') |
| 104 |
END ; |
| 105 |
RETURN system (string (Command)) |
| 106 |
ELSE |
| 107 |
Command := WriteS (fo, Command) |
| 108 |
END ; |
| 109 |
RETURN 0 |
| 110 |
END FlushCommand ; |
| 111 |
|
| 112 |
|
| 113 |
(* |
| 114 |
GenerateLinkCommand - generate the appropriate linkage command |
| 115 |
with the correct options. |
| 116 |
*) |
| 117 |
|
| 118 |
PROCEDURE GenerateLinkCommand ; |
| 119 |
BEGIN |
| 120 |
IF UseAr |
| 121 |
THEN |
| 122 |
Command := ConCat (ArProgram, InitString (' rc ')) ; |
| 123 |
IF TargetFound |
| 124 |
THEN |
| 125 |
Command := ConCat (Command, Target) ; |
| 126 |
Command := ConCatChar (Command, ' ') |
| 127 |
ELSE |
| 128 |
WriteString (StdErr, 'need target with ar') ; WriteLine (StdErr) ; Close (StdErr) ; |
| 129 |
exit (1) |
| 130 |
END |
| 131 |
ELSIF UseLibtool |
| 132 |
THEN |
| 133 |
Command := InitString ('libtool --tag=CC --mode=link gcc ') ; |
| 134 |
IF BOption # NIL |
| 135 |
THEN |
| 136 |
Command := ConCat (Command, Dup (BOption)) ; |
| 137 |
Command := ConCatChar (Command, ' ') |
| 138 |
END ; |
| 139 |
IF DebugFound |
| 140 |
THEN |
| 141 |
Command := ConCat (Command, Mark (InitString ('-g '))) |
| 142 |
END ; |
| 143 |
IF ProfileFound |
| 144 |
THEN |
| 145 |
Command := ConCat(Command, Mark(InitString ('-p '))) |
| 146 |
END ; |
| 147 |
Command := ConCat (Command, FOptions) ; |
| 148 |
IF Shared |
| 149 |
THEN |
| 150 |
Command := ConCat (Command, Mark (InitString ('-shared '))) |
| 151 |
END ; |
| 152 |
IF TargetFound |
| 153 |
THEN |
| 154 |
Command := ConCat (Command, Mark (InitString ('-o '))) ; |
| 155 |
Command := ConCat (Command, Target) ; |
| 156 |
Command := ConCatChar (Command, ' ') |
| 157 |
END ; |
| 158 |
IF ProfileFound |
| 159 |
THEN |
| 160 |
Command := ConCat (Command, Mark (InitString ('-lgmon '))) |
| 161 |
END |
| 162 |
END |
| 163 |
END GenerateLinkCommand ; |
| 164 |
|
| 165 |
|
| 166 |
(* |
| 167 |
GenerateRanlibCommand - generate the appropriate ranlib command. |
| 168 |
*) |
| 169 |
|
| 170 |
PROCEDURE GenerateRanlibCommand ; |
| 171 |
BEGIN |
| 172 |
Command := ConCat (RanlibProgram, Mark (InitStringChar (' '))) ; |
| 173 |
IF TargetFound |
| 174 |
THEN |
| 175 |
Command := ConCat (Command, Target) ; |
| 176 |
Command := ConCatChar (Command, ' ') |
| 177 |
ELSE |
| 178 |
WriteString (StdErr, 'need target with ranlib') ; WriteLine (StdErr) ; Close (StdErr) ; |
| 179 |
exit (1) |
| 180 |
END |
| 181 |
END GenerateRanlibCommand ; |
| 182 |
|
| 183 |
|
| 184 |
(* |
| 185 |
RemoveLinkOnly - removes the <onlylink> prefix, if present. This will occur |
| 186 |
for a definition for "C" module where there is no |
| 187 |
module constructor/destructor (_init and _finish pairs). |
| 188 |
Otherwise, s, is returned. |
| 189 |
*) |
| 190 |
|
| 191 |
PROCEDURE RemoveLinkOnly (s: String) : String ; |
| 192 |
VAR |
| 193 |
t: String ; |
| 194 |
BEGIN |
| 195 |
t := InitString ('<onlylink>') ; |
| 196 |
IF Equal (Mark (Slice (s, 0, Length (t)-1)), t) |
| 197 |
THEN |
| 198 |
RETURN RemoveWhitePrefix (Slice (Mark (s), Length (t), 0)) |
| 199 |
ELSE |
| 200 |
RETURN s |
| 201 |
END |
| 202 |
END RemoveLinkOnly ; |
| 203 |
|
| 204 |
|
| 205 |
(* |
| 206 |
ConCatStartupFile - check to see if the startup object file has not been added |
| 207 |
to the command line and if so add it. |
| 208 |
*) |
| 209 |
|
| 210 |
PROCEDURE ConCatStartupFile ; |
| 211 |
BEGIN |
| 212 |
IF RegisterModuleObject (objects, StartupFile) |
| 213 |
THEN |
| 214 |
IF UseLibtool |
| 215 |
THEN |
| 216 |
Command := ConCat (Command, Mark (Sprintf1 (Mark (InitString ('%s.lo')), |
| 217 |
StartupFile))) |
| 218 |
ELSE |
| 219 |
Command := ConCat (Command, Mark (Sprintf1 (Mark (InitString ('%s.o')), |
| 220 |
StartupFile))) |
| 221 |
END |
| 222 |
END |
| 223 |
END ConCatStartupFile ; |
| 224 |
|
| 225 |
|
| 226 |
(* |
| 227 |
GenObjectSuffix - |
| 228 |
*) |
| 229 |
|
| 230 |
PROCEDURE GenObjectSuffix () : String ; |
| 231 |
BEGIN |
| 232 |
IF UseLibtool |
| 233 |
THEN |
| 234 |
RETURN InitString ('lo') |
| 235 |
ELSE |
| 236 |
RETURN InitString ('o') |
| 237 |
END |
| 238 |
END GenObjectSuffix ; |
| 239 |
|
| 240 |
|
| 241 |
(* |
| 242 |
GenArchiveSuffix - |
| 243 |
*) |
| 244 |
|
| 245 |
PROCEDURE GenArchiveSuffix () : String ; |
| 246 |
BEGIN |
| 247 |
IF UseLibtool |
| 248 |
THEN |
| 249 |
RETURN InitString ('la') |
| 250 |
ELSE |
| 251 |
RETURN InitString ('a') |
| 252 |
END |
| 253 |
END GenArchiveSuffix ; |
| 254 |
|
| 255 |
|
| 256 |
(* |
| 257 |
LookupObjectFile - attempts to lookup the location of file name.extension |
| 258 |
from using the -fobject-path path. NIL is retured if |
| 259 |
the object file is not found. extension will be |
| 260 |
marked and deleted. |
| 261 |
*) |
| 262 |
|
| 263 |
PROCEDURE LookupObjectFile (name, extension: String) : String ; |
| 264 |
VAR |
| 265 |
location, |
| 266 |
filename: String ; |
| 267 |
BEGIN |
| 268 |
filename := CalculateFileName (name, Mark (extension)) ; |
| 269 |
IF FindSourceFile (filename, location) |
| 270 |
THEN |
| 271 |
RETURN location |
| 272 |
ELSE |
| 273 |
RETURN NIL |
| 274 |
END |
| 275 |
END LookupObjectFile ; |
| 276 |
|
| 277 |
|
| 278 |
(* |
| 279 |
ConCatModuleObject - this object will be associated with a module, therefore |
| 280 |
we remember it and make sure that it is not duplicated on the |
| 281 |
command line by the user. |
| 282 |
*) |
| 283 |
|
| 284 |
PROCEDURE ConCatModuleObject (module: String) ; |
| 285 |
VAR |
| 286 |
location: String ; |
| 287 |
BEGIN |
| 288 |
location := LookupObjectFile (module, GenObjectSuffix ()) ; |
| 289 |
IF location = NIL |
| 290 |
THEN |
| 291 |
location := LookupObjectFile (module, GenArchiveSuffix ()) ; |
| 292 |
IF location # NIL |
| 293 |
THEN |
| 294 |
Archives := ConCatChar (ConCat (Archives, location), ' ') |
| 295 |
END |
| 296 |
ELSE |
| 297 |
IF RegisterModuleObject (objects, location) |
| 298 |
THEN |
| 299 |
Command := ConCat (ConCatChar (Command, ' '), location) |
| 300 |
END |
| 301 |
END |
| 302 |
END ConCatModuleObject ; |
| 303 |
|
| 304 |
|
| 305 |
(* |
| 306 |
FindModulesInFileList - |
| 307 |
*) |
| 308 |
|
| 309 |
PROCEDURE FindModulesInFileList ; |
| 310 |
VAR |
| 311 |
text: String ; |
| 312 |
BEGIN |
| 313 |
REPEAT |
| 314 |
text := RemoveComment (RemoveWhitePrefix( ReadS (fi)), Comment) ; |
| 315 |
IF (NOT EqualArray (text, '')) AND (NOT (IgnoreMain AND Equal (text, MainModule))) |
| 316 |
THEN |
| 317 |
text := RemoveLinkOnly (text) ; |
| 318 |
ConCatModuleObject (text) |
| 319 |
END |
| 320 |
UNTIL EOF (fi) ; |
| 321 |
IF (NOT EqualArray (MainObject, "")) AND RegisterModuleObject (objects, MainObject) |
| 322 |
THEN |
| 323 |
Command := ConCat (ConCatChar (Command, ' '), MainObject) |
| 324 |
END |
| 325 |
END FindModulesInFileList ; |
| 326 |
|
| 327 |
|
| 328 |
(* |
| 329 |
CollectObjects - |
| 330 |
*) |
| 331 |
|
| 332 |
PROCEDURE CollectObjects (Command: String) : String ; |
| 333 |
VAR |
| 334 |
i, h: CARDINAL ; |
| 335 |
name: String ; |
| 336 |
BEGIN |
| 337 |
i := 1 ; |
| 338 |
h := HighIndice (CmdLineObjects) ; |
| 339 |
WHILE i <= h DO |
| 340 |
name := GetIndice (CmdLineObjects, i) ; |
| 341 |
IF NOT IsRegistered (objects, name) |
| 342 |
THEN |
| 343 |
Command := ConCat (ConCatChar (Command, ' '), Dup (name)) |
| 344 |
END ; |
| 345 |
INC (i) |
| 346 |
END ; |
| 347 |
IF Debugging |
| 348 |
THEN |
| 349 |
fprintf1 (StdErr, "objects on command line: %s\n", Command) |
| 350 |
END ; |
| 351 |
RETURN Command |
| 352 |
END CollectObjects ; |
| 353 |
|
| 354 |
|
| 355 |
(* |
| 356 |
CollectArchives - |
| 357 |
*) |
| 358 |
|
| 359 |
PROCEDURE CollectArchives (Command: String) : String ; |
| 360 |
BEGIN |
| 361 |
IF LibrariesFound |
| 362 |
THEN |
| 363 |
Command := ConCat (ConCatChar (Command, ' '), Libraries) |
| 364 |
END ; |
| 365 |
RETURN Command |
| 366 |
END CollectArchives ; |
| 367 |
|
| 368 |
|
| 369 |
(* |
| 370 |
AddProgramModule - add the program module to the Command string, providing |
| 371 |
that the user did not specify it on the command line. |
| 372 |
*) |
| 373 |
|
| 374 |
PROCEDURE AddProgramModule (Command: String) : String ; |
| 375 |
BEGIN |
| 376 |
IF Debugging |
| 377 |
THEN |
| 378 |
fprintf1 (StdErr, "mainobject: %s\n", MainObject) |
| 379 |
END ; |
| 380 |
IF (NOT EqualArray (MainObject, "")) AND RegisterModuleObject (objects, MainObject) |
| 381 |
THEN |
| 382 |
IF Debugging |
| 383 |
THEN |
| 384 |
fprintf1 (StdErr, "first time: %s\n", MainObject) |
| 385 |
END ; |
| 386 |
Command := ConCat (ConCatChar (Command, ' '), MainObject) |
| 387 |
ELSE |
| 388 |
IF Debugging |
| 389 |
THEN |
| 390 |
fprintf0 (StdErr, "(ignored)\n") |
| 391 |
END |
| 392 |
END ; |
| 393 |
RETURN Command |
| 394 |
END AddProgramModule ; |
| 395 |
|
| 396 |
|
| 397 |
(* |
| 398 |
GenCC - writes out the linkage command for the C compiler. |
| 399 |
*) |
| 400 |
|
| 401 |
PROCEDURE GenCC ; |
| 402 |
VAR |
| 403 |
Error: INTEGER ; |
| 404 |
BEGIN |
| 405 |
GenerateLinkCommand ; |
| 406 |
ConCatStartupFile ; |
| 407 |
FindModulesInFileList ; |
| 408 |
Command := AddProgramModule (Command) ; |
| 409 |
Command := ConCat (Command, Archives) ; |
| 410 |
Command := CollectObjects (Command) ; |
| 411 |
Command := CollectArchives (Command) ; |
| 412 |
Error := FlushCommand () ; |
| 413 |
IF Error=0 |
| 414 |
THEN |
| 415 |
IF UseRanlib |
| 416 |
THEN |
| 417 |
GenerateRanlibCommand ; |
| 418 |
Error := FlushCommand () ; |
| 419 |
IF Error#0 |
| 420 |
THEN |
| 421 |
fprintf1 (StdErr, 'ranlib failed with exit code %d\n', Error) ; |
| 422 |
Close (StdErr) ; |
| 423 |
exit (Error) |
| 424 |
END |
| 425 |
END |
| 426 |
ELSE |
| 427 |
fprintf1 (StdErr, 'ar failed with exit code %d\n', Error) ; |
| 428 |
Close (StdErr) ; |
| 429 |
exit (Error) |
| 430 |
END |
| 431 |
END GenCC ; |
| 432 |
|
| 433 |
|
| 434 |
(* |
| 435 |
WriteModuleName - displays a module name, ModuleName, with formatted spaces |
| 436 |
after the string. |
| 437 |
*) |
| 438 |
|
| 439 |
(* |
| 440 |
PROCEDURE WriteModuleName (ModuleName: String) ; |
| 441 |
BEGIN |
| 442 |
ModuleName := WriteS (fo, ModuleName) ; |
| 443 |
IF KillString (WriteS (fo, Mark (Mult (Mark (InitString (' ')), MaxSpaces-Length(ModuleName))))) = NIL |
| 444 |
THEN |
| 445 |
END |
| 446 |
END WriteModuleName ; |
| 447 |
*) |
| 448 |
|
| 449 |
|
| 450 |
(* |
| 451 |
CheckCC - checks to see whether all the object files can be found |
| 452 |
for each module. |
| 453 |
*) |
| 454 |
|
| 455 |
PROCEDURE CheckCC ; |
| 456 |
VAR |
| 457 |
s, t, u: String ; |
| 458 |
Error : INTEGER ; |
| 459 |
BEGIN |
| 460 |
Error := 0 ; |
| 461 |
REPEAT |
| 462 |
s := RemoveComment (RemoveWhitePrefix (ReadS (fi)), Comment) ; |
| 463 |
IF NOT EqualArray (s, '') |
| 464 |
THEN |
| 465 |
s := RemoveLinkOnly (s) ; |
| 466 |
t := Dup (s) ; |
| 467 |
t := CalculateFileName (s, Mark (GenObjectSuffix ())) ; |
| 468 |
IF FindSourceFile (t, u) |
| 469 |
THEN |
| 470 |
IF KillString (WriteS (fo, Mark (Sprintf2 (Mark (InitString ('%-20s : %s\n')), t, u)))) = NIL |
| 471 |
THEN |
| 472 |
END ; |
| 473 |
u := KillString (u) |
| 474 |
ELSE |
| 475 |
t := KillString (t) ; |
| 476 |
(* try finding .a archive *) |
| 477 |
t := CalculateFileName (s, Mark (GenArchiveSuffix ())) ; |
| 478 |
IF FindSourceFile (t, u) |
| 479 |
THEN |
| 480 |
IF KillString (WriteS (fo, Mark (Sprintf2 (Mark (InitString ('%-20s : %s\n')), t, u)))) = NIL |
| 481 |
THEN |
| 482 |
END ; |
| 483 |
u := KillString (u) |
| 484 |
ELSE |
| 485 |
IF KillString (WriteS (fo, Mark (Sprintf1 (InitString ('%-20s : distinct object or archive not found\n'), t)))) = NIL |
| 486 |
THEN |
| 487 |
END ; |
| 488 |
Error := 1 |
| 489 |
END |
| 490 |
END |
| 491 |
END |
| 492 |
UNTIL EOF (fi) ; |
| 493 |
Close (fo) ; |
| 494 |
exit (Error) |
| 495 |
END CheckCC ; |
| 496 |
|
| 497 |
|
| 498 |
(* |
| 499 |
ProcessTarget - copies the specified target file into Target |
| 500 |
and sets the boolean TargetFound. |
| 501 |
*) |
| 502 |
|
| 503 |
PROCEDURE ProcessTarget (i: CARDINAL) ; |
| 504 |
BEGIN |
| 505 |
IF NOT GetArg (Target, i) |
| 506 |
THEN |
| 507 |
fprintf0 (StdErr, 'cannot get target argument after -o\n') ; |
| 508 |
Close (StdErr) ; |
| 509 |
exit (1) |
| 510 |
END ; |
| 511 |
TargetFound := TRUE |
| 512 |
END ProcessTarget ; |
| 513 |
|
| 514 |
|
| 515 |
(* |
| 516 |
StripModuleExtension - returns a String without an extension from, s. |
| 517 |
It only considers '.obj', '.o' and '.lo' as |
| 518 |
extensions. |
| 519 |
*) |
| 520 |
|
| 521 |
PROCEDURE StripModuleExtension (s: String) : String ; |
| 522 |
VAR |
| 523 |
t: String ; |
| 524 |
BEGIN |
| 525 |
t := ExtractExtension (s, Mark (InitString ('.lo'))) ; |
| 526 |
IF s=t |
| 527 |
THEN |
| 528 |
t := ExtractExtension (s, Mark (InitString ('.obj'))) ; |
| 529 |
IF s=t |
| 530 |
THEN |
| 531 |
RETURN ExtractExtension (s, Mark(InitString('.o'))) |
| 532 |
END |
| 533 |
END ; |
| 534 |
RETURN t |
| 535 |
END StripModuleExtension ; |
| 536 |
|
| 537 |
|
| 538 |
(* |
| 539 |
ProcessStartupFile - copies the specified startup file name into StartupFile. |
| 540 |
*) |
| 541 |
|
| 542 |
PROCEDURE ProcessStartupFile (i: CARDINAL) ; |
| 543 |
BEGIN |
| 544 |
IF GetArg (StartupFile, i) |
| 545 |
THEN |
| 546 |
StartupFile := StripModuleExtension (StartupFile) |
| 547 |
ELSE |
| 548 |
fprintf0 (StdErr, 'cannot get startup argument after --startup\n') ; |
| 549 |
Close (StdErr) ; |
| 550 |
exit (1) |
| 551 |
END |
| 552 |
END ProcessStartupFile ; |
| 553 |
|
| 554 |
|
| 555 |
(* |
| 556 |
IsALibrary - returns TRUE if, a, is a library. If TRUE we add it to the |
| 557 |
Libraries string. |
| 558 |
*) |
| 559 |
|
| 560 |
PROCEDURE IsALibrary (s: String) : BOOLEAN ; |
| 561 |
BEGIN |
| 562 |
IF EqualArray (Mark (Slice (s, 0, 2)), '-l') |
| 563 |
THEN |
| 564 |
LibrariesFound := TRUE ; |
| 565 |
Libraries := ConCat (ConCatChar (Libraries, ' '), s) ; |
| 566 |
RETURN TRUE |
| 567 |
ELSE |
| 568 |
RETURN FALSE |
| 569 |
END |
| 570 |
END IsALibrary ; |
| 571 |
|
| 572 |
|
| 573 |
(* |
| 574 |
IsALibraryPath - |
| 575 |
*) |
| 576 |
|
| 577 |
PROCEDURE IsALibraryPath (s: String) : BOOLEAN ; |
| 578 |
BEGIN |
| 579 |
IF EqualArray (Mark (Slice (s, 0, 2)), '-L') |
| 580 |
THEN |
| 581 |
IF UseLibtool |
| 582 |
THEN |
| 583 |
LibrariesFound := TRUE ; |
| 584 |
Libraries := ConCat (ConCatChar (Libraries, ' '), s) |
| 585 |
END ; |
| 586 |
RETURN TRUE |
| 587 |
ELSE |
| 588 |
RETURN FALSE |
| 589 |
END |
| 590 |
END IsALibraryPath ; |
| 591 |
|
| 592 |
|
| 593 |
(* |
| 594 |
AddCommandLineObject - adds, s, to a list of objects specified on the command line. |
| 595 |
*) |
| 596 |
|
| 597 |
PROCEDURE AddCommandLineObject (s: String) ; |
| 598 |
BEGIN |
| 599 |
s := Dup (s) ; |
| 600 |
IncludeIndiceIntoIndex (CmdLineObjects, s) ; |
| 601 |
IF RegisterModuleObject (CmdLine, s) |
| 602 |
THEN |
| 603 |
IF Debugging |
| 604 |
THEN |
| 605 |
fprintf1 (StdErr, "object registered first time: %s\n", s) |
| 606 |
END |
| 607 |
ELSE |
| 608 |
IF Debugging |
| 609 |
THEN |
| 610 |
fprintf1 (StdErr, " object ignored: %s\n", s) |
| 611 |
END |
| 612 |
END |
| 613 |
END AddCommandLineObject ; |
| 614 |
|
| 615 |
|
| 616 |
(* |
| 617 |
IsAnObject - returns TRUE if, a, is a library. |
| 618 |
*) |
| 619 |
|
| 620 |
PROCEDURE IsAnObject (s: String) : BOOLEAN ; |
| 621 |
BEGIN |
| 622 |
IF ((Length (s) > 2) AND EqualArray (Mark (Slice (s, -2, 0)), '.o')) OR |
| 623 |
((Length (s) > 4) AND EqualArray (Mark (Slice (s, -4, 0)), '.obj')) |
| 624 |
THEN |
| 625 |
RETURN TRUE |
| 626 |
ELSE |
| 627 |
RETURN FALSE |
| 628 |
END |
| 629 |
END IsAnObject ; |
| 630 |
|
| 631 |
|
| 632 |
(* |
| 633 |
AdditionalFOptions - add an -f option to the compiler. |
| 634 |
*) |
| 635 |
|
| 636 |
PROCEDURE AdditionalFOptions (s: String) ; |
| 637 |
BEGIN |
| 638 |
FOptions := ConCat (FOptions, Mark (s)) ; |
| 639 |
FOptions := ConCatChar (FOptions, ' ') |
| 640 |
END AdditionalFOptions ; |
| 641 |
|
| 642 |
|
| 643 |
(* |
| 644 |
DisplayHelp - |
| 645 |
*) |
| 646 |
|
| 647 |
PROCEDURE DisplayHelp ; |
| 648 |
BEGIN |
| 649 |
fprintf0 (StdErr, 'Usage: gm2lcc [-c][-g][-h][--help][--main mainmodule]\n'); |
| 650 |
fprintf0 (StdErr, ' [--mainobject objectname][-Bdirectory][-p][--exec][-fshared]\n'); |
| 651 |
fprintf0 (StdErr, ' [--ignoremain][--ar][-fobject-path=path][-ftarget-ar=arname]\n'); |
| 652 |
fprintf0 (StdErr, ' [-ftarget-ranlib=ranlibname][-o outputname][--startup filename]\n') ; |
| 653 |
fprintf0 (StdErr, ' [-foption][-llibname][-Lpath] filename\n'); |
| 654 |
exit (0) |
| 655 |
END DisplayHelp ; |
| 656 |
|
| 657 |
|
| 658 |
(* |
| 659 |
ScanArguments - scans arguments for flags: -fobject-path= -g and -B |
| 660 |
*) |
| 661 |
|
| 662 |
PROCEDURE ScanArguments ; |
| 663 |
VAR |
| 664 |
filename, |
| 665 |
s : String ; |
| 666 |
i : CARDINAL ; |
| 667 |
FoundFile: BOOLEAN ; |
| 668 |
BEGIN |
| 669 |
FoundFile := FALSE ; |
| 670 |
filename := NIL ; |
| 671 |
i := 1 ; |
| 672 |
WHILE GetArg (s, i) DO |
| 673 |
IF EqualArray (s, '-h') OR EqualArray (s, '--help') |
| 674 |
THEN |
| 675 |
DisplayHelp |
| 676 |
ELSIF EqualArray (s, '-g') |
| 677 |
THEN |
| 678 |
DebugFound := TRUE |
| 679 |
ELSIF EqualArray (s, '-c') |
| 680 |
THEN |
| 681 |
CheckFound := TRUE |
| 682 |
ELSIF EqualArray (s, '--main') |
| 683 |
THEN |
| 684 |
INC (i) ; |
| 685 |
IF NOT GetArg (MainModule, i) |
| 686 |
THEN |
| 687 |
fprintf0 (StdErr, 'expecting modulename after the --main option\n') ; |
| 688 |
Close (StdErr) ; |
| 689 |
exit (1) |
| 690 |
END |
| 691 |
ELSIF EqualArray (s, '--mainobject') |
| 692 |
THEN |
| 693 |
INC (i) ; |
| 694 |
IF GetArg (MainObject, i) |
| 695 |
THEN |
| 696 |
(* do nothing. *) |
| 697 |
ELSE |
| 698 |
fprintf0 (StdErr, 'expecting an object file after the --mainobject option\n') ; |
| 699 |
Close (StdErr) ; |
| 700 |
exit (1) |
| 701 |
END |
| 702 |
ELSIF EqualArray (Mark (Slice (s, 0, 2)), '-B') |
| 703 |
THEN |
| 704 |
CompilerDir := KillString (CompilerDir) ; |
| 705 |
IF Length (s) = 2 |
| 706 |
THEN |
| 707 |
INC(i) ; |
| 708 |
IF NOT GetArg (CompilerDir, i) |
| 709 |
THEN |
| 710 |
fprintf0 (StdErr, 'expecting path after -B option\n') ; |
| 711 |
Close (StdErr) ; |
| 712 |
exit (1) |
| 713 |
END |
| 714 |
ELSE |
| 715 |
CompilerDir := Slice (s, 2, 0) |
| 716 |
END ; |
| 717 |
BOption := Dup (s) |
| 718 |
ELSIF EqualArray (s, '-p') |
| 719 |
THEN |
| 720 |
ProfileFound := TRUE |
| 721 |
ELSIF EqualArray (s, '-v') |
| 722 |
THEN |
| 723 |
VerboseFound := TRUE |
| 724 |
ELSIF EqualArray (s, '--exec') |
| 725 |
THEN |
| 726 |
ExecCommand := TRUE |
| 727 |
ELSIF EqualArray (s, '-fshared') |
| 728 |
THEN |
| 729 |
Shared := TRUE |
| 730 |
ELSIF EqualArray (s, '--ignoremain') |
| 731 |
THEN |
| 732 |
IgnoreMain := TRUE |
| 733 |
ELSIF EqualArray (s, '--ar') |
| 734 |
THEN |
| 735 |
UseAr := TRUE ; |
| 736 |
UseRanlib := TRUE ; |
| 737 |
UseLibtool := FALSE |
| 738 |
ELSIF EqualArray (Mark (Slice (s, 0, 14)), '-fobject-path=') |
| 739 |
THEN |
| 740 |
PrependSearchPath (Slice (s, 14, 0)) |
| 741 |
ELSIF EqualArray (Mark (Slice (s, 0, 12)), '-ftarget-ar=') |
| 742 |
THEN |
| 743 |
ArProgram := KillString (ArProgram) ; |
| 744 |
ArProgram := Slice (s, 12, 0) |
| 745 |
ELSIF EqualArray (Mark (Slice (s, 0, 16)), '-ftarget-ranlib=') |
| 746 |
THEN |
| 747 |
RanlibProgram := KillString (RanlibProgram) ; |
| 748 |
RanlibProgram := Slice (s, 16, 0) |
| 749 |
ELSIF EqualArray (s, '-o') |
| 750 |
THEN |
| 751 |
INC (i) ; (* Target found *) |
| 752 |
ProcessTarget (i) |
| 753 |
ELSIF EqualArray (s, '--startup') |
| 754 |
THEN |
| 755 |
INC (i) ; (* Target found. *) |
| 756 |
ProcessStartupFile (i) |
| 757 |
ELSIF EqualArray (Mark (Slice (s, 0, 2)), '-f') |
| 758 |
THEN |
| 759 |
AdditionalFOptions (s) |
| 760 |
ELSIF IsALibrary (s) OR IsALibraryPath (s) |
| 761 |
THEN |
| 762 |
ELSIF IsAnObject (s) |
| 763 |
THEN |
| 764 |
AddCommandLineObject (s) |
| 765 |
ELSE |
| 766 |
IF FoundFile |
| 767 |
THEN |
| 768 |
fprintf2 (StdErr, 'already specified input filename (%s), unknown option (%s)\n', filename, s) ; |
| 769 |
Close (StdErr) ; |
| 770 |
exit (1) |
| 771 |
ELSE |
| 772 |
(* must be input filename. *) |
| 773 |
Close (StdIn) ; |
| 774 |
fi := OpenToRead (s) ; |
| 775 |
IF NOT IsNoError (fi) |
| 776 |
THEN |
| 777 |
fprintf1 (StdErr, 'failed to open %s\n', s) ; |
| 778 |
Close (StdErr) ; |
| 779 |
exit (1) |
| 780 |
END ; |
| 781 |
FoundFile := TRUE ; |
| 782 |
filename := Dup (s) |
| 783 |
END |
| 784 |
END ; |
| 785 |
INC (i) |
| 786 |
END |
| 787 |
END ScanArguments ; |
| 788 |
|
| 789 |
|
| 790 |
(* |
| 791 |
Init - initializes the global variables. |
| 792 |
*) |
| 793 |
|
| 794 |
PROCEDURE Init ; |
| 795 |
BEGIN |
| 796 |
DebugFound := FALSE ; |
| 797 |
CheckFound := FALSE ; |
| 798 |
TargetFound := FALSE ; |
| 799 |
ProfileFound := FALSE ; |
| 800 |
IgnoreMain := FALSE ; |
| 801 |
UseAr := FALSE ; |
| 802 |
UseLibtool := FALSE ; |
| 803 |
UseRanlib := FALSE ; |
| 804 |
VerboseFound := FALSE ; |
| 805 |
Shared := FALSE ; |
| 806 |
ArProgram := InitString ('ar') ; |
| 807 |
RanlibProgram := InitString ('ranlib') ; |
| 808 |
MainModule := InitString ('') ; |
| 809 |
StartupFile := InitString ('mod_init') ; |
| 810 |
fi := StdIn ; |
| 811 |
fo := StdOut ; |
| 812 |
ExecCommand := FALSE ; |
| 813 |
|
| 814 |
CompilerDir := InitString ('') ; |
| 815 |
|
| 816 |
FOptions := InitString ('') ; |
| 817 |
Archives := NIL ; |
| 818 |
Path := NIL ; |
| 819 |
LibrariesFound:= FALSE ; |
| 820 |
Libraries := InitString ('') ; |
| 821 |
Command := NIL ; |
| 822 |
Target := NIL ; |
| 823 |
BOption := NIL ; |
| 824 |
objects := InitFileObject () ; |
| 825 |
CmdLine := InitFileObject () ; |
| 826 |
CmdLineObjects:= InitIndex (1) ; |
| 827 |
MainObject := InitString ('') ; |
| 828 |
|
| 829 |
ScanArguments ; |
| 830 |
IF CheckFound |
| 831 |
THEN |
| 832 |
CheckCC |
| 833 |
ELSE |
| 834 |
GenCC |
| 835 |
END ; |
| 836 |
Close (fo) |
| 837 |
END Init ; |
| 838 |
|
| 839 |
|
| 840 |
BEGIN |
| 841 |
Init |
| 842 |
END gm2lcc. |