-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtc.sh
690 lines (572 loc) · 13.6 KB
/
htc.sh
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#!/bin/bash
#
# htc - script for comfortable use of HiTech C
VERSION=0.72
LAST_UPDATE="09 Jun 2002"
#############################################################################
#
# Some points of consideration:
#
# - By setting the appropriate values for HTC_LIBRARYPATHS and HTC_CRT,
# you can make htc compile applications for OS-es other than MSXDOS or
# CP/M -- for UZIX, for example :-)
#
# - you need to have cpmemu installed
#
# - you need to have our other script, named 'htlibr', installed for linking
#
# - the path to HiTech C's .com files has to be fully lowercase
# (restriction of cpmemu :-/)
#
# - HiTech C is expected to be installed as follows:
#
# ...$HTC_PATH/ - HiTech C base dir
# bin - CPM binaries (p1, cgen, zas, opt and link)
# inc - 'system' include files
# lib - 'system' libraries (libc.lib, libf.lib ...)
# and the C RunTime startup code (CRT.O)
#
#############################################################################
#############################################################################
#
# Usage
#
Usage ()
{
cat <<-END
htc - z80 C cross-compiler using Hi-Tech C and cpmemu
version $VERSION - $LAST_UPDATE - Aurora (Eric Boon & Manuel Bilderbeek)
Usage: htc [options] file ...
Options:
-o<file> Output to <file> (default: aout.com)
-O Turn optimization on
-D<name[=value]> #define name (with value)
-U<name> #undef name
-I<path> Include <path> in #include path
-E Preprocess only
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-v Be verbose
-l<library> Link lib<library>.lib
-m Request the linker to produce a link map
-h Print this help
Filetype is determined by extension:
.c C source
.i Pre-processed C source
.as .asm .gen Assembler source
.o .obj Object files
.lib Libraries
ENVIRONMENT VARIABLES:
HTC_PATH Path to HiTech C install
(/usr/local/lib/hitech)
HTC_CPMEMU Path to 'cpm', the CP/M emulator
(/usr/local/bin/cpm)
HTC_LIBRARYPATHS Path to search for libraries
("./lib ../lib $HTC_PATH/lib")
HTC_CRT Path to C run-time object code
($HTC_PATH/lib/crt.o)
TIPS:
* If you plan to use files that originate from CP/M or MSX-DOS,
remember to 'cpm2unix' them to get rid of ^Z and other rubbish
the CP/M leaves at the end of the file! Htc can't and therefore
doesn't check that, but will definitely choke!
* The -l option is recognized anywhere on the cmd line, other options
only at the beginning.
DISCLAIMER:
It works for us, there's no guarantee that it will for you! :-)
Nevertheless, please send bug reports, questions, remarks,
thank-you's or beer to: Aurora1@Chello.nl
COPYRIGHT:
Find yourself a copy of the GNU Public License.
END
exit 1
}
#############################################################################
#
# Try to find the CPM emulator
#
Check_cpmemu ()
{
CPMEMU_LIST="$HTC_CPMEMU `which cpm` /usr/local/bin/cpm "
for MY_CPM in $CPMEMU_LIST ; do
if [ ! -x $MY_CPM ] ; then break ; fi
done
if [ -z $MY_CPM ] ; then
echo "* $ME: Unable to find cpm emulator" \
"Please set \$HTC_CPMEMU appropriately"
exit 1
fi
}
#############################################################################
#
# Try to find Hi Tech C
#
Check_hitech ()
{
MY_HTC_PATH=$HTC_PATH
if [ -z $MY_HTC_PATH ] ; then
MY_HTC_PATH="/usr/local/lib/hitech"
fi
if [ ! -d $MY_HTC_PATH ] ; then
echo "* $ME: Unable to find HiTech C" \
"Please set \$HTC_PATH appropriately"
exit 1
fi
if [ $VERBOSE == 1 ] ; then
echo "HITECH PATH : $MY_HTC_PATH"
fi
P1=$MY_HTC_PATH/bin/p1.com
CGEN=$MY_HTC_PATH/bin/cgen.com
OPTIM=$MY_HTC_PATH/bin/optim.com
ZAS=$MY_HTC_PATH/bin/zas.com
LINK=$MY_HTC_PATH/bin/link.com
if [[ ! ( -e $P1 && -e $CGEN && -e $OPTIM && -e $ZAS && -e $LINK ) ]] ; then
echo "* $ME: Unable to find one or more HiTech C components! Oops..."
exit 1
fi
MY_LIB_PATHS=$HTC_LIBRARYPATHS
if [ -z $MY_LIB_PATHS ] ; then
MY_LIB_PATHS=" ./lib ../lib $MY_HTC_PATH/lib "
fi
if [ $VERBOSE == 1 ] ; then
echo "LIBRARY PATHS : $MY_LIB_PATHS"
fi
MY_CRT=$HTC_CRT
if [ -z $MY_CRT ] ; then
MY_CRT=$MY_HTC_PATH/lib/crt.o
fi
if [ $DO_LINK ] ; then
if [ ! -f $MY_CRT ] ; then
echo "* $ME: Unable to find run-time startup code. " \
"Please set \$HTC_CRT"
exit 1
fi
if [ $VERBOSE == 1 ] ; then
echo "RUNTIME OBJECT : $MY_CRT"
fi
fi
}
#############################################################################
#
# Find GCC
#
Check_gcc ()
{
if ! which gcc >/dev/null ; then
echo "* $ME: Unable to find GCC! Please set your \$PATH appropriately"
exit 1
fi
}
#############################################################################
#
# Find htlibr
#
Check_htlibr ()
{
if ! which htlibr >/dev/null ; then
echo "* $ME: Unable to find htlibr! Please set your \$PATH appropriately"
exit 1
fi
}
#############################################################################
#
# Pre-processing
#
do_cpp ()
{
if [ $VERBOSE == 1 ] ; then
echo "- Preprocessing $FILENAME"
fi
rm -f $FILENAME.t TMP_$FILENAME.c $FILENAME.i
# some conversion - gcc doesn't like #asm/#endasm, so we temporarily
# replace that with __asm and __endasm
awk ' { gsub(/#asm/,"__asm") ; \
gsub(/#endasm/, "__endasm") ; \
print } ' $FILENAME.c > $FILENAME.t1
if ( gcc -x c -E -nostdinc -I$MY_HTC_PATH/inc \
$INCLUDE $DEFINE $UNDEF $FILENAME.t1 > $FILENAME.t2 ) ; then
awk '/^#/ { print "# " $2 " " $3 ; } \
/^([^#]|$)/ { gsub(/__asm/, "#asm") ; \
gsub(/__endasm/, "#endasm"); \
print ; }' $FILENAME.t2 > $FILENAME.i ;
if [ $DO_COMPILE == 1 ] ; then
do_cgen
rm $FILENAME.i
fi
fi
rm -f $FILENAME.t1 $FILENAME.t2
}
#############################################################################
#
# Compiling
#
do_cgen ()
{
if [ $DO_COMPILE == 1 ] ; then
if [ $VERBOSE == 1 ] ; then
echo "- Compiling $FILENAME"
fi
$MY_CPM -f -x $P1 $FILENAME.i $FILENAME.t1
$MY_CPM -f -x $CGEN $FILENAME.t1 $FILENAME.as
rm $FILENAME.t1
do_opt
if [ $DO_ASSEMBLE == 1 ] ; then
OLDFILEEXT=$FILEEXT
FILEEXT=".as"
do_asm
rm -f $FILENAME.as
FILEEXT=$OLDFILEEXT
fi
fi
}
#############################################################################
#
# Optimizing
#
do_opt ()
{
if [ $DO_OPTIM == 1 ] ; then
if [ $VERBOSE == 1 ] ; then
echo "- Optimizing $FILENAME"
fi
$MY_CPM -f -x $OPTIM $FILENAME.as $FILENAME.t
rm -f $FILENAME.as
mv $FILENAME.t $FILENAME.as
fi
}
#############################################################################
#
# Assembling
#
do_asm ()
{
if [ $DO_ASSEMBLE == 1 ] ; then
if [ $VERBOSE == 1 ] ; then
echo "- Assembling $FILENAME"
fi
if [ $DO_OPTIM == 1 ] ; then
ZASOPT="-J -N"
else
ZASOPT="-N"
fi
$MY_CPM -f -x $ZAS $ZASOPT -o$FILENAME.o ${FILENAME}$FILEEXT
fi
}
#############################################################################
#
# Linking
#
do_link ()
{
if [ $DO_LINK == 1 ] ; then
if [ $DO_MAP == 1 ] ; then
MAPFILE=${TARGET/%*([^.])/map}
MAP=-M$MAPFILE
else
MAP=
MAPFILE="<none>"
fi
if [ $VERBOSE == 1 ] ; then
echo "- Linking to $TARGET, mapfile=$MAPFILE"
fi
#
# build temporary library to avoid problems with command lines longer than 128
# characters
#
if [[ -e tmp.lib || -e tmpcrt.o ]] ; then
echo "Found an existing tmp.lib or tmpcrt.o, which is used internally."
echo "Fudeba!"
exit 1
fi
htlibr -b r tmp.lib $OBJECTS
cp $MY_CRT tmpcrt.o
$MY_CPM -f -x $LINK -Z $MAP -Ptext=0,data,bss -C100H -O$TARGET \
tmpcrt.o tmp.lib $REALLIBS
rm tmpcrt.o tmp.lib
if [ ! -s $TARGET ] ; then
echo "* $ME: linking failed. No ouput. Aborting..."
rm -f $TARGET
return 1
fi
fi
return 0
}
#############################################################################
#
# M A I N
#
#############################################################################
#
# Enable extended pattern matching
#
shopt -s extglob
#
# Preset some vars...
#
TARGET=""
DEFINE="-DMSX"
UNDEF=""
INCLUDE="-I."
VERBOSE=0
LIBS=""
DO_OPTIM=0
DO_COMPILE=1
DO_ASSEMBLE=1
DO_LINK=1
DO_MAP=0
#
# Whoami ?
#
ME=${0/*\//}
#
# Collect options...
#
while getopts D:EI:OSU:cl:mo:vh c; do
case $c in
D)
DEFINE="$DEFINE -D$OPTARG"
;;
U)
UNDEF="$UNDEF -U$OPTARG"
;;
E)
DO_COMPILE=0
DO_ASSEMBLE=0
DO_LINK=0
;;
I)
INCLUDE="$INCLUDE -I$OPTARG"
;;
O)
DO_OPTIM=1
;;
S)
DO_ASSEMBLE=0
DO_LINK=0
;;
c)
DO_LINK=0
;;
l)
LIBS="$LIBS $OPTARG"
;;
m)
DO_MAP=1
;;
o)
if [ ! -z $TARGET ] ; then
echo "* $ME: Multiple targets"
exit 1
else
TARGET=$OPTARG
fi
;;
v)
VERBOSE=1
;;
*)
Usage
;;
esac
done
if [ $VERBOSE == 1 ] ; then
echo "$ME v$VERSION - $LAST_UPDATE - (C) 2002 Aurora"
fi
#
# Show'em what we're gonna do
#
if [ $VERBOSE == 1 ] ; then
if [ $DO_LINK == 1 ] ; then
echo -n "TARGET : "
if [ -z $TARGET ] ; then
echo "(not set)"
else
echo $TARGET
fi
fi
echo "DEFINE : " $DEFINE
echo "INCLUDE : " $INCLUDE
echo -n "COMPILE : cpp"
if [ $DO_COMPILE == 1 ] ; then
echo -n " p1 gcen"
if [ $DO_OPTIM == 1 ] ; then
echo -n " opt"
fi
if [ $DO_ASSEMBLE == 1 ] ; then
echo -n " zas"
if [ $DO_LINK == 1 ] ; then
echo -n " link"
fi
fi
fi
echo ""
fi
let CNT=$OPTIND-1
shift $CNT
unset CNT
FILES=( $* )
if [ -z $FILES ] ; then
echo "* $ME: No input files. (Type $ME -h to get some help.)"
exit 1
fi
if [ $VERBOSE == 1 ] ; then
echo "FILES : ${FILES[*]} (${#FILES[*]} files to process...)"
fi
#
# Check if everything is there
#
Check_cpmemu
Check_hitech
Check_gcc
Check_htlibr
#
# Ready to rock and roll...
#
#
# Now process each file
#
for FILE in ${FILES[*]} ; do
#
# Check whether we found another library ...
#
if [[ $FILE == -l* ]] ; then
LIBNAME=${FILE/-l/}
if [ -z $LIBNAME ] ; then
echo "* $ME: Missing library name for -l"
exit 1
fi
LIBS="$LIBS lib$LIBNAME.lib"
if [ $VERBOSE == 1 ] ; then
echo " Found -l for lib$LIBNAME.lib"
fi
# go on with next $FILE
continue
fi
#
# ... or a dangling option
#
if [[ $FILE == "-*" ]] ; then
echo "* $ME: Unexpected option $FILE - skipping"
continue
fi
#
# Does the file we want to process exist?
#
if [[ ! ( -e $FILE ) ]] ; then
echo "* $ME: Unable to find $FILE!"
exit 1
fi
#
# Pattern magic :-) - get file basename, extension and path
#
FILENAME=${FILE/%.+([^.])/}
FILEEXT=${FILE/${FILENAME}/}
FILEPATH=${FILE/%*([^\/])/}
FILENAME=${FILENAME/${FILEPATH}/}
#
# No path names in source files. Tolerate ./, though
#
if [[ ! ( $FILEPATH == "./" || -z $FILEPATH ) ]] ; then
echo "* $ME: $FILE - pathnames in source/object files are not allowed! Sorry."
exit 1
fi
if [ $VERBOSE == 1 ] ; then
echo "Processing ${FILENAME}$FILEEXT"
fi
case $FILEEXT in
.c)
do_cpp
OBJECTS="$OBJECTS ${FILENAME}.o"
;;
.i)
do_cgen
OBJECTS="$OBJECTS ${FILENAME}.o"
;;
.as|.asm|.gen)
do_asm
OBJECTS="$OBJECTS ${FILENAME}.o"
;;
.o|.obj)
if [ $VERBOSE == 1 ] ; then
echo "- Nothing to do for $FILENAME"
fi
OBJECTS="$OBJECTS ${FILENAME}$FILEEXT"
;;
.lib)
if [ $VERBOSE == 1 ] ; then
echo "- Nothing to do for $FILENAME"
fi
LIBS="$LIBS ${FILENAME}$FILEEXT"
;;
*)
echo "* $ME: Couldn't determine how to process $FILE!"
exit 1
;;
esac
done
#
# Done compiling objects - now link the stuff
#
LINK_OK=0
if [ $DO_LINK == 1 ] ; then
LIBS="$LIBS libc.lib"
if [ $VERBOSE == 1 ] ; then
echo "LIBS : " $LIBS
fi
#
# Find the libraries
#
# RMLIBS are libraries which are copied from elsewhere to here.
# This has to be done since the cpm linker doesn't recognize
# pathnames :-( These have to be removed after linking
#
RMLIBS=" "
REALLIBS=" "
for LIB in $LIBS ; do
# check current dir (.)
if [ -e "${LIB}" ] ; then
REALLIBS="$REALLIBS ${LIB}"
continue
fi
# check the other paths
LIBFOUND=0
for LIBPATH in $MY_LIB_PATHS ; do
THELIB="$LIBPATH/${LIB}"
if [ -e $THELIB ] ; then
cp $THELIB .
RMLIBS="$RMLIBS ${LIB}"
REALLIBS="$REALLIBS ${LIB}"
LIBFOUND=1
break
fi
done
if [ $LIBFOUND == 0 ] ; then
echo "* $ME: Unable to find ${LIB}!"
exit 1
fi
done
#
# Check target
#
if [ -z $TARGET ] ; then
TARGET=${FILES[0]/%*([^.])/com}
echo "* $ME: Outputting to implicit target $TARGET"
fi
#
# Link!
#
if do_link ; then
LINK_OK=0
else
LINK_OK=1
fi
#
# Remove trash
#
rm -f $RMLIBS
fi
# Done! Thank you for flying htc
if [[ $LINK_OK == 0 && $VERBOSE == 1 ]] ; then
echo "* $ME: Done!"
fi
exit $LINK_OK