Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
Signed-off-by: leleliu008 <leleliu008@gmail.com>
  • Loading branch information
leleliu008 committed Apr 17, 2024
1 parent c0e82a8 commit f5cf795
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 65 deletions.
114 changes: 100 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ndk-pkg

A package manager for [Android NDK](https://developer.android.google.cn/ndk) to build projects that are written in C/C++/Rust/Golang.
A package builder/manager for [Android NDK](https://developer.android.google.cn/ndk) to build projects that are written in C, C++, Rust, Golang, etc

## Caveats

Expand Down Expand Up @@ -146,22 +146,23 @@ a typical hierarchical structure under `~/.ndk-pkg` directory looks like below:
```text
~/.ndk-pkg
├── core
│   ├── SourceCodePro-Light.otf
│   ├── cacert.pem
│   ├── fonts.conf
│   ├── ndk-pkg-core-2024.05.15-linux-x86_64.tar.xz
│   ├── ndk-pkg-core-latest-release-version
│   ├── profile
│   ├── sed-in-place
│   ├── sed-in-place.c
│   ├── uppm
│   ├── uppm-0.15.0-linux-x86_64.tar.xz
│   ├── uppm-latest-release
│   ├── wrapper-native-c++
│   ├── wrapper-native-c++.c
│   ├── wrapper-native-cc
│   ├── wrapper-native-cc.c
│   ├── wrapper-native-c++
│   ├── wrapper-native-objc
│   ├── wrapper-native-objc.c
│   ├── wrapper-target-c++
│   ├── wrapper-target-cc
│   └── wrapper-target-cc.c
│   ├── wrapper-target-c++
│   ├── zig-cc
│   ├── zig-c++
│   ├── zig-ar
│   └── zig-ranlib
├── downloads
│   ├── 8f74213b56238c85a50a5329f77e06198771e70dd9a739779f4c02f65d971313.tgz
│   ├── b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30.tgz
Expand All @@ -171,28 +172,24 @@ a typical hierarchical structure under `~/.ndk-pkg` directory looks like below:
│   │   ├── f39a5f7836ac7ca1e04de14c8103e663d0b375a524a40e537258747e2deb3c0b
│   │   │   ├── include
│   │   │   ├── lib
│   │   │   ├── lib-for-apk
│   │   │   └── share
│   │   └── zlib -> f39a5f7836ac7ca1e04de14c8103e663d0b375a524a40e537258747e2deb3c0b
│   ├── android-33-armeabi-v7a
│   │   ├── cc9b367d5068ef6b8aaaee38ec2a25691da35e02757c7e0d83aff3775aef3323
│   │   │   ├── include
│   │   │   ├── lib
│   │   │   ├── lib-for-apk
│   │   │   └── share
│   │   └── zlib -> cc9b367d5068ef6b8aaaee38ec2a25691da35e02757c7e0d83aff3775aef3323
│   ├── android-33-x86
│   │   ├── a0718632fe829426c1d946e6658cc7586da0039e99d5a140d1e402a6b4a4e2f3
│   │   │   ├── include
│   │   │   ├── lib
│   │   │   ├── lib-for-apk
│   │   │   └── share
│   │   └── zlib -> a0718632fe829426c1d946e6658cc7586da0039e99d5a140d1e402a6b4a4e2f3
│   └── android-33-x86_64
│   ├── c099047714d4ce1402d66346da88d14f25c313b0d4c879520198426ebc2f36fe
│   │   ├── include
│   │   ├── lib
│   │   ├── lib-for-apk
│   │   └── share
│   └── zlib -> c099047714d4ce1402d66346da88d14f25c313b0d4c879520198426ebc2f36fe
├── native
Expand All @@ -206,6 +203,95 @@ a typical hierarchical structure under `~/.ndk-pkg` directory looks like below:
│   │   ├── share
│   │   └── receipt.txt
│   └── automake -> 30b5043e2c5513343152506e5b1e14436ddbb654f7edf69167df05f117fcdb16
├── uppm
│   ├── downloads
│   │   ├── fe6b6f7db67a20ccca0385ae38c4aafc7b2bfedc98f9d86880dfeb127a56c012.txz
│   │   └── ff66b70c830a38d331d44f6c25a37b582471def9a161c93902bac7bea3098319.tgz
│   ├── installed
│   │   ├── android-ndk-r26d
│   │   │   ├── CHANGELOG.md
│   │   │   ├── NOTICE
│   │   │   ├── NOTICE.toolchain
│   │   │   ├── README.md
│   │   │   ├── build
│   │   │   ├── meta
│   │   │   ├── ndk-build
│   │   │   ├── ndk-gdb
│   │   │   ├── ndk-lldb
│   │   │   ├── ndk-stack
│   │   │   ├── ndk-which
│   │   │   ├── prebuilt
│   │   │   ├── python-packages
│   │   │   ├── shader-tools
│   │   │   ├── simpleperf
│   │   │   ├── source.properties
│   │   │   ├── sources
│   │   │   ├── toolchains
│   │   │   └── wrap.sh
│   │   ├── bash
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── bsdtar
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── coreutils
│   │   │   ├── bin
│   │   │   ├── libexec
│   │   │   └── share
│   │   ├── curl
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── d2
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── dot_static
│   │   │   └── bin
│   │   ├── findutils
│   │   │   ├── bin
│   │   │   ├── libexec
│   │   │   ├── share
│   │   │   └── var
│   │   ├── gawk
│   │   │   ├── bin
│   │   │   ├── etc
│   │   │   ├── libexec
│   │   │   └── share
│   │   ├── git
│   │   │   ├── bin
│   │   │   ├── libexec
│   │   │   └── share
│   │   ├── grep
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── gsed
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── jq
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── patchelf
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── pkg-config
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── sysinfo
│   │   │   └── bin
│   │   ├── tree
│   │   │   ├── bin
│   │   │   └── share
│   │   ├── xxd
│   │   │   └── bin
│   │   ├── yq
│   │   │   └── bin
│   │   └── zip
│   │   ├── bin
│   │   └── share
│   └── repos.d
│   └── official-core
│   ├── LICENSE
│   ├── README.md
│   └── formula
├── repos.d
│   └── official-core
│   ├── formula
Expand Down
119 changes: 68 additions & 51 deletions ndk-pkg
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ filetype_from_url() {
PACKAGE_LDFLAGS="$(yq '.ldflags | select(. != null)' "$PACKAGE_FORMULA_FILEPATH")"

PACKAGE_INCLUDE="$(yq '.include | select(. != null)' "$PACKAGE_FORMULA_FILEPATH")"
PACKAGE_SYMLINK="$(yq '.symlink | select(. != null)' "$PACKAGE_FORMULA_FILEPATH")"
PACKAGE_SFSLINK="$(yq '.sfslink | select(. != null)' "$PACKAGE_FORMULA_FILEPATH")"

PACKAGE_PARALLEL="$(yq '.parallel | select(. != null)' "$PACKAGE_FORMULA_FILEPATH")"

Expand Down Expand Up @@ -2829,6 +2831,9 @@ EOF
# http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#dynamic_section
DT_SONAME="$(patchelf --print-soname "$LIBRARY_FILEPATH")"

# On Android, a so file must have the DT_SONAME
# https://github.com/android/ndk/issues/1865
# https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md
if [ -z "$DT_SONAME" ] ; then
abort 1 "unexpected so file: $LIBRARY_FILEPATH\n DT_SONAME: not set"
fi
Expand All @@ -2842,9 +2847,10 @@ EOF
for DT_NEEDED in $DT_NEEDED_LIST
do
case $DT_NEEDED in
# besure DT_NEEDED entries all are filenames, not filepaths
lib*.so)
;;
*) abort 1 "unexpected so file: $LIBRARY_FILEPATH\n DT_NEEDED: $DT_NEEDED"
*) abort 1 "unexpected so file: $LIBRARY_FILEPATH\n invalid DT_NEEDED: $DT_NEEDED"
esac
done

Expand Down Expand Up @@ -4917,6 +4923,14 @@ __install_the_given_package() {

#########################################################################################

if [ "$TARGET_PLATFORM_NBIT" -eq 64 ] ; then
ANDROID_DYNAMIC_LINKER_PATH='/system/bin/linker64'
else
ANDROID_DYNAMIC_LINKER_PATH='/system/bin/linker'
fi

#########################################################################################

PACKAGE_FORMULA_FILEPATH="$SESSION_DIR/$PACKAGE_NAME.yml"

step "view formula"
Expand Down Expand Up @@ -5135,7 +5149,7 @@ EOF
CFLAGS_FOR_BUILD="$CFLAGS_FOR_BUILD -Os"
CXXFLAGS_FOR_BUILD="$CXXFLAGS_FOR_BUILD -Os"
OBJCFLAGS_FOR_BUILD="$OBJCFLAGS_FOR_BUILD -Os"
LDFLAGS_FOR_BUILD="$LDFLAGS_FOR_BUILD -Wl,-S"
LDFLAGS_FOR_BUILD="$LDFLAGS_FOR_BUILD -Wl,-s"
fi

export CFLAGS_FOR_BUILD
Expand Down Expand Up @@ -6206,7 +6220,7 @@ EOF
PPFLAGS="$PPFLAGS -DNDEBUG"
CCFLAGS="$CCFLAGS -Os"
XXFLAGS="$XXFLAGS -Os"
LDFLAGS="$LDFLAGS -Wl,-S"
LDFLAGS="$LDFLAGS -Wl,-s"
fi

##############################################
Expand Down Expand Up @@ -7008,15 +7022,6 @@ $PACKAGE_DOTWEAK

#########################################################################################

step "strip ELF files"

for f in $STRIPFILES
do
run "$STRIP" "$STRIP_ARG" "$f" || true
done

#########################################################################################

[ "$PACKAGE_USE_BSYSTEM_CARGO" = 1 ] && {
rm -f "$PACKAGE_INSTALL_DIR/.crates.toml"
rm -f "$PACKAGE_INSTALL_DIR/.crates2.json"
Expand Down Expand Up @@ -7117,14 +7122,6 @@ $PACKAGE_DOTWEAK
}

__check_elf_files() {
unset STRIPFILES

if [ "$BUILD_TYPE" = release ] ; then
DOSTRIP=1
else
DOSTRIP=0
fi

cd "$PACKAGE_INSTALL_DIR"

FILEPATHs="$(find -not -name . -type f)"
Expand All @@ -7134,7 +7131,7 @@ __check_elf_files() {

for FILEPATH in $FILEPATHs
do
[ -w "$FILEPATH" ] || continue
[ -e "$FILEPATH" ] || continue

FILE_HEADER_ACTUAL="$(xxd -u -p -l 20 "$FILEPATH")"

Expand All @@ -7154,45 +7151,65 @@ __check_elf_files() {
abort 1 "ELF file header mismatch: $FILEPATH\n expect : $FILE_HEADER_EXPACT\n actual : $FILE_HEADER_ACTUAL"
fi

# may report: patchelf: cannot find section '.interp'. The input file is most likely statically linked
PT_INTERP="$(patchelf --print-interpreter "$FILEPATH" 2>/dev/null || true)"
#####################################################################

if [ -n "$PT_INTERP" ] ; then
if [ "$TARGET_PLATFORM_NBIT" -eq 64 ] ; then
ANDROID_DYNAMIC_LINKER_PATH='/system/bin/linker64'
else
ANDROID_DYNAMIC_LINKER_PATH='/system/bin/linker'
fi

if [ "$PT_INTERP" != "$ANDROID_DYNAMIC_LINKER_PATH" ] ; then
abort 1 "ELF file PT_INTERP mismatch: $FILEPATH\n expect : $ANDROID_DYNAMIC_LINKER_PATH\n actual : $PT_INTERP"
fi
# 03 means it is a shared library or dynamically linked executable
if [ "$ELF_TYPE" = '03' ] ; then
# patchelf would report a error if there is no '.interp' section in a ELF file
PT_INTERP="$(patchelf --print-interpreter "$FILEPATH" 2>/dev/null || true)"

DT_SONAME=
DT_SONAME="$(patchelf --print-soname "$FILEPATH")"
if [ -n "$PT_INTERP" ] ; then
if [ "$PT_INTERP" != "$ANDROID_DYNAMIC_LINKER_PATH" ] ; then
abort 1 "ELF file PT_INTERP mismatch: $FILEPATH\n expect : $ANDROID_DYNAMIC_LINKER_PATH\n actual : $PT_INTERP"
fi

if [ -z "$DT_SONAME" ] ; then
# In general, a shared library would not have the .interp section.
# a dynamically linked executable always have the .interp section.
#
# https://source.android.com/security/enhancements/enhancements50
# Android 5.0 and later only support position-independent executables (-fPIE)
# Android 5.0 and later, a dynamically linked executable must be a PIE(Position Independent Executable)
if ! "$READELF" -d "$FILEPATH" | grep FLAGS_1 | grep -q PIE ; then
abort 1 "invalid ELF file: $FILEPATH. Android 5.0 and later requires dynamically linked executable to support PIE (position-independent executables)."
abort 1 "invalid ELF file: $FILEPATH\n Android 5.0 and later requires dynamically linked executable to support PIE (position-independent executables)."
fi

# A dynamically linked executable always have the .interp section.
# In general, a shared library would not have the .interp section, but that is not always the case, for example, libcap/lib/libpsx.so is not only a library but also a executable.
case "${FILEPATH##*/}" in
*.so)
IS_SHARED_LIBRAY=1 ;;
*.so.*)
IS_SHARED_LIBRAY=1 ;;
*) IS_SHARED_LIBRAY=0 ;;
esac
else
IS_SHARED_LIBRAY=1
fi
fi

if basename "$FILEPATH" | grep -q '^lib.*\.so' ; then
:
else
if [ "$DOSTRIP" = 1 ] ; then
if [ -z "$STRIPFILES" ] ; then
STRIPFILES="$FILEPATH"
else
STRIPFILES="$FILEPATH $STRIPFILES"
#####################################################################

if [ "$IS_SHARED_LIBRAY" = 1 ] ; then
# http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#dynamic_section
DT_SONAME="$(patchelf --print-soname "$FILEPATH")"

# On Android, a so file must have the DT_SONAME
# https://github.com/android/ndk/issues/1865
# https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md
if [ -z "$DT_SONAME" ] ; then
abort 1 "invalid .so file: $FILEPATH\n no DT_SONAME was set in .dynamic section"
fi
fi

#####################################################################

# a \n-separated list
DT_NEEDED_LIST="$(patchelf --print-needed "$FILEPATH")"

for DT_NEEDED in $DT_NEEDED_LIST
do
case $DT_NEEDED in
# besure DT_NEEDED entries all are filenames, not filepaths
lib*.so)
;;
*) abort 1 "unexpected ELF file: $FILEPATH\n invalid DT_NEEDED: $DT_NEEDED"
esac
done
fi
esac
done
Expand Down Expand Up @@ -8724,7 +8741,7 @@ NDKPKG_PATH="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"

NDKPKG_ARGS="$*"

NDKPKG_VERSION=0.8.13
NDKPKG_VERSION=0.8.14

NDKPKG_ZSH_COMPLETION_SCRIPT_URL='https://raw.githubusercontent.com/leleliu008/ndk-pkg/master/ndk-pkg-zsh-completion'
NDKPKG_OFFICIAL_FORMULA_REPO_URL='https://github.com/leleliu008/ndk-pkg-formula-repository-official-core.git'
Expand Down

0 comments on commit f5cf795

Please sign in to comment.