• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# This file is part of the openHiTLS project.
4#
5# openHiTLS is licensed under the Mulan PSL v2.
6# You can use this software according to the terms and conditions of the Mulan PSL v2.
7# You may obtain a copy of Mulan PSL v2 at:
8#
9#     http://license.coscl.org.cn/MulanPSL2
10#
11# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14# See the Mulan PSL v2 for more details.
15# Build different miniaturized targets and perform basic functional testing.
16
17set -eu
18
19PARAM_LIST=$@
20
21CUR_DIR=`pwd`
22HITLS_ROOT_DIR=`realpath $CUR_DIR/../../`
23HITLS_BUILD_DIR=$HITLS_ROOT_DIR/build
24
25FEATURES=()
26TEST_FEATURE=""
27BUILD_HITLS="on"
28EXE_TEST="on"
29SHOW_SIZE="on" # size libhitls_*.a
30SHOW_MACRO="off"
31
32ASM_TYPE=""
33
34NO_LIB=""
35
36LIB_TYPE="static"
37DEBUG="off"
38ADD_OPTIONS=""
39DEL_OPTIONS=""
40SYSTEM=""
41BITS=64
42ENDIAN="little"
43FEATURE_CONFIG_FILE=""
44
45print_usage() {
46    printf "Usage: $0\n"
47    printf "  %-25s %s\n" "help"                    "Print this help."
48    printf "  %-25s %s\n" "macro"                   "INFO: Obtains the macro of the hitls."
49    printf "  %-25s %s\n" "no-size"                 "INFO: Do not list the detail of the object files in static libraries."
50    printf "  %-25s %s\n" "no-build"                "BUILD: Do not build hitls."
51    printf "  %-25s %s\n" "enable=a;b;c"            "BUILD: Specify the features of the build."
52    printf "  %-25s %s\n" "x8664|armv8"             "BUILD: Specify the type of assembly to build."
53    printf "  %-25s %s\n" "linux|dopra"             "BUILD: Specify the type of system to build."
54    printf "  %-25s %s\n" "32"                      "BUILD: Specify the number of system bits to 32, default is 64."
55    printf "  %-25s %s\n" "big"                     "BUILD: Specify the endian mode of the system to big, default is little."
56    printf "  %-25s %s\n" "debug"                   "BUILD: Build HiTLS with debug flags."
57    printf "  %-25s %s\n" "asan"                    "BUILD: Build HiTLS with asan flags."
58    printf "  %-25s %s\n" "test=a"                  "TEST: Specify the feature for which the test is to be performed."
59    printf "  %-25s %s\n" "no-tls"                  "TEST: Do not link hitls_tls related libraries."
60    printf "  %-25s %s\n" "no-crypto"               "TEST: Do not link hitls_crypto related libraries."
61    printf "  %-25s %s\n" "no-mpa"                  "TEST: Do not link hitls_mpa related libraries."
62    printf "  %-25s %s\n" "no-exe-test"             "TEST: Do not exe tests."
63    printf "\nexample:\n"
64    printf "  %-50s %-30s\n" "bash mini_build_test.sh enable=sha1,sha2,sha3 test=sha1,sha3" "Build sha1, sha2 and sha3, test sha1 and sha2."
65    printf "  %-50s %-30s\n" "bash mini_build_test.sh enable=sha1,sm3 armv8" "Build sha1 and sm3 and enable armv8 assembly."
66}
67
68parse_option()
69{
70    for i in $PARAM_LIST
71    do
72        key=${i%%=*}
73        value=${i#*=}
74        case "${key}" in
75            "help")
76                print_usage
77                exit 0;
78                ;;
79            "macro")
80                SHOW_MACRO="on"
81                ADD_OPTIONS="${ADD_OPTIONS} -E -dM"
82                LIB_TYPE="static"
83                ;;
84            "no-size")
85                SHOW_SIZE="off"
86                ;;
87            "no-build")
88                BUILD_HITLS="off"
89                ;;
90            "x8664"|"armv8")
91                ASM_TYPE=$key
92                ;;
93            "linux"|"dopra")
94                SYSTEM=$key
95                ;;
96            "32")
97                BITS=32
98                ;;
99            "big")
100                ENDIAN="big"
101                ;;
102            "enable")
103                FEATURES=(${value//,/ })
104                if [[ $value == *entropy* || $value == *hitls_crypto* ]]; then
105                    ADD_OPTIONS="$ADD_OPTIONS -DHITLS_SEED_DRBG_INIT_RAND_ALG=CRYPT_RAND_SHA256 -DHITLS_CRYPTO_ENTROPY_DEVRANDOM"
106                fi
107                ;;
108            "debug")
109                ADD_OPTIONS="$ADD_OPTIONS -O0 -g3 -gdwarf-2"
110                DEL_OPTIONS="$DEL_OPTIONS -O2 -D_FORTIFY_SOURCE=2"
111                ;;
112            "asan")
113                ADD_OPTIONS="$ADD_OPTIONS -fsanitize=address -fsanitize-address-use-after-scope -O0 -g3 -fno-stack-protector -fno-omit-frame-pointer -fgnu89-inline"
114                DEL_OPTIONS="$DEL_OPTIONS -fstack-protector-strong -fomit-frame-pointer -O2 -D_FORTIFY_SOURCE=2"
115                ;;
116            "feature-config")
117                # First try to find file with ASM_TYPE suffix
118                if [ -n "$ASM_TYPE" ]; then
119                    FEATURE_CONFIG_FILE=$(find $HITLS_ROOT_DIR -name "${value}_${ASM_TYPE}.json" -type f | head -n 1)
120                fi
121                # If not found with suffix, try the original filename
122                if [ -z "$FEATURE_CONFIG_FILE" ]; then
123                    FEATURE_CONFIG_FILE=$(find $HITLS_ROOT_DIR -name "${value}.json" -type f | head -n 1)
124                fi
125                if [ -z "$FEATURE_CONFIG_FILE" ]; then
126                    echo "Error: Cannot find feature config file '${value}.json' or '${value}.json' under $HITLS_ROOT_DIR"
127                    exit 1
128                fi
129                ;;
130            "test")
131                LIB_TYPE="static shared"
132                TEST_FEATURE=$value
133                if [[ $value == *cmvp* ]]; then
134                    ADD_OPTIONS="$ADD_OPTIONS -DHITLS_CRYPTO_DRBG_GM -DHITLS_CRYPTO_CMVP_INTEGRITY"
135                fi
136                ;;
137            "no-exe-test")
138                EXE_TEST="off"
139                ;;
140            "no-tls")
141                NO_LIB="$NO_LIB no-tls"
142                ;;
143            "no-crypto")
144                NO_LIB="$NO_LIB no-crypto"
145                ;;
146            "no-mpa")
147                NO_LIB="$NO_LIB no-mpa"
148                ;;
149            *)
150                echo "Wrong parameter: $key"
151                exit 1
152                ;;
153        esac
154    done
155}
156
157show_size()
158{
159    cd $HITLS_BUILD_DIR
160    libs=`find -name '*.a'`
161    echo "$libs"
162
163    array=(${libs//\n/ })
164    for lib in ${array[@]}
165    do
166        ls -lh ${lib}
167        echo -e ""
168        size ${lib} | grep -v "0	      0	      0	      0	      0"
169    done
170}
171
172show_macro()
173{
174    cd ${HITLS_BUILD_DIR}
175    grep "#define HITLS_" libhitls_bsl.a | grep -v HITLS_VERSION |awk '{print $2}' > macro_new.txt
176    sort macro_new.txt | uniq >unique_macro.txt
177    cat unique_macro.txt
178}
179
180process_feature_config()
181{
182    local config_file="$1"
183    local endian="$2"
184    local bits="$3"
185    local asm_type="$4"
186    local build_dir="$5"
187
188    python3 - "$config_file" "$endian" "$bits" "$asm_type" "$build_dir" <<END
189#!/usr/bin/env python
190import json
191import sys
192import os
193
194if __name__ == "__main__":
195    config_file = sys.argv[1]
196    endian = sys.argv[2]
197    bits = int(sys.argv[3])
198    asm_type = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else None
199    build_dir = sys.argv[5]
200    # Read the current config
201    with open(config_file, 'r') as f:
202        config = json.load(f)
203    # Update the fields
204    config['endian'] = endian
205    config['bits'] = bits
206    if asm_type:
207        config['asmType'] = asm_type
208    else:
209        # If no asm_type is defined, remove the "asm" field from hitls_crypto
210        config['asmType'] = "no_asm"
211        if 'libs' in config and 'hitls_crypto' in config['libs'] and 'asm' in config['libs']['hitls_crypto']:
212            del config['libs']['hitls_crypto']['asm']
213
214    # Create build directory if it doesn't exist
215    os.makedirs(build_dir, exist_ok=True)
216    # Save to build directory
217    output_file = os.path.join(build_dir, 'feature_config_modified.json')
218    with open(output_file, 'w') as f:
219        json.dump(config, f, indent=4)
220    # Print the output file path for the shell script to use
221    print(output_file)
222END
223}
224
225mini_config()
226{
227    enables="--enable"
228    for feature in ${FEATURES[@]}
229    do
230        enables="$enables $feature"
231    done
232
233    if [ "$FEATURE_CONFIG_FILE" != "" ]; then
234        MODIFIED_CONFIG_FILE=$(process_feature_config "$FEATURE_CONFIG_FILE" "$ENDIAN" "$BITS" "$ASM_TYPE" "$HITLS_ROOT_DIR/build/")
235        enables="--feature_config $MODIFIED_CONFIG_FILE"
236    fi
237
238    echo "python3 configure.py --lib_type $LIB_TYPE $enables --endian=$ENDIAN --bits=$BITS"
239    python3 $HITLS_ROOT_DIR/configure.py --lib_type $LIB_TYPE  $enables --endian=$ENDIAN --bits=$BITS
240
241    if [ "$ASM_TYPE" != "" ]; then
242        echo "python3 configure.py --asm_type $ASM_TYPE"
243        python3 $HITLS_ROOT_DIR/configure.py --asm_type $ASM_TYPE
244    fi
245
246    if [ "$SYSTEM" != "" ]; then
247        echo "python3 configure.py --system $SYSTEM"
248        python3 $HITLS_ROOT_DIR/configure.py --system $SYSTEM
249    fi
250
251    if [ "$ADD_OPTIONS" != "" -o "$DEL_OPTIONS" != "" ]; then
252        echo "python3 configure.py --add_options=\"$ADD_OPTIONS\" --del_options=\"$DEL_OPTIONS\""
253        python3 $HITLS_ROOT_DIR/configure.py --add_options="$ADD_OPTIONS" --del_options="$DEL_OPTIONS"
254    fi
255}
256
257check_cmd_res()
258{
259    if [ "$?" -ne "0" ]; then
260        echo "Error: $1"
261        exit 1
262    fi
263}
264
265build_hitls()
266{
267    # cleanup
268    cd $HITLS_ROOT_DIR
269    rm -rf $HITLS_BUILD_DIR
270    mkdir $HITLS_BUILD_DIR
271    cd $HITLS_BUILD_DIR
272
273    # config
274    mini_config
275    check_cmd_res "configure.py"
276
277    # cmake ..
278    cmake .. > cmake.txt
279
280    # cmake ..
281    check_cmd_res "cmake .."
282
283    # make
284    make -j > make.txt
285    check_cmd_res "make -j"
286}
287
288get_testfiles_by_features()
289{
290    cd $HITLS_ROOT_DIR/testcode/test_config
291    # 参数:被测试的特性列表(以逗号分隔)
292    python3 - "$1" <<END
293#!/usr/bin/env python
294import os, sys, json
295if __name__ == "__main__":
296    with open('crypto_test_config.json', 'r') as f:
297        test_config1 = json.loads(f.read())
298    with open('tls_test_config.json', 'r') as f:
299        test_config2 = json.loads(f.read())
300    files = set()
301    for fea in sys.argv[1].split(","):
302        files.update(test_config1['testFeatures'].get(fea, ''))
303        files.update(test_config2['testFeatures'].get(fea, ''))
304    sys.stdout.write('%s' % '|'.join(files))
305END
306}
307
308get_testcases_by_testfile()
309{
310    cd $HITLS_ROOT_DIR/testcode/test_config/
311    # 参数:测试文件,获取需执行的测试用例
312    python3 - "$1" <<END
313#!/usr/bin/env python
314import os, sys, json
315if __name__ == "__main__":
316    with open('crypto_test_config.json', 'r') as f:
317        test_config1 = json.loads(f.read())
318    with open('tls_test_config.json', 'r') as f:
319        test_config2 = json.loads(f.read())
320    if sys.argv[1] not in test_config1['testSuiteCases'] and sys.argv[1] not in test_config2['testSuiteCases']:
321        raise ValueError('The test case of file %s is not configured in file crypto_test_config.json or tls_test_config.json.'% sys.argv[1])
322    cases = set()
323    if sys.argv[1] in test_config1['testSuiteCases']:
324        cases.update(test_config1['testSuiteCases'][sys.argv[1]])
325    if sys.argv[1] in test_config2['testSuiteCases']:
326        cases.update(test_config2['testSuiteCases'][sys.argv[1]])
327    sys.stdout.write('%s' % ' '.join(cases))
328END
329}
330
331exe_file_testcases()
332{
333    test_file=$1
334    # Get test cases according to test file.
335    cd $HITLS_ROOT_DIR/testcode/script
336    test_cases=`get_testcases_by_testfile $test_file`
337    echo "test cases: $test_cases"
338
339    cd $HITLS_ROOT_DIR/testcode/output
340    ./$test_file ${test_cases} NO_DETAIL
341    check_cmd_res "exe $test_file failed"
342}
343
344test_feature()
345{
346    features=$1
347
348    cd $HITLS_ROOT_DIR/testcode/script
349    files=`get_testfiles_by_features $features`
350    echo "files: $files"
351
352    if [ -z $files ]; then
353        return
354    fi
355
356    bash build_sdv.sh run-tests="$files" $NO_LIB no-demos no-sctp
357
358    if [ $EXE_TEST == "on" ]; then
359        # exe test
360        file_array=(${files//|/ })
361        for file in ${file_array[@]}
362        do
363            exe_file_testcases $file
364        done
365    fi
366}
367
368parse_option
369
370# build securec
371if [ ! -d "${HITLS_ROOT_DIR}/platform/Secure_C/lib" ]; then
372    cd ${HITLS_ROOT_DIR}/platform/Secure_C
373    make -j
374fi
375
376if [ "${BUILD_HITLS}" = "on" ]; then
377    build_hitls
378fi
379
380if [ "${SHOW_SIZE}" = "on" ]; then
381    show_size
382fi
383
384if [ "${SHOW_MACRO}" = "on" ]; then
385    show_macro
386    exit 0
387fi
388
389if [ "$TEST_FEATURE" != "" ]; then
390    test_feature $TEST_FEATURE
391fi
392