| 1 | #!/usr/bin/env bash |
| 2 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | # You may not use this file except in compliance with the License. |
| 6 | # A copy of the License is located at |
| 7 | # |
| 8 | # http://aws.amazon.com/apache2.0 |
| 9 | # |
| 10 | # or in the "license" file accompanying this file. This file is distributed |
| 11 | # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | # express or implied. See the License for the specific language governing |
| 13 | # permissions and limitations under the License. |
| 14 | # |
| 15 | set -eo pipefail |
| 16 | |
| 17 | usage() { |
| 18 | echo "test_install_shared_and_static.sh build_dir" |
| 19 | echo "Checks that installed s2n-config.cmake chooses appropriately between shared and static." |
| 20 | echo "Note that you MUST build against the version of libcrypto that's actually installed on the system," |
| 21 | echo "because installing libs2n.so forces it to use the system's libcrypto.so." |
| 22 | exit 1 |
| 23 | } |
| 24 | |
| 25 | if [ "$#" -ne 1 ]; then |
| 26 | usage |
| 27 | fi |
| 28 | |
| 29 | WORK_DIR=$1 |
| 30 | |
| 31 | source codebuild/bin/s2n_setup_env.sh |
| 32 | source codebuild/bin/jobs.sh |
| 33 | |
| 34 | COMMON_S2N_BUILD_ARGS=(-H. -DCMAKE_PREFIX_PATH=$LIBCRYPTO_ROOT -DBUILD_TESTING=OFF) |
| 35 | |
| 36 | # create installation dir with libs2n.so |
| 37 | if [ ! -d $WORK_DIR/s2n-install-shared ]; then |
| 38 | (set -x; cmake -B$WORK_DIR/s2n-build-shared -DCMAKE_INSTALL_PREFIX=$WORK_DIR/s2n-install-shared -DBUILD_SHARED_LIBS=ON ${COMMON_S2N_BUILD_ARGS[@]}) |
| 39 | (set -x; cmake --build $WORK_DIR/s2n-build-shared --target install -- -j $JOBS) |
| 40 | fi |
| 41 | |
| 42 | # create installation dir with libs2n.a |
| 43 | if [ ! -d $WORK_DIR/s2n-install-static ]; then |
| 44 | (set -x; cmake -B$WORK_DIR/s2n-build-static -DCMAKE_INSTALL_PREFIX=$WORK_DIR/s2n-install-static -DBUILD_SHARED_LIBS=OFF ${COMMON_S2N_BUILD_ARGS[@]}) |
| 45 | (set -x; cmake --build $WORK_DIR/s2n-build-static --target install -- -j $JOBS) |
| 46 | fi |
| 47 | |
| 48 | # create installation dir with both libs2n.so and libs2n.a |
| 49 | if [ ! -d $WORK_DIR/s2n-install-both ]; then |
| 50 | (set -x; cmake -B$WORK_DIR/s2n-build-shared-both -DCMAKE_INSTALL_PREFIX=$WORK_DIR/s2n-install-both -DBUILD_SHARED_LIBS=ON ${COMMON_S2N_BUILD_ARGS[@]}) |
| 51 | (set -x; cmake --build $WORK_DIR/s2n-build-shared-both --target install -- -j $JOBS) |
| 52 | |
| 53 | (set -x; cmake -B$WORK_DIR/s2n-build-static-both -DCMAKE_INSTALL_PREFIX=$WORK_DIR/s2n-install-both -DBUILD_SHARED_LIBS=OFF ${COMMON_S2N_BUILD_ARGS[@]}) |
| 54 | (set -x; cmake --build $WORK_DIR/s2n-build-static-both --target install -- -j $JOBS) |
| 55 | fi |
| 56 | |
| 57 | # write out source of a small cmake project, containing: |
| 58 | # - mylib: a library that uses s2n |
| 59 | # - myapp: executable that uses mylib |
| 60 | rm -rf $WORK_DIR/myapp-src |
| 61 | mkdir -p $WORK_DIR/myapp-src |
| 62 | |
| 63 | cat <<EOF > $WORK_DIR/myapp-src/mylib.c |
| 64 | extern int s2n_init(void); |
| 65 | |
| 66 | void mylib_init(void) { |
| 67 | s2n_init(); |
| 68 | } |
| 69 | EOF |
| 70 | |
| 71 | cat <<EOF > $WORK_DIR/myapp-src/myapp.c |
| 72 | extern void mylib_init(void); |
| 73 | |
| 74 | int main() { |
| 75 | mylib_init(); |
| 76 | } |
| 77 | EOF |
| 78 | |
| 79 | cat <<EOF > $WORK_DIR/myapp-src/CMakeLists.txt |
| 80 | cmake_minimum_required (VERSION 3.0) |
| 81 | project (myapp C) |
| 82 | |
| 83 | add_library(mylib mylib.c) |
| 84 | find_package(s2n REQUIRED) |
| 85 | target_link_libraries(mylib PRIVATE AWS::s2n) |
| 86 | |
| 87 | add_executable(myapp myapp.c) |
| 88 | target_link_libraries(myapp PRIVATE mylib) |
| 89 | EOF |
| 90 | |
| 91 | # build myapp and mylib, confirm that expected type of libs2n is used |
| 92 | build_myapp() { |
| 93 | local BUILD_SHARED_LIBS=$1 # ("BUILD_SHARED_LIBS=ON" or "BUILD_SHARED_LIBS=OFF") |
| 94 | local S2N_INSTALL_DIR=$2 # which s2n-install dir should be used |
| 95 | local LIBS2N_EXPECTED=$3 # ("libs2n.so" or "libs2n.a") which type of libs2n is expected to be used |
| 96 | |
| 97 | echo "---------------------------------------------------------------------" |
| 98 | echo "building myapp with $BUILD_SHARED_LIBS looking-in:$S2N_INSTALL_DIR should-use:$LIBS2N_EXPECTED" |
| 99 | |
| 100 | local MYAPP_BUILD_DIR=$WORK_DIR/myapp-build |
| 101 | rm -rf $MYAPP_BUILD_DIR/ |
| 102 | |
| 103 | local S2N_INSTALL_PATH=$(realpath $WORK_DIR/$S2N_INSTALL_DIR) |
| 104 | |
| 105 | (set -x; cmake -H$WORK_DIR/myapp-src -B$MYAPP_BUILD_DIR -D$BUILD_SHARED_LIBS "-DCMAKE_PREFIX_PATH=$S2N_INSTALL_PATH;$LIBCRYPTO_ROOT") |
| 106 | (set -x; cmake --build $MYAPP_BUILD_DIR) |
| 107 | |
| 108 | LDD_OUTPUT=$(ldd $MYAPP_BUILD_DIR/myapp) |
| 109 | echo "$LDD_OUTPUT" |
| 110 | |
| 111 | if echo "$LDD_OUTPUT" | grep -q libs2n.so; then |
| 112 | local LIBS2N_ACTUAL=libs2n.so |
| 113 | else |
| 114 | local LIBS2N_ACTUAL=libs2n.a |
| 115 | fi |
| 116 | |
| 117 | if [ $LIBS2N_ACTUAL != $LIBS2N_EXPECTED ]; then |
| 118 | echo "test failure: used $LIBS2N_ACTUAL, but expected to use $LIBS2N_EXPECTED" |
| 119 | exit 1 |
| 120 | fi |
| 121 | } |
| 122 | |
| 123 | # if only shared libs2n.so is available, that's what should get used |
| 124 | build_myapp BUILD_SHARED_LIBS=ON s2n-install-shared libs2n.so |
| 125 | build_myapp BUILD_SHARED_LIBS=OFF s2n-install-shared libs2n.so |
| 126 | |
| 127 | # if only static libs2n.a is available, that's what should get used |
| 128 | build_myapp BUILD_SHARED_LIBS=ON s2n-install-static libs2n.a |
| 129 | build_myapp BUILD_SHARED_LIBS=OFF s2n-install-static libs2n.a |
| 130 | |
| 131 | # if both libs2n.so and libs2n.a are available... |
| 132 | build_myapp BUILD_SHARED_LIBS=ON s2n-install-both libs2n.so # should choose libs2n.so |
| 133 | build_myapp BUILD_SHARED_LIBS=OFF s2n-install-both libs2n.a # should choose libs2n.a |