mkexport.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #!/usr/bin/env bash
  2. # tools/mkexport.sh
  3. #
  4. # Copyright (C) 2011-2012, 2014, 2016, 2019 Gregory Nutt. All rights reserved.
  5. # Author: Gregory Nutt <gnutt@nuttx.org>
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. #
  11. # 1. Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # 2. Redistributions in binary form must reproduce the above copyright
  14. # notice, this list of conditions and the following disclaimer in
  15. # the documentation and/or other materials provided with the
  16. # distribution.
  17. # 3. Neither the name NuttX nor the names of its contributors may be
  18. # used to endorse or promote products derived from this software
  19. # without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. # POSSIBILITY OF SUCH DAMAGE.
  33. #
  34. # Get the input parameter list
  35. USAGE="USAGE: $0 [-d] [-z] [-u] [-t <top-dir> [-x <lib-ext>] [-a <apps-dir>] [-m <make-exe>] -l \"lib1 [lib2 [lib3 ...]]\""
  36. unset TOPDIR
  37. unset LIBLIST
  38. unset TGZ
  39. unset APPDIR
  40. unset BOARDDIR
  41. USRONLY=n
  42. LIBEXT=.a
  43. while [ ! -z "$1" ]; do
  44. case $1 in
  45. -a )
  46. shift
  47. APPDIR="$1"
  48. ;;
  49. -b )
  50. shift
  51. BOARDDIR="$1"
  52. ;;
  53. -d )
  54. set -x
  55. ;;
  56. -l )
  57. shift
  58. LIBLIST=$1
  59. ;;
  60. -m )
  61. shift
  62. MAKE="$1"
  63. ;;
  64. -t )
  65. shift
  66. TOPDIR=$1
  67. ;;
  68. -u )
  69. USRONLY=y
  70. ;;
  71. -x )
  72. shift
  73. LIBEXT=$1
  74. ;;
  75. -z )
  76. TGZ=y
  77. ;;
  78. -h )
  79. echo $USAGE
  80. exit 0
  81. ;;
  82. * )
  83. echo "Unrecognized argument: $1"
  84. echo $USAGE
  85. exit 1
  86. ;;
  87. esac
  88. shift
  89. done
  90. # Check arguments
  91. if [ -z "${TOPDIR}" -o -z "${LIBLIST}" ]; then
  92. echo "MK: Missing required arguments"
  93. echo $USAGE
  94. exit 1
  95. fi
  96. if [ ! -d "${TOPDIR}" ]; then
  97. echo "MK: Directory ${TOPDIR} does not exist"
  98. exit 1
  99. fi
  100. # Check configuration
  101. # Verify that we have Make.defs, .config, and .version files.
  102. if [ ! -f "${TOPDIR}/Make.defs" ]; then
  103. echo "MK: Directory ${TOPDIR}/Make.defs does not exist"
  104. exit 1
  105. fi
  106. if [ ! -f "${TOPDIR}/.config" ]; then
  107. echo "MK: Directory ${TOPDIR}/.config does not exist"
  108. exit 1
  109. fi
  110. if [ ! -f "${TOPDIR}/.version" ]; then
  111. echo "MK: File ${TOPDIR}/.version does not exist"
  112. exit 1
  113. fi
  114. # Check if the make environment variable has been defined
  115. if [ -z "${MAKE}" ]; then
  116. MAKE=`which make`
  117. fi
  118. # Get the version string
  119. source "${TOPDIR}/.version"
  120. if [ ! -z "${CONFIG_VERSION_STRING}" -a "${CONFIG_VERSION_STRING}" != "0.0" ]; then
  121. VERSION="-${CONFIG_VERSION_STRING}"
  122. fi
  123. # Create the export directory
  124. EXPORTSUBDIR="nuttx-export${VERSION}"
  125. EXPORTDIR="${TOPDIR}/${EXPORTSUBDIR}"
  126. # If the export directory already exists, then remove it and create a new one
  127. if [ -d "${EXPORTDIR}" ]; then
  128. echo "MK: Removing old export directory"
  129. rm -rf "${EXPORTDIR}"
  130. fi
  131. # Remove any possible previous results
  132. rm -f "${EXPORTDIR}.tar"
  133. rm -f "${EXPORTDIR}.zip"
  134. rm -f "${EXPORTDIR}.tar.gz"
  135. # Create the export directory and some of its subdirectories
  136. mkdir "${EXPORTDIR}" || { echo "MK: 'mkdir ${EXPORTDIR}' failed"; exit 1; }
  137. mkdir "${EXPORTDIR}/startup" || { echo "MK: 'mkdir ${EXPORTDIR}/startup' failed"; exit 1; }
  138. mkdir "${EXPORTDIR}/libs" || { echo "MK: 'mkdir ${EXPORTDIR}/libs' failed"; exit 1; }
  139. mkdir "${EXPORTDIR}/scripts" || { echo "MK: 'mkdir ${EXPORTDIR}/scripts' failed"; exit 1; }
  140. mkdir "${EXPORTDIR}/tools" || { echo "MK: 'mkdir ${EXPORTDIR}/tools' failed"; exit 1; }
  141. if [ "X${USRONLY}" != "Xy" ]; then
  142. mkdir "${EXPORTDIR}/arch" || { echo "MK: 'mkdir ${EXPORTDIR}/arch' failed"; exit 1; }
  143. fi
  144. # Copy the .config file
  145. cp -a "${TOPDIR}/.config" "${EXPORTDIR}/.config" ||
  146. { echo "MK: Failed to copy ${TOPDIR}/.config to ${EXPORTDIR}/.config"; exit 1; }
  147. # Copy the Make.defs files
  148. cp -a "${TOPDIR}/Make.defs" "${EXPORTDIR}/Make.defs" ||
  149. { echo "MK: Failed to copy ${TOPDIR}/Make.defs to ${EXPORTDIR}/Make.defs"; exit 1; }
  150. # Extract information from the Make.defs file. A Makefile can do this best
  151. ${MAKE} -C "${TOPDIR}/tools" -f Makefile.export TOPDIR="${TOPDIR}" EXPORTDIR="${EXPORTDIR}"
  152. source "${EXPORTDIR}/makeinfo.sh"
  153. rm -f "${EXPORTDIR}/makeinfo.sh"
  154. rm -f "${EXPORTDIR}/Make.defs"
  155. # Verify the build info that we got from makeinfo.sh
  156. if [ ! -d "${ARCHDIR}" ]; then
  157. echo "MK: Directory ${ARCHDIR} does not exist"
  158. exit 1
  159. fi
  160. # Copy the depends script
  161. cp "${TOPDIR}/tools/mkdeps.c" "${EXPORTDIR}/tools/."
  162. cp "${TOPDIR}/tools/incdir.c" "${EXPORTDIR}/tools/."
  163. # Copy the default linker script
  164. cp -f "${TOPDIR}/binfmt/libelf/gnu-elf.ld" "${EXPORTDIR}/scripts/."
  165. # Copy the board config script
  166. if [ -f "${BOARDDIR}/scripts/Config.mk" ]; then
  167. cp -f "${BOARDDIR}/scripts/Config.mk" "${EXPORTDIR}/scripts/."
  168. fi
  169. # Is there a linker script in this configuration?
  170. if [ "X${USRONLY}" != "Xy" ]; then
  171. # LDPATH can contain multiple files.
  172. # The "Copy additional ld scripts" step might copy a file multiple times.
  173. for LDSCRIPT in ${LDPATH}; do
  174. # Apparently so. Verify that the script exists
  175. if [ ! -f "${LDSCRIPT}" ]; then
  176. echo "MK: File ${LDSCRIPT} does not exist"
  177. exit 1
  178. fi
  179. # Copy the linker script
  180. cp -p "${LDSCRIPT}" "${EXPORTDIR}/scripts/." || \
  181. { echo "MK: cp ${LDSCRIPT} failed"; exit 1; }
  182. # Copy additional ld scripts
  183. LDDIR="$(dirname "${LDSCRIPT}")"
  184. for f in "${LDDIR}"/*.ld ; do
  185. [ -f "${f}" ] && cp -f "${f}" "${EXPORTDIR}/scripts/."
  186. done
  187. done
  188. fi
  189. # Save the compilation options
  190. echo "ARCHCFLAGS = ${ARCHCFLAGS}" >"${EXPORTDIR}/scripts/Make.defs"
  191. echo "ARCHCPUFLAGS = ${ARCHCPUFLAGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  192. echo "ARCHCXXFLAGS = ${ARCHCXXFLAGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  193. echo "ARCHPICFLAGS = ${ARCHPICFLAGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  194. echo "ARCHWARNINGS = ${ARCHWARNINGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  195. echo "ARCHWARNINGSXX = ${ARCHWARNINGSXX}" >>"${EXPORTDIR}/scripts/Make.defs"
  196. echo "ARCHOPTIMIZATION = ${ARCHOPTIMIZATION}" >>"${EXPORTDIR}/scripts/Make.defs"
  197. echo "CROSSDEV = ${CROSSDEV}" >>"${EXPORTDIR}/scripts/Make.defs"
  198. echo "CC = ${CC}" >>"${EXPORTDIR}/scripts/Make.defs"
  199. echo "CXX = ${CXX}" >>"${EXPORTDIR}/scripts/Make.defs"
  200. echo "CPP = ${CPP}" >>"${EXPORTDIR}/scripts/Make.defs"
  201. echo "LD = ${LD}" >>"${EXPORTDIR}/scripts/Make.defs"
  202. echo "AR = ${AR}" >>"${EXPORTDIR}/scripts/Make.defs"
  203. echo "NM = ${NM}" >>"${EXPORTDIR}/scripts/Make.defs"
  204. echo "STRIP = ${STRIP}" >>"${EXPORTDIR}/scripts/Make.defs"
  205. echo "OBJCOPY = ${OBJCOPY}" >>"${EXPORTDIR}/scripts/Make.defs"
  206. echo "OBJDUMP = ${OBJDUMP}" >>"${EXPORTDIR}/scripts/Make.defs"
  207. echo "NXFLATLDFLAGS1 = ${NXFLATLDFLAGS1}" >>"${EXPORTDIR}/scripts/Make.defs"
  208. echo "NXFLATLDFLAGS2 = ${NXFLATLDFLAGS2}" >>"${EXPORTDIR}/scripts/Make.defs"
  209. echo "OBJEXT = ${OBJEXT}" >>"${EXPORTDIR}/scripts/Make.defs"
  210. echo "LIBEXT = ${LIBEXT}" >>"${EXPORTDIR}/scripts/Make.defs"
  211. echo "EXEEXT = ${EXEEXT}" >>"${EXPORTDIR}/scripts/Make.defs"
  212. echo "HOSTCC = ${HOSTCC}" >>"${EXPORTDIR}/scripts/Make.defs"
  213. echo "HOSTINCLUDES = ${HOSTINCLUDES}" >>"${EXPORTDIR}/scripts/Make.defs"
  214. echo "HOSTCFLAGS = ${HOSTCFLAGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  215. echo "HOSTLDFLAGS = ${HOSTLDFLAGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  216. echo "HOSTEXEEXT = ${HOSTEXEEXT}" >>"${EXPORTDIR}/scripts/Make.defs"
  217. echo "LDNAME = ${LDNAME}" >>"${EXPORTDIR}/scripts/Make.defs"
  218. # Additional compilation options when the kernel is built
  219. if [ "X${USRONLY}" != "Xy" ]; then
  220. echo "EXTRA_LIBS = ${EXTRA_LIBS}" >>"${EXPORTDIR}/scripts/Make.defs"
  221. echo "EXTRA_OBJS = ${EXTRA_OBJS}" >>"${EXPORTDIR}/scripts/Make.defs"
  222. echo "HEAD_OBJ = ${HEAD_OBJ}" >>"${EXPORTDIR}/scripts/Make.defs"
  223. echo "LDENDGROUP = ${LDENDGROUP}" >>"${EXPORTDIR}/scripts/Make.defs"
  224. echo "LDFLAGS = ${LDFLAGS}" >>"${EXPORTDIR}/scripts/Make.defs"
  225. echo "LDSTARTGROUP = ${LDSTARTGROUP}" >>"${EXPORTDIR}/scripts/Make.defs"
  226. fi
  227. # Copy the system map file(s)
  228. if [ -r ${TOPDIR}/System.map ]; then
  229. cp -a "${TOPDIR}/System.map" "${EXPORTDIR}/."
  230. fi
  231. if [ -r ${TOPDIR}/User.map ]; then
  232. cp -a "${TOPDIR}/User.map" "${EXPORTDIR}/."
  233. fi
  234. # Copy the NuttX include directory (retaining attributes and following symbolic links)
  235. cp -LR -p "${TOPDIR}/include" "${EXPORTDIR}/." || \
  236. { echo "MK: 'cp ${TOPDIR}/include' failed"; exit 1; }
  237. # Copy the startup object file(s)
  238. ${MAKE} -C ${ARCHDIR} export_startup TOPDIR=${TOPDIR} EXPORT_DIR="${EXPORTDIR}"
  239. # Copy architecture-specific header files into the arch export sub-directory.
  240. # This is tricky because each architecture does things in a little different
  241. # way.
  242. #
  243. # First copy any header files in the architecture src/ sub-directory (some
  244. # architectures keep all of the header files there, some a few, and others
  245. # none
  246. cp -f "${ARCHDIR}"/*.h "${EXPORTDIR}"/arch/. 2>/dev/null
  247. # Then look a list of possible places where other architecture-specific
  248. # header files might be found. If those places exist (as directories or
  249. # as symbolic links to directories, then copy the header files from
  250. # those directories into the EXPORTDIR
  251. if [ "X${USRONLY}" != "Xy" ]; then
  252. ARCH_HDRDIRS="arm armv7-m avr avr32 board common chip mips32"
  253. for hdir in $ARCH_HDRDIRS; do
  254. # Does the directory (or symbolic link) exist?
  255. if [ -d "${ARCHDIR}/${hdir}" -o -h "${ARCHDIR}/${hdir}" ]; then
  256. # Yes.. create a export sub-directory of the same name
  257. mkdir "${EXPORTDIR}/arch/${hdir}" || \
  258. { echo "MK: 'mkdir ${EXPORTDIR}/arch/${hdir}' failed"; exit 1; }
  259. # Then copy the header files (only) into the new directory
  260. cp -f "${ARCHDIR}"/${hdir}/*.h "${EXPORTDIR}"/arch/${hdir}/. 2>/dev/null
  261. # Most architectures have low directory called "hardware" that
  262. # holds the header files
  263. if [ -d "${ARCHDIR}/${hdir}/hardware" ]; then
  264. # Yes.. create a export sub-directory of the same name
  265. mkdir "${EXPORTDIR}/arch/${hdir}/hardware" || \
  266. { echo "MK: 'mkdir ${EXPORTDIR}/arch/${hdir}/hardware' failed"; exit 1; }
  267. # Then copy the header files (only) into the new directory
  268. cp -f "${ARCHDIR}"/${hdir}/hardware/*.h "${EXPORTDIR}"/arch/${hdir}/hardware/. 2>/dev/null
  269. fi
  270. fi
  271. done
  272. # Copy OS internal header files as well. They are used by some architecture-
  273. # specific header files.
  274. mkdir "${EXPORTDIR}/arch/os" || \
  275. { echo "MK: 'mkdir ${EXPORTDIR}/arch/os' failed"; exit 1; }
  276. OSDIRS="clock environ errno group init irq mqueue paging pthread sched semaphore signal task timer wdog"
  277. for dir in ${OSDIRS}; do
  278. mkdir "${EXPORTDIR}/arch/os/${dir}" || \
  279. { echo "MK: 'mkdir ${EXPORTDIR}/arch/os/${dir}' failed"; exit 1; }
  280. cp -f "${TOPDIR}"/sched/${dir}/*.h "${EXPORTDIR}"/arch/os/${dir}/. 2>/dev/null
  281. done
  282. # Add the board library to the list of libraries
  283. if [ -f "${ARCHDIR}/board/libboard${LIBEXT}" ]; then
  284. LIBLIST="${LIBLIST} ${ARCHSUBDIR}/board/libboard${LIBEXT}"
  285. fi
  286. fi
  287. LDLIBS=`basename -a ${LIBLIST} | sed -e "s/lib/-l/g" -e "s/\.${LIBEXT:1}//g" | tr "\n" " "`
  288. if [ "X${USRONLY}" != "Xy" ]; then
  289. echo "LDLIBS = ${LDLIBS}" >>"${EXPORTDIR}/scripts/Make.defs"
  290. fi
  291. # Then process each library
  292. for lib in ${LIBLIST}; do
  293. if [ ! -f "${TOPDIR}/${lib}" ]; then
  294. echo "MK: Library ${TOPDIR}/${lib} does not exist"
  295. exit 1
  296. fi
  297. cp ${TOPDIR}/${lib} ${EXPORTDIR}/libs
  298. done
  299. # Process extra librarys
  300. for lib in ${EXTRA_LIBS}; do
  301. # Convert library name
  302. if [ ${lib:0:2} = "-l" ]; then
  303. lib=`echo "${lib}" | sed -e "s/-l/lib/" -e "s/$/${LIBEXT}/"`
  304. fi
  305. for path in ${EXTRA_LIBPATHS}; do
  306. # Skip the library path options
  307. if [ ${#path} == 2 ]; then continue; fi
  308. if [ ${path:0:2} = "-l" ] || [ ${path:0:2} = "-L" ]; then
  309. path=${path:2}
  310. fi
  311. # Export the extra librarys
  312. if [ -f "${path}/${lib}" ]; then
  313. cp -a ${path}/${lib} ${EXPORTDIR}/libs
  314. break
  315. fi
  316. done
  317. done
  318. # Copy the essential build script file(s)
  319. cp -f "${TOPDIR}/tools/Config.mk" "${EXPORTDIR}/tools/"
  320. cp -f "${TOPDIR}/tools/copydir.bat" "${EXPORTDIR}/tools/"
  321. cp -f "${TOPDIR}/tools/copydir.sh" "${EXPORTDIR}/tools/"
  322. cp -f "${TOPDIR}/tools/define.bat" "${EXPORTDIR}/tools/"
  323. cp -f "${TOPDIR}/tools/define.sh" "${EXPORTDIR}/tools/"
  324. cp -f "${TOPDIR}/tools/incdir.bat" "${EXPORTDIR}/tools/"
  325. cp -f "${TOPDIR}/tools/incdir.sh" "${EXPORTDIR}/tools/"
  326. cp -f "${TOPDIR}/tools/link.bat" "${EXPORTDIR}/tools/"
  327. cp -f "${TOPDIR}/tools/link.sh" "${EXPORTDIR}/tools/"
  328. cp -f "${TOPDIR}/tools/unlink.bat" "${EXPORTDIR}/tools/"
  329. cp -f "${TOPDIR}/tools/unlink.sh" "${EXPORTDIR}/tools/"
  330. # Now tar up the whole export directory
  331. cd "${TOPDIR}" || \
  332. { echo "MK: 'cd ${TOPDIR}' failed"; exit 1; }
  333. if [ -e "${APPDIR}/Makefile" ]; then
  334. "${MAKE}" -C "${APPDIR}" EXPORTDIR="$(cd "${EXPORTSUBDIR}" ; pwd )" TOPDIR="${TOPDIR}" export || \
  335. { echo "MK: call make export for APPDIR not supported"; }
  336. fi
  337. if [ "X${TGZ}" = "Xy" ]; then
  338. tar cvf "${EXPORTSUBDIR}.tar" "${EXPORTSUBDIR}" 1>/dev/null
  339. gzip -f "${EXPORTSUBDIR}.tar"
  340. else
  341. zip -r "${EXPORTSUBDIR}.zip" "${EXPORTSUBDIR}" 1>/dev/null
  342. fi
  343. # Clean up after ourselves
  344. rm -rf "${EXPORTSUBDIR}"