BUILD 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. #!/bin/bash
  2. #BUILD script
  3. #
  4. #Copyright (C) 2015 Rafael
  5. #
  6. #This program is free software; you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation; either version 2 of the License, or
  9. #(at your option) any later version.
  10. #
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. #GNU General Public License for more details.
  15. #
  16. #You should have received a copy of the GNU General Public License along
  17. #with this program; if not, write to the Free Software Foundation, Inc.,
  18. #51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. #
  21. #Search for config:
  22. _BUILD_DIR=`dirname $0`
  23. export _BUILD_DIR=$(cd "$_BUILD_DIR"; pwd)
  24. . ${_BUILD_DIR}/BUILD.conf
  25. #ARGS:
  26. # $1 -> pass the $? return
  27. # $2 -> component name
  28. check_error()
  29. {
  30. if [ $1 -ne 0 ]
  31. then
  32. echo "ERROR $2"
  33. exit 1
  34. fi
  35. }
  36. #ARGS:
  37. # $1 -> test name
  38. build_a_test()
  39. {
  40. cd ${TEST_DIR}/$1
  41. DO_NOTHING=0
  42. LDFLAGS_EXTRA=
  43. CFLAGS_EXTRA=
  44. test_${1} 1
  45. if [ $DO_NOTHING -eq 0 ]
  46. then
  47. echo "$CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN"
  48. $CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN
  49. if [ $? -ne 0 ]
  50. then
  51. echo "ERROR compiling $1"
  52. cd - > /dev/null
  53. exit 1
  54. fi
  55. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  56. echo $LD $LDFLAGS_EXTRA $LDFLAGS $LD_LIBS -o $BUILD_OUT *.o
  57. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT *.o $LD_LIBS
  58. if [ $? -ne 0 ]
  59. then
  60. echo "ERROR linking $1"
  61. cd - > /dev/null
  62. exit 1
  63. fi
  64. fi
  65. cd - > /dev/null
  66. }
  67. clean_kernel()
  68. {
  69. cd "$KRL_DIR"
  70. printf "Cleaning kernel "
  71. rm -f *.o *.root.c *.out errstr.h init.h amd64^l.h libboot.a ../boot/*.o boot$CONF.c
  72. if [ $? -eq 0 ]
  73. then
  74. printf "OK\n"
  75. else
  76. printf "ERROR\n"
  77. fi
  78. cd - > /dev/null
  79. }
  80. compile_kernel()
  81. {
  82. CONF=$KERNEL_CONF
  83. SOURCE=$KERNEL_SOURCE
  84. CFLAGS="$KERNEL_CFLAGS"
  85. if [ -n "$BUILD_DEBUG" ]
  86. then
  87. CFLAGS="$KERNEL_CFLAGS $KERNEL_CFLAGS_DEBUG"
  88. fi
  89. WARNFLAGS="$KERNEL_WARNFLAGS"
  90. UCFLAGS="$KERNEL_UCFLAGS $BUILD_DEBUG"
  91. GLOBIGNORE=$KERNEL_GLOBIGNORE
  92. PATH_ORI=`pwd`
  93. cd "$KRL_DIR"
  94. echo "$AWK -v objtype=$ARCH -f ../mk/parse -- -mkdevc $CONF > $CONF.c"
  95. $AWK -v objtype=$ARCH -f ../mk/parse -- -mkdevc $CONF > $CONF.c
  96. check_error $? "executing $AWK"
  97. echo "$AWK -f ../mk/mkenumb amd64.h > amd64l.h"
  98. $AWK -f ../mk/mkenumb amd64.h > amd64l.h # mkenumb is shell independent
  99. check_error $? "executing $AWK"
  100. ## BOOT ##
  101. BOOTDIR=../boot
  102. BOOTLIB=libboot.a
  103. echo "Parsing boot${CONF}.c"
  104. echo "$AWK -f ../mk/parse -- -mkbootconf $CONF > boot$CONF.c"
  105. $AWK -f ../mk/parse -- -mkbootconf $CONF > boot$CONF.c
  106. check_error $? "executing $AWK"
  107. echo "Making Boot"
  108. cd ../boot
  109. echo $CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -c *.c
  110. $CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -c *.c
  111. check_error $? "compiling $BOOTLIB"
  112. echo "$AR ${BOOTLIB} *.o"
  113. $AR rv $BOOTLIB *.o
  114. check_error $? "building $BOOTLIB"
  115. cd - > /dev/null
  116. echo "$CC boot${CONF}"
  117. echo "$CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c boot$CONF.c"
  118. $CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c boot$CONF.c
  119. check_error $? "compiling boot${CONF}"
  120. echo "$CC printstub"
  121. echo "$CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c ../boot/printstub.c"
  122. $CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c ../boot/printstub.c
  123. check_error $? "compiling printstub.c"
  124. echo "$LD boot${CONF}.out"
  125. $LD -static -o boot$CONF.elf.out boot$CONF.o printstub.o $LDFLAGS -L$BOOTDIR -lboot -lip -lauth -lc -e_main
  126. check_error $? "linking boot$CONF.elf.out"
  127. ## systab.c ##
  128. echo "$AWK -f ../mk/parse -- -mksystab ${SRC_DIR}/libc/9syscall/sys.h $CONF > systab.c"
  129. $AWK -f ../mk/parse -- -mksystab ${SRC_DIR}/libc/9syscall/sys.h $CONF > systab.c
  130. check_error $? "parsing systab.c"
  131. echo "$CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -c systab.c"
  132. $CC $CFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -c systab.c
  133. check_error $? "compiling systab.c"
  134. ## init.h ##
  135. echo "$CC $UCFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c init9.c"
  136. $CC $UCFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c init9.c
  137. check_error $? "compiling init9.c"
  138. echo "$CC $UCFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c ../port/initcode.c"
  139. $CC $UCFLAGS $WARNFLAGS -I${INC_ARCH} -I${INC_DIR} -I. -mcmodel=small -c ../port/initcode.c
  140. check_error $? "compiling initcode.c"
  141. echo "$LD -static -o init init9.o initcode.o $LDFLAGS -lc -e_main -Ttext=0x200020"
  142. $LD -static -o init init9.o initcode.o $LDFLAGS -lc -e_main -Ttext=0x200020
  143. check_error $? "linking init"
  144. echo "${UTIL_DIR}/elf2c init > init.h"
  145. ${UTIL_DIR}/elf2c init > init.h
  146. check_error $? "executing elf2c"
  147. ## errstr.h ##
  148. echo "$AWK -f ../mk/parse -- -mkerrstr $CONF > errstr.h"
  149. $AWK -f ../mk/parse -- -mkerrstr $CONF > errstr.h
  150. check_error $? "parsing errstr"
  151. ## mkroot ##
  152. #strip boot$CONF.elf.out
  153. #check_error $? "to strip boot$CONF.elf.out"
  154. #echo "${UTIL_DIR}/data2c "boot$CONF"_out boot$CONF.elf.out >> k8cpu.root.c"
  155. #${UTIL_DIR}/data2c "boot$CONF"_out boot$CONF.elf.out >> k8cpu.root.c
  156. #check_error $? "executing data2c"
  157. # we need a WAY better way to do this. We're having to do it for each file.
  158. echo "${UTIL_DIR}/data2c boot_fs boot.fs >> k8cpu.root.c"
  159. ${UTIL_DIR}/data2c boot_fs boot.fs >> k8cpu.root.c
  160. check_error $? "executing data2c"
  161. echo "cp ${BASEDIR}/rc/lib/rcmain rcmain"
  162. cp ${BASEDIR}/rc/lib/rcmain rcmain
  163. echo "${UTIL_DIR}/data2c _rc_lib_rcmain rcmain >> k8cpu.root.c"
  164. ${UTIL_DIR}/data2c _rc_lib_rcmain rcmain >> k8cpu.root.c
  165. check_error $? "executing data2c"
  166. ##### Factotum / ipconfig ######
  167. ##cp /sys/src/cmd/auth/factotum/factotum.elf.out factotum.elf.out
  168. ##strip factotum.elf.out
  169. ##data2c _amd64_bin_auth_factotum factotum.elf.out >> k8cpu.root.c
  170. ##cp /sys/src/cmd/ip/ipconfig/ipconfig.elf.out ipconfig.elf.out
  171. ##strip ipconfig.elf.out
  172. ##data2c _amd64_bin_ip_ipconfig ipconfig.elf.out >> k8cpu.root.c
  173. ## rc or test ##
  174. if [ -n "$TEST_APP" ]
  175. then
  176. build_a_test "$TEST_APP"
  177. #test_${1} 1
  178. cp ${TEST_DIR}/${TEST_APP}/${BUILD_OUT} rc.elf.out
  179. check_error $? "copying test $TEST_APP"
  180. else
  181. echo "cp ${CMD_DIR}/rc/rc.elf.out rc.elf.out"
  182. cp ${CMD_DIR}/rc/rc.elf.out rc.elf.out
  183. check_error $? "copying rc"
  184. fi
  185. echo "strip rc.elf.out"
  186. strip rc.elf.out
  187. check_error $? "to strip rc"
  188. echo "${UTIL_DIR}/data2c _amd64_bin_rc rc.elf.out >> k8cpu.root.c"
  189. ${UTIL_DIR}/data2c _amd64_bin_rc rc.elf.out >> k8cpu.root.c
  190. check_error $? "executing data2c"
  191. ## Rest of programs into ramfs ##
  192. RAMFS_LIST="bind mount echo cat cp ls ip/ipconfig/ipconfig ip/ping ip/telnet ip/dhcpclient srv ps mkdir pwd chmod rio/rio date dd aux/vga/vga"
  193. for elem in $RAMFS_LIST
  194. do
  195. elf_out=`basename $elem.elf.out`
  196. bname=`basename $elem`
  197. echo "cp ${CMD_DIR}/$elem.elf.out $elf_out"
  198. cp ${CMD_DIR}/$elem.elf.out $elf_out
  199. check_error $? "copying $elem"
  200. echo "strip $elf_out"
  201. strip $elf_out
  202. check_error $? "to strip $elem"
  203. echo "${UTIL_DIR}/data2c _amd64_bin_$bname $elf_out>> k8cpu.root.c"
  204. ${UTIL_DIR}/data2c _amd64_bin_$bname $elf_out>> k8cpu.root.c
  205. check_error $? "executing data2c"
  206. done
  207. ## Making all ##
  208. echo
  209. echo "Making all in kernel directories"
  210. echo
  211. echo $CC $CFLAGS $WARNFLAGS -I${INC_DIR} -I${INC_ARCH} -I. -c $SOURCE
  212. $CC $CFLAGS $WARNFLAGS -I${INC_DIR} -I${INC_ARCH} -I. -c $SOURCE
  213. check_error $? "compiling kernel"
  214. GLOBIGNORE=
  215. cd "$PATH_ORI" > /dev/null
  216. }
  217. gen_symtab_obj()
  218. {
  219. $NM -n $KERNEL_OBJECT > $KSYM_MAP
  220. #awk 'BEGIN{ print "//#include <kdebug.h>";
  221. #print "struct symtab_entry gbl_symtab[]={" }
  222. #{ if(NF==3){print "{\"" $3 "\", 0x" $1 "},"}}
  223. #END{print "{0,0} };"}' $KSYM_MAP > $KSYM_C
  224. echo "FIX THIS SOMEDAY"
  225. #$CC $NOSTDINC_FLAGS $AKAROSINCLUDE $CFLAGS_KERNEL -o $KSYM_O -c $KSYM_C
  226. }
  227. link_kernel()
  228. {
  229. PATH_ORI=`pwd`
  230. cd "$KRL_DIR"
  231. rm init9.o initcode.o bootk8cpu.o printstub.o
  232. LDFLAGS="$KERNEL_LDFLAGS"
  233. LINKER_SCRIPT=kernel.ld
  234. KSYM_MAP=./ksyms.map
  235. KSYM_C=./ksyms-refl.c
  236. KSYM_O=./ksyms-refl.o
  237. #entry.o MUST be first.
  238. GLOBIGNORE=entry.o
  239. LIST_OBJ=`ls *.o`
  240. REMAINING_ARGS="entry.o $LIST_OBJ ${LIB_DIR}/klibc.a ${LIB_DIR}/klibip.a ${LIB_DIR}/klibdraw.a"
  241. # Generates the first version of $KERNEL_OBJECT
  242. echo $LD $LDFLAGS -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS
  243. $LD $LDFLAGS -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS
  244. check_error $? "linking $KERNEL_OBJECT"
  245. # Generates a C and obj file with a table of the correct size, with relocs
  246. echo FIX ME gen_symtab_obj
  247. # Links the syms with the kernel and inserts the glb_symtab in the kernel.
  248. echo $LD $LDFLAGS -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS # $KSYM_O
  249. $LD $LDFLAGS -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS # $KSYM_O
  250. check_error $? "linking $KERNEL_OBJECT"
  251. # Need to recheck/compute the symbols (table size won't change)
  252. # gen_symtab_obj
  253. # Final link
  254. # $LD -T $LINKER_SCRIPT -o $KERNEL_OBJECT $REMAINING_ARGS # $KSYM_O
  255. # And objdump for debugging
  256. echo "objdump -S $KERNEL_OBJECT > $KERNEL_OBJECT.asm"
  257. objdump -S $KERNEL_OBJECT > $KERNEL_OBJECT.asm
  258. check_error $? "generating $KERNEL_OBJECT.asm"
  259. objcopy -I elf64-x86-64 -O elf32-i386 9k 9k.32bit
  260. check_error $? "creating 9k.32bit"
  261. cd "$PATH_ORI"
  262. }
  263. build_kernel()
  264. {
  265. clean_kernel
  266. compile_kernel
  267. link_kernel
  268. echo "KERNEL COMPILED OK"
  269. }
  270. build_go_utils()
  271. {
  272. cd "${UTIL_DIR}"
  273. for i in `ls *.go`
  274. do
  275. go build $i
  276. if [ $? -ne 0 ]
  277. then
  278. printf "\nERROR compiling $i \n"
  279. exit 1
  280. fi
  281. done
  282. cd - > /dev/null
  283. }
  284. check_utils()
  285. {
  286. if [ -f "${UTIL_DIR}"/mksys ]
  287. then
  288. echo 0
  289. else
  290. echo 1
  291. fi
  292. }
  293. check_lib_dir()
  294. {
  295. if [ ! -d "$LIB_DIR" ]
  296. then
  297. mkdir "$LIB_DIR"
  298. if [ $? -ne 0 ]
  299. then
  300. echo "ERROR creating <$LIB_DIR> directory"
  301. fi
  302. fi
  303. }
  304. #ARGS:
  305. #$1 -> libname
  306. build_l()
  307. {
  308. if [ $DO_NOTHING -eq 0 ]
  309. then
  310. for j in $BUILD_IN
  311. do
  312. echo "$CC $CFLAGS_LIB $CFLAGS_EXTRA $BUILD_DEBUG -c $j"
  313. $CC $CFLAGS_LIB $CFLAGS_EXTRA $BUILD_DEBUG -c $j
  314. if [ $? -ne 0 ]
  315. then
  316. printf "\n\n ERROR COMPILING $1\n"
  317. exit 1
  318. fi
  319. done
  320. echo "$AR rv \"$BUILD_OUT\" *.o"
  321. $AR rv "$BUILD_OUT" *.o
  322. if [ $? -ne 0 ]
  323. then
  324. printf "\n\n ERROR CREATING LIBRARY $1\n"
  325. fi
  326. fi
  327. }
  328. #ARGS:
  329. #$1 -> libname
  330. build_kl()
  331. {
  332. if [ $DO_NOTHING -eq 0 ]
  333. then
  334. echo "$CC $CFLAGS_KLIB $CFLAGS_EXTRA -c $BUILD_IN"
  335. $CC $CFLAGS_KLIB $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN
  336. if [ $? -ne 0 ]
  337. then
  338. printf "\n\n ERROR COMPILING $1\n"
  339. exit 1
  340. fi
  341. echo "$AR rv \"$BUILD_OUT\" *.o"
  342. $AR rv "$BUILD_OUT" *.o
  343. if [ $? -ne 0 ]
  344. then
  345. printf "\n\n ERROR CREATING LIBRARY $1\n"
  346. fi
  347. fi
  348. }
  349. #ARGS:
  350. #$1 -> Lib name
  351. #$2 -> 0 a lib | 1 a klib
  352. build_a_lib()
  353. {
  354. PATH_ORI=`pwd`
  355. cd "${SRC_DIR}/$1"
  356. CFLAGS_EXTRA=
  357. SUBDIRS=
  358. DO_NOTHING=0
  359. if [ $2 -eq 1 ]
  360. then
  361. k${1} 1
  362. else
  363. $1 1
  364. fi
  365. if [ -n "$SUBDIRS" ]
  366. then
  367. for k in $SUBDIRS
  368. do
  369. CFLAGS_EXTRA=
  370. DO_NOTHING=0
  371. cd $k
  372. if [ $2 -eq 1 ]
  373. then
  374. k${1}_$k 1
  375. build_kl k${1}
  376. else
  377. $1_$k 1
  378. build_l $1
  379. fi
  380. cd ..
  381. done
  382. else
  383. if [ $2 -eq 1 ]
  384. then
  385. build_kl $1
  386. else
  387. build_l $1
  388. fi
  389. fi
  390. cd "$PATH_ORI" > /dev/null
  391. }
  392. #ARGS:
  393. #$1 -> Lib name
  394. #$2 -> 0 a lib | 1 a klib
  395. clean_a_lib()
  396. {
  397. PATH_ORI=`pwd`
  398. cd "${SRC_DIR}/$1"
  399. SUBDIRS=
  400. DO_NOTHING=0
  401. if [ $2 -eq 1 ]
  402. then
  403. k${1} 2
  404. else
  405. $1 2
  406. fi
  407. if [ $DO_NOTHING -eq 0 ]
  408. then
  409. if [ -n "$SUBDIRS" ]
  410. then
  411. for k in $SUBDIRS
  412. do
  413. DO_NOTHING=0
  414. cd $k
  415. if [ $2 -eq 1 ]
  416. then
  417. k${1}_$k 2
  418. if [ $DO_NOTHING -eq 0 ]
  419. then
  420. printf "Cleaning k${1}/$k "
  421. $CLEAN_COM
  422. if [ $? -eq 0 ]
  423. then
  424. printf "OK\n"
  425. else
  426. printf "ERROR\n"
  427. fi
  428. fi
  429. else
  430. $1_$k 2
  431. if [ $DO_NOTHING -eq 0 ]
  432. then
  433. printf "Cleaning ${1}/$k "
  434. $CLEAN_COM
  435. if [ $? -eq 0 ]
  436. then
  437. printf "OK\n"
  438. else
  439. printf "ERROR\n"
  440. fi
  441. fi
  442. fi
  443. cd ..
  444. done
  445. else
  446. if [ $2 -eq 1 ]
  447. then
  448. K="k"
  449. else
  450. K=""
  451. fi
  452. printf "Cleaning ${K}$1 "
  453. $CLEAN_COM
  454. if [ $? -eq 0 ]
  455. then
  456. printf "OK\n"
  457. else
  458. printf "ERROR\n"
  459. fi
  460. fi
  461. fi
  462. cd "$PATH_ORI" > /dev/null
  463. }
  464. #ARGS:
  465. #$1 -> ACTION:
  466. # 1) Build
  467. # 2) Clean
  468. build_libs()
  469. {
  470. if [ $1 -eq 1 ]
  471. then
  472. SALIDA=`check_utils`
  473. if [ $SALIDA -eq 1 ]
  474. then
  475. build_go_utils
  476. fi
  477. fi
  478. for i in $BUILD_LIBS
  479. do
  480. if [ $1 -eq 1 ]
  481. then
  482. build_a_lib $i 0
  483. else
  484. clean_a_lib $i 0
  485. fi
  486. done
  487. if [ $1 -eq 1 ]
  488. then
  489. echo "ALL LIBS COMPILED OK"
  490. fi
  491. }
  492. #ARGS:
  493. #$1 -> ACTION:
  494. # 1) Build
  495. # 2) Clean
  496. build_klibs()
  497. {
  498. if [ $1 -eq 1 ]
  499. then
  500. SALIDA=`check_utils`
  501. if [ $SALIDA -eq 1 ]
  502. then
  503. build_go_utils
  504. fi
  505. fi
  506. for i in $BUILD_KLIBS
  507. do
  508. if [ $1 -eq 1 ]
  509. then
  510. build_a_lib $i 1
  511. else
  512. clean_a_lib $i 1
  513. fi
  514. done
  515. if [ $1 -eq 1 ]
  516. then
  517. echo "ALL KLIBS COMPILED OK"
  518. fi
  519. }
  520. #ARGS:
  521. # $1 -> LIB name
  522. do_link_lib()
  523. {
  524. AUX=`echo $1 | grep ^lib`
  525. _LIB_NAME=
  526. if [ -n "$AUX" ]
  527. then
  528. #Name format is libName
  529. _LIB_NAME=`echo $AUX | cut -c 4-`
  530. _LIB_NAME="-l$_LIB_NAME"
  531. else
  532. #Name format is Name
  533. _LIB_NAME="-l$1"
  534. fi
  535. echo $_LIB_NAME
  536. }
  537. #ARGS:
  538. # $1 -> libs list
  539. process_libs_to_link()
  540. {
  541. _RETURN=
  542. for i in $1
  543. do
  544. _RETURN=${_RETURN}" "`do_link_lib $i`
  545. done
  546. echo $_RETURN
  547. }
  548. #ARGS:
  549. # $1 -> cmd name
  550. build_a_cmd()
  551. {
  552. # test if binary has its own subdir
  553. if [ -d "${CMD_DIR}/$1" ]
  554. then
  555. cd ${CMD_DIR}/$1
  556. else
  557. cd ${CMD_DIR}
  558. fi
  559. DO_NOTHING=0
  560. LDFLAGS_EXTRA=
  561. CFLAGS_EXTRA=
  562. BUILD_DIR=
  563. cmd_${1} 1
  564. if [ -n "$BUILD_DIR" ]
  565. then
  566. cd -
  567. cd "$BUILD_DIR"
  568. fi
  569. if [ $DO_NOTHING -eq 0 ]
  570. then
  571. echo "$CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN"
  572. $CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN
  573. if [ $? -ne 0 ]
  574. then
  575. echo "ERROR compiling $1"
  576. cd - > /dev/null
  577. exit 1
  578. fi
  579. # If it's a stand alone source don't get all object files
  580. if [ -d "${CMD_DIR}/$1" ]
  581. then
  582. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  583. echo $LD $LDFLAGS $LDFLAGS_EXTRA $LD_LIBS -o $BUILD_OUT *.o
  584. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT *.o $LD_LIBS
  585. elif [ -n "$BUILD_DIR" ] && [ -d `dirname $BUILD_DIR`"/$1" ]
  586. then
  587. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  588. echo $LD $LDFLAGS $LDFLAGS_EXTRA $LD_LIBS -o $BUILD_OUT *.o
  589. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT *.o $LD_LIBS
  590. else
  591. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  592. echo $LD $LDFLAGS $LDFLAGS_EXTRA $LD_LIBS -o $BUILD_OUT $1.o
  593. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT $1.o $LD_LIBS
  594. fi
  595. if [ $? -ne 0 ]
  596. then
  597. echo "ERROR linking $1"
  598. cd - > /dev/null
  599. exit 1
  600. fi
  601. fi
  602. cd - > /dev/null
  603. }
  604. #ARGS:
  605. # $1 -> cmd name
  606. clean_a_cmd()
  607. {
  608. cd ${CMD_DIR}/$1
  609. DO_NOTHING=0
  610. cmd_${1} 2
  611. if [ $DO_NOTHING -eq 0 ]
  612. then
  613. printf "Cleaning $1 "
  614. $CLEAN_COM
  615. if [ $? -eq 0 ]
  616. then
  617. printf "OK\n"
  618. else
  619. printf "ERROR\n"
  620. fi
  621. fi
  622. }
  623. #ARGS:
  624. #$1 -> ACTION:
  625. # 1) Build
  626. # 2) Clean
  627. build_cmds()
  628. {
  629. for i in $BUILD_CMD
  630. do
  631. if [ $1 -eq 1 ]
  632. then
  633. build_a_cmd $i
  634. else
  635. clean_a_cmd $i
  636. fi
  637. done
  638. if [ $1 -eq 1 ]
  639. then
  640. echo "ALL CMDS COMPILED OK"
  641. fi
  642. }
  643. show_help()
  644. {
  645. printf "\n\nBUILD script for Harvey\n\n"
  646. printf "OPTIONS:\n"
  647. printf " all \tBuild all components\n"
  648. printf " cleanll \tClean all components\n"
  649. printf " libs \tBuild the libraries\n"
  650. printf " libs <libname>\tBuild the library <libname>\n"
  651. printf " cleanlibs\tClean the libraries\n"
  652. printf " klibs \tBuild the Klibraries\n"
  653. printf " klibs <libname>\tBuild the Klibrary <libname>\n"
  654. printf " cleanklibs\tClean the Klibraries\n"
  655. printf " utils \tBuild go utils\n"
  656. printf " cmd \tBuild all cmds \n"
  657. printf " cmd <cmdname>\tBuild cmd named <cmdname>\n"
  658. printf " cleancmd \tClean the cmds\n"
  659. printf " kernel \tBuild kernel\n"
  660. printf " cleankernel\tClean kernel\n"
  661. printf "\nFLAGS:\n"
  662. printf " -g \tCompile with debugs flags\n"
  663. printf " -t <test> \tCompile <test> app and package it with the kernel"
  664. printf "\n"
  665. }
  666. ### MAIN SCRIPT ####
  667. if [ -z "$1" ]
  668. then
  669. show_help
  670. exit 1
  671. else
  672. #BUILD_DEBUG=
  673. #Until we have a stable kernel, debug mode is the default.
  674. BUILD_DEBUG="$CFLAGS_DEBUG"
  675. TEST_APP=
  676. while [ -n "$1" ]
  677. do
  678. case "$1" in
  679. "-g")
  680. BUILD_DEBUG="$CFLAGS_DEBUG"
  681. ;;
  682. "-t"*)
  683. #is -tSomething?
  684. TEST_APP=${1:2}
  685. if [ -z "$TEST_APP" ]
  686. then
  687. if [ -n "$2" ]
  688. then
  689. #form -t something
  690. TEST_APP="$2"
  691. shift
  692. else
  693. #-t nothing
  694. echo "Error. Use -t testapp"
  695. exit 1
  696. fi
  697. fi
  698. ;;
  699. "all")
  700. check_lib_dir
  701. build_go_utils
  702. build_libs 1
  703. build_klibs 1
  704. build_cmds 1
  705. build_kernel
  706. printf "\n\nALL COMPONENTS COMPILED\n\n"
  707. ;;
  708. "cleanall")
  709. build_libs 2
  710. build_klibs 2
  711. build_cmds 2
  712. clean_kernel
  713. printf "\n\nALL COMPONENTS CLEANED\n\n"
  714. ;;
  715. "libs")
  716. check_lib_dir
  717. if [ -z "$2" ]
  718. then
  719. build_libs 1
  720. else
  721. build_a_lib "$2" 0
  722. shift
  723. fi
  724. ;;
  725. "klibs")
  726. check_lib_dir
  727. if [ -z "$2" ]
  728. then
  729. build_klibs 1
  730. else
  731. build_a_lib "$2" 1
  732. shift
  733. fi
  734. ;;
  735. "cleanklibs")
  736. build_klibs 2
  737. ;;
  738. "cleanlibs")
  739. build_libs 2
  740. ;;
  741. "utils")
  742. build_go_utils
  743. ;;
  744. "cmd")
  745. if [ -z "$2" ]
  746. then
  747. build_cmds 1
  748. else
  749. build_a_cmd "$2"
  750. shift
  751. fi
  752. ;;
  753. "cleancmd")
  754. build_cmds 2
  755. ;;
  756. "kernel")
  757. build_kernel
  758. ;;
  759. "cleankernel")
  760. clean_kernel
  761. ;;
  762. *)
  763. echo "Invalid option <$1>"
  764. exit 1
  765. ;;
  766. esac
  767. shift
  768. done
  769. fi