BUILD 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. ### FOR BUILD TESTS PROGRAMS, Provisional ###
  37. #ARGS:
  38. # $1 -> test name
  39. build_a_test()
  40. {
  41. cd ${TEST_DIR}/$1
  42. DO_NOTHING=0
  43. LDFLAGS_EXTRA=
  44. CFLAGS_EXTRA=
  45. test_${1} 1
  46. if [ $DO_NOTHING -eq 0 ]
  47. then
  48. echo "$CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN"
  49. $CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN
  50. if [ $? -ne 0 ]
  51. then
  52. echo "ERROR compiling $1"
  53. cd - > /dev/null
  54. exit 1
  55. fi
  56. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  57. echo $LD $LDFLAGS_EXTRA $LDFLAGS $LD_LIBS -o $BUILD_OUT *.o
  58. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT *.o $LD_LIBS
  59. if [ $? -ne 0 ]
  60. then
  61. echo "ERROR linking $1"
  62. cd - > /dev/null
  63. exit 1
  64. fi
  65. fi
  66. cd - > /dev/null
  67. }
  68. ##########################
  69. compile_kernel()
  70. {
  71. export HARVEY="$_BUILD_DIR"
  72. cd "$KRL_DIR"
  73. $HARVEY/util/build $KERNEL_CONF.json
  74. cd "$PATH_ORI" > /dev/null
  75. }
  76. build_kernel()
  77. {
  78. compile_kernel
  79. if [ $? -ne 0 ]; then
  80. echo "SOMETHING WENT WRONG!"
  81. else
  82. echo "KERNEL COMPILED OK"
  83. fi
  84. }
  85. build_go_utils()
  86. {
  87. cd "${UTIL_DIR}"
  88. for i in `ls *.go`
  89. do
  90. go build $i
  91. if [ $? -ne 0 ]
  92. then
  93. printf "\nERROR compiling $i \n"
  94. exit 1
  95. fi
  96. done
  97. cd - > /dev/null
  98. }
  99. check_utils()
  100. {
  101. if [ -f "${UTIL_DIR}"/mksys ]
  102. then
  103. echo 0
  104. else
  105. echo 1
  106. fi
  107. }
  108. ## Be sure amd64/lib is there!! ##
  109. check_lib_dir()
  110. {
  111. if [ ! -d "$LIB_DIR" ]
  112. then
  113. mkdir "$LIB_DIR"
  114. if [ $? -ne 0 ]
  115. then
  116. echo "ERROR creating <$LIB_DIR> directory"
  117. fi
  118. fi
  119. }
  120. build_libs()
  121. {
  122. export HARVEY="$_BUILD_DIR"
  123. cd "$SRC_DIR"
  124. $HARVEY/util/build libs.json
  125. cd "$PATH_ORI" > /dev/null
  126. }
  127. build_klibs()
  128. {
  129. export HARVEY="$_BUILD_DIR"
  130. cd "$SRC_DIR"
  131. $HARVEY/util/build klibs.json
  132. cd "$PATH_ORI" > /dev/null
  133. }
  134. #### FOR BUILD CMD, Provisional!! ####
  135. #ARGS:
  136. # $1 -> LIB name
  137. do_link_lib()
  138. {
  139. AUX=`echo $1 | grep ^lib`
  140. _LIB_NAME=
  141. if [ -n "$AUX" ]
  142. then
  143. #Name format is libName
  144. _LIB_NAME=`echo $AUX | cut -c 4-`
  145. _LIB_NAME="-l$_LIB_NAME"
  146. else
  147. #Name format is Name
  148. _LIB_NAME="-l$1"
  149. fi
  150. echo $_LIB_NAME
  151. }
  152. #ARGS:
  153. # $1 -> libs list
  154. process_libs_to_link()
  155. {
  156. _RETURN=
  157. for i in $1
  158. do
  159. _RETURN=${_RETURN}" "`do_link_lib $i`
  160. done
  161. echo $_RETURN
  162. }
  163. #ARGS:
  164. # $1 -> cmd name
  165. build_a_cmd()
  166. {
  167. # test if binary has its own subdir
  168. if [ -d "${CMD_DIR}/$1" ]
  169. then
  170. cd ${CMD_DIR}/$1
  171. else
  172. cd ${CMD_DIR}
  173. fi
  174. DO_NOTHING=0
  175. LDFLAGS_EXTRA=
  176. CFLAGS_EXTRA=
  177. BUILD_DIR=
  178. cmd_${1} 1
  179. if [ -n "$BUILD_DIR" ]
  180. then
  181. cd -
  182. cd "$BUILD_DIR"
  183. fi
  184. if [ $DO_NOTHING -eq 0 ]
  185. then
  186. echo "$CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN"
  187. $CC $CFLAGS_CMD $CFLAGS_EXTRA $BUILD_DEBUG -c $BUILD_IN
  188. if [ $? -ne 0 ]
  189. then
  190. echo "ERROR compiling $1"
  191. cd - > /dev/null
  192. exit 1
  193. fi
  194. # If it's a stand alone source don't get all object files
  195. if [ -d "${CMD_DIR}/$1" ]
  196. then
  197. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  198. echo $LD $LDFLAGS $LDFLAGS_EXTRA $LD_LIBS -o $BUILD_OUT *.o
  199. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT *.o $LD_LIBS
  200. elif [ -n "$BUILD_DIR" ] && [ -d `dirname $BUILD_DIR`"/$1" ]
  201. then
  202. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  203. echo $LD $LDFLAGS $LDFLAGS_EXTRA $LD_LIBS -o $BUILD_OUT *.o
  204. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT *.o $LD_LIBS
  205. else
  206. LD_LIBS=`process_libs_to_link "$LIBS_TO_LINK"`
  207. echo $LD $LDFLAGS $LDFLAGS_EXTRA $LD_LIBS -o $BUILD_OUT $1.o
  208. $LD $LDFLAGS_EXTRA $LDFLAGS -o $BUILD_OUT $1.o $LD_LIBS
  209. fi
  210. if [ $? -ne 0 ]
  211. then
  212. echo "ERROR linking $1"
  213. cd - > /dev/null
  214. exit 1
  215. fi
  216. fi
  217. cd - > /dev/null
  218. }
  219. #ARGS:
  220. # $1 -> cmd name
  221. clean_a_cmd()
  222. {
  223. if [ -d "${CMD_DIR}/$1" ]
  224. then
  225. cd ${CMD_DIR}/$1
  226. else
  227. if [ $1 = ipconfig ]
  228. then
  229. cd ${CMD_DIR}/ip/$1
  230. else
  231. cd ${CMD_DIR}
  232. fi
  233. fi
  234. DO_NOTHING=0
  235. cmd_${1} 2
  236. if [ $DO_NOTHING -eq 0 ]
  237. then
  238. printf "Cleaning $1 "
  239. $CLEAN_COM
  240. if [ $? -eq 0 ]
  241. then
  242. printf "OK\n"
  243. else
  244. printf "ERROR\n"
  245. fi
  246. fi
  247. }
  248. #ARGS:
  249. #$1 -> ACTION:
  250. # 1) Build
  251. # 2) Clean
  252. build_cmds()
  253. {
  254. for i in $BUILD_CMD
  255. do
  256. if [ $1 -eq 1 ]
  257. then
  258. build_a_cmd $i
  259. else
  260. clean_a_cmd $i
  261. fi
  262. done
  263. if [ $1 -eq 1 ]
  264. then
  265. echo "ALL CMDS COMPILED OK"
  266. fi
  267. }
  268. #############################
  269. show_help()
  270. {
  271. printf "\n\nBUILD script for Harvey\n\n"
  272. printf "OPTIONS:\n"
  273. printf " all \tBuild all components\n"
  274. printf " cleanll \tClean all components\n"
  275. printf " libs \tBuild the libraries\n"
  276. printf " libs <libname>\tBuild the library <libname>\n"
  277. printf " cleanlibs\tClean the libraries\n"
  278. printf " klibs \tBuild the Klibraries\n"
  279. printf " klibs <libname>\tBuild the Klibrary <libname>\n"
  280. printf " cleanklibs\tClean the Klibraries\n"
  281. printf " utils \tBuild go utils\n"
  282. printf " cmd \tBuild all cmds \n"
  283. printf " cmd <cmdname>\tBuild cmd named <cmdname>\n"
  284. printf " cleancmd \tClean the cmds\n"
  285. printf " kernel \tBuild kernel\n"
  286. printf " cleankernel\tClean kernel\n"
  287. printf "\nFLAGS:\n"
  288. printf " -g \tCompile with debugs flags\n"
  289. printf " -t <test> \tCompile <test> app and package it with the kernel"
  290. printf "\n"
  291. }
  292. ### MAIN SCRIPT ####
  293. if [ -z "$1" ]
  294. then
  295. show_help
  296. exit 1
  297. else
  298. #BUILD_DEBUG=
  299. #Until we have a stable kernel, debug mode is the default.
  300. BUILD_DEBUG="$CFLAGS_DEBUG"
  301. TEST_APP=
  302. while [ -n "$1" ]
  303. do
  304. case "$1" in
  305. "-g")
  306. BUILD_DEBUG="$CFLAGS_DEBUG"
  307. ;;
  308. "-t"*)
  309. #is -tSomething?
  310. TEST_APP=${1:2}
  311. if [ -z "$TEST_APP" ]
  312. then
  313. if [ -n "$2" ]
  314. then
  315. #form -t something
  316. TEST_APP="$2"
  317. shift
  318. else
  319. #-t nothing
  320. echo "Error. Use -t testapp"
  321. exit 1
  322. fi
  323. fi
  324. ;;
  325. "all")
  326. check_lib_dir
  327. build_go_utils
  328. build_libs
  329. build_klibs
  330. build_cmds 1 ### Provisional
  331. build_kernel
  332. printf "\n\nALL COMPONENTS COMPILED\n\n"
  333. ;;
  334. "cleanall")
  335. printf "\n\nALL COMPONENTS ARE CLEANED AT BUILD TIME\n\n"
  336. ;;
  337. "libs")
  338. check_lib_dir
  339. if [ -z "$2" ]
  340. then
  341. build_libs
  342. fi
  343. ;;
  344. "klibs")
  345. check_lib_dir
  346. if [ -z "$2" ]
  347. then
  348. build_klibs
  349. fi
  350. ;;
  351. "cleanklibs")
  352. printf "\n\nALL COMPONENTS ARE CLEANED AT BUILD TIME\n\n"
  353. ;;
  354. "cleanlibs")
  355. printf "\n\nALL COMPONENTS ARE CLEANED AT BUILD TIME\n\n"
  356. ;;
  357. "utils")
  358. build_go_utils
  359. ;;
  360. "cmd")
  361. if [ -z "$2" ]
  362. then
  363. build_cmds 1
  364. else
  365. build_a_cmd "$2"
  366. shift
  367. fi
  368. ;;
  369. "cleancmd")
  370. build_cmds 2
  371. ;;
  372. "kernel")
  373. build_kernel
  374. ;;
  375. "cleankernel")
  376. printf "\n\nALL COMPONENTS ARE CLEANED AT BUILD TIME\n\n"
  377. ;;
  378. *)
  379. echo "Invalid option <$1>"
  380. exit 1
  381. ;;
  382. esac
  383. shift
  384. done
  385. fi