build-pkg.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #!/bin/sh
  2. # Copyright (C) 2018-2025 Nick Peng ([email protected])
  3. CURR_DIR=$(cd $(dirname $0);pwd)
  4. WORKDIR=$CURR_DIR/target
  5. VER="`date +"1.%Y.%m.%d-%H%M"`"
  6. CODE_DIR="$CURR_DIR/.."
  7. IS_BUILD_SMARTDNS=1
  8. OUTPUTDIR=$CURR_DIR
  9. SMARTDNS_WEBUI_URL="https://github.com/pymumu/smartdns-webui/archive/refs/heads/main.zip"
  10. SMARTDNS_WEBUI_SOURCE="$WORKDIR/smartdns-webui"
  11. SMARTDNS_STATIC_DIR="$WORKDIR/smartdns-static"
  12. SMARTDNS_WITH_LIBS=0
  13. export CC
  14. export STRIP
  15. export WORKDIR
  16. WITH_UI=0
  17. showhelp()
  18. {
  19. echo "Usage: $0 [OPTION]"
  20. echo "Options:"
  21. echo " --platform [luci|luci-compat|debian|openwrt|optware|linux] build for platform. "
  22. echo " --arch [all|armhf|arm64|x86-64|...] build for architecture, e.g. "
  23. echo " --cross-tool [cross-tool] cross compiler, e.g. mips-openwrt-linux-"
  24. echo " --with-ui build with smartdns-ui plugin."
  25. echo ""
  26. echo "Advance Options:"
  27. echo " --static static link smartdns"
  28. echo " --only-package only package, not build source"
  29. echo " --filearch [arch] output file arch, default: equal --arch"
  30. echo " --outputdir [dir] output package to specific directory"
  31. echo " "
  32. echo "Example:"
  33. echo " build luci:"
  34. echo " $0 --platform luci"
  35. echo " build luci:"
  36. echo " $0 --platform luci-compat"
  37. echo " build debian:"
  38. echo " $0 --platform debian --arch x86-64"
  39. echo " build raspbian pi:"
  40. echo " $0 --platform debian --arch arm64 --with-ui"
  41. echo " build optware mips:"
  42. echo " $0 --platform optware --arch mipsbig"
  43. echo " build openwrt mips:"
  44. echo " $0 --platform openwrt --arch mips"
  45. echo " build generic linux:"
  46. echo " $0 --platform linux --arch x86-64 --with-ui"
  47. }
  48. init_env()
  49. {
  50. if [ -z "$CC" ]; then
  51. CC=gcc
  52. fi
  53. mkdir -p $WORKDIR
  54. if [ $? -ne 0 ]; then
  55. echo "create work directory failed"
  56. return 1
  57. fi
  58. if [ "$STATIC" = "yes" ] && [ $WITH_UI -eq 1 ]; then
  59. SMARTDNS_WITH_LIBS=1
  60. fi
  61. check_cc="`echo "$CC" | grep -E "(\-gcc|\-cc)"`"
  62. if [ ! -z "$check_cc" ]; then
  63. TARGET_ARCH="`$CC -dumpmachine`"
  64. echo "target arch: $TARGET_ARCH"
  65. fi
  66. if [ $SMARTDNS_WITH_LIBS -eq 1 ]; then
  67. case "$TARGET_ARCH" in
  68. *arm*)
  69. NEED_UPDATE_ARM_CP15=1
  70. echo "Update arm cp15"
  71. ;;
  72. *)
  73. ;;
  74. esac
  75. LINKER_NAME=`$CC -Xlinker -v 2>&1 | grep -oP '(?<=-dynamic-linker )[^ ]+'`
  76. if [ -z "$LINKER_NAME" ]; then
  77. echo "get linker name failed"
  78. return 1
  79. fi
  80. LINKER_NAME=`basename $LINKER_NAME`
  81. LINKER_SYSROOT="`$CC --print-sysroot`"
  82. export BINDGEN_EXTRA_CLANG_ARGS="--sysroot=$LINKER_SYSROOT"
  83. echo "linker name: $LINKER_NAME"
  84. fi
  85. }
  86. copy_smartdns_libs()
  87. {
  88. SMARTDNS_BIN="$CODE_DIR/src/smartdns"
  89. copy_libs_recursive $SMARTDNS_BIN
  90. if [ $? -ne 0 ]; then
  91. echo "copy libs failed"
  92. return 1
  93. fi
  94. LIB_WEBUI_SO="$CODE_DIR/plugin/smartdns-ui/target/smartdns_ui.so"
  95. copy_libs_recursive $LIB_WEBUI_SO
  96. if [ $? -ne 0 ]; then
  97. echo "copy libs failed"
  98. return 1
  99. fi
  100. }
  101. copy_libs_recursive()
  102. {
  103. local lib=$1
  104. local lib_path=`$CC -print-file-name=$lib`
  105. if [ -z "$lib_path" ]; then
  106. return 0
  107. fi
  108. if [ -e $SMARTDNS_STATIC_DIR/lib/$lib ]; then
  109. return 0
  110. fi
  111. local tmp_path="`echo "$lib_path" | grep "libc.so"`"
  112. if [ ! -z "$tmp_path" ]; then
  113. LIBC_PATH="$tmp_path"
  114. fi
  115. if [ "$lib" != "$SMARTDNS_BIN" ]; then
  116. echo "copy $lib_path to $SMARTDNS_STATIC_DIR/lib"
  117. cp $lib_path $SMARTDNS_STATIC_DIR/lib
  118. if [ $? -ne 0 ]; then
  119. echo "copy $lib failed"
  120. return 1
  121. fi
  122. fi
  123. local shared_libs="`objdump -p $lib_path | grep NEEDED | awk '{print $2}'`"
  124. for sub_lib in $shared_libs; do
  125. copy_libs_recursive $sub_lib
  126. if [ $? -ne 0 ]; then
  127. return 1
  128. fi
  129. done
  130. return 0
  131. }
  132. copy_linker()
  133. {
  134. LINK_PATH=`$CC -print-file-name=$LINKER_NAME`
  135. SYM_LINKER_NAME=`readlink -f $LINK_PATH`
  136. echo "linker: $LINK_PATH"
  137. echo "sym linker: $SYM_LINKER_NAME"
  138. echo "libc: $LIBC_PATH"
  139. if [ "$SYM_LINKER_NAME" = "$LIBC_PATH" ]; then
  140. ln -f -s $(basename $LIBC_PATH) $SMARTDNS_STATIC_DIR/$(basename $LINKER_NAME)
  141. else
  142. cp $LINK_PATH $SMARTDNS_STATIC_DIR/lib -af
  143. if [ $? -ne 0 ]; then
  144. echo "copy $lib failed"
  145. return 1
  146. fi
  147. SYM_LINKER_NAME=`readlink $SMARTDNS_STATIC_DIR/$LINKER_NAME`
  148. if [ ! -e $SMARTDNS_STATIC_DIR/$SYM_LINKER_NAME ]; then
  149. SYM_LINKER_NAME=`basename $SYM_LINKER_NAME`
  150. ln -f -s $SYM_LINKER_NAME $SMARTDNS_STATIC_DIR/lib/$LINKER_NAME
  151. fi
  152. fi
  153. ln -f -s ${LINKER_NAME} ${SMARTDNS_STATIC_DIR}/lib/ld-linux.so
  154. if [ $? -ne 0 ]; then
  155. echo "copy $lib failed"
  156. return 1
  157. fi
  158. return 0
  159. }
  160. build_smartdns()
  161. {
  162. MAKE_WITH_UI=""
  163. if [ $WITH_UI -eq 1 ]; then
  164. MAKE_WITH_UI="WITH_UI=1"
  165. fi
  166. if [ "$PLATFORM" = "luci" ]; then
  167. return 0
  168. fi
  169. make -C $CODE_DIR clean $MAKE_ARGS
  170. if [ $SMARTDNS_WITH_LIBS -eq 1 ]; then
  171. export LDFLAGS='-Wl,-dynamic-linker,'lib/$(echo $LINKER_NAME)' -Wl,-rpath,\$$ORIGIN:\$$ORIGIN/lib'
  172. echo "LDFLAGS: $LDFLAGS"
  173. RUSTFLAGS='-C link-arg=-Wl,-rpath,$$ORIGIN'
  174. echo "Building smartdns with specific linker..."
  175. unset STATIC
  176. fi
  177. RUSTFLAGS="$RUSTFLAGS" make -C $CODE_DIR $MAKE_WITH_UI all -j8 VER=$VER $MAKE_ARGS
  178. if [ $? -ne 0 ]; then
  179. echo "make smartdns failed"
  180. exit 1
  181. fi
  182. $STRIP -d $CODE_DIR/src/smartdns >/dev/null 2>&1
  183. rm -fr $SMARTDNS_STATIC_DIR
  184. if [ $SMARTDNS_WITH_LIBS -eq 0 ]; then
  185. return 0;
  186. fi
  187. echo "copy smartdns binary to $SMARTDNS_STATIC_DIR"
  188. mkdir -p $SMARTDNS_STATIC_DIR/lib
  189. if [ $? -ne 0 ]; then
  190. echo "create target directory failed"
  191. return 1
  192. fi
  193. cp $CODE_DIR/src/smartdns $SMARTDNS_STATIC_DIR/
  194. if [ $? -ne 0 ]; then
  195. echo "copy smartdns binary failed"
  196. return 1
  197. fi
  198. cp $CURR_DIR/run-smartdns $SMARTDNS_STATIC_DIR
  199. chmod +x $SMARTDNS_STATIC_DIR/run-smartdns
  200. if [ "$NEED_UPDATE_ARM_CP15" = "1" ]; then
  201. sed -i 's/NEED_CHECK_ARM_CP15=0/NEED_CHECK_ARM_CP15=1/' $SMARTDNS_STATIC_DIR/run-smartdns
  202. if [ $? -ne 0 ]; then
  203. echo "sed run-smartdns failed"
  204. return 1
  205. fi
  206. fi
  207. copy_smartdns_libs
  208. if [ $? -ne 0 ]; then
  209. echo "copy smartdns libs failed"
  210. return 1
  211. fi
  212. rm $SMARTDNS_STATIC_DIR/lib/smartdns_ui.so >/dev/null 2>&1
  213. copy_linker
  214. if [ $? -ne 0 ]; then
  215. echo "copy linker failed"
  216. return 1
  217. fi
  218. return 0
  219. }
  220. build_webpages()
  221. {
  222. if [ ! -f "$WORKDIR/smartdns-webui.zip" ]; then
  223. echo "smartdns-webui source not found, downloading..."
  224. wget -O $WORKDIR/smartdns-webui.zip $SMARTDNS_WEBUI_URL
  225. if [ $? -ne 0 ]; then
  226. echo "Failed to download smartdns-webui source at $SMARTDNS_WEBUI_URL"
  227. return 1
  228. fi
  229. fi
  230. if [ ! -d "$SMARTDNS_WEBUI_SOURCE" ]; then
  231. echo "smartdns-webui source not found, unzipping..."
  232. unzip -q $WORKDIR/smartdns-webui.zip -d $WORKDIR
  233. if [ $? -ne 0 ]; then
  234. echo "Failed to unzip smartdns-webui source."
  235. return 1
  236. fi
  237. mv $WORKDIR/smartdns-webui-main $SMARTDNS_WEBUI_SOURCE
  238. if [ $? -ne 0 ]; then
  239. echo "Failed to rename smartdns-webui directory."
  240. return 1
  241. fi
  242. fi
  243. if [ ! -d "$SMARTDNS_WEBUI_SOURCE" ]; then
  244. echo "smartdns-webui source not found."
  245. return 1
  246. fi
  247. if [ ! -f "$SMARTDNS_WEBUI_SOURCE/package.json" ]; then
  248. echo "smartdns-webui source is not valid."
  249. return 1
  250. fi
  251. if [ -f "$SMARTDNS_WEBUI_SOURCE/out/index.html" ]; then
  252. echo "smartdns-webui already built, skipping build."
  253. return 0
  254. fi
  255. echo "Building smartdns-webui..."
  256. npm install --prefix $SMARTDNS_WEBUI_SOURCE
  257. if [ $? -ne 0 ]; then
  258. echo "Failed to install smartdns-webui dependencies."
  259. return 1
  260. fi
  261. npm run build --prefix $SMARTDNS_WEBUI_SOURCE
  262. if [ $? -ne 0 ]; then
  263. echo "Failed to build smartdns-webui."
  264. return 1
  265. fi
  266. echo "smartdns-webui build completed."
  267. return 0
  268. }
  269. build()
  270. {
  271. echo "build package for $PLATFORM"
  272. if [ $IS_BUILD_SMARTDNS -eq 1 ]; then
  273. build_smartdns
  274. if [ $? -ne 0 ]; then
  275. return 1
  276. fi
  277. fi
  278. build_webpages
  279. if [ $? -ne 0 ]; then
  280. echo "build smartdns-ui failed"
  281. return 1
  282. fi
  283. WITH_UI_ARGS=""
  284. if [ $WITH_UI -eq 1 ] && [ "$PLATFORM" != "luci" ]; then
  285. WITH_UI_ARGS="--with-ui"
  286. fi
  287. chmod +x $CODE_DIR/package/$PLATFORM/make.sh
  288. $CODE_DIR/package/$PLATFORM/make.sh -o $CURR_DIR --arch $ARCH --ver $VER --filearch $FILEARCH $WITH_UI_ARGS -o $OUTPUTDIR
  289. if [ $? -ne 0 ]; then
  290. echo "build package for $PLATFORM failed"
  291. return 1
  292. fi
  293. echo "build package for $PLATFORM success."
  294. return 0
  295. }
  296. main()
  297. {
  298. OPTS=`getopt -o o:h --long arch:,filearch:,ver:,platform:,cross-tool:,with-nftables,static,only-package,with-ui,outputdir: \
  299. -n "" -- "$@"`
  300. if [ "$#" -le "1" ]; then
  301. showhelp
  302. exit 1
  303. fi
  304. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  305. # Note the quotes around `$TEMP': they are essential!
  306. eval set -- "$OPTS"
  307. while true; do
  308. case "$1" in
  309. --arch)
  310. ARCH="$2"
  311. shift 2;;
  312. --filearch)
  313. FILEARCH="$2"
  314. shift 2;;
  315. --platform)
  316. PLATFORM="$2"
  317. shift 2;;
  318. --cross-tool)
  319. CROSS_TOOL="$2"
  320. shift 2;;
  321. --static)
  322. export STATIC="yes"
  323. shift 1;;
  324. --only-package)
  325. IS_BUILD_SMARTDNS=0
  326. shift 1;;
  327. --outputdir)
  328. OUTPUTDIR="$2"
  329. shift 2;;
  330. --with-ui)
  331. WITH_UI=1
  332. shift 1;;
  333. --ver)
  334. VER="$2"
  335. shift 2;;
  336. -h | --help )
  337. showhelp
  338. return 0
  339. shift ;;
  340. -- ) shift; break ;;
  341. * ) break ;;
  342. esac
  343. done
  344. if [ -z "$PLATFORM" ]; then
  345. echo "please input platform"
  346. echo "run $0 -h for help."
  347. return 1
  348. fi
  349. if [ "$PLATFORM" = "luci" ]; then
  350. ARCH="all"
  351. fi
  352. if [ -z "$ARCH" ]; then
  353. echo "please input arch."
  354. echo "run $0 -h for help."
  355. return 1
  356. fi
  357. if [ -z "$FILEARCH" ]; then
  358. FILEARCH="$ARCH"
  359. fi
  360. if [ -z "$OUTPUTDIR" ]; then
  361. OUTPUTDIR=$CURR_DIR
  362. fi
  363. if [ ! -z "$CROSS_TOOL" ]; then
  364. CC="${CROSS_TOOL}gcc"
  365. STRIP="${CROSS_TOOL}strip"
  366. fi
  367. if [ -z "$CC" ]; then
  368. CC="gcc"
  369. fi
  370. if [ -z "$STRIP" ]; then
  371. if [ ! -z "`echo $CC | grep '\-gcc'`" ]; then
  372. STRIP="`echo "$CC" | sed 's/-gcc\$/-strip/g'`"
  373. else
  374. STRIP="strip"
  375. fi
  376. fi
  377. if [ ! -e "`which $CC`" ]; then
  378. echo "Cannot find compiler $CC"
  379. return 1
  380. fi
  381. init_env
  382. build
  383. }
  384. main $@
  385. exit $?