xref: /aosp_15_r20/external/libwebsockets/gen_lws_config_h.sh (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1#!/bin/bash
2#
3# lws_config{,_private}.h are created by running cmake
4# We are not allowed to use cmake to do the Android build
5# These files for now have to be created offline
6#
7debug_mode="false"
8if [[ $1 == "debug" ]] || [[ $1 == "-d" ]]; then
9  debug_mode="true"
10fi
11
12function remove_trailing_slash {
13  if [[ $1 == "/" ]]; then
14    echo $i
15  else
16    echo ${1%/}
17  fi
18}
19
20set -o errexit
21MY_LOCATION="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
22MY_LOCATION=$(remove_trailing_slash $MY_LOCATION)
23CMAKE_LIST_FILE="$MY_LOCATION/CMakeLists.txt"
24
25ANDROID_ROOTDIR="$(realpath $MY_LOCATION/../..)"
26ANDROID_ROOTDIR=$(remove_trailing_slash $ANDROID_ROOTDIR)
27
28BUILD_DIR=$MY_LOCATION/build_dir
29
30# included by CMakeLists.txt
31CMAKE_ANDROID_FILE="$MY_LOCATION/CMakeAndroidLists.txt"
32
33# used to generate CMAKE_ANDROID_FILE
34CMAKE_ANDROID_FILE_TEMPLATE="$MY_LOCATION/CMakeAndroidLists.txt.template"
35
36function find_lib_path() {
37  # find the library in the out directory, and returns the directory's absolute path
38  #
39  # $1 -> libname
40  # $2 -> optional, lib file extension
41  local libext=${2:-".a"}
42  local libname="$1$libext"
43  local res=`find ${ANDROID_ROOTDIR}/out -type f | egrep "$libname$" | egrep "$(uname -m)" | egrep -i "$(uname -s)"`
44  echo $res
45}
46
47function sed_in_cmake_android() {
48  # replace in-place 'from' with 'to' in CMAKE_ANDROID_FILE
49  #
50  # $1: from
51  # $2: to
52  # $3: CMAKE_ANDROID_FILE
53  #
54  local cmake_android_file=${3:-"${CMAKE_ANDROID_FILE}"}
55  local from="____""$1""____"
56  local to=$(echo $2 | sed -r 's/\//\\\//g')
57  if [[ $debug_mode == "true" ]]; then
58    echo "${FUNCNAME[0]} replaces in $cmake_android_file "
59    echo "     " $from
60    echo "with " $to
61  fi
62  sed -i "s/$from/$to/g" $cmake_android_file
63}
64
65function build_prerequisites() {
66  local modules=("$@")
67  (
68  pushd ${ANDROID_ROOTDIR}
69  source ${ANDROID_ROOTDIR}/build/envsetup.sh
70  for mod in "${modules[@]}"; do
71    mmm $mod
72  done
73  popd
74  )
75}
76
77function create_android_list() {
78  #
79  # by filling out the form in CMAKE_ANDROID_FILE_TEMPLATE,
80  # create CMAKE_ANDROID_FILE that is included in CMakeLists.txt
81  #
82  # The CMAKE_ANDROID_FILE defines library/include directories for
83  # libraries provided by somewhere in Android Root
84  #
85  rm -f ${CMAKE_ANDROID_FILE} || true
86  cp -f ${CMAKE_ANDROID_FILE_TEMPLATE} ${CMAKE_ANDROID_FILE}
87  sed_in_cmake_android "android_src_root_dir" ${ANDROID_ROOTDIR} ${CMAKE_ANDROID_FILE}
88  # libraries that libwebsockets would depend on
89  local libs=("libssl" "libcrypto" "libcap")
90  # Android components that provide the "libs" above
91  local components=("external/boringssl" "external/libcap")
92  build_prerequisites "${components[@]}"
93  for lib in ${libs[@]}; do
94    local libfile_path=$(find_lib_path $lib)
95    local libpath=$(dirname ${libfile_path})
96    libpath=$(remove_trailing_slash $libpath)
97    str_to_replae="build_""$lib""_dir"
98    sed_in_cmake_android "$str_to_replae" $libpath ${CMAKE_ANDROID_FILE}
99  done
100}
101
102# create CMakeAndroidLists.txt
103create_android_list
104
105extension=$(date | md5sum | cut -d ' ' -f 1)
106cp -f $CMAKE_LIST_FILE $CMAKE_LIST_FILE.$extension
107
108# make CMakeLists.txt buildable
109sed -i 's/DO-NOT-BUILD-ME-AS-IS//g' ${CMAKE_ANDROID_FILE}
110
111# replace CHECK_FUNCTION_EXISTS with check_function_exists
112sed -i 's/CHECK_FUNCTION_EXISTS/check_function_exists/g' $CMAKE_LIST_FILE
113
114# run cmake to configure
115rm -fr $BUILD_DIR || true
116LIBWEBSOCKETS_SRC_ROOT=$MY_LOCATION
117mkdir -p $BUILD_DIR
118
119if [[ $debug_mode == "true" ]]; then
120  pushd $BUILD_DIR
121  cmake VERBOSE=1 ..
122  popd
123else
124  (pushd $BUILD_DIR > /dev/null 2>&1; cmake .. > /dev/null 2>&1; popd > /dev/null 2>&1)
125fi
126
127cp -f $BUILD_DIR/lws_config.h $LIBWEBSOCKETS_SRC_ROOT/include
128cp -f $BUILD_DIR/lws_config_private.h $LIBWEBSOCKETS_SRC_ROOT/include
129
130# clean up
131if [[ $debug_mode != "true" ]]; then
132  rm -fr $BUILD_DIR || true
133  rm -f $CMAKE_ANDROID_FILE || true
134  mv -f $CMAKE_LIST_FILE.$extension $CMAKE_LIST_FILE
135fi
136